Trousers

Bundlers and SSR

Both adapters use the same Rust compiler. Use the adapter for your build tool and load the stylesheet it emits.

esbuild

sh
npm install -D esbuild @trousers/esbuild
js
import { build } from 'esbuild';
import trousers from '@trousers/esbuild';

await build({
    entryPoints: ['src/app.tsx'],
    bundle: true,
    outdir: 'dist',
    jsx: 'automatic',
    jsxImportSource: '@trousers/react',
    plugins: [trousers()],
});

For this entry, esbuild writes dist/app.css next to dist/app.js. Link app.css in the HTML that loads the app. The JSX settings enable the css prop.

Rolldown

sh
npm install -D rolldown @trousers/rolldown
js
import { defineConfig } from 'rolldown';
import trousers from '@trousers/rolldown';

export default defineConfig({
    input: 'src/app.tsx',
    output: { dir: 'dist', format: 'es' },
    transform: {
        jsx: { runtime: 'automatic', importSource: '@trousers/react' },
    },
    plugins: [trousers()],
});

Rolldown emits one trousers.css asset for the live styles across the build. Use cssFileName to choose a different output path. Asset URL resolution and CSS minification can be handled by your CSS pipeline.

Server rendering

For separate client and server builds, run the Trousers adapter for both. Use trousers({ emitCss: false }) in the server build and link the client stylesheet in the server-rendered HTML. Build the shared styles from the same source so the generated class names match during hydration.

If a style is reachable only from the server, the client build cannot collect its CSS. Include that style in the build that produces the stylesheet. The Next.js example does this with a shared, precompiled style module.

Multiple esbuild entries

Read the entry's cssBundle field from esbuild's metafile when generating HTML. Use the buildEntries helper to build independent entry-specific stylesheets. Enable sourcemap to map emitted rules back to the style objects.

esbuild API · Rolldown API