In a Rails 8.1 app with jsbundling-rails (esbuild) and cssbundling-rails, the Sass theme intermittently vanishes after a TypeScript file starts importing a CSS file; why, and how do you fix it?
answer
- two builds, one output folder
- esbuild emits CSS for CSS imported from JS
- both write builds/application.css
- last writer wins
- rename one output in package.json
basics
~20 sWhen an esbuild entry point imports CSS, esbuild writes a CSS file named after that entry point, app/assets/builds/application.css, which is the same file cssbundling-rails' Sass build writes. The two builds overwrite each other; rename one output in package.json.
solid answer
~40 sBoth gems use `app/assets/builds` with no namespacing. The esbuild script bundles `app/javascript/*.*` with `--outdir=app/assets/builds`; once `application.ts` imports a stylesheet, esbuild also emits `builds/application.css` with that CSS. cssbundling-rails' Sass script writes `builds/application.css` too. Whichever build ran last wins: in development each watcher overwrites the other's file on its own saves, and at precompile the result depends on prerequisite order. The jsbundling-rails README names this case. Fix it by giving one output another name in `package.json` — for example compile the theme to `builds/theme.css`, or the esbuild entry under a different name — and update the layout's tags; `stylesheet_link_tag :app` then links both files. Related trap: esbuild-copied images should use `--asset-names=[name]-[hash].digested` so Propshaft does not digest them a second time.
code
json · 6 lines{
"scripts": {
"build": "esbuild app/javascript/*.* --bundle --sourcemap --format=esm --outdir=app/assets/builds --public-path=/assets --loader:.png=file --asset-names=[name]-[hash].digested",
"build:css": "sass ./app/assets/stylesheets/application.sass.scss:./app/assets/builds/theme.css --no-source-map --load-path=node_modules"
}
}go deeper
Recall that every build gem writes into app/assets/builds and that two outputs with the same name overwrite each other.
Explain why esbuild emits application.css when TypeScript imports CSS, and how renaming one output in package.json fixes it.
Diagnose silent build collisions from bin/dev output and builds contents, and handle esbuild-copied assets with the .digested naming rule.
Treat the builds folder as a shared namespace: agree output names across tools so new build steps cannot silently clobber old ones.
## The setup A property-management app uses **jsbundling-rails** with esbuild for TypeScript and **cssbundling-rails** with Sass for its theme. Both install a script in `package.json`, and both write into the same folder: | Script | Command (abridged) | Writes | |---|---|---| | `build` (jsbundling-rails) | `esbuild app/javascript/*.* --bundle --outdir=app/assets/builds ...` | `builds/application.js`, `.js.map` | | `build:css` (cssbundling-rails, Sass) | `sass ./app/assets/stylesheets/application.sass.scss:./app/assets/builds/application.css ...` | `builds/application.css` | As long as the TypeScript imports only TypeScript, the outputs never meet. ## What changes when TypeScript imports CSS A developer adds a date-range component for lease terms and writes `import "./lease_calendar.css"` in a file that `application.ts` imports. When esbuild bundles an entry point whose module graph contains CSS, it emits a **second output beside the JavaScript**, named after the entry point: `app/assets/builds/application.css`. That is exactly the file the Sass build owns. Now two processes write one path: 1. In development, `bin/dev` runs `yarn build --watch` and `yarn build:css --watch`. Saving a TypeScript file rewrites `application.css` with the calendar's few rules; saving a Sass partial rewrites it with the theme. The page looks right or wrong depending on which file was saved last. 2. At `assets:precompile`, both `javascript:build` and `css:build` run as prerequisites. The order depends on which gem's Rake file loaded first, and the later one silently wins. 3. Nothing errors. Propshaft digests whatever `application.css` contains, and `stylesheet_link_tag :app` links it. The jsbundling-rails README lists this case under its FAQ, with the fix: change the output name for one of the two builds. ## Fixing it Pick one of these, all edits to `package.json` plus the matching tag: - **Rename the Sass output.** Change `build:css` to write `./app/assets/builds/theme.css`. With the Rails 8.1 layout's `stylesheet_link_tag :app`, both `application.css` (from esbuild) and `theme.css` are linked automatically, in sorted order; with an explicit `stylesheet_link_tag "application"` you add `"theme"`. - **Rename the esbuild entry point.** Rename `app/javascript/application.ts` to, say, `bundle.ts`, so esbuild writes `bundle.js` and `bundle.css`, and point `javascript_include_tag` at `"bundle"`. - **Stop importing CSS from TypeScript.** Move `lease_calendar.css` into the Sass theme with `@use`, so only one build produces CSS. After the change, clear `app/assets/builds` of the stale `application.css` (or run both builds once) so neither version lingers. ## The related trap: assets emitted by esbuild The same README covers images. If TypeScript imports `../images/building.png` and the build uses `--loader:.png=file`, esbuild copies the image into `builds` with its own hash and writes its URL into the bundle. Propshaft would then add its **own** digest to that file during precompile, so the URL inside the bundle no longer matches any file in `public/assets`. Adding `--asset-names=[name]-[hash].digested` makes esbuild name the file `building-5SRKKTLZ.digested.png`; Propshaft leaves files matching `-<hash>.digested.<ext>` alone, so the URL in the bundle stays valid. ## Diagnosing collisions in general 1. List `app/assets/builds` and note which script writes each file; a name with two writers is the bug. 2. Watch `bin/dev` output: two processes logging writes to the same path confirms it. 3. In a production build, inspect `public/assets/.manifest.json` for the entry and check the digested file's content. 4. Keep a short comment in `package.json` naming each output, so the next tool added does not reuse a name. ## Why the gems do not prevent it `app/assets/builds` is a convention, not a registry. Each gem's installer writes a sensible default and assumes it owns its file names. That keeps the setup simple and lets any tool participate, at the price of collisions when two tools pick the same default. ## Other collisions worth checking - **tailwindcss-rails plus cssbundling-rails.** The first writes `builds/tailwind.css`, the second `builds/application.css`; no clash by default, but two CSS pipelines are rarely intended. - **dartsass-rails builds with overlapping names.** Two `config.dartsass.builds` entries mapping to the same output file overwrite each other on every build. - **A hand-written file in `builds`.** The folder is git-ignored and regenerated; a file placed there by hand disappears from fresh clones and CI, so it works locally and fails everywhere else. - **Old outputs after a rename.** Renaming an output leaves the previous file in a developer's local `builds` until it is deleted; with `stylesheet_link_tag :app` that stale file keeps being linked locally. A short checklist when adding any new build tool: list the files it writes, compare them with what is already in `app/assets/builds`, and decide which tag links each one.
- Why does the collision cause no error anywhere?Each build simply writes its file and exits successfully. Propshaft digests whatever `app/assets/builds/application.css` holds at precompile, and `stylesheet_link_tag :app` links it. The only symptom is CSS content that depends on which build ran last.
- Why does --asset-names=[name]-[hash].digested matter for images esbuild copies?esbuild writes the image's URL into the bundle using its own hashed name. Propshaft treats any file named `-<hash>.digested.<ext>` as already digested and keeps its name, so the URL in the bundle still matches the file in `public/assets`. Without the suffix Propshaft would add a second digest and the bundle's URL would 404.
- esbuild never emits CSS, so the Sass build must be broken
- Propshaft merges the two application.css files during precompile
- assets:precompile fails loudly when two builds write the same file
- Propshaft re-digesting esbuild's copied images is harmless
- Running bin/rails assets:clobber prevents the collision permanently