Skip to content

Zero-Runtime Build Extraction

How to eliminate the typestyles runtime in production and emit a static CSS file

Without a bundler integration, typestyles injects CSS at runtime when components render. With @typestyles/vite, you can make production zero-runtime by default: if the plugin finds a convention entry file under the project root, it resolves an extraction module and defaults to mode: 'build' (runtime + HMR in dev, static CSS + no client injection on vite build). Apps that prefer injection-only can set mode: 'runtime' or omit a convention entry and any extract.modules list.

Typestyles supports an optional build extraction mode through its bundler integrations. When enabled:

  • All styles are extracted at build time and written to a static .css asset.
  • The client bundle swaps typestyles' stylesheet module for a no-op build via the bundler integrations, so the CSS insertion runtime is absent — not merely disabled.
  • No <style> injection happens in the browser — the CSS file is served directly.

The same recipe(), tokens.create(), and keyframes.create() APIs work identically in both modes.

Runnable examples (monorepo root, after pnpm install):

Bundler / app Command README
Vite + React pnpm vite-app dev examples/vite-app
Next.js + verify pnpm next-app build examples/next-app
esbuild pnpm esbuild-app test examples/esbuild-app
Rollup / Rolldown pnpm rollup-app build examples/rollup-app

See also examples/README.md and the doc ↔ example map.


Why runtime in dev and extraction in production?

Development: You want instant feedback. The typestyles runtime plus the Vite plugin’s HMR hooks let you change tokens or components without running a separate Node extraction step on every save.

Production: You want a plain .css file: normal browser caching, parallel download/parse with JS, and no style injection work on the main thread after load.

The Vite plugin implements this split when at least one extraction module is resolved (explicit extract.modules, or an auto-discovered convention entry): it defaults to mode: 'build', which only disables the runtime and emits CSS during vite build. vite dev keeps injection enabled.


How it works

  1. The bundler plugin (Vite, Rollup, Rolldown) scans your source files for typestyles imports.
  2. Those modules are bundled and executed in a Node.js subprocess.
  3. getRegisteredCss() collects all CSS that was registered during that execution.
  4. The collected CSS is written as a static asset (e.g. typestyles.css) and linked in the HTML.
  5. The __TYPESTYLES_RUNTIME_DISABLED__ flag is defined as "true" so the browser bundle uses a no-op sheet that never injects CSS.
  6. In the client bundle, the bundler integration redirects typestyles' stylesheet module to a no-op sheet, so the CSS insertion runtime is not just disabled — it is absent from the bundle (see The no-op sheet).

The no-op sheet

Every typestyles module reaches the stylesheet through one file, typestyles/dist/sheet.js. Next to it ships sheet-noop.js, which exports the same API but does nothing: no stylesheet state, no <style> plumbing, no insertion registry, no collision bookkeeping. In mode: 'build' client bundles, the bundler integration redirects the first file to the second. Serialization, class naming, and data-attribute generation are unchanged — producing the same class names / data attributes never required CSS registration, only computing them.

Full runtime (default) mode: 'build' client bundle
Class names, data attributes ✓ ✓ identical output
tokens.create, createTheme, keyframes registers CSS computes refs/classes, registers nothing
getRegisteredCss() full sheet always ''
DOM <style> plumbing, stylesheet state present absent — not in the graph at all
Dev-time collision warnings and bookkeeping present (non-production) absent

The swap replaces one leaf module, not the package. There is still a single copy of everything else, so every entry point is covered — typestyles, typestyles/css, typestyles/color, … — and they all share one registry. No import specifier is rewritten and there is no separate entry to import.

Automatic swap (bundler integrations)

When an integration resolves to mode: 'build' (the default when a convention entry or extract.modules resolves), it redirects the sheet in the client bundle. No source changes are needed. Two properties make the swap safe:

  • Output parity — class names and data attributes are computed identically with or without the sheet, so extraction CSS and client class names always agree.
  • Dev, hybrid and SSR are untouched — the swap exists only for client bundles in mode: 'build'. Development builds, mode: 'runtime' / mode: 'hybrid' and server/SSR/node builds keep the real runtime (and HMR) exactly as before. hybrid intentionally does not swap: dynamic runtime styles need real registration.

Extraction and SSR pipelines (typestyles/server, typestyles/build, typestyles/hmr) run in Node, which never applies the redirect, so they keep the full runtime. Same for @typestyles/react's internal imports.

Mechanics differ per integration, but the semantics are identical:

