Vite Plugin
The Vite plugin statically extracts styles at build time into optimized CSS with full HMR support.
Install
Section titled “Install”bun add typewritingclassbun add -d typewritingclass-compilerpnpm add typewritingclasspnpm 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, Solid, Svelte, Vue, etc.):
plugins: [react(), twcPlugin()]Virtual CSS module
Section titled “Virtual CSS module”Import once in your entry file:
// src/main.ts or src/main.tsximport 'virtual:twc.css'If TypeScript complains, add a declaration:
/// <reference types="vite/client" />declare module 'virtual:twc.css' {}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 generation —
virtual:twc.csscollects all extracted rules, deduplicates, orders by layer, and returns the final CSS. - 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 fails on a file, the original source is preserved and typewritingclass/inject is prepended for runtime fallback.