Webpacker makes it easy to use the JavaScript pre-processor and bundler webpack 4.x.x+ 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 v5.x.x. Please refer to 4-x-stable branch for 4.x documentation.
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 `
$3
Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
/packs/application.js, include this at the top of the file:`js import 'core-js/stable' import 'regenerator-runtime/runtime' `
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
or tag, you can use the asset_pack_path helper:`erb
`
If you are using new webpack 4 split chunks API, then consider using
javascript_packs_with_chunks_tag helper, which creates html tags for a pack and all the dependent chunks.`erb <%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
`
Important: Pass all your pack names when using
javascript_packs_with_chunks_tag helper otherwise you will get duplicated chunks on the page.`erb <%# DO %> <%= javascript_packs_with_chunks_tag 'calendar', 'map' %>
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.
$3
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.
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/javascript/packs/*.js files and automatically reload the browser to match.`bash
webpack dev server
./bin/webpack-dev-server
watcher
./bin/webpack --watch --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 back to on-demand compilation.
You can use environment variables as options supported by webpack-dev-server in the form