Rollup and Esbuild can both handle JavaScript files in a Rails app. I made some little experiments, both locally and in-production.

Β· Ruby  Β· 2 min read

Rollup vs Esbuild for Rails

Rollup and Esbuild can both handle JavaScript files in a Rails app. I made some little experiments, both locally and in-production.

Rollup and esbuild are two ways to produce an production-optimized JavaScript file, thanks to jsbundling-rails.

ESBuild vs Rollup locally

You can create a new Rails application, with rollup for handling JS assets, with

rails new myapp --database=postgresql -j=rollup -c=tailwind

postgresql is necessary if you want to try the final result in production.

For esbuild, try

rails new myapp --database=postgresql -j=esbuild -c=tailwind

Difference between ESBuild and Rollup locally

Both are very transparent IMHO. I wrote several articles about the best way to handle JS with Rails some years ago, now I have to admit those articles are deprecated.

It’s set-and-forget setup, you don’t have to care much about how JS files are handled. How great is that!

The only noticeable difference

Apart from this nice abstraction, I have noticed one difference though : by default, you can debug easily JS by navigation through uncompressed JS files, when you use rollup.

With Rollup :

β”œβ”€β”€ assets
β”‚   β”œβ”€β”€ application-e07fd.js
β”œβ”€β”€ javascript
β”‚   β”œβ”€β”€ controller
β”‚   β”‚   β”œβ”€β”€ application.js
β”‚   β”œβ”€β”€ turbo_streams
β”‚   β”‚   β”œβ”€β”€ focus.js
β”‚   β”‚   β”œβ”€β”€ log.js
β”‚   β”œβ”€β”€ darkener.js
β”‚   β”œβ”€β”€ sidebar.js

How good is that? I can debug whatever I want easily.

With Esbuild :

β”œβ”€β”€ assets
β”‚   β”œβ”€β”€ application-a42fc.js

You only have the compressed version with esbuild. Not a big deal, but here I prefer the rollup version.

Experimentation on production website

I created a production-ready app, from a template that has already images, Tailwind (for CSS) and some Stimulus controllers - so assets are here.

I created a first one with ESBuild, and another with Rollup

Results with Esbuild

So I deployed this template on a Heroku-like platform, and let’s see what displaying the Homepage means in terms of resources

StatusMethodFileTypeSize
200GET/html34.11kb
200GETapplication-73da2.csscss76.88kb
200GETapplication-e037f.jsjs514.80kb
200GETlogo-da45f.pngpng7.94kb

Ok, that’s a good comparison basis. Assets are compressed, so not so easy to read and debug, but after all we are in production mode.

Results with Rollup

StatusMethodFileTypeSize
200GET/html34.11kb
200GETapplication-73da2.csscss76.88kb
200GETapplication-e037f.jsjs494.52kb
200GETlogo-da45f.pngpng7.94kb

Ok, no surprise about HTML, CSS, and image.

But what about JS? The compression seems more efficient - remember this is the exact same app.

Moreover, it’s still possible to debug JS files (in the web developer tool of the browser) in production website.

I’m not sure this behaviour is intentional, or desirable. But I’m impressed that rollup still allow me to debug things, with a better compression result.

Summary

Both ESBuild and rollup will achieve the same result with Rails. They take care of assets, and you don’t have to care much about it. But rollup seems a little bit better in terms of performance and debug-ability.

Share:
Back to Blog

Related Posts

View All Posts Β»
The simplest turbo-frame example

The simplest turbo-frame example

Turbo frame is a powerful feature of Hotwire, here is a quick memo about how to follow conventions in the simplest case. Convention over configuration is powerful, but sometimes it is also a problem when you are not sure anymore about conventions ;) so I plan to release more "simplest *** Hotwire feature".

My honest opinion about Hatchbox

My honest opinion about Hatchbox

Hatchbox.io is a deployment platform for Ruby-on-Rails users. I used it recently, so for people ashamed of trying, here is my review.