Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Bundle and transpile JavaScript in Rails with esbuild, rollup.js, bun, or Webpack.

License

NotificationsYou must be signed in to change notification settings

rails/jsbundling-rails

Repository files navigation

UseBun,esbuild,rollup.js, orWebpack to bundle your JavaScript, then deliver it via the asset pipeline in Rails. This gem provides installers to get you going with the bundler of your choice in a new Rails application, and a convention to useapp/assets/builds to hold your bundled output as artifacts that are not checked into source control (the installer adds this directory to.gitignore by default).

You develop using this approach by running the bundler in watch mode in a terminal withyarn build --watch (and your Rails server in another, if you're not using something likepuma-dev). You can also use./bin/dev, which will start both the Rails server and the JS build watcher (along with a CSS build watcher, if you're also usingcssbundling-rails).

Whenever the bundler detects changes to any of the JavaScript files in your project, it'll bundleapp/javascript/application.js intoapp/assets/builds/application.js (and all other entry points configured). You can refer to the build output in your layout using the standard asset pipeline approach with<%= javascript_include_tag "application", defer: true %>.

When you deploy your application to production, the#"https://bun.sh" rel="nofollow">bun,npm,pnpm, oryarn), and then runs the build script defined inpackage.json to process all the entry points, as it would in development. The latter files are then picked up by the asset pipeline, digested, and copied into public/assets, as any other asset pipeline file.

This also happens in testing where the bundler attaches to thetest:prepare task to ensure the JavaScript has been bundled before testing commences. If your testing library of choice does not call thetest:prepare Rake task, ensure that your test suite runs#"auto">That's it!

You can configure your bundler options in thebuild script inpackage.json or via the installer-generatedbun.config.js for Bun,rollup.config.js for rollup.js orwebpack.config.json for Webpack (esbuild does not have a default configuration format, and we don't intend to use esbuild as an API in order to hack around it).

If you're already usingwebpacker and you're wondering if you should migrate tojsbundling-rails, have a look atthe high-level comparison. If you're looking to migrate from webpacker, see themigration guide.

If you want to use webpack features likecode splitting andhot module reloading, consider using the official fork ofwebpacker,shakapacker.

Installation

If you are installing esbuild, rollup, or webpack, you must already have node installed on your system. You will also need npx version 7.1.0 or later.

If you are using Bun, then you must have the Bun runtime already installed onyour system.

To get started run:

./bin/bundle add jsbundling-rails
./bin/rails #"auto">Or, in Rails 7+, you can preconfigure your new application to use a specific bundler withrails new myapp -j [bun|esbuild|rollup|webpack].

FAQ

Is there a work-around for lack of glob syntax on Windows?

The default build script for esbuild relies on theapp/javascript/*.* glob pattern to compile multiple entrypoints automatically. This glob pattern is not available by default on Windows, so you need to change the build script inpackage.json to manually list the entrypoints you wish to compile.

Why does bun/esbuild overwrite my application.css?

If youimport CSS in your application.js while using esbuild or Bun, you'll be creating both anapp/assets/builds/application.jsandapp/assets/builds/application.css file when bundling. The latter can conflict with theapp/assets/builds/application.css produced bycssbundling-rails. The solution is to either change the output file for bun/esbuild (and the references for that) or for cssbundling. Both are specified inpackage.json.

How can I reference static assets in JavaScript code?

Suppose you have an imageapp/javascript/images/example.png that you need to reference in frontend code built with esbuild.

  1. Create the image atapp/javascript/images/example.png.
  2. Inpackage.json, under"scripts" and"build", add the additional arguments:
    • --loader:.png=file This instructs esbuild to copy png files to the build directory.
    • --asset-names=[name]-[hash].digested This tells esbuild to append.digested to the file name so that sprockets or propshaft will not append an additional digest hash to the file.
  3. When esbuild runs, it will copy the png file to something likeapp/assets/builds/example-5SRKKTLZ.digested.png.
  4. In frontend code, the image is available for import by its original name:import Example from "../images/example.png".
  5. The image itself can now be referenced by its imported name, e.g. in React,<img src={Example} />.
  6. The path of the image resolves to/assets/example-5SRKKTLZ.digested.png, which is served by the asset pipeline.

License

JavaScript Bundling for Rails is released under theMIT License.


[8]ページ先頭

©2009-2025 Movatter.jp