How It Works
styled-static uses a Vite plugin to transform your styled components at build time through AST parsing and virtual CSS modules. This page explains the complete transformation pipeline.Architecture Overview
The transformation happens in several stages:Plugin Order: Post-React Transform
The plugin usesenforce: 'post' to run after the React plugin:
React.createElement() calls. This means:
- Vite’s built-in parser (acorn) can parse the code without JSX syntax
- Works with ALL file types:
.js,.jsx,.ts,.tsx,.mjs,.cjs - Zero dependencies needed for parsing
Transformation Pipeline
Step 1: Quick Import Check
Before parsing, the plugin performs a fast string check:Step 2: AST Parsing
The plugin uses Vite’s built-in parser (Rollup’s acorn):Step 3: Import Detection
The parser walks the AST to find styled-static imports and their local names:Step 4: Template Detection & Classification
The plugin finds all tagged template literals and classifies them: Supported template types:Step 5: Variant Detection
The plugin also detects variant calls:Step 6: CSS Extraction & Hashing
For each template, the plugin extracts the CSS content and generates a unique class name:/home/daytona/workspace/source/src/hash.ts:8.
Step 7: CSS Wrapping
The CSS is wrapped appropriately based on type:Step 8: Virtual CSS Module Creation
Each styled component gets a unique virtual CSS module:resolveId() and load() hooks.
Code Generation
The plugin generates different code based on template type:styled.element → Inline Component
Object.assign()creates a function with a static.classNameproperty- The function is the component (takes props, returns React element)
m()merges base class with user’s className prop.classNameproperty enables manual composition
styled(Component) → Extended Component
.className property includes the base component’s class for proper cascade order.
css → String Literal
keyframes → String Literal
createGlobalStyle → Null Component
Virtual CSS Module Loading
Development Mode
In dev mode, virtual modules return JavaScript that injects CSS into the DOM:- Instant HMR (no page reload)
- Source mapping via
sourceURLcomment - Proper cleanup on hot reload
Production Mode
In production, virtual modules return raw CSS for Vite’s CSS pipeline:- Minification
- Autoprefixing (if Lightning CSS enabled)
- Deduplication
- Code splitting
- Asset extraction
className Cascade Order
When components are extended, classes are ordered for proper CSS cascade:- Base styles first (
.ss-abc) - Extension styles second (
.ss-def) - overrides base - User className last (
custom) - overrides all
!important or specificity hacks.
Source Maps
The plugin generates high-resolution source maps using MagicString:- Debugging original source in browser DevTools
- Proper stack traces in errors
- Accurate code coverage reports
Memory Management
The plugin cleans up stale CSS modules during HMR:Performance Optimizations
1. Fast Import Check
Avoids parsing files that don’t use styled-static (string search is faster than AST parsing).2. Hybrid Variant Strategy
For variants with many values, the plugin uses a hoisted static map:/home/daytona/workspace/source/src/codegen.ts:82-223 for implementation.
3. Skip node_modules
The plugin skips transforming files innode_modules to avoid processing third-party code.
Next Steps
- Learn about Static Extraction - deep dive into CSS extraction
- Learn about Zero Runtime - understand the minimal runtime