Static Extraction
The Tamagui compiler performs “static extraction” - analyzing your code at build time to convert runtime JavaScript into optimized CSS classes. This guide explains what can be extracted and how to maximize optimization.What Can Be Extracted?
The compiler can extract props that can be evaluated at build time.✅ Fully Extractable
String Literals
Number Literals
Boolean Values
Theme Variables
Simple Ternaries
Object Spreads with Static Values
Conditional Spreads
Media Query Props
Pseudo-State Styles
Platform-Specific Styles
⚠️ Partially Extractable
These patterns can be partially optimized:Variables from Module Scope
Imported Constants
importsWhitelist:
Ternaries with Evaluation
❌ Not Extractable
These patterns require runtime and can’t be fully optimized:Function Calls
Props from Component Props
Complex Expressions
Dynamic Spreads
State & Hooks
Extraction Rules
The compiler follows these rules when extracting:Rule 1: Static Values Only
Values must be determinable at build time:Rule 2: Whitelisted Imports
Imported values need explicit whitelisting:Rule 3: No External Functions
Function calls bail out:Rule 4: Ternaries Must Be Normalized
Complex ternaries get simplified:Rule 5: Spreads Merge Forward
Later props override earlier ones:Optimization Patterns
Pattern 1: Use Static Variants
Define variants in styled() for best extraction:Pattern 2: Hoist Constants
Move calculations outside components:Pattern 3: Use Theme Tokens
Theme variables extract perfectly:Pattern 4: Leverage Media Queries
Use shorthand media props:Pattern 5: Conditional Spreads
Replace dynamic props with conditionals:Pattern 6: Split Dynamic and Static
Separate extracted and runtime props:styled() Component Extraction
The compiler can extract styles fromstyled() definitions:
What Gets Extracted from styled()
✅ Base styles (shown above)✅ Media queries
✅ Pseudo states (hover, press, focus)
❌ Variants (kept for runtime flexibility)
❌ Dynamic context (group styles, animations)
Dynamic Component Discovery
The compiler can find styled() components across files:Debugging Extraction
Enable Debug Mode
Adddebug prop to see what’s extracted:
Check Generated Code
Inspect the compiled output:Use Verbose Logging
Extraction Statistics
After compilation, you can see stats:Common Issues
Issue: Props Not Extracting
Symptom: Styles appear instyle={} instead of className
Causes:
- Props are dynamic (from component props)
- Using function calls
- Complex expressions
Issue: Import Not Recognized
Symptom: Components not optimized despite importing from ‘tamagui’ Cause: Package not incomponents array
Solution:
Issue: Constants Not Evaluating
Symptom: Imported constants treated as dynamic Cause: Import not whitelisted Solution:Issue: styled() Not Extracting
Symptom: styled() components not generating CSS Cause: Dynamic evaluation disabled Solution:Best Practices
- Prefer static values: Use literals and theme tokens
- Hoist constants: Move calculations outside renders
- Use variants: Define variations in styled() definitions
- Whitelist carefully: Only whitelist truly static imports
- Leverage media queries: Use
$sm,$mdprops instead of hooks - Split concerns: Separate static and dynamic props
- Test extraction: Use debug mode to verify optimization
Next Steps
- How It Works - Understand the compilation process
- Installation - Configure the compiler for your bundler