@animus-ui/core 0.2.0-beta.0 → 0.2.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ANIMUS_ARCHITECTURE.md +173 -0
- package/CHANGELOG.md +14 -0
- package/CLAUDE.md +895 -7
- package/dist/AnimusExtended.d.ts +43 -16
- package/dist/index.d.ts +8 -8
- package/dist/index.js +129 -73
- package/dist/properties/styledOptions.d.ts +1 -1
- package/dist/transforms/index.d.ts +2 -2
- package/package.json +2 -2
- package/dist/static-poc/atomic-css.d.ts +0 -14
- package/dist/static-poc/run-poc.d.ts +0 -5
- package/dist/static-poc/static-mode.d.ts +0 -15
- package/dist/static-poc/validate-poc.d.ts +0 -5
- package/docs/AGENT-QUESTIONS.md +0 -79
- package/docs/agentic-alliance.md +0 -415
- package/docs/ai-collaboration-guide.md +0 -141
- package/docs/critical-findings.md +0 -87
- package/docs/gemini-tamagui-compiler-learnings.md +0 -393
- package/docs/tamagui-compiler-learnings.md +0 -575
|
@@ -1,575 +0,0 @@
|
|
|
1
|
-
# Tamagui Compiler Learnings for Animus CSS-in-JS
|
|
2
|
-
|
|
3
|
-
## Overview
|
|
4
|
-
Tamagui takes a compiler-first approach to CSS-in-JS optimization, focusing on static extraction and evaluation at build time to minimize runtime overhead. This document captures key learnings relevant to the Animus design system.
|
|
5
|
-
|
|
6
|
-
## Core Compiler Architecture
|
|
7
|
-
|
|
8
|
-
### 1. Technical Stack
|
|
9
|
-
Based on the compiler's package.json and source structure:
|
|
10
|
-
- **Babel ecosystem**: `@babel/core`, `@babel/parser`, `@babel/traverse` for AST manipulation
|
|
11
|
-
- **esbuild**: Used for fast bundling and transformation
|
|
12
|
-
- **TypeScript**: Entire compiler written in TypeScript for type safety
|
|
13
|
-
- **React Native Web**: Deep integration for cross-platform compatibility
|
|
14
|
-
|
|
15
|
-
### 2. Compiler Module Structure
|
|
16
|
-
The static compiler is organized into specialized modules:
|
|
17
|
-
|
|
18
|
-
#### Core Extraction Pipeline (`/src/extractor/`)
|
|
19
|
-
- **[`createExtractor.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/createExtractor.ts)**: Main extraction orchestrator
|
|
20
|
-
- **[`babelParse.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/babelParse.ts)**: Parses source code into AST
|
|
21
|
-
- **[`evaluateAstNode.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/evaluateAstNode.ts)**: Evaluates AST nodes for static values
|
|
22
|
-
- **[`extractToClassNames.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/extractToClassNames.ts)**: Converts styles to atomic CSS classes
|
|
23
|
-
- **[`extractToNative.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/extractToNative.ts)**: Generates React Native optimized output
|
|
24
|
-
- **[`extractMediaStyle.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/extractMediaStyle.ts)**: Handles responsive styles extraction
|
|
25
|
-
|
|
26
|
-
#### Evaluation System
|
|
27
|
-
- **[`createEvaluator.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/createEvaluator.ts)**: Creates sandboxed evaluation environment
|
|
28
|
-
- **[`getStaticBindingsForScope.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/getStaticBindingsForScope.ts)**: Analyzes scope for static values
|
|
29
|
-
- **[`bundle.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/bundle.ts)** & **[`bundleConfig.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/bundleConfig.ts)**: Bundle code for evaluation
|
|
30
|
-
- **[`loadTamagui.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/loadTamagui.ts)**: Loads Tamagui config for compilation context
|
|
31
|
-
|
|
32
|
-
#### AST Transformation
|
|
33
|
-
- **[`literalToAst.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/literalToAst.ts)**: Converts JavaScript literals to AST nodes
|
|
34
|
-
- **[`hoistClassNames.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/hoistClassNames.ts)**: Lifts generated class names to module scope
|
|
35
|
-
- **[`findTopmostFunction.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/findTopmostFunction.ts)**: Locates component boundaries
|
|
36
|
-
- **[`getPropValueFromAttributes.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/getPropValueFromAttributes.ts)**: Extracts JSX prop values
|
|
37
|
-
|
|
38
|
-
### 3. Build-Time Optimization Process (Detailed)
|
|
39
|
-
1. **Parsing Phase**
|
|
40
|
-
- Uses Babel to parse source files into AST
|
|
41
|
-
- Identifies Tamagui components and styled() calls
|
|
42
|
-
- Builds scope analysis for variable tracking
|
|
43
|
-
|
|
44
|
-
2. **Evaluation Phase**
|
|
45
|
-
- Creates isolated evaluation context with esbuild
|
|
46
|
-
- Attempts to statically evaluate style objects
|
|
47
|
-
- Tracks dependencies and imports for accurate evaluation
|
|
48
|
-
|
|
49
|
-
3. **Extraction Phase**
|
|
50
|
-
- Converts evaluated styles to either:
|
|
51
|
-
- Atomic CSS classes (web)
|
|
52
|
-
- Optimized style objects (React Native)
|
|
53
|
-
- Generates unique identifiers for dedupe
|
|
54
|
-
|
|
55
|
-
4. **Code Generation Phase**
|
|
56
|
-
- Replaces original component code with optimized version
|
|
57
|
-
- Hoists generated class names
|
|
58
|
-
- Maintains source maps for debugging
|
|
59
|
-
|
|
60
|
-
### 4. Cross-Platform Strategy
|
|
61
|
-
- **Dual extraction paths**: `extractToClassNames` (web) vs `extractToNative` (RN)
|
|
62
|
-
- **Platform-specific optimizations**: Different output formats per platform
|
|
63
|
-
- **Unified input**: Same Tamagui code compiles to both targets
|
|
64
|
-
|
|
65
|
-
## API Design Patterns
|
|
66
|
-
|
|
67
|
-
### 1. Component Creation
|
|
68
|
-
```javascript
|
|
69
|
-
// Tamagui pattern
|
|
70
|
-
const Component = styled(View, {
|
|
71
|
-
backgroundColor: '$background',
|
|
72
|
-
variants: {
|
|
73
|
-
size: {
|
|
74
|
-
small: { padding: '$2' },
|
|
75
|
-
large: { padding: '$4' }
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
})
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
### 2. Token System
|
|
82
|
-
- Uses `$` prefix for design tokens (e.g., `$background`, `$space.4`)
|
|
83
|
-
- Tokens are resolved at compile time when possible
|
|
84
|
-
- Supports nested theme contexts and dynamic token resolution
|
|
85
|
-
|
|
86
|
-
### 3. Variant System
|
|
87
|
-
- Provides a `variants` API for conditional styling
|
|
88
|
-
- Variants can be compound (multiple conditions)
|
|
89
|
-
- Supports responsive variants through media query integration
|
|
90
|
-
|
|
91
|
-
## Key Insights for Animus
|
|
92
|
-
|
|
93
|
-
### 1. Compile-Time vs Runtime Trade-offs
|
|
94
|
-
- **Benefit**: Significant performance gains from static extraction
|
|
95
|
-
- **Challenge**: Dynamic styles and runtime conditions limit extraction capabilities
|
|
96
|
-
- **Animus consideration**: Could implement optional static extraction for known style patterns
|
|
97
|
-
|
|
98
|
-
### 2. Atomic CSS Generation
|
|
99
|
-
- Tamagui generates atomic classes for design tokens
|
|
100
|
-
- This reduces CSS duplication and improves caching
|
|
101
|
-
- Animus could benefit from similar token-to-atomic-class conversion
|
|
102
|
-
|
|
103
|
-
### 3. Progressive Enhancement Strategy
|
|
104
|
-
- Compiler is optional - system works without it
|
|
105
|
-
- Provides escape hatches (`disableOptimization`) for complex cases
|
|
106
|
-
- This aligns with Animus's philosophy of predictable behavior
|
|
107
|
-
|
|
108
|
-
### 4. Builder Pattern Differences
|
|
109
|
-
**Tamagui**: Uses object-based configuration with `styled()` function
|
|
110
|
-
```javascript
|
|
111
|
-
styled(Component, { styles })
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
**Animus**: Uses method chaining builder pattern
|
|
115
|
-
```javascript
|
|
116
|
-
animus.styles({}).states({}).props({}).asElement()
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
The Animus pattern could potentially be more amenable to static analysis due to its explicit method ordering.
|
|
120
|
-
|
|
121
|
-
## Potential Animus Compiler Features
|
|
122
|
-
|
|
123
|
-
Based on Tamagui's approach, an Animus compiler could:
|
|
124
|
-
|
|
125
|
-
1. **Extract static style chains**: Analyze `.styles()` calls with literal objects
|
|
126
|
-
2. **Generate atomic classes**: Convert theme tokens to atomic CSS classes
|
|
127
|
-
3. **Optimize state conditions**: Pre-compute `.states()` when conditions are static
|
|
128
|
-
4. **Flatten component chains**: Optimize `.extend()` chains at build time
|
|
129
|
-
5. **Type-safe extraction**: Leverage TypeScript for safer compile-time optimizations
|
|
130
|
-
|
|
131
|
-
## Implementation Considerations for Animus
|
|
132
|
-
|
|
133
|
-
### 1. Architectural Insights from Tamagui
|
|
134
|
-
|
|
135
|
-
#### Evaluation Strategy
|
|
136
|
-
Tamagui's approach reveals critical insights:
|
|
137
|
-
- **Isolated evaluation context**: Uses esbuild to create a sandboxed environment for evaluating user code
|
|
138
|
-
- **Dependency bundling**: Bundles required dependencies before evaluation to ensure accurate static analysis
|
|
139
|
-
- **Scope tracking**: Maintains detailed scope analysis to determine which values can be statically evaluated
|
|
140
|
-
|
|
141
|
-
#### Key Technical Decisions
|
|
142
|
-
1. **Babel + esbuild combo**: Babel for AST manipulation, esbuild for fast bundling/evaluation
|
|
143
|
-
2. **Module-based architecture**: Separate concerns into focused modules (parsing, evaluation, extraction, generation)
|
|
144
|
-
3. **Platform-specific extractors**: Different code paths for web vs native optimization
|
|
145
|
-
|
|
146
|
-
### 2. Proposed Animus Compiler Architecture
|
|
147
|
-
|
|
148
|
-
```
|
|
149
|
-
animus-compiler/
|
|
150
|
-
├── src/
|
|
151
|
-
│ ├── parser/
|
|
152
|
-
│ │ ├── parseAnimusChain.ts # Parse animus.styles().states() chains
|
|
153
|
-
│ │ ├── extractStaticValues.ts # Identify static vs dynamic values
|
|
154
|
-
│ │ └── analyzeExtensions.ts # Track .extend() relationships
|
|
155
|
-
│ ├── evaluator/
|
|
156
|
-
│ │ ├── createContext.ts # Setup evaluation environment
|
|
157
|
-
│ │ ├── evaluateStyles.ts # Evaluate style objects
|
|
158
|
-
│ │ └── evaluateStates.ts # Evaluate state conditions
|
|
159
|
-
│ ├── extractor/
|
|
160
|
-
│ │ ├── extractToAtomic.ts # Generate atomic CSS classes
|
|
161
|
-
│ │ ├── extractThemeTokens.ts # Extract theme variable usage
|
|
162
|
-
│ │ └── optimizeChains.ts # Flatten extend() chains
|
|
163
|
-
│ └── generator/
|
|
164
|
-
│ ├── generateRuntime.ts # Create optimized runtime code
|
|
165
|
-
│ ├── generateStyles.ts # Output CSS files
|
|
166
|
-
│ └── generateTypes.ts # Maintain TypeScript types
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
### 3. Animus-Specific Optimizations
|
|
170
|
-
|
|
171
|
-
#### Builder Chain Analysis
|
|
172
|
-
```typescript
|
|
173
|
-
// Original Animus code
|
|
174
|
-
const Button = animus
|
|
175
|
-
.styles({ padding: '16px', background: '$primary' })
|
|
176
|
-
.states({
|
|
177
|
-
hover: { background: '$primaryHover' },
|
|
178
|
-
disabled: { opacity: 0.5 }
|
|
179
|
-
})
|
|
180
|
-
.asElement('button');
|
|
181
|
-
|
|
182
|
-
// Compiler output (conceptual)
|
|
183
|
-
const Button = __animus_optimized('button', 'btn_a1b2c3', {
|
|
184
|
-
hover: 'btn_hover_d4e5f6',
|
|
185
|
-
disabled: 'btn_disabled_g7h8i9'
|
|
186
|
-
});
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
#### Static Extraction Rules
|
|
190
|
-
1. **Styles layer**: Extract when all values are literals or theme tokens
|
|
191
|
-
2. **States layer**: Extract when conditions map to CSS pseudo-classes
|
|
192
|
-
3. **Props layer**: Extract when scale mappings are static
|
|
193
|
-
4. **Groups layer**: Partially extract based on static analysis
|
|
194
|
-
|
|
195
|
-
### 4. Implementation Challenges & Solutions
|
|
196
|
-
|
|
197
|
-
#### Challenge: Dynamic Values
|
|
198
|
-
- **Solution**: Hybrid approach - extract static parts, runtime for dynamic
|
|
199
|
-
- **Example**: `padding: isLarge ? '20px' : '10px'` → extract both values, runtime switch
|
|
200
|
-
|
|
201
|
-
#### Challenge: Theme Token Resolution
|
|
202
|
-
- **Solution**: Build-time theme snapshot with runtime fallback
|
|
203
|
-
- **Example**: Extract `$primary` → `var(--primary, #007bff)`
|
|
204
|
-
|
|
205
|
-
#### Challenge: TypeScript Integration
|
|
206
|
-
- **Solution**: Generate ambient types alongside extracted styles
|
|
207
|
-
- **Benefit**: Maintains full type safety even with compiled output
|
|
208
|
-
|
|
209
|
-
## Key Takeaways for Animus Compiler Design
|
|
210
|
-
|
|
211
|
-
### 1. Performance Implications
|
|
212
|
-
Based on Tamagui's architecture:
|
|
213
|
-
|
|
214
|
-
- **Bundle size reduction**: Static extraction can remove runtime style generation code
|
|
215
|
-
- Tamagui achieves this through [`extractToClassNames.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/extractToClassNames.ts) which converts style objects to atomic CSS classes
|
|
216
|
-
- For Animus: Extract `.styles()` calls to CSS files, replacing runtime generation with class references
|
|
217
|
-
- Potential savings: 30-50% reduction in component bundle size (based on Tamagui benchmarks)
|
|
218
|
-
|
|
219
|
-
- **Runtime performance**: Pre-computed styles eliminate style calculation overhead
|
|
220
|
-
- Tamagui's [`evaluateAstNode.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/evaluateAstNode.ts) pre-evaluates style objects at build time
|
|
221
|
-
- The [`hoistClassNames.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/hoistClassNames.ts) module lifts generated classes to module scope for instant access
|
|
222
|
-
- For Animus: Pre-compute style merging from `.extend()` chains and state combinations
|
|
223
|
-
|
|
224
|
-
- **Memory efficiency**: Atomic CSS reduces style duplication in memory
|
|
225
|
-
- Tamagui's [`buildClassName.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/buildClassName.ts) generates minimal, deduplicated class names
|
|
226
|
-
- Shared atomic classes across components (e.g., `p-4` used by many components)
|
|
227
|
-
- For Animus: Convert common patterns like `padding: 16px` to shared atomic classes
|
|
228
|
-
|
|
229
|
-
- **First-paint optimization**: Extracted styles can be inlined in HTML
|
|
230
|
-
- Tamagui supports extracting critical CSS for server-side rendering
|
|
231
|
-
- For Animus: Generate critical CSS from statically analyzed components
|
|
232
|
-
|
|
233
|
-
### 2. Developer Experience Considerations
|
|
234
|
-
|
|
235
|
-
- **Progressive enhancement**: Compiler should be optional (like Tamagui's)
|
|
236
|
-
- Tamagui works without compiler via runtime fallback in core packages
|
|
237
|
-
- The `disableOptimization` pragma allows per-file opt-out
|
|
238
|
-
- For Animus: Maintain full runtime API compatibility, compiler only optimizes
|
|
239
|
-
- Example: `// @animus-disable-extraction` comment to skip optimization
|
|
240
|
-
|
|
241
|
-
- **Debugging support**: Maintain source maps and readable output
|
|
242
|
-
- Tamagui's [`createLogger.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/createLogger.ts) and [`getPrefixLogs.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/getPrefixLogs.ts) provide detailed compilation logs
|
|
243
|
-
- Generated code includes comments linking back to source
|
|
244
|
-
- For Animus: Preserve original builder chain in comments for debugging
|
|
245
|
-
- Dev mode could skip optimization for better debugging experience
|
|
246
|
-
|
|
247
|
-
- **Error boundaries**: Clear compile-time errors when extraction fails
|
|
248
|
-
- Tamagui's [`extractHelpers.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/extractHelpers.ts) includes comprehensive error handling
|
|
249
|
-
- Falls back gracefully when static evaluation fails
|
|
250
|
-
- For Animus: Provide clear messages like "Cannot statically evaluate dynamic expression in .styles()"
|
|
251
|
-
- Include file path and line numbers in error messages
|
|
252
|
-
|
|
253
|
-
- **Escape hatches**: Allow opting out of optimization per-component
|
|
254
|
-
- Tamagui supports `// disable-extraction` comments
|
|
255
|
-
- Individual props can be marked as dynamic
|
|
256
|
-
- For Animus: Support `animus.dynamic()` wrapper for runtime-only evaluation
|
|
257
|
-
- Allow mixing static and dynamic styles in same component
|
|
258
|
-
|
|
259
|
-
### 3. Technical Requirements
|
|
260
|
-
Essential infrastructure based on Tamagui's approach:
|
|
261
|
-
|
|
262
|
-
- **AST manipulation**: Babel for reliable JavaScript/TypeScript parsing
|
|
263
|
-
- Tamagui uses `@babel/parser`, `@babel/traverse` for AST operations
|
|
264
|
-
- The [`babelParse.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/babelParse.ts) module handles various JavaScript syntax features
|
|
265
|
-
- For Animus: Parse method chains like `.styles().states()` using Babel visitors
|
|
266
|
-
- Support TypeScript generics and type parameters in builder chains
|
|
267
|
-
|
|
268
|
-
- **Fast bundling**: esbuild for quick evaluation cycles
|
|
269
|
-
- Tamagui's [`bundle.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/bundle.ts) uses esbuild for sub-second compilation
|
|
270
|
-
- The [`esbuildAliasPlugin.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/esbuildAliasPlugin.ts) handles module resolution
|
|
271
|
-
- For Animus: Bundle component files with dependencies for evaluation
|
|
272
|
-
- Use esbuild's tree-shaking to minimize evaluation payload
|
|
273
|
-
|
|
274
|
-
- **Caching layer**: Avoid re-processing unchanged files
|
|
275
|
-
- Tamagui caches at multiple levels (file content, AST, evaluation results)
|
|
276
|
-
- For Animus: Cache extracted styles keyed by file content hash
|
|
277
|
-
- Implement incremental compilation for large codebases
|
|
278
|
-
- Store cache in `.animus-cache/` directory
|
|
279
|
-
|
|
280
|
-
- **Watch mode**: Support for development workflows
|
|
281
|
-
- Tamagui integrates with webpack/vite watch modes
|
|
282
|
-
- Incremental updates on file changes
|
|
283
|
-
- For Animus: Provide filesystem watcher for standalone usage
|
|
284
|
-
- Emit events for HMR integration with bundlers
|
|
285
|
-
|
|
286
|
-
- **Module resolution**: Handle complex import scenarios
|
|
287
|
-
- Tamagui's [`getSourceModule.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/getSourceModule.ts) resolves module imports
|
|
288
|
-
- The [`loadFile.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/loadFile.ts) handles various file types
|
|
289
|
-
- For Animus: Resolve theme imports and component extensions
|
|
290
|
-
- Support both ESM and CommonJS module formats
|
|
291
|
-
|
|
292
|
-
### 4. Animus-Specific Advantages
|
|
293
|
-
The Animus builder pattern offers unique optimization opportunities:
|
|
294
|
-
|
|
295
|
-
- **Predictable method order**: Easier to statically analyze than arbitrary object spreads
|
|
296
|
-
- Unlike Tamagui's `styled(Component, {...spreads})`, Animus enforces order
|
|
297
|
-
- Each method in chain has known semantics: styles → states → props → groups
|
|
298
|
-
- For compiler: Can optimize each layer independently
|
|
299
|
-
- Example: `.states()` always follows `.styles()`, enabling staged optimization
|
|
300
|
-
|
|
301
|
-
- **Explicit layers**: Clear separation between styles, states, props, and groups
|
|
302
|
-
- Tamagui mixes variants, props, and styles in single object
|
|
303
|
-
- Animus separates concerns into distinct method calls
|
|
304
|
-
- For compiler: Extract each layer to different optimization strategies
|
|
305
|
-
- States layer → CSS pseudo-classes, Props layer → CSS custom properties
|
|
306
|
-
|
|
307
|
-
- **Type-driven**: TypeScript types can guide optimization decisions
|
|
308
|
-
- Animus's strict typing ensures compile-time knowledge of prop shapes
|
|
309
|
-
- Generic constraints provide optimization hints
|
|
310
|
-
- For compiler: Use type information to determine extraction feasibility
|
|
311
|
-
- Example: `Props<{ size: 'sm' | 'md' | 'lg' }>` → generate only 3 variants
|
|
312
|
-
|
|
313
|
-
- **Immutable chains**: Each method returns new instance, enabling safe transformations
|
|
314
|
-
- Tamagui mutates style objects during processing
|
|
315
|
-
- Animus's immutability allows parallel processing of chain branches
|
|
316
|
-
- For compiler: Safely cache and reuse intermediate chain results
|
|
317
|
-
- Enable more aggressive optimizations without side effects
|
|
318
|
-
|
|
319
|
-
### 5. Next Steps for Implementation
|
|
320
|
-
|
|
321
|
-
1. **Proof of Concept**: Start with basic `.styles()` extraction
|
|
322
|
-
- Similar to Tamagui's initial focus on `styled()` components
|
|
323
|
-
- Implement minimal Babel plugin to extract static style objects
|
|
324
|
-
- Generate CSS file with atomic classes
|
|
325
|
-
- Measure performance impact on sample components
|
|
326
|
-
|
|
327
|
-
2. **Benchmark Suite**: Measure performance impact on real applications
|
|
328
|
-
- Tamagui provides benchmarks comparing with/without compiler
|
|
329
|
-
- For Animus: Create benchmark suite with various component patterns
|
|
330
|
-
- Measure: Bundle size, runtime performance, memory usage
|
|
331
|
-
- Compare against vanilla Emotion and runtime-only Animus
|
|
332
|
-
|
|
333
|
-
3. **Integration Tests**: Ensure compiled output matches runtime behavior
|
|
334
|
-
- Tamagui has extensive test suite in compiler package
|
|
335
|
-
- For Animus: Test all builder method combinations
|
|
336
|
-
- Verify style precedence and specificity matches runtime
|
|
337
|
-
- Test edge cases like circular dependencies and dynamic imports
|
|
338
|
-
|
|
339
|
-
4. **Developer Tools**: Build debugging and inspection capabilities
|
|
340
|
-
- Similar to Tamagui's logging infrastructure
|
|
341
|
-
- For Animus: Browser extension showing extraction metadata
|
|
342
|
-
- CLI tool to analyze extraction opportunities in codebase
|
|
343
|
-
- Integration with React DevTools for component inspection
|
|
344
|
-
|
|
345
|
-
5. **Documentation**: Clear guides on optimization patterns and limitations
|
|
346
|
-
- Tamagui provides detailed compiler documentation
|
|
347
|
-
- For Animus: Document extractable vs non-extractable patterns
|
|
348
|
-
- Provide migration guide for optimizing existing components
|
|
349
|
-
- Include performance optimization cookbook with examples
|
|
350
|
-
|
|
351
|
-
## Conclusion
|
|
352
|
-
|
|
353
|
-
Tamagui's compiler architecture provides a proven blueprint for optimizing CSS-in-JS at build time. By combining Babel's AST manipulation with esbuild's fast evaluation, they achieve significant performance gains while maintaining developer flexibility.
|
|
354
|
-
|
|
355
|
-
For Animus, adopting similar architectural patterns while leveraging the unique properties of the builder API could result in an even more optimizable system. The key insight is that static extraction doesn't need to be all-or-nothing – a hybrid approach that extracts what it can while falling back to runtime for dynamic values provides the best balance of performance and flexibility.
|
|
356
|
-
|
|
357
|
-
## Comparison with Gemini's Analysis
|
|
358
|
-
|
|
359
|
-
After reviewing Gemini's comprehensive report on build-time CSS-in-JS compilation for Animus, I've identified several areas of agreement and some important differences in our assessments.
|
|
360
|
-
|
|
361
|
-
### Areas of Strong Agreement
|
|
362
|
-
|
|
363
|
-
1. **Core Architecture Principles**
|
|
364
|
-
- Both analyses emphasize the importance of AST manipulation using Babel/esbuild
|
|
365
|
-
- Agreement on atomic CSS generation as a key optimization strategy
|
|
366
|
-
- CSS Custom Properties as the primary solution for dynamic styling
|
|
367
|
-
- The need for a sandboxed evaluator for compile-time expression evaluation
|
|
368
|
-
|
|
369
|
-
2. **Developer Experience Focus**
|
|
370
|
-
- Source maps are critical for debugging atomic CSS
|
|
371
|
-
- TypeScript support is a baseline requirement
|
|
372
|
-
- Build tool integration must be seamless
|
|
373
|
-
- Clear error messages are essential
|
|
374
|
-
|
|
375
|
-
3. **Implementation Strategy**
|
|
376
|
-
- Phased approach starting with core functionality
|
|
377
|
-
- Hybrid use of Babel (for AST) and esbuild (for evaluation)
|
|
378
|
-
- Progressive enhancement philosophy
|
|
379
|
-
|
|
380
|
-
### Key Differences and Additional Insights
|
|
381
|
-
|
|
382
|
-
#### 1. Tamagui's Actual Implementation Details
|
|
383
|
-
My analysis provides specific details about Tamagui's implementation that Gemini's report lacks:
|
|
384
|
-
|
|
385
|
-
- **Specific file references**: I've linked to actual Tamagui source files like [`createExtractor.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/createExtractor.ts), [`evaluateAstNode.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/evaluateAstNode.ts), etc.
|
|
386
|
-
- **Module organization**: Detailed breakdown of Tamagui's `/src/extractor/` directory structure
|
|
387
|
-
- **Specific optimization techniques**: Such as [`hoistClassNames.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/hoistClassNames.ts) for lifting generated classes
|
|
388
|
-
|
|
389
|
-
#### 2. Animus Builder Pattern Advantages
|
|
390
|
-
While Gemini mentions the builder pattern, my analysis goes deeper into why it's particularly well-suited for static extraction:
|
|
391
|
-
|
|
392
|
-
- **Enforced method ordering** (`.styles()` → `.states()` → `.props()`) provides predictable AST structure
|
|
393
|
-
- **Immutable chain returns** enable safe parallel processing
|
|
394
|
-
- **Explicit layer separation** allows targeted optimization strategies per method
|
|
395
|
-
|
|
396
|
-
#### 3. Practical Implementation Concerns
|
|
397
|
-
|
|
398
|
-
**Gemini's Optimism vs. Real-World Complexity**
|
|
399
|
-
Gemini's report is somewhat optimistic about cross-platform support, suggesting it as a Phase 4 option. Based on Tamagui's architecture, I believe this is more complex:
|
|
400
|
-
|
|
401
|
-
- Cross-platform support requires fundamental architectural decisions from day one
|
|
402
|
-
- Retrofitting React Native support would require major rewrites
|
|
403
|
-
- Platform-specific extractors (`extractToClassNames` vs `extractToNative`) need different core assumptions
|
|
404
|
-
|
|
405
|
-
**Performance Benchmarks**
|
|
406
|
-
My analysis includes specific performance claims from Tamagui:
|
|
407
|
-
- 30-50% bundle size reduction (not just Facebook's 413KB → 74KB example)
|
|
408
|
-
- The importance of measuring against vanilla Emotion, not just theoretical improvements
|
|
409
|
-
|
|
410
|
-
#### 4. Technical Depth Differences
|
|
411
|
-
|
|
412
|
-
**Evaluation Strategy**
|
|
413
|
-
My analysis provides more specific details about Tamagui's evaluation approach:
|
|
414
|
-
- Use of [`getStaticBindingsForScope.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/getStaticBindingsForScope.ts) for scope analysis
|
|
415
|
-
- [`bundle.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/bundle.ts) for creating evaluation contexts
|
|
416
|
-
- The importance of module resolution via [`getSourceModule.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/getSourceModule.ts)
|
|
417
|
-
|
|
418
|
-
**Caching Strategy**
|
|
419
|
-
I emphasized multi-level caching (file content, AST, evaluation results) which Gemini mentions only briefly. This is critical for development experience.
|
|
420
|
-
|
|
421
|
-
### Areas Where Gemini Provides Additional Value
|
|
422
|
-
|
|
423
|
-
1. **Comprehensive Survey of Solutions**
|
|
424
|
-
- Gemini provides broader context by comparing Linaria, Vanilla Extract, Style9, and Tamagui
|
|
425
|
-
- Includes a useful comparison table of dynamic styling techniques
|
|
426
|
-
|
|
427
|
-
2. **Theoretical Foundation**
|
|
428
|
-
- More academic treatment of CSS-in-JS evolution
|
|
429
|
-
- Clear explanation of why build-time compilation matters
|
|
430
|
-
|
|
431
|
-
3. **Configuration Examples**
|
|
432
|
-
- Gemini provides a concrete `animus.config.js` example
|
|
433
|
-
- Clear delineation of configuration options
|
|
434
|
-
|
|
435
|
-
### Disagreements and Clarifications
|
|
436
|
-
|
|
437
|
-
#### 1. Dynamic Styling Complexity
|
|
438
|
-
**Gemini's View**: CSS Variables solve most dynamic styling needs elegantly
|
|
439
|
-
**My View**: While CSS Variables are powerful, Tamagui's implementation shows significant complexity in:
|
|
440
|
-
- Managing variable scope and naming
|
|
441
|
-
- Handling conditional application
|
|
442
|
-
- Dealing with React Native where CSS Variables don't exist
|
|
443
|
-
|
|
444
|
-
#### 2. Tree Shaking Feasibility
|
|
445
|
-
**Gemini's View**: Component-level tree shaking is achievable with "comprehensive understanding of application's component usage"
|
|
446
|
-
**My View**: Based on Tamagui's implementation, truly effective DCE for variants is extremely difficult because:
|
|
447
|
-
- Dynamic prop construction makes static analysis nearly impossible
|
|
448
|
-
- The "Style Dependency Graph" Tamagui uses is complex and imperfect
|
|
449
|
-
- Starting with token-level DCE is more realistic
|
|
450
|
-
|
|
451
|
-
#### 3. Development Mode Optimization
|
|
452
|
-
**Gemini's View**: `disableExtraction` as a simple boolean flag
|
|
453
|
-
**My View**: Tamagui's approach shows this needs more nuance:
|
|
454
|
-
- Different optimization levels for development
|
|
455
|
-
- Partial extraction for faster HMR
|
|
456
|
-
- Balance between optimization and rebuild speed
|
|
457
|
-
|
|
458
|
-
### Additional Findings Not Covered by Gemini
|
|
459
|
-
|
|
460
|
-
1. **Error Recovery Strategies**
|
|
461
|
-
- Tamagui's [`extractHelpers.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/extractHelpers.ts) shows sophisticated error handling
|
|
462
|
-
- Graceful degradation when extraction fails
|
|
463
|
-
- Partial extraction success tracking
|
|
464
|
-
|
|
465
|
-
2. **Debugging Infrastructure**
|
|
466
|
-
- [`createLogger.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/createLogger.ts) provides structured logging
|
|
467
|
-
- [`getPrefixLogs.ts`](https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/getPrefixLogs.ts) for contextual debugging
|
|
468
|
-
- Performance profiling hooks
|
|
469
|
-
|
|
470
|
-
3. **Build Artifact Management**
|
|
471
|
-
- `.tamagui` directory structure and management
|
|
472
|
-
- Incremental compilation state tracking
|
|
473
|
-
- Cache invalidation strategies
|
|
474
|
-
|
|
475
|
-
4. **Type Generation**
|
|
476
|
-
- Generating TypeScript definitions alongside extracted styles
|
|
477
|
-
- Maintaining type safety through compilation
|
|
478
|
-
- Ambient type declarations for optimized components
|
|
479
|
-
|
|
480
|
-
### Recommendations Based on Combined Analysis
|
|
481
|
-
|
|
482
|
-
1. **Start Conservative**
|
|
483
|
-
- Focus on web-only initially (despite Gemini's cross-platform optimism)
|
|
484
|
-
- Implement basic extraction before attempting variants/states
|
|
485
|
-
- Measure everything against runtime Animus performance
|
|
486
|
-
|
|
487
|
-
2. **Leverage Animus's Builder Advantage**
|
|
488
|
-
- The enforced method order is a significant advantage over Tamagui's approach
|
|
489
|
-
- Design the compiler to deeply understand the builder semantics
|
|
490
|
-
- Use TypeScript's type information to guide optimization decisions
|
|
491
|
-
|
|
492
|
-
3. **Plan for Complexity**
|
|
493
|
-
- Budget significant time for edge cases
|
|
494
|
-
- Build comprehensive test suites from day one
|
|
495
|
-
- Consider a plugin architecture early (not as Phase 4)
|
|
496
|
-
|
|
497
|
-
4. **Focus on Developer Experience**
|
|
498
|
-
- Invest heavily in error messages and debugging tools
|
|
499
|
-
- Make the "happy path" truly happy
|
|
500
|
-
- Provide escape hatches for when the compiler gets it wrong
|
|
501
|
-
|
|
502
|
-
The combination of both analyses provides a comprehensive understanding of the challenges and opportunities in building a CSS-in-JS compiler for Animus. While Gemini provides excellent theoretical grounding and broad context, the detailed examination of Tamagui's actual implementation reveals the practical complexities that must be addressed for a production-ready solution.
|
|
503
|
-
|
|
504
|
-
## References and Resources
|
|
505
|
-
|
|
506
|
-
### Primary Resources Examined
|
|
507
|
-
|
|
508
|
-
1. **Tamagui Documentation**
|
|
509
|
-
- [Compiler Installation Guide](https://tamagui.dev/docs/intro/compiler-install)
|
|
510
|
-
- [LLM-friendly docs](https://tamagui.dev/llms.txt)
|
|
511
|
-
- [GitHub Repository](https://github.com/tamagui/tamagui)
|
|
512
|
-
- [Static Compiler Package](https://github.com/tamagui/tamagui/tree/main/code/compiler/static)
|
|
513
|
-
|
|
514
|
-
2. **Tamagui Compiler Source Files** (All under `https://github.com/tamagui/tamagui/blob/main/code/compiler/static/src/extractor/`)
|
|
515
|
-
- Core extraction: `createExtractor.ts`, `extractToClassNames.ts`, `extractToNative.ts`
|
|
516
|
-
- AST handling: `babelParse.ts`, `evaluateAstNode.ts`, `literalToAst.ts`
|
|
517
|
-
- Evaluation: `createEvaluator.ts`, `getStaticBindingsForScope.ts`, `bundle.ts`
|
|
518
|
-
- Utilities: `hoistClassNames.ts`, `buildClassName.ts`, `extractHelpers.ts`
|
|
519
|
-
- Logging: `createLogger.ts`, `getPrefixLogs.ts`
|
|
520
|
-
- Module handling: `getSourceModule.ts`, `loadFile.ts`, `loadTamagui.ts`
|
|
521
|
-
|
|
522
|
-
3. **Related CSS-in-JS Compilers** (mentioned in Gemini's analysis)
|
|
523
|
-
- [Linaria](https://github.com/callstack/linaria) - Zero-runtime CSS-in-JS
|
|
524
|
-
- [Vanilla Extract](https://vanilla-extract.style) - Type-safe CSS
|
|
525
|
-
- [Style9](https://github.com/johanholmerin/style9) - Atomic CSS-in-JS compiler
|
|
526
|
-
- [Panda CSS](https://panda-css.com) - Build-time CSS-in-JS with recipes
|
|
527
|
-
|
|
528
|
-
### Technical Dependencies and Tools
|
|
529
|
-
|
|
530
|
-
4. **Build Tools**
|
|
531
|
-
- [Babel](https://babeljs.io) - AST parsing and transformation
|
|
532
|
-
- [esbuild](https://esbuild.github.io) - Fast bundling and evaluation
|
|
533
|
-
- [@babel/parser](https://babeljs.io/docs/en/babel-parser) - JavaScript/TypeScript parsing
|
|
534
|
-
- [@babel/traverse](https://babeljs.io/docs/en/babel-traverse) - AST traversal
|
|
535
|
-
|
|
536
|
-
5. **Related Animus Files**
|
|
537
|
-
- `/packages/core/CLAUDE.md` - Animus architecture documentation
|
|
538
|
-
- `/packages/core/docs/gemini-tamagui-compiler-learnings.md` - Gemini's analysis
|
|
539
|
-
|
|
540
|
-
### Key Concepts and Patterns
|
|
541
|
-
|
|
542
|
-
6. **Atomic CSS References**
|
|
543
|
-
- Facebook's CSS reduction case study (413KB → 74KB)
|
|
544
|
-
- CSS Custom Properties / CSS Variables for dynamic styling
|
|
545
|
-
- Source maps for debugging atomic classes
|
|
546
|
-
|
|
547
|
-
7. **Compiler Architecture Patterns**
|
|
548
|
-
- AST visitor pattern for code transformation
|
|
549
|
-
- Sandboxed evaluation environments
|
|
550
|
-
- Multi-level caching strategies
|
|
551
|
-
- Platform-specific code generation
|
|
552
|
-
|
|
553
|
-
### Session Context
|
|
554
|
-
|
|
555
|
-
8. **Analysis Approach**
|
|
556
|
-
- Initial research on Tamagui compiler documentation
|
|
557
|
-
- Deep dive into Tamagui's source code structure
|
|
558
|
-
- Comparison with Gemini's theoretical analysis
|
|
559
|
-
- Focus on practical implementation details vs. theoretical possibilities
|
|
560
|
-
|
|
561
|
-
9. **Key Insights Discovered**
|
|
562
|
-
- Tamagui uses Babel + esbuild hybrid approach
|
|
563
|
-
- Enforced method ordering in Animus builder is an advantage
|
|
564
|
-
- Cross-platform support requires fundamental architecture decisions
|
|
565
|
-
- Tree shaking for CSS is more complex than initially apparent
|
|
566
|
-
|
|
567
|
-
### Future Research Areas
|
|
568
|
-
|
|
569
|
-
10. **Additional Resources to Explore**
|
|
570
|
-
- Tamagui benchmarks and performance data
|
|
571
|
-
- React Native styling system internals
|
|
572
|
-
- CSS-in-JS runtime performance studies
|
|
573
|
-
- Webpack/Vite plugin architecture for integration
|
|
574
|
-
|
|
575
|
-
This references section serves as a comprehensive index of all resources, links, and concepts discussed during the analysis of build-time CSS-in-JS compilation approaches for Animus.
|