How do jsbundling-rails, cssbundling-rails, dartsass-rails and tailwindcss-rails hook into bin/rails assets:precompile and test:prepare, and what do SKIP_JS_BUILD and SKIP_CSS_BUILD change?
answer
- Rake enhance adds a prerequisite
- javascript:install runs before javascript:build
- test:prepare, else spec:prepare, else db:test:prepare
- SKIP_JS_BUILD, SKIP_CSS_BUILD, SKIP_YARN_INSTALL
- tailwindcss:build has no skip variable
basics
~20 sEach gem calls Rake's enhance to make its build task a prerequisite of assets:precompile and test:prepare, so the build runs first and a failure aborts the task. SKIP_JS_BUILD and SKIP_CSS_BUILD stop jsbundling, cssbundling and dartsass from attaching; tailwindcss-rails always attaches.
solid answer
~30 sWhen their Rake files load, the gems run `Rake::Task["assets:precompile"].enhance(["javascript:build"])` (likewise `css:build`, `dartsass:build`, `tailwindcss:build`), which adds a prerequisite: the build runs before Propshaft copies and digests, and a failing build raises and aborts the precompile. They hook `test:prepare`, or `spec:prepare` or `db:test:prepare` if that is what exists, so `bin/rails test` builds first. `javascript:build` itself depends on `javascript:install` (`bun install`, `yarn install`, `pnpm install` or `npm install`, chosen by lockfile, then by what is on PATH) unless `SKIP_YARN_INSTALL` or `SKIP_BUN_INSTALL` is set. `SKIP_JS_BUILD` stops jsbundling-rails attaching; `SKIP_CSS_BUILD` stops cssbundling-rails and dartsass-rails. tailwindcss-rails attaches unconditionally. Note `bin/rails test path/to/file` skips `test:prepare` entirely.
code
ruby · 6 lines# from jsbundling-rails' lib/tasks/jsbundling/build.rake
unless ENV["SKIP_JS_BUILD"]
if Rake::Task.task_defined?("assets:precompile")
Rake::Task["assets:precompile"].enhance(["javascript:build"])
end
endgo deeper
Recall that the bundling gems run their builds automatically before assets:precompile and before tests.
Explain Rake prerequisites via enhance, the install-then-build order, and how the package manager is chosen from lockfiles.
Design CI and image builds around the hooks: when to set SKIP_JS_BUILD or SKIP_YARN_INSTALL, and the stale-bundle risk of skipping blindly.
Weigh building assets inside precompile against a separate pipeline stage, considering cache reuse, build time and failure visibility.
## The mechanism: Rake prerequisites A Rake task can have **prerequisites**, tasks that run before its own body. `Rake::Task#enhance` with an array adds to that list. Every bundling gem uses it in its Rake file, so nothing in your app has to call the build explicitly: | Gem | Prerequisite it adds to `assets:precompile` | Skip variable | |---|---|---| | jsbundling-rails | `javascript:build` | `SKIP_JS_BUILD` | | cssbundling-rails | `css:build` | `SKIP_CSS_BUILD` | | dartsass-rails | `dartsass:build` | `SKIP_CSS_BUILD` | | tailwindcss-rails | `tailwindcss:build` | none | Because the build is a prerequisite, it finishes before Propshaft's precompile copies `app/assets/builds` into `public/assets`. If the build command exits non-zero, the task raises — for example *"jsbundling-rails: Command build failed, ensure `yarn build` runs without errors"* — and the precompile, and with it the image build or deploy, stops. ## What javascript:build and css:build actually run 1. **Install first.** `javascript:build` has `javascript:install` as its own prerequisite unless `SKIP_YARN_INSTALL` or `SKIP_BUN_INSTALL` is set; `css:build` does the same with `css:install`. 2. **Pick the tool.** jsbundling-rails checks lockfiles in order — `bun.lockb`, `yarn.lock`, `pnpm-lock.yaml`, `package-lock.json` — then falls back to whichever of bun, yarn, pnpm or npm is on `PATH`. cssbundling-rails requires both the tool on `PATH` and one of its lockfiles, and recognises both `bun.lockb` and the newer text `bun.lock`. 3. **Run the script.** `bun run build`, `yarn build`, `pnpm build` or `npm run build` for JavaScript; the `build:css` script for CSS. dartsass-rails and tailwindcss-rails run a binary shipped through a gem (`sass-embedded`, `tailwindcss-ruby`), so their build needs no Node at all. ## The test hook Each gem also enhances, in order of preference, `test:prepare`, else `spec:prepare`, else `db:test:prepare`. `bin/rails test` runs `test:prepare` before the suite, which is why a CI job with a fresh checkout still gets built assets for system tests. Two catches: - `bin/rails test` skips `test:prepare` when you pass a file path or a `-n` name filter, so running one system test file after editing TypeScript can exercise a stale bundle. - A test runner that never calls one of those tasks needs the build run explicitly, as the jsbundling-rails README notes. ## When the skip variables are useful - **The build already ran.** A CI pipeline that builds assets in an earlier step, or a Docker stage that compiled them, can set `SKIP_JS_BUILD=1` or `SKIP_CSS_BUILD=1` so `assets:precompile` only digests. - **Dependencies are already installed.** `SKIP_YARN_INSTALL=1` avoids a second `yarn install` when the image layer already ran it. - **The risk:** skipping the build while `app/assets/builds` is empty or stale makes precompile digest whatever is there. The deploy succeeds, then `javascript_include_tag "application"` raises `Propshaft::MissingAssetError` or the site ships last week's JavaScript. ## The property-management pipeline For a team with TypeScript through jsbundling-rails and a Sass theme through dartsass-rails, one `bin/rails assets:precompile` in the build does, in order: 1. `javascript:install` (`yarn install`), then `javascript:build` (`yarn build`) into `app/assets/builds/application.js`. 2. `dartsass:build`, compiling `app/assets/stylesheets/application.scss` into `app/assets/builds/application.css`. 3. Propshaft's precompile, digesting everything into `public/assets` and writing `.manifest.json`. The order of steps 1 and 2 depends on which gem's Rake file loads first; they are independent, so it does not matter. That the image needs Node and Yarn for step 1 is a Dockerfile concern. ## What clobber does `assets:clobber` is enhanced too: dartsass-rails and tailwindcss-rails add their own clobber tasks, which delete the `.css` files they built in `app/assets/builds`, so a clobber leaves no stale CSS for the next run to digest. ## Failure modes to recognise - **"Command install failed, ensure yarn is installed".** `javascript:install` ran and found no Yarn: the build image lacks Node tooling, or the lockfile names a tool you did not install. - **"Command build failed, ensure `yarn build` runs without errors".** The bundler itself failed, usually a TypeScript syntax error or a missing package; precompile stops here, which is the safe outcome. - **Precompile succeeds but the page raises `Propshaft::MissingAssetError`.** The build was skipped or wrote somewhere else; check the skip variables and the `--outdir` or output path in `package.json`. - **System tests see old behaviour.** A test file was run by path, so `test:prepare` did not rebuild; run `yarn build` first or run the whole suite. - **The build runs twice in CI.** A separate asset step plus `assets:precompile` both build; set the skip variables on the second run once you trust the first.
- Can SKIP_CSS_BUILD stop the Tailwind build during precompile?No. tailwindcss-rails enhances `assets:precompile` with `tailwindcss:build` without checking any environment variable, unlike cssbundling-rails and dartsass-rails, which honour `SKIP_CSS_BUILD`. To avoid a second Tailwind run you would have to accept it or restructure the build so precompile runs once.
- A deploy fails with "jsbundling-rails: Command install failed, ensure yarn is installed". What does that tell you?`javascript:install` ran as a prerequisite of `javascript:build` and found no working Yarn in the build environment, even though `yarn.lock` selected it. Install Node and Yarn in the build stage, switch the lockfile to the tool you do have, or set `SKIP_YARN_INSTALL` if dependencies were installed in an earlier step.
- You must add javascript:build to your deploy script, or precompile skips it
- A failing yarn build only logs a warning and precompile continues
- SKIP_CSS_BUILD also disables the tailwindcss-rails build
- bin/rails test always rebuilds assets, even for a single file
- dartsass-rails needs Node in the Docker image to compile Sass