# Webpacker
![travis-ci status](https://api.travis-ci.org/rails/webpacker.svg?branch=master)
[![node.js](https://img.shields.io/badge/node-%3E%3D%206.0.0-brightgreen.svg)](https://nodejs.org/en/)
[![Gem](https://img.shields.io/gem/v/webpacker.svg)](https://github.com/rails/webpacker)
Webpacker makes it easy to use the JavaScript pre-processor and bundler
[webpack 3.x.x+](https://webpack.js.org/)
to manage application-like JavaScript in Rails. It coexists with the asset pipeline,
as the primary purpose for webpack is app-like JavaScript, not images, CSS, or
even JavaScript Sprinkles (that all continues to live in app/assets).
However, it is possible to use Webpacker for CSS, images and fonts assets as well,
in which case you may not even need the asset pipeline. This is mostly relevant when exclusively using component-based JavaScript frameworks.
## Table of Contents
- [Prerequisites](#prerequisites)
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Development](#development)
- [webpack configuration](#webpack-configuration)
- [Upgrading](#upgrading)
- [Yarn Integrity](#yarn-integrity)
- [Integrations](#integrations)
- [React](#react)
- [Angular with TypeScript](#angular-with-typescript)
- [Vue](#vue)
- [Elm](#elm)
- [Paths](#paths)
- [Resolved](#resolved)
- [Watched](#watched)
- [Deployment](#deployment)
- [Docs](#docs)
- [License](#license)
## Prerequisites
* Ruby 2.2+
* Rails 4.2+
* Node.js 6.0.0+
* Yarn 0.25.2+
## Features
* [webpack 3.x.x](https://webpack.js.org/)
* ES6 with [babel](https://babeljs.io/)
* Automatic code splitting using multiple entry points
* Stylesheets - Sass and CSS
* Images and fonts
* PostCSS - Auto-Prefixer
* Asset compression, source-maps, and minification
* CDN support
* React, Angular, Elm and Vue support out-of-the-box
* Rails view helpers
* Extensible and configurable
## Installation
You can either add Webpacker during setup of a new Rails 5.1+ application
using new `--webpack` option:
```bash
# Available Rails 5.1+
rails new myapp --webpack
```
Or add it to your `Gemfile`:
```ruby
# Gemfile
gem 'webpacker', '~> 3.0'
# OR if you prefer to use master
gem 'webpacker', git: 'https://github.com/rails/webpacker.git'
```
and finally, run following to install Webpacker:
```bash
bundle
bundle exec rails webpacker:install
# OR (on rails version < 5.0)
bundle exec rake webpacker:install
```
### Usage
Once installed you can start writing modern ES6-flavored JavaScript app today:
```yml
app/javascript:
├── packs:
│ # only webpack entry files here
│ └── application.js
└── src:
│ └── application.css
└── images:
└── logo.svg
```
You can then link the JavaScript pack in Rails view using `javascript_pack_tag` helper.
If you have styles imported in your pack file, you can link using `stylesheet_pack_tag`:
```erb
<%= javascript_pack_tag 'application' %>
<%= stylesheet_pack_tag 'application' %>
```
If you want to link a static asset for `` or `` tag, you
can use `asset_pack_path` helper:
```erb
```
**Note:** In order for your styles or static assets files to be available in your view,
you would need to link them in your "pack" or entry file.
### Development
Webpacker ships with two binstubs: `./bin/webpack` and `./bin/webpack-dev-server`.
Both are thin wrappers around the standard `webpack.js` and `webpack-dev-server.js`
executable to ensure that the right configuration file and environment variables
are loaded depending on your environment.
In development, Webpacker compiles on demand rather than upfront by default. This
happens when you refer to any of the pack assets using the Webpacker helper methods.
That means you don't have to run any separate process. Compilation errors are logged
to the standard Rails log.
If you want to use live code reloading, or you have enough JavaScript that on-demand compilation is too slow, you'll need to run `./bin/webpack-dev-server` or `ruby ./bin/webpack-dev-server` if on windows,
in a separate terminal from `bundle exec rails s`. This process will watch for changes
in the `app/javascript/packs/*.js` files and automatically reload the browser to match.
```bash
# webpack dev server
./bin/webpack-dev-server
# watcher
./bin/webpack --colors --progress
# standalone build
./bin/webpack
```
Once you start this development server, Webpacker will automatically start proxying all
webpack asset requests to this server. When you stop the server, it'll revert to
on-demand compilation again.
You can use environment variables as options supported by
[webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the
form `WEBPACKER_DEV_SERVER_