Integration How the redirect is applied
@typestyles/vite (and @typestyles/astro, which wraps it) resolveId hook on client builds (not SSR); re-points the resolved sheet to sheet-noop
@typestyles/rollup same resolveId approach (also works for Rolldown, which accepts Rollup plugins)
@typestyles/esbuild (also Bun) onResolve hook, skipped when platform: 'node' (Bun's bun / node targets)
@typestyles/webpack (also Rspack) module-factory hook on non-node targets that rewrites the resolved resource
@typestyles/next/build (withTypestyles / withTypestylesExtract) same webpack hook on client bundles only — server bundles keep the real runtime for SSR

The redirect is a plain relative-file swap on purpose: it needs no package imports/exports condition support, so it behaves the same in every bundler and does not affect tools that don't understand conditions (Parcel without flags, Metro, older Jest) — those simply keep the full runtime.

Leak detection

If the real sheet ends up in a mode: 'build' client bundle anyway (a pre-bundled dependency that inlines its own copy of typestyles, a conflicting alias, a second typestyles install), the Vite, Rollup and webpack integrations print a warning naming the module, so you find out at build time instead of by inspecting bundle size. typestyles' own test suite also bundles every client entry point in build mode and fails if the real sheet is present.

Package metadata

The published package declares a conservative sideEffects allowlist — hmr, server, build, and testing are the only entries marked as having side effects (e.g. import 'typestyles/hmr' registers its hooks on import, and must never be tree-shaken away). All other entry points and modules are side-effect-free, which lets consumer bundlers drop unreferenced dev machinery.

Compile-time transform (experimental)

Swapping the sheet removes CSS insertion, but a client bundle still runs recipe() and friends to compute class names, so the serializer, the variant and override machinery and every style config still ship. With compile: true the build removes those calls instead.

During extraction (the Node pass that already produces your CSS), typestyles records a small JSON plan for each recipe: its base classes, slots, dimensions and defaults — everything a call of that recipe needs. In client bundles, recipe({ name: 'button', ... }) is then replaced by createAttrsRecipeFromPlan(<plan>) from typestyles/lite (under 1 KB gzipped). The style config, the token tables only it used and the CSS machinery have no remaining references and tree-shake away. Class names and data-* attributes are identical; the CSS file is unchanged, and extraction still runs exactly once.

js
// vite.config.js — returns two plugins when compile is on
import typestyles from '@typestyles/vite';
export default { plugins: [typestyles({ compile: true })] };
Integration Option Notes
@typestyles/vite compile: true Runs after Vite's TypeScript/JSX transforms, so it handles app sources and dependencies.
@typestyles/rollup compile: true List the plugin after any TypeScript/JSX plugin: only plain-JS modules are rewritten.
@typestyles/esbuild (and Bun) compile: true Handles .js/.mjs/.cjs and .ts/.mts/.cts (types stripped with esbuild first, using your tsconfig); .jsx/.tsx are left alone.
@typestyles/webpack (and Rspack) compile: true A post loader, so it sees plain JS after ts-loader/babel/swc. Relies on the minifier (on by default in production) to drop the now-unused style factories.
@typestyles/next not yet

What is planned. Recipes whose call behavior is fully determined by names: attribute mode (dimensioned and slot recipes), flat and multi-slot recipes, and class-name modes (semantic, bem, template, hashed, compact, atomic) for dimensioned recipes with compound variants, plus slot recipes in semantic/bem/template. Hashed/compact/atomic flat, multi-slot and slot recipes are not planned yet.

What is left alone. Calls that can't be matched safely keep the full runtime: names that aren't string literals, configs with a spread or more than one name key, a name used in more than one scope (including by a recipe shape that isn't planned), and any API whose result is read at runtime from a call the transform can't see — notably tokens.createTheme() / tokens.declare() and anything wrapped in your own helper (createMyTheme({ name })). If that keeps a meaningful part of the runtime in your bundle, the Vite and Rollup integrations print a warning naming the modules that still reach it.

Known differences from the full runtime. The lite handle carries .vars (the same { name, var } refs, recorded in the plan), so assigning recipe vars per instance works. It does not carry recipe metadata (getComponentMeta), does not emit development warnings (unknown variant dimensions, invalid options), and exposes defaults from the recipe schema only. Apps that read those at runtime should leave compile off.

Parity is tested by running every plan shape against the real runtime for a grid of selections.


Theme extraction

Theme and token CSS needs no separate build step. Because extraction is execute-and-collect, everything a module registers when it runs — including the :root { --… } blocks from tokens.create and the .theme-* classes from createTheme — lands in the same static CSS asset as your component styles. Theme CSS is a byproduct of normal extraction across all bundler integrations: Vite, Rollup / Rolldown, esbuild / Bun, webpack / Rspack, Next.js, and Astro.

ts
// src/typestyles-entry.ts
import { createTypeStyles } from 'typestyles';

const { tokens } = createTypeStyles({ scopeId: 'app' });

export const color = tokens.create('color', { primary: '#0066ff', surface: '#ffffff' });
tokens.createTheme({
  name: 'dark',
  tokens: { color: { primary: '#66aaff', surface: '#111111' } },
});
css
/* typestyles.css (emitted at build time) */
:root {
  --color-primary: #0066ff;
  --color-surface: #ffffff;
}
.theme-dark {
  --color-primary: #66aaff;
  --color-surface: #111111;
}

This means TypeStyles never needs a "did you forget to build your theme" runtime warning. Some zero-runtime libraries treat themes as a separate compilation unit — Astryx, for example, falls back to runtime theme evaluation and warns when a theme wasn't pre-built with astryx theme build. In TypeStyles there is no unbuilt-theme state to fall back from: if your entry module reaches the theme, the theme is in the CSS file.

The one thing to get right is the same as for component styles: the modules calling tokens.create / createTheme must be reachable from your convention entry (or explicit extract.modules).

Design systems with many recipes: add a registry module (for example src/themeable-refs.ts) that side-effect-imports every recipe file and exports getRegisteredComponentRefs(ts) from your createTypeStyles runtime. Wire it with Vite extract.registeredComponentsModule: 'src/themeable-refs.ts', or set extract.include: 'allRegisteredComponents' to auto-append a convention registry file when present (src/themeable-refs.ts, src/typestyles/themeable-refs.ts, …). The plain typestyles-entry side-effect imports are not enough on their own — the registry module must pull in each recipe so recipe() runs during extract. Full theme + override walkthrough: End-to-end theming.

To guard against a theme module dropping out of the entry graph, assert on a token variable or theme class with verifyTypestylesBuild() (see Verify extraction in CI):

ts
verifyTypestylesBuild({
  root: process.cwd(),
  cssFile: 'dist/typestyles.css',
  requiredCssSubstrings: [':root { --color-primary:', '.theme-dark'],
});

Vite

Install the Vite plugin:

bash
npm install --save-dev @typestyles/vite

In vite.config.ts, either add a convention entry so the plugin can discover it, or set extract.modules yourself. When at least one module resolves, mode defaults to 'build' (runtime stays on during vite dev; extraction and zero-runtime apply on vite build).

Convention entry (minimal config): add a file at one of these paths (first match wins), re-exporting or importing every registration side effect — for example src/typestyles-entry.ts that imports your tokens and components. After the src/… entries, the same names are tried under a top-level styles/ folder (common in Next.js apps without src/).

text
src/typestyles-entry.ts
src/typestyles.ts
src/styles/index.ts
src/styles.ts
styles/typestyles-entry.ts
styles/typestyles.ts
ts
import { defineConfig } from 'vite';
import typestyles from '@typestyles/vite';

export default defineConfig({
  plugins: [
    // Discovers src/typestyles-entry.ts (etc.); mode defaults to 'build'
    typestyles(),
  ],
});

Explicit modules (multiple entries or non-standard paths):

ts
import { defineConfig } from 'vite';
import typestyles from '@typestyles/vite';

export default defineConfig({
  plugins: [
    typestyles({
      extract: {
        modules: ['src/styles/tokens.ts', 'src/styles/components.ts'],
        fileName: 'assets/typestyles-[hash].css', // optional; omitted → content-hashed asset
      },
    }),
  ],
});

On vite build, a local module that calls recipe() and is reachable from the app must also be reachable from the extract entry. The build fails with the module path and the recipe name. Set coverage: false when the extract is intentionally a subset:

ts
typestyles({ coverage: false });

Explicit modes are still available when you need them:

ts
typestyles({
  mode: 'runtime', // force injection-only (even if extract is set)
  extract: { modules: ['src/styles/index.ts'] },
});

Modes

Mode Description
"runtime" Default when no extraction modules resolve (no extract.modules, no discovered convention file). Injection only; no emitted CSS file.
"build" Default when at least one module resolves (explicit or discovered). CSS is extracted on vite build; runtime disabled only in production builds.
"hybrid" CSS is extracted AND the runtime is kept (useful for dynamic styles not known at build time).

Linking the CSS file

Add a <link rel="stylesheet" href="/typestyles.css" /> to index.html only when you need manual control — @typestyles/vite injects this link into HTML entry points when extraction is enabled. During vite dev, the dev server serves that stable URL. Production builds emit a content-hashed CSS asset and point the link at it. A literal extract.fileName (for example typestyles.css) opts out of hashing.


Rollup / Rolldown

Install the Rollup plugin:

bash
npm install --save-dev @typestyles/rollup

Add a convention entry file (for example src/typestyles-entry.ts) or pass explicit extract.modules. When a convention entry resolves, mode defaults to "build".

js
// rollup.config.js
import typestylesRollupPlugin from '@typestyles/rollup';

export default {
  input: 'src/main.ts',
  plugins: [
    typestylesRollupPlugin(), // discovers src/typestyles-entry.ts (etc.)
  ],
};

Explicit extraction:

js
typestylesRollupPlugin({
  mode: 'build',
  extract: {
    modules: ['src/styles/index.ts'],
  },
});

esbuild

Install the esbuild plugin:

bash
npm install --save-dev @typestyles/esbuild esbuild
js
// build.mjs
import { build } from 'esbuild';
import typestylesEsbuildPlugin from '@typestyles/esbuild';

await build({
  entryPoints: ['src/main.ts'],
  bundle: true,
  outfile: 'dist/index.js',
  plugins: [typestylesEsbuildPlugin()],
});

Convention entry discovery matches Vite and Rollup. The plugin writes typestyles.css to esbuild’s outdir (default: dist/typestyles.css).

Bun

Bun.build accepts esbuild-style plugins, so the same plugin works there with no extra package:

js
import { build } from 'bun';
import typestylesEsbuildPlugin from '@typestyles/esbuild';

await build({
  entrypoints: ['src/main.ts'],
  outdir: 'dist',
  target: 'browser',
  plugins: [typestylesEsbuildPlugin()],
});

The zero-runtime swap applies to target: 'browser' only. target: 'bun' and target: 'node' builds keep the full runtime, the same as esbuild's platform: 'node'.


webpack

Install the webpack plugin:

bash
npm install --save-dev @typestyles/webpack webpack
js
// webpack.config.mjs
import TypestylesWebpackPlugin from '@typestyles/webpack';

export default {
  entry: './src/index.ts',
  plugins: [new TypestylesWebpackPlugin()],
};

When a convention entry resolves, the plugin emits typestyles.css as a build asset and defines __TYPESTYLES_RUNTIME_DISABLED__ for client bundles.

Rspack

Rspack implements webpack's plugin API, so the same plugin is the Rspack integration — no extra package:

js
// rspack.config.mjs
import TypestylesWebpackPlugin from '@typestyles/webpack';

export default {
  entry: './src/index.ts',
  plugins: [new TypestylesWebpackPlugin()],
};

Extraction, the no-op sheet swap (non-node targets) and the leak warning all behave as they do in webpack.


Next.js

Install the Next.js integration:

bash
npm install --save-dev @typestyles/next

Use withTypestyles so behavior matches @typestyles/vite: when NODE_ENV === 'production' and a convention entry file exists under the project root (same discovery order as Vite — see the list above), the client bundle disables <style> injection and swaps in the no-op sheet (no CSS runtime in the browser bundle; see The no-op sheet). Server bundles keep the full runtime so SSR collection and per-route CSS keep working (withTypestylesExtract under the hood). In development, the config is left unchanged so the runtime stays on (no need to branch on NODE_ENV yourself).

js
// next.config.mjs
import { withTypestyles } from '@typestyles/next/build';

export default withTypestyles({
  // your existing config, e.g. transpilePackages: ['typestyles', '@typestyles/next'],
});

Run extraction before next build (CI or a prebuild script). Defaults match the Vite story: buildTypestylesForNext({ root }) discovers a convention entry, then writes app/typestyles.css and app/typestyles.manifest.json (override with cssOutFile, manifestOutFile, or modules when you need a custom layout).

When an app/ directory exists, extraction also emits per-route critical CSS under app/_typestyles/routes/ and upgrades the manifest to version 2 with a routes map. Disable with routeCss: false or customize routeCssOutDir.

ts
// scripts/typestyles-build.mts
import { buildTypestylesForNext } from '@typestyles/next/build';

await buildTypestylesForNext({ root: process.cwd() });

Import the emitted CSS from your App Router layout (import './typestyles.css'), or load route-scoped CSS at request time with getRouteCss (see SSR — per-route CSS).

For full manual control, withTypestylesExtract is still available (always merges production defines). discoverDefaultExtractModules and DEFAULT_EXTRACT_MODULE_CANDIDATES are re-exported from @typestyles/next/build (same as @typestyles/build-runner / @typestyles/vite).

Verify extraction in CI

Zero-runtime extraction is execute-and-collect: styles that never run during the build step are omitted from the CSS file. @typestyles/vite fails vite build when a local module reachable from the app calls recipe() and is missing from the extract entry (see Vite; opt out with coverage: false). Other registrations, and extracts from Rollup, esbuild, webpack, and Next, still need a check against the emitted file.

Use verifyTypestylesBuild() from @typestyles/build-runner (also re-exported from @typestyles/next/build) after your build step to fail fast when output is missing or incomplete:

ts
// scripts/typestyles-verify.mts
import { verifyTypestylesBuild } from '@typestyles/build-runner';

verifyTypestylesBuild({
  root: process.cwd(),
  cssFile: 'app/typestyles.css',
  manifestFile: 'app/typestyles.manifest.json',
  minBytes: 500,
  // App-specific: class names / token vars you expect from the entry graph
  requiredCssSubstrings: ['.my-scope-button {', ':root { --my-scope-color-primary:'],
});

Checks performed:

  • CSS file exists and meets minBytes (defaults to non-empty when omitted)
  • Optional requiredCssSubstrings — catch missing imports from the entry barrel
  • Optional manifest exists with version: 1 or version: 2 and css pointing at your stylesheet path
  • For v2 manifests, optional minRouteEntries and per-route CSS file checks

Wire it into package.json before next build (see @examples/next):

json
{
  "scripts": {
    "typestyles:build": "tsx ./scripts/typestyles-build.mts",
    "typestyles:verify": "tsx ./scripts/typestyles-verify.mts",
    "build": "pnpm typestyles:build && pnpm typestyles:verify && next build"
  }
}

For Vite/Rollup, pass the emitted asset path (e.g. dist/typestyles.css) and omit manifestFile unless you write one yourself.

Add TypestylesStylesheet to your root layout to handle streaming SSR (React 18 App Router):

tsx
// app/layout.tsx
import { TypestylesStylesheet } from '@typestyles/next/client';

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        <TypestylesStylesheet />
        {children}
      </body>
    </html>
  );
}

