NOTE: The master branch now hosts the code for v6.x.x. Please refer to 5-x-stable branch for 5.x documentation.
You can either add Webpacker during setup of a new Rails 5.1+ application using new
# Available Rails 5.1+ rails new myapp --webpack
Or add it to your
# Gemfile gem 'webpacker', '~> 5.x'
OR if you prefer to use master
Finally, run the following to install Webpacker:
bundle bundle exec rails webpacker:install
OR (on rails version < 5.0)
bundle exec rake webpacker:install
Optional: To fix "unmet peer dependency" warnings,
yarn.lockchanges, such as when pulling down changes to your local environment in a team settings, be sure to keep your NPM packages up-to-date:
/packs/application.js, include this at the top of the file:
import 'core-js/stable' import 'regenerator-runtime/runtime'
If you want to link a static asset foror
tag, you can use the
If you are using new webpack 4 split chunks API, then consider using
Important: Pass all your pack names when using
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.
Webpacker ships with two binstubs:
./bin/webpack-dev-server. Both are thin wrappers around the standard
webpack-dev-server.jsexecutables 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.
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
# webpack dev server ./bin/webpack-dev-server
./bin/webpack --watch --colors --progress
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 back to on-demand compilation.
You can use environment variables as options supported by webpack-dev-server in the form
WEBPACKER_DEV_SERVER_. Please note that these environmental variables will always take precedence over the ones already set in the configuration file, and that the same environmental variables must be available to the
WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpack-dev-server
By default, the webpack dev server listens on
localhostin development for security purposes. However, if you want your app to be available over local LAN IP or a VM instance like vagrant, you can set the
Note: You need to allow webpack-dev-server host as an allowed origin for
connect-srcif you are running your application in a restrict CSP environment (like Rails 5.2+). This can be done in Rails 5.2+ in the CSP initializer
config/initializers/content_security_policy.rbwith a snippet like this:
Rails.application.config.content_security_policy do |policy| policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development? end
Note: Don't forget to prefix
rubywhen running these binstubs on Windows
See docs/webpack for modifying webpack configuration and loaders.
Out of the box Webpacker ships with - development, test and production environments in
config/webpacker.ymlhowever, in most production apps extra environments are needed as part of deployment workflow. Webpacker supports this out of the box from version 3.4.0+ onwards.
You can choose to define additional environment configurations in webpacker.yml,
staging: <<: production depends on precompilation of packs prior to booting for performance. compile: false cache manifest.json performance cache_manifest: true compile staging a separate directory public_output_path: packs-staging>
or, Webpacker will use production environment as a fallback environment for loading configurations. Please note,NODE_ENVcan either be set toproduction,developmentortest. This means you don't need to create additional environment files insideconfig/webpacker/*and instead use webpacker.yml to load different configurations usingRAILS_ENV.
For example, the below command will compile assets in production mode but will use staging configurations fromconfig/webpacker.ymlif available or use fallback production environment configuration:RAILS_ENV=staging bundle exec rails assets:precompile
And, this will compile in development mode and load configuration for cucumber environment if defined in webpacker.yml or fallback to production configurationRAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile
Please note, binstubs compiles in development mode however rake tasks compiles in production mode.# Compiles in development mode unless NODE_ENV is specified ./bin/webpack ./bin/webpack-dev-server
compiles in production mode by default unless NODE_ENV is specified
bundle exec rails assets:precompile bundle exec rails webpacker:compile
Or to install the latest release (including pre-releases)
yarn add @rails/[email protected]
Webpacker ships with basic out-of-the-box integration. You can see a list of available commands/tasks by runningbundle exec rails webpacker.
Included install integrations:
See Integrations for further details.
The configuration for what webpack is supposed to compile by default rests on the convention that every file in
webpacker.ymlconfiguration is turned into their own output files (or entry points, as webpack calls it). Therefore you don't want to put anything inside
packsdirectory that you do not want to be an entry file. As a rule of thumb, put all files you want to link in your views inside "packs" directory and keep everything else under
Suppose you want to change the source directory from
frontendand output to
assets/packs. This is how you would do it:
# config/webpacker.yml source_path: frontend source_entry_path: packs public_output_path: assets/packs # outputs to => public/assets/packs
Similarly you can also control and configure
# config/webpacker.yml development: dev_server: host: localhost port: 3035
If you have
hmrturned to true, then the
stylesheet_pack_tagwill create the appropriate HTML tags.
If you are adding Webpacker to an existing app that has most of the assets inside
app/assetsor inside an engine, and you want to share that with webpack modules, you can use the
additional_pathsoption available in
config/webpacker.yml. This lets you add additional paths that webpack should lookup when resolving modules:
You can then import these items inside your modules like so:
// Note it's relative to parent directory i.e. app/assets import 'stylesheets/main' import 'images/rails.png'
Note: Please be careful when adding paths here otherwise it will make the compilation slow, consider adding specific paths instead of whole parent directory if you just need to reference one or two modules
Webpacker hooks up a new
assets:precompile, which gets run whenever you run
assets:precompile. If you are not using Sprockets,
webpacker:compileis automatically aliased to
assets:precompile. Similar to sprockets both rake tasks will compile packs in production mode but will use
RAILS_ENVto load configuration from
When compiling assets for production on a remote server, such as a continuous integration environment, it's recommended to use
yarn install --frozen-lockfileto install NPM packages on the remote host to ensure that the installed packages match the
We encourage you to contribute to Webpacker! See CONTRIBUTING for guidelines about how to proceed.
Webpacker is released under the MIT License.