skip to content

When does importmap-rails stop fitting a Rails 8.1 app, for instance once the team wants TypeScript, JSX or packages that only ship CommonJS, and what signals tell you to switch to a bundler?

level: seniorimportance: should knowfreq 34%

answer

  1. the browser runs files exactly as served
  2. no transpile: no TypeScript or JSX
  3. packages need browser ESM builds
  4. JS only: CSS imports not handled
  5. jsbundling-rails writes app/assets/builds

basics

~20 s

importmap-rails serves files exactly as written, so it fits only code the browser can run directly: plain ES modules and packages with browser ESM builds. TypeScript, JSX, CommonJS-only packages, CSS imported from JavaScript or a need for tree-shaking point to jsbundling-rails.

solid answer

~40 s

Import maps only remap names; nothing transforms the file in between. So **TypeScript and JSX** cannot be used, because no step compiles them, and Rails 8.1 offers no transpile hook for importmap apps. Packages must exist as **browser ESM**: `bin/importmap pin` relies on the JSPM generator (or unpkg / jsDelivr) to provide such a build, and packages that need Node-specific resolution or a build step may not work. Only **JavaScript** is vendored, so a package that imports its own CSS from JavaScript needs manual handling. There is **no tree-shaking or code splitting** beyond per-file loading. When a team needs any of those, the move is `jsbundling-rails` (esbuild, bun, rollup or webpack) writing into `app/assets/builds`, which Propshaft then digests; the bundling setup itself is that gem's territory.

code

javascript · 10 lines
javascript
// app/javascript/controllers/stay_dates_controller.js  -- fine with import maps
import { Controller } from "@hotwired/stimulus"
import flatpickr from "flatpickr"

export default class extends Controller {
  connect() { flatpickr(this.element) }
}

// A .tsx component such as StayCalendar.tsx would need a compile step
// that importmap-rails does not have.

go deeper

for a junior

Recall that importmap-rails has no compile step, so TypeScript and JSX do not work with it.

for a middle

Explain the limits from the mechanism: names remapped, files untouched, packages need browser ESM builds, CSS not vendored.

for a senior

Name the concrete signals for switching, what moving to jsbundling-rails changes in the app, and why Propshaft stays.

for a principal

Make the platform call: a no-build default for a Hotwire app versus a bundler for a component-heavy front end, including CI and onboarding costs.

## What import maps actually do An import map is a lookup table: bare module name in, URL out. **importmap-rails** writes that table from `config/importmap.rb`, and Propshaft serves the files the table points at. Between the file on disk and the browser there is **no transformation** — Propshaft only digests names and rewrites asset URLs in CSS. That is the whole design: no Node, no build step, no bundle. It is also the boundary of where it fits. ## Where it stops fitting | Need | Why import maps cannot meet it | |---|---| | **TypeScript** | browsers do not run `.ts`; nothing in the importmap pipeline strips types | | **JSX / TSX** | JSX is not JavaScript the browser understands; it must be compiled | | **CommonJS-only packages** | the browser needs ES module builds; whether one exists depends on the package and the CDN generator | | **CSS imported from JS** | `bin/importmap pin` fetches only JavaScript modules; a package's `import "./styles.css"` is not handled | | **Tree-shaking, minification, code splitting** | each pinned file is served whole, one request per module | | **Many hundreds of modules** | preload lists and request counts grow with every file; a bundler collapses them | None of these is about Rails versions: they follow from the browser executing exactly what the server sends. ## Where it fits well - An app whose JavaScript is mostly **Stimulus controllers and Turbo**, the default Rails 8.1 stack. - A handful of third-party packages with good ESM builds, vendored by `bin/importmap pin`. - Teams that want no Node toolchain on developer machines, in CI or in the Docker image. - Caching per file: a change to one controller invalidates only that file's digest, not a whole bundle. ## Signals that it is time to switch 1. The team proposes TypeScript or a component library written in JSX. 2. A required package fails in the browser after pinning because its published build is CommonJS or expects a bundler. 3. Workarounds pile up: hand-copied CSS for packages, pins written by hand without version comments, patched vendored files. 4. The network panel shows dozens of module requests on first load even with preloading. One or two of these on a single page might be handled locally — a vendored file, a `preload: false` pin. Several, or a strategic decision such as TypeScript, mean the pipeline is wrong for the app. ## What switching involves, at the level this topic owns - Remove `importmap-rails`, `config/importmap.rb`, `bin/importmap` and the `javascript_importmap_tags` call; drop `stale_when_importmap_changes` from `ApplicationController`. - Install `jsbundling-rails` and choose esbuild, bun, rollup or webpack. Its build writes into `app/assets/builds`, which is on Propshaft's load path, and the layout links the built file with `javascript_include_tag`. - Move third-party code from `vendor/javascript` into `package.json` dependencies so the bundler resolves it. - The details of the bundling gem — its build script, `bin/dev`, `Procfile.dev` and the precompile hook — are a separate topic. ## Running both is possible but rarely worth it Nothing stops an app from keeping import maps for most pages and loading one bundled file for a heavy area such as an admin dashboard. It doubles the mental model, the CI checks (`bin/importmap audit` plus a JavaScript package audit) and the ways a module can fail to load. Most teams pick one: import maps while the code is browser-ready ES modules, a bundler once it is not. ## How to answer in an interview State the mechanism first — names are remapped, files are not transformed — and derive the limits from it. Then give the trade the default makes: no build tooling and fine-grained caching, in exchange for no transpiling and one request per module. ## Misconceptions worth correcting - **"Propshaft will compile it."** Propshaft digests and copies; the only rewriting it does is of asset references — CSS `url()`, source-map comments and the `RAILS_ASSET_URL` call in JavaScript. - **"JSPM converts any package."** The generator serves what it can build for the browser; packages that depend on Node built-ins or a bundler's resolution may still fail at runtime. - **"Switching means a new asset pipeline."** A bundler replaces import maps, not Propshaft: the built files land in `app/assets/builds` and Propshaft keeps digesting them. - **"Import maps are slow."** With preloading and HTTP/2 the request count is rarely the bottleneck for a typical Hotwire app; the real limit is the missing compile step.

  • Could you keep import maps and still use TypeScript by compiling .ts files yourself?
    Only by adding a compile step that writes plain `.js` somewhere on the asset load path, which reintroduces the Node toolchain import maps were chosen to avoid. At that point `jsbundling-rails` gives the same compile step with a supported layout (`app/assets/builds`) and build hooks.
  • A pinned package fails in the browser with an error about require or module. What is the likely cause?
    The file the CDN provided is not a browser ES module, often because the package publishes only CommonJS or expects a bundler to resolve it. Try pinning `--from` another provider, look for an ESM build of the package, or treat it as a sign the app needs a bundler.

  • importmap-rails compiles TypeScript with esbuild during assets:precompile
  • Propshaft strips TypeScript types when it digests .ts files
  • Any npm package works with import maps because JSPM converts everything
  • Import maps tree-shake unused exports out of vendored packages
  • Switching to a bundler means replacing Propshaft as well