One of the notable features of the recent Rails 7 release is CSS/JS bundling, which replaces Rails 6’s Webpacker.
Rails now creates projects that will add two parameters.
For example, to use esbuild and sass you can use the following command.
|
|
But most people are probably concerned about how to switch from an existing project to a new solution, as explained further below.
Script switching
Rails has added two gems cssbundling-rails
and jsbundling-rails
to support the bundling scheme. They depend on Rails >= 6, so you don’t need to upgrade to Rails 7 to use them. Add the following to the Gemfile.
After executing bundle install
, install the required front-end tools with the following command.
You can see what has changed by using git diff
, the things to note are.
- the CSS and JS build source files are now
app/assets/stylesheets/application.sass.scss
andapp/javascript/application.js
, which need to be migrated manually. app/assets/builds
is the place to store the compiled static files.- add a
bin/dev
script to start rails server and CSS/JS build process together withforeman
, the configuration file is Procfile.dev. - Check the tags in the layout and remove the webpacker reference.
If the project files are placed according to the default Rails 6 directory structure, you can see the CSS and JS build processes working if you start the development process with bin/dev
.
Manual Switching
If your project is more complex and scripted installation is not sufficient, then it can be handled manually. In reality the contents of cssbundling-rails
and jsbundling-rails
are just some installation scripts and Rake tasks, there is no runtime code. Understanding how to manually switch to CSS/JS bundling can make the configuration more compatible with the project’s needs. Here’s how to do it.
Adding a builds directory
Assume that the project’s static files are stored in the following structure.
First create the builds
directory to store the compiled files.
Add this directory to .gitignore
to avoid checking in the files inside.
Modify app/assets/config/manifest.js
to remove the following configuration.
|
|
Add the following configuration.
|
|
Now Assets pipeline knows to get the static files from the builds
directory.
Configuring sass
Installing sass
.
|
|
Add the following to package.json
.
Create the file /app/assets/stylesheets/application.scss
and migrate the contents of app/assets/stylesheets/application.css
into it.
Note that the require_tree
and require_self
comments provided by Assets Pipeline no longer work and should be replaced with @import
from sass
.
If it works, use yarn build:css
to see the CSS compiled successfully.
Configuring esbuild
Install esbuild
.
|
|
Add the following to package.json
.
Create the file app/javascript/application.js
and migrate the contents of app/javascript/packs/application.js
into it.
Note that if you used webpacker before, you may need to change the require syntax if you switch to esbuild, for example, import "channels"
should be changed to import ". /channels"
. Also webpack dependent statements will need to be changed, for example Stimulus’ Webpack Helpers will not be available.
If it works, you can see that the JS compilation was successful using yarn build:js
.
Adding the bin/dev script
Now you need to start three processes in your development environment, a rails server, a sass, and an esbuild. starting these three processes each time you develop can be tedious, so you can use some tools to manage them, here is foreman for example.
Add a bin/dev
file with the following content
Add executable permissions to bin/dev
.
|
|
Add the file Procfile.dev
with the following contents
This allows you to start three processes together with bin/dev
while developing.
Adding the Rake task
The last thing is for Assets Pipeline to properly invoke external builds when compiling static files.
Add the file lib/tasks/build.rake
with the following contents
|
|
If it works, build:css
and build:js
will now be executed automatically when you execute bin/rails assets:precompile
.
This is the process of manually switching between CSS/JS bundling. Depending on the complexity of your project, you may need to make changes accordingly. After the switch is complete, you can delete the webpacker-related packages and configuration.
Summary
After reading how to switch bundling manually, you should see that the bundling is done before the Assets Pipeline. After compiling, you put the files into the builds
directory and tell Assets Pipeline to read them from that directory.
This is a process that decouples the Rails main body from the front-end toolchain, so we can work with the front-end files in any way we like, as long as it’s finally incorporated into the Assets Pipeline management. This adds a great deal of flexibility and eliminates the need to fight the framework’s default configuration - it’s all a matter of choice.