Entry module requirements

The modules array should list files that register styles either directly or by importing other style files. A common pattern is a single styles/index.ts barrel file:

ts
// src/styles/index.ts
export * from './tokens'; // tokens.create(...)
export * from './button'; // recipe(...)
export * from './card'; // recipe(...)
export * from './typography'; // recipe(...)
ts
// vite.config.ts
typestyles({
  extract: { modules: ['src/styles/index.ts'] },
});

Switching between modes

Typestyles is designed for incremental migration from runtime to build extraction:

  1. Start in runtime-only mode — omit a convention entry and any extract.modules (or set mode: 'runtime').
  2. Add a convention entry or extract — default build mode gives dev runtime + prod extraction in Vite.
  3. Use hybrid when you need a static baseline plus runtime for dynamic values.

Checking runtime disabled state

You can check whether the runtime is disabled at any point:

ts
// The RUNTIME_DISABLED variable is defined by the bundler plugin.
// This will tree-shake to `false` in runtime mode and `true` in build mode.
declare const __TYPESTYLES_RUNTIME_DISABLED__: string | undefined;

const isStaticCSS =
  typeof __TYPESTYLES_RUNTIME_DISABLED__ !== 'undefined' &&
  __TYPESTYLES_RUNTIME_DISABLED__ === 'true';

Limitations

  • Dynamic styles — styles that are created based on runtime data (e.g. user-provided values) cannot be extracted at build time. Use the hybrid mode or keep those styles in runtime mode.
  • Lazy routes — a dynamically imported module that calls recipe() is part of the app graph. On vite build it must also be reachable from the extract entry, or the build fails. Import it from the convention entry, or set coverage: false when that CSS should stay out of the production extract.
  • Server Components (Next.js) — TypestylesStylesheet handles streaming SSR for React Server Components. Ensure it is present in your root layout, or use build-time extraction. See SSR — RSC patterns.