Before Rails 6
With Rails 5.1+, we could provide an option to use Webpacker as the compiler while creating a new rails application,
rails new myapp --webpack
Or to use it with existing Rails application,
# Gemfile gem 'webpacker', '~> 4.x'
And then install it with the Rails application,
bundle bundle exec rails webpacker:install
After Rails 6
When we create a new application with Rails 6, the Webpacker gem will be installed and
webpacker:install will be run by the Rails application generator.
By default, The
application.js pack in
The Action Cable stubs will be created in the
What is Webpacker?
Webpacker directory structure with Rails 6,
packs directory contains entry points for webpack. All the files in the
packs directory are compiled by Webpacker. The
require("@rails/ujs").start() require("turbolinks").start() require("@rails/activestorage").start() require("channels")
How to use Webpacker with packs?
For instance, with Rails 6, the
app/views/layouts/application.html.erb layout as follows,
It is encouraged to place the actual application logic in a relevant structure within
How to configure Webpacker?
The configuration information for Webpacker lies in
config/webpacker.yml. We can change options like source path, packs path and many more from the configuration file.
config/webpack directory. More on webpack configuration with Webpacker here.
In production mode
rake assets:precompile does the job. The
assets:precompile rake task runs
webpacker:compile by default to generate webpack compiled assets.
We can also use live reloading and hot module replacement in development mode with the help of
binstubs provided by Webpacker. More on that can be found here.