Skip to content

Vite Plugin

The Vite plugin statically extracts styles at build time into optimized CSS with full HMR support.

Terminal window
pnpm add typewritingclass
pnpm add -D typewritingclass-compiler
vite.config.ts
import { defineConfig } from 'vite'
import twcPlugin from 'typewritingclass-compiler'
export default defineConfig({
plugins: [twcPlugin()],
})

Add your framework plugin alongside it:

// React, Svelte, Vue -- order doesn't matter
plugins: [react(), twcPlugin()]
twcPlugin({
strict: true, // default: true -- errors on non-static cx() args
})

See Strict Mode for details.

  1. Theme loading — imports theme modules and passes them to the Rust compiler.
  2. Transform — for each .ts/.tsx/.js/.jsx file with typewritingclass imports, the Rust compiler extracts styles and replaces calls with class name strings.
  3. CSS injection — the plugin automatically injects the extracted CSS via a virtual module. No manual imports needed.
  4. HMR — file changes invalidate the virtual module. The browser receives updated CSS without a full reload.

Generated automatically via MagicString. DevTools trace CSS rules back to the original TypeScript source file and line.

  • Errors appear in Vite’s browser overlay and fail production builds.
  • Warnings appear in the terminal.
DiagnosticCause
Cannot statically evaluate argumentA cx() arg is a variable. Wrap in dynamic() or disable strict mode.
Unknown utility functionUnrecognized function inside cx().
Property conflict detectedSame CSS property set multiple times in one cx() call.

If the compiler cannot statically extract a file, the original source is preserved and the runtime injector is automatically prepended as a fallback.