songjiz / importmap-rails

Use ESM with importmap to manage modern JavaScript in Rails without transpiling or bundling.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Importmap for Rails

Import maps let you import JavaScript modules using logical names that map to versioned/digested files – directly from the browser. So you can build modern JavaScript applications using JavaScript libraries made for ESM without the need for transpiling or bundling. Without the need for such preprocessing, you can build advanced Rails applications without Webpack, Yarn, NPM, or any other part of the JavaScript toolchain. All you need is the asset pipeline that's already included in Rails.

With this approach you'll ship many small JavaScript files instead of one big JavaScript file. Thanks to HTTP2 that no longer carries a material performance penalty during the initial transport, and in fact offers substantial benefits over the long run due to better caching dynamics. Whereas before, any change to any JavaScript file included in your big bundle would invalidate the cache for the the whole bundle, now only the cache for that single file is invalidated.

There's native support for import maps in Chrome/Edge 89+, and a shim available for any browser with basic ESM support. So your app will be able to work with all the evergreen browsers.

Installation

  1. Add importmap-rails to your Gemfile with gem 'importmap-rails' (make sure it's included before any gems using it!)
  2. Run ./bin/bundle install
  3. Run ./bin/rails importmap:install

By default, all the files in app/assets/javascripts and the three major Rails JavaScript libraries are already mapped. You can add more mappings in config/initializers/importmap.rb.

Note: In order to use JavaScript from Rails frameworks like Action Cable, Action Text, and Active Storage, you must be running Rails 7.0+. This was the first version that shipped with ESM compatible builds of these libraries.

Usage

The import map is configured programmatically through the Rails.application.config.importmap assignment, which by default is setup in config/initializers/importmap.rb after running the installer. (Note that since this is a config initializer, you must restart your development server after making any changes.)

This programmatically configured import map is inlined in the <head> of your application layout using <%= javascript_importmap_tags %>, which will setup the JSON configuration inside a <script type="importmap"> tag. After that, the es-module-shim is loaded, and then finally the application entrypoint is imported via <script type="module">import "application"</script>. That logical entrypoint, application, is mapped in the importmap script tag to the file app/assets/javascripts/application.js, which is copied and digested by the asset pipeline.

It's in app/assets/javascripts/application.js you setup your application by importing any of the modules that have been defined in the import map. You can use the full ESM functionality of importing any particular export of the modules or everything.

It makes sense to use logical names that match the package names used by NPM, such that if you later want to start transpiling or bundling your code, you'll not have to change any module imports.

Use with Hotwire

This gem was designed for use with Hotwire in mind. The Hotwire gems, like turbo-rails and stimulus-rails (both bundled as hotwire-rails), are automatically configured for use with importmap-rails. This means you won't have to manually setup the path mapping in config/initializers/importmap.rb, and instead can simply refer to the logical names directly in your app/assets/javascripts/application.js, like so:

import "@hotwired/turbo-rails"
import "@hotwired/stimulus-autoloader"

Use with Skypack (and other CDNs)

Instead of mapping JavaScript modules to files in your application's path, you can also reference them directly from JavaScript CDNs like Skypack. Simply add them to the config/initializers/importmap.rb with the URL instead of the local path:

Rails.application.config.importmap.draw do
  pin "trix", to: "https://cdn.skypack.dev/trix"
  pin "md5", to: "https://cdn.skypack.dev/md5"
end

Now you can use these in your application.js entrypoint like you would any other module:

import "trix"
import md5 from "md5"
console.log(md5("Hash it out"))

Preloading pinned modules

To mitigate the waterfall effect where the browser has to load one file after another before it can get to the deepest nested import, we use modulepreload links in browsers that support it. Pinned modules are preloaded by default, but you can turn this off with preload: false.

Example:

# config/initializers/importmap.rb
Rails.application.config.importmap.draw do
  pin "trix", to: "https://cdn.skypack.dev/trix"
  pin "md5", to: "https://cdn.skypack.dev/md5", preload: false
end

# app/views/layouts/application.html.erb
<%= javascript_importmap_tags %> 

# will include the following link before the importmap is setup:
<link rel="modulepreload" href="https://cdn.skypack.dev/trix">
...

Caching the import map and preload modules

The import map should be cached in production, and is so by default via the config.importmap.cached option that will be set to the same value as config.action_controller.perform_caching, unless explicitly set differently.

Expected errors from using the es-module-shim

While import maps are native in Chrome and Edge, they need a shim in other browsers that'll produce a JavaScript console error like TypeError: Module specifier, 'application' does not start with "/", "./", or "../".. This error is normal and does not have any user-facing consequences.

License

Importmap for Rails is released under the MIT License.

About

Use ESM with importmap to manage modern JavaScript in Rails without transpiling or bundling.

License:MIT License


Languages

Language:JavaScript 45.8%Language:Ruby 45.3%Language:HTML 7.4%Language:CSS 1.0%Language:Shell 0.4%