# Webpacker
[![Ruby specs](https://github.com/rails/webpacker/workflows/Ruby%20specs/badge.svg)](https://github.com/rails/webpacker/actions)
[![Jest specs](https://github.com/rails/webpacker/workflows/Jest%20specs/badge.svg)](https://github.com/rails/webpacker/actions)
[![Rubocop](https://github.com/rails/webpacker/workflows/Rubocop/badge.svg)](https://github.com/rails/webpacker/actions)
[![JS lint](https://github.com/rails/webpacker/workflows/JS%20lint/badge.svg)](https://github.com/rails/webpacker/actions)
[![node.js](https://img.shields.io/badge/node-%3E%3D%2010.17.0-brightgreen.svg)](https://www.npmjs.com/package/@rails/webpacker)
[![Gem](https://img.shields.io/gem/v/webpacker.svg)](https://rubygems.org/gems/webpacker)
Webpacker makes it easy to use the JavaScript pre-processor and bundler
[Webpack v5](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.
**NOTE:** The master branch now hosts the code for v6.x.x. Please refer to [5-x-stable](https://github.com/rails/webpacker/tree/5-x-stable) branch for 5.x documentation.
## Table of Contents
- [Prerequisites](#prerequisites)
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Development](#development)
- [Webpack Configuration](#webpack-configuration)
- [Custom Rails environments](#custom-rails-environments)
- [Upgrading](#upgrading)
- [Paths](#paths)
- [Resolved](#resolved)
- [Watched](#watched)
- [Deployment](#deployment)
- [Contributing](#contributing)
- [License](#license)
## Prerequisites
- Ruby 2.4+
- Rails 5.2+
- Node.js 10.22.1+ || 12+ || 14+
- Yarn 1.x+ || 2.x+
## Features
- [Webpack v5](https://webpack.js.org/)
- ES6 with [babel](https://babeljs.io/)
- Automatic code splitting using multiple entry points
- Asset compression, source-maps, and minification
- CDN support
- Rails view helpers
- Extensible and configurable
### Optional support
_requires extra packages to be installed_
- Stylesheets - Sass, Less, Stylus and Css, PostCSS
- CoffeeScript
- TypeScript
- React
## 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', '~> 6.x'
# OR if you prefer to use master
gem 'webpacker', git: 'https://github.com/rails/webpacker.git'
yarn add https://github.com/rails/webpacker.git
```
Finally, run the following to install Webpacker:
```bash
bundle
bundle exec rails webpacker:install
# OR (on rails version < 5.0)
bundle exec rake webpacker:install
```
Optional: To fix ["unmet peer dependency" warnings](https://github.com/rails/webpacker/issues/1078),
```bash
yarn upgrade
```
When `package.json` and/or `yarn.lock` changes, such as when pulling down changes to your local environment in a team settings, be sure to keep your NPM packages up-to-date:
```bash
yarn install
```
### Usage
Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
```yml
app/packs:
├── entrypoints:
│ # Only Webpack entry files here
│ └── application.js
│ └── application.css
└── src:
│ └── my_component.js
└── stylesheets:
│ └── my_styles.css
└── images:
└── logo.svg
```
You can then link the JavaScript pack in Rails views using the `javascript_pack_tag` helper. If you have styles imported in your pack file, you can link them by using `stylesheet_pack_tag`:
```erb
<%= javascript_pack_tag 'application' %>
<%= stylesheet_pack_tag 'application' %>
```
If you want to link a static asset for `` tag, you can use the `asset_pack_path` helper:
```erb
```
Or use the dedicated helper:
```erb
<%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
<%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>
```
If you want to create a favicon:
```erb
<%= favicon_pack_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png' %>
```
If you want to preload a static asset in your `
`, you can use the `preload_pack_asset` helper:
```erb
<%= preload_pack_asset 'fonts/fa-regular-400.woff2' %>
```
If you want to use images in your stylesheets:
```css
.foo {
background-image: url('../images/logo.svg')
}
```
Note, if you are using server-side rendering of JavaScript with dynamic code-spliting,
as is often done with extensions to Webpacker, like [React on Rails](https://github.com/shakacode/react_on_rails)
your JavaScript should create the link prefetch HTML tags that you will use, so you won't
need to use to `asset_pack_path` in those circumstances.
**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. Otherwise, Webpack won't know
to package up those files.
### 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`
executables to ensure that the right configuration files and environmental variables
are loaded based 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.
This means that you don't have to run any separate processes. Compilation errors are logged
to the standard Rails log. However, this auto-compilation happens when a web request
is made that requires an updated webpack build, not when files change. Thus, that can
be painfully slow for front-end development in this default way. Instead, you should either
run the `bin/webpack --watch` or run `./bin/webpack-dev-server`
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`.
Windows users will need to run these commands in a terminal separate from `bundle exec rails s`.
This process will watch for changes in the `app/packs/entrypoints/*.js` files and automatically
reload the browser to match. This feature is also known as
[Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/).
HMR is only the first step to running "Fast Refresh" with React. For more information
on how to configure rails/webpacker for Fast Refresh with React, see article
[HMR and React Hot Reloading](https://github.com/shakacode/react_on_rails/blob/master/docs/rails-webpacker-react-integration-options.md#hmr-and-react-hot-reloading).
```bash
# webpack dev server
./bin/webpack-dev-server
# watcher
./bin/webpack --watch --colors --progress
# standalone build
./bin/webpack
```
Once you start this webpack development server, Webpacker will automatically start proxying all
webpack asset requests to this server. When you stop this server, Rails will detect
that it's not running and Rails will revert back to on-demand compilation _if_ you have
the `compile` option set to true in your `config/webpacker.yml`
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_