View helpers #
<%= rspack_javascript_tag "application", "data-turbo-track": "reload" %>
<%= rspack_stylesheet_tag "application" %>rspack_javascript_tag adds defer by default. Both helpers take several entries (rspack_javascript_tag "application", "admin") and any option that javascript_include_tag or stylesheet_link_tag accepts. Keep rspack_stylesheet_tag in <head>.
Each build writes app/assets/builds/.rspack-manifest.json, listing the scripts and stylesheets of each entry. The helpers link exactly those files, so an entry without CSS gets no stylesheet link. Because the manifest is a dotfile, it never reaches public/assets. rspack serve writes its own .rspack-dev-manifest.json, which the helpers read while it runs, so a build alongside it (bin/rails test runs one) does not change what dev pages link.
Entry points #
Besides app/javascript/application.js (or .ts, .jsx, .tsx), every file in app/javascript/entrypoints/ becomes its own bundle, named after the file. Stylesheets work too, with no JavaScript needed:
app/javascript/application.js -> application.js
app/javascript/entrypoints/admin.ts -> admin.js
app/javascript/entrypoints/checkout.js -> checkout.js (+ checkout.bundle.css if it imports CSS)
app/javascript/entrypoints/print.scss -> print.bundle.css (no print.js)<%# app/views/layouts/admin.html.erb %>
<%= rspack_javascript_tag "admin" %>
<%= rspack_stylesheet_tag "print", media: "print" %>admin.js and admin.css side by side make one entry.
Several entries can share a page. Under rspack serve the helpers link one shared runtime.js, so hot updates reach every entry. A build makes each entry self-contained instead, so <entry>.js runs on its own. On a page with two entries, a module both of them import (Turbo, your Stimulus application, React) is then loaded and run twice, each entry with its own copy. To share one runtime in builds too, add this to rspack.config.mjs:
export default railsConfig((config) => {
config.optimization.runtimeChunk = { name: "runtime" };
});The helpers then link runtime.js before the entries, once per page. Only load the entries through the helpers after this, since <entry>.js no longer runs without runtime.js.
Restart the dev server after adding a new entry point.
Importing app code #
Import files in app/javascript from anywhere with #/, the subpath imports prefix that Rspack resolves from package.json:
import { formatDate } from "#/lib/dates" // app/javascript/lib/dates.js (or .ts)
import logo from "#/images/logo.svg"rspack:install adds "imports": { "#/*": "./app/javascript/*" } to package.json. rspack:install:typescript adds the same mapping to the paths of tsconfig.json, so type checking and your editor follow it too. Bare imports such as import "lib/dates" keep working as they did with importmap.
Stimulus #
bin/rails rspack:install:stimulusThis installs @hotwired/stimulus and adds app/javascript/controllers/autoload.js, imported from your entry. Every *_controller.js (or .ts) under app/javascript/controllers is registered automatically, with the usual naming: admin/user_card_controller.js becomes admin--user-card.
Under rspack serve, editing or adding a controller hot-swaps it in the open page, so form input and other page state survive.
autoload.js belongs to rspack_rails, like config/rspack/rails.mjs. rspack:sync and rspack:build rewrite it when the gem changes, and save any hand edits to tmp/rspack_rails/ first. A copy without a checksum line, as 0.17.0 and earlier wrote it, counts as edited and is saved too. To register controllers your own way, copy it to a file of your own, import that instead and delete autoload.js.
If your entry imported ./controllers (the stimulus-rails index), the installer switches it to ./controllers/autoload. stimulus-rails may still rewrite controllers/index.js when you run bin/rails generate stimulus, but nothing imports that file any more.
CSS #
Import CSS from JavaScript, and Rspack extracts it to app/assets/builds/<entry>.bundle.css:
// app/javascript/application.js
import "./styles/application.css"<%= rspack_stylesheet_tag "application" %>*.module.cssfiles are compiled as CSS modules. Import their class names as a namespace (import styles fromfails the build: a CSS module has no default export):JavaScript import * as styles from "./Button.module.css" button.className = styles.primary // a scoped name ending in "Button_module_css-primary", a short hash in production button.classList.add(styles["is-active"])Under
rspack serve, CSS changes apply without a page reload.The
.bundlesuffix keeps the output from clashing withapp/assets/stylesheets/application.css.
Tailwind CSS #
bin/rails rspack:install:tailwindThis installs tailwindcss and @tailwindcss/webpack, creates app/javascript/application.css with @import "tailwindcss";, imports it from your entry and adds rspack_stylesheet_tag "application" to the layout.
Classes in your views, helpers and JavaScript are picked up automatically. Tailwind skips what .gitignore lists, but a Docker build has no .gitignore, so application.css also leaves out tmp, log, storage and app/assets/builds with @source not lines. Without them, the image’s production CSS picks up unused classes from bootsnap’s cache in tmp/. Under rspack serve, editing a view reloads the page with the new classes already built, and CSS edits apply without a reload.
Coming from tailwindcss-rails? See Migrating.
Sass #
bin/rails rspack:install:sassThis installs sass-loader and sass-embedded and creates app/javascript/application.scss, imported from your entry.
- Import any
.scssor.sassfile from JavaScript.*.module.scssfiles are CSS modules, imported the same way (import * as styles from "./Card.module.scss"). app/assets/stylesheetsis a load path, so@use "variables"findsapp/assets/stylesheets/_variables.scss.- Inside partials,
url()is resolved from the file that was imported from JavaScript, not from the partial.
Coming from dartsass-rails? See Migrating. On Sprockets, see Sass files on Sprockets.
React #
bin/rails rspack:install:reactThis installs react, react-dom and React Fast Refresh, and adds app/javascript/components/ with a sample Hello component. Render any component from a view:
<%= react_component "Hello", name: "World" %>
<%= react_component "admin/Chart", { points: @points }, html: { class: "chart" } %>- Components are found by path, with no registration step:
admin/Chartisapp/javascript/components/admin/Chart.jsx(or.tsx). - They mount when their element enters the page and unmount when it leaves, so Turbo Drive, frames and streams work.
- Props are passed as JSON.
- Under
rspack serve, edits apply through Fast Refresh and components keep their state. - The loader,
app/javascript/components/mount.js, is kept up to date by rspack_rails the same way as the Stimulus autoloader. - With TypeScript set up, the installer also sets
"jsx": "react-jsx"intsconfig.jsonand adds@types/react.
React Compiler. To memoize components automatically with the React Compiler, set RSPACK_REACT_COMPILER=true (Rspack 2.1 or later). It runs in Rspack’s built-in SWC, with no Babel. Set it for every build, including assets:precompile on deploy, so development and production behave the same.
TypeScript #
.ts and .tsx files compile out of the box with Rspack’s built-in SWC loader, Stimulus controllers (*_controller.ts) included. SWC strips types without checking them. For type checking:
bin/rails rspack:install:typescript # tsconfig.json, typescript, a "typecheck" script
npm run typecheck- The installer adds
app/javascript/types/assets.d.ts, so imports of stylesheets, images and fonts type-check (import heroUrl from "./hero.png"is astring). - Type errors show in the dev server overlay while you work (
ts-checker-rspack-plugin). Builds stay fast and leave checking totypecheck. pathsaliases intsconfig.jsonare honored, so"@/*": ["./app/javascript/*"]makesimport "@/lib/api"work in both TypeScript and Rspack.
Images and fonts #
Import an image from JavaScript, or reference images and fonts from CSS with url():
body { background: url("./images/hero.png"); }
@font-face { font-family: Brand; src: url("./fonts/brand.woff2"); }- Files under 8 KB are inlined as data URIs.
- Larger ones are written to
app/assets/buildsas<name>-<hash>.digested.<ext>, and Propshaft serves them without fingerprinting them a second time. - A root-relative
url()is left for Rails: Rspack neither bundles nor fingerprints it. Propshaft looks it up inapp/assets, sourl("/logo.svg")becomes the fingerprintedapp/assets/images/logo.svg. A file that only exists inpublic/, such asurl("/icon.png"), stays as written. Propshaft logs “Unable to resolve” for it, which is harmless.
Web Workers #
Create a worker from a new URL() next to import.meta.url, and Rspack bundles the worker file with its imports as a chunk of its own:
// app/javascript/controllers/report_controller.ts
const worker = new Worker(new URL("../workers/report.worker.ts", import.meta.url), { name: "report" })
worker.postMessage({ rows })- The chunk is written as
<name>-<hash>.digested.js(report-…here, from thenameoption) and loaded from the same origin as the page:/rspack/underrspack serve, the assets path after a build. - A worker can import app code and packages like any other module, so code shared with the page lives in one file.
- TypeScript checks a worker against the
DOMlibrary fromtsconfig.json, wherepostMessagetakes a target origin. Call it as(self as unknown as Worker).postMessage(result)inside the worker.
What rspack:install changes #
- Creates
rspack.config.mjs(your config, built onconfig/rspack/rails.mjs; see Configuration) andapp/javascript/application.js. An existingapplication.ts,.jsxor.tsxis kept as the entry. - Adds
build,build:watchanddev(rspack serve) scripts topackage.json. - Adds
rspack_javascript_tag "application"to your layouts (ERB, Slim or Haml), and replaces Rails 8’sstylesheet_link_tag :app, which would also link Rspack’s bundles. - Adds a
js:process running the dev server toProcfile.dev, and sets upbin/dev. - Makes the
Dockerfileinstall Node.js and your node modules so Kamal deploys can precompile (Deploying), and makesbin/setupinstall node modules. - In a dev container, adds Node.js and forwards the dev server port (Dev containers).
- Installs
@rspack/cli,@rspack/coreand@rspack/dev-server.
Running it again is safe: it leaves your config, entry, layouts and Procfile.dev alone when they are already set up.