Use ESM with importmap to manage modern JavaScript in Rails without transpiling or bundling
open sourceMITself-hostablefree
Importmap for Rails lets a Rails app use modern JavaScript libraries written as ES modules straight from the browser, without Node, npm, a bundler or a transpiler. config/importmap.rb maps logical names to versioned, digested files; bin/importmap pin downloads packages from a JavaScript CDN into vendor/javascript, and the asset pipeline (Propshaft) serves them. Many small files cache better than one bundle over HTTP/2. It's included in new Rails apps since Rails 7, and adds preloading and automatic Subresource Integrity.
Problems it solves
- use JavaScript packages in a Rails app without installing Node or running a bundler
- drop webpack and yarn from our Rails app's toolchain
- pin a JavaScript library version and vendor it into the repo
- a JavaScript change shouldn't bust the browser cache for every other script
- add integrity hashes to every JavaScript module we load
- preload the JavaScript modules a page needs
Features
- config/importmap.rb pins
- bin/importmap pin, unpin, outdated and audit
- downloads packages into vendor/javascript
- preloading pinned modules
- automatic Subresource Integrity
- local modules in app/javascript
Capabilities
| Has a command line tool | plan not known |
|---|
Alternatives
The same job
These do what Importmap for Rails does.
- Shakapacker Use Webpack to manage app-like JavaScript modules in Rails
