How the Compiler Works
The Tamagui compiler is a sophisticated build-time optimization system built on Babel. This guide explains the internal mechanics and optimization strategies.Architecture Overview
The compiler consists of several layers:Key Packages
@tamagui/static: Core extraction engine (code/compiler/static/)@tamagui/static-worker: Worker pool management@tamagui/vite-plugin: Vite integration@tamagui/next-plugin: Next.js integration@tamagui/metro-plugin: React Native/Metro integration
Compilation Pipeline
When a file is processed, it goes through these stages:1. Parse (Babel AST)
The source code is parsed into an Abstract Syntax Tree:2. Validate Imports
The extractor checks if the file imports Tamagui components:3. Traverse JSX Elements
The compiler walks the AST looking for JSX elements:4. Evaluate Props
Each prop is evaluated to determine if it can be extracted:5. Generate Styles
Extracted styles are converted to atomic CSS:6. Transform JSX
The JSX is rewritten with optimized attributes:7. Generate Output
Finally, code and CSS are generated:Optimization Strategies
Component Flattening
Tamagui components are replaced with native elements:- All props can be statically extracted
- No dynamic spreads are present
- No animation props exist
- Component accepts className (web)
Atomic CSS Generation
Styles are converted to atomic (single-property) CSS classes:- Extreme CSS reuse across components
- Tiny incremental cost for new components
- Optimal gzip compression
Media Query Optimization
Responsive props become CSS media queries:- Detects
$sm,$md, etc. props - Generates media query wrappers
- Merges rules with same breakpoint
Pseudo-State Extraction
Hover, focus, and press states become CSS pseudo-classes:Ternary Normalization
Conditional styles are optimized:- Normalizes ternaries into simplified forms
- Evaluates both branches statically
- Generates className conditionals
Theme Variable Handling
Theme tokens are converted to CSS variables:Caching Strategy
The Vite plugin implements sophisticated caching:- Shared: One cache across all Vite environments (SSR, client)
- Deduplicated: Concurrent requests for same file share work
- Size-limited: Cache clears at 64MB to prevent memory issues
Component Discovery
The compiler can discoverstyled() components dynamically:
Debug Output
In development, the compiler adds debug attributes:Bailout Conditions
The compiler skips optimization when:Dynamic Spreads
Complex Expressions
Animation Props
Group Styles
Platform Differences
Web Target
- Generates atomic CSS classes
- Outputs
.cssfiles - Uses
classNameprop - Optimizes for bundle size
Native Target
- Generates StyleSheet objects
- No className (uses style prop)
- Variables stay as-is (no CSS vars)
- Optimizes for runtime performance
Performance Metrics
Typical improvements with compiler enabled:- Bundle size: 20-40% smaller
- Initial load: 30-50% faster
- Runtime overhead: 80-95% reduced
- CSS file size: Atomic structure = high compression
Source Code Reference
Key files to explore:code/compiler/static/src/extractor/createExtractor.ts- Main extraction logic (~2000 lines)code/compiler/static/src/extractor/extractToClassNames.ts- Web className generationcode/compiler/vite-plugin/src/plugin.ts- Vite integration with cachingcode/compiler/next-plugin/src/withTamagui.ts- Next.js webpack configuration
Next Steps
- Static Extraction - Learn what can be extracted and optimization patterns
- Installation - Set up the compiler for your project