Vite Plugin
The Vite plugin statically extracts styles at build time into optimized CSS with full HMR support.
Install
Section titled “Install”pnpm add typewritingclasspnpm add -D typewritingclass-compilerbun add typewritingclassbun add -d typewritingclass-compilernpm install typewritingclassnpm install -D typewritingclass-compilerConfigure
Section titled “Configure”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 matterplugins: [react(), twcPlugin()]Options
Section titled “Options”twcPlugin({ strict: true, // default: true -- errors on non-static cx() args})See Strict Mode for details.
How it works
Section titled “How it works”- Theme loading — imports theme modules and passes them to the Rust compiler.
- Transform — for each
.ts/.tsx/.js/.jsxfile withtypewritingclassimports, the Rust compiler extracts styles and replaces calls with class name strings. - CSS injection — the plugin automatically injects the extracted CSS via a virtual module. No manual imports needed.
- HMR — file changes invalidate the virtual module. The browser receives updated CSS without a full reload.
Source maps
Section titled “Source maps”Generated automatically via MagicString. DevTools trace CSS rules back to the original TypeScript source file and line.
Diagnostics
Section titled “Diagnostics”- Errors appear in Vite’s browser overlay and fail production builds.
- Warnings appear in the terminal.
| Diagnostic | Cause |
|---|---|
Cannot statically evaluate argument | A cx() arg is a variable. Wrap in dynamic() or disable strict mode. |
Unknown utility function | Unrecognized function inside cx(). |
Property conflict detected | Same CSS property set multiple times in one cx() call. |
Fallback
Section titled “Fallback”If the compiler cannot statically extract a file, the original source is preserved and the runtime injector is automatically prepended as a fallback.