Do I need ImportMap Rails or JSBundling

Rails 7: Do I need ImportMap-rails?

If starting a new Rails 7 app and the answer to the above question is NO, you will be using JSBundling. When creating a new rails app, you will do so with the –css or –javascript flags when running rails new. Follow this guide for JSBundling.

If starting a new Rails 7 app and the answer to the above question is YES, you will NOT be using the --javascript=[---] and --css=[...] flags.

First, read this When Rails Ejected Node and the History of Compiled JS in Rails.

Next, this post will help you understand how to get started with ImportMap-Rails.

Advantages of Not Having Node Packages

• Deployments are much smaller and faster.

• You won’t need to have a tree of node dependancies in your pipeline.

When You Will Need Bundling

• If you have transpiled Typescript or React JSX.

• If you must rely on node bundles or are targeting older browsers.

Remember, Importmap can work for modern browsers only and requires that you install “pin” your JS in one of three ways: (1) Via a CDN, (2) from the vendor folder of a Gem, or (3) from your own vendor/javascript folder.

If the Javascript you want to use written in the ES Module style (that is, written with import/export statements), is available on a CDN, from a Gem, or you can bundle it in your own repository, you can use Importmap. If that applies to all of the JS in your app, then you can use only Importmap and don’t need to have any Node dependancies.

If, on the other hand, you have a reason to depend on Node node modules, you will need to go with bundling.