Jsbundling-Rails

Jsbundling-Rails is a Rails-supported gem that enables JavaScript bundling using modern tools like esbuild, Webpack, or Rollup.

Table of Contents

What is Jsbundling-Rails in Ruby on Rails?

Jsbundling-Rails is a Rails-supported gem for managing and bundling JavaScript using external bundlers. It was introduced alongside Rails 7 to give developers flexibility in choosing their preferred JavaScript tooling.

Unlike Importmap, which avoids bundling, Jsbundling-Rails relies on bundlers to compile, combine, and optimize JavaScript files before they are served to the browser.

Rails integrates with tools like esbuild, Webpack, or Rollup, allowing developers to write modern JavaScript, use npm packages, and manage dependencies efficiently. Among these, esbuild is the most commonly recommended choice for new projects due to its exceptional build speed.

In short, Jsbundling-Rails brings traditional JavaScript build tooling into Rails in a streamlined way.

Why is Jsbundling-Rails Useful?

Some applications require advanced JavaScript features and dependencies that go beyond what import maps can handle.

Jsbundling-Rails is useful because it:

  • Supports modern JavaScript frameworks and libraries

  • Allows use of npm/yarn packages

  • Enables code splitting and optimization

  • Works well with complex front-end applications

  • Provides flexibility in choosing bundlers

  • Handles large-scale JavaScript efficiently

It is ideal for projects that need more control over JavaScript architecture.

How Jsbundling-Rails Works?

Jsbundling-Rails connects Rails with a JavaScript bundler to process and compile assets.

Key components:

  • Bundler Setup: Choose esbuild, Webpack, or Rollup

  • Package Management: Use npm or yarn for dependencies

  • Entry Points: Main JavaScript files (e.g., application.js)

  • Build Process: Bundler compiles and outputs optimized files into app/assets/builds/

  • Integration with Rails: Output files in app/assets/builds/ are picked up by Sprockets or Propshaft and served to the browser

Example

Scenario 1: Using an external JavaScript package with a bundler

Installing a package (via npm):

npm install lodash 

Using the package (app/javascript/application.js):

import _ from "lodash"

console.log(_.capitalize("hello from rails")) 

The bundler processes this import and includes lodash in the final bundled file.

Scenario 2: Writing and bundling custom JavaScript

Custom module (app/javascript/greet.js):

export function greet() { 
  console.log("Hello from custom JS") 
} 

Using the module (app/javascript/application.js):

import { greet } from "./greet" 

greet() 

The bundler combines these files into a single optimized output for the browser.

These examples show how Jsbundling-Rails enables structured, scalable JavaScript development with bundling.

Where to Use Jsbundling-Rails?

  • Applications using modern JavaScript frameworks (React, Vue, etc.)

  • Projects with complex front-end logic

  • Apps requiring npm/yarn packages

  • Large-scale applications with heavy JavaScript usage

  • When optimization (minification, tree-shaking) is needed

  • Teams comfortable with JavaScript build tools

In Summary

Jsbundling-Rails is a Rails-supported gem that integrates modern JavaScript bundlers like esbuild, Webpack, and Rollup into Rails, enabling efficient dependency management, optimization, and scalable front-end development. The bundled output is placed in app/assets/builds/ and served via Sprockets or Propshaft.

It's best suited for applications that require advanced JavaScript capabilities beyond simple setups. esbuild is generally the recommended bundler for new projects thanks to its speed advantage.