figma-to-code-agent 0.3.0
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/CHANGELOG.md +55 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +198 -0
- package/README.md +149 -0
- package/dist/FigmaToCodeAgent.d.ts +55 -0
- package/dist/FigmaToCodeAgent.d.ts.map +1 -0
- package/dist/FigmaToCodeAgent.js +251 -0
- package/dist/FigmaToCodeAgent.js.map +1 -0
- package/dist/assets/index.d.ts +6 -0
- package/dist/assets/index.d.ts.map +1 -0
- package/dist/assets/index.js +7 -0
- package/dist/assets/index.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +261 -0
- package/dist/cli.js.map +1 -0
- package/dist/extraction/FigmaAPIClient.d.ts +53 -0
- package/dist/extraction/FigmaAPIClient.d.ts.map +1 -0
- package/dist/extraction/FigmaAPIClient.js +275 -0
- package/dist/extraction/FigmaAPIClient.js.map +1 -0
- package/dist/extraction/FigmaCache.d.ts +28 -0
- package/dist/extraction/FigmaCache.d.ts.map +1 -0
- package/dist/extraction/FigmaCache.js +120 -0
- package/dist/extraction/FigmaCache.js.map +1 -0
- package/dist/extraction/MCPClient.d.ts +75 -0
- package/dist/extraction/MCPClient.d.ts.map +1 -0
- package/dist/extraction/MCPClient.js +254 -0
- package/dist/extraction/MCPClient.js.map +1 -0
- package/dist/extraction/index.d.ts +9 -0
- package/dist/extraction/index.d.ts.map +1 -0
- package/dist/extraction/index.js +27 -0
- package/dist/extraction/index.js.map +1 -0
- package/dist/extraction/types.d.ts +122 -0
- package/dist/extraction/types.d.ts.map +1 -0
- package/dist/extraction/types.js +6 -0
- package/dist/extraction/types.js.map +1 -0
- package/dist/generation/AICodeOptimizer.d.ts +10 -0
- package/dist/generation/AICodeOptimizer.d.ts.map +1 -0
- package/dist/generation/AICodeOptimizer.js +33 -0
- package/dist/generation/AICodeOptimizer.js.map +1 -0
- package/dist/generation/ReactGenerator.d.ts +27 -0
- package/dist/generation/ReactGenerator.d.ts.map +1 -0
- package/dist/generation/ReactGenerator.js +423 -0
- package/dist/generation/ReactGenerator.js.map +1 -0
- package/dist/generation/VueGenerator.d.ts +23 -0
- package/dist/generation/VueGenerator.d.ts.map +1 -0
- package/dist/generation/VueGenerator.js +337 -0
- package/dist/generation/VueGenerator.js.map +1 -0
- package/dist/generation/index.d.ts +6 -0
- package/dist/generation/index.d.ts.map +1 -0
- package/dist/generation/index.js +7 -0
- package/dist/generation/index.js.map +1 -0
- package/dist/generation/types.d.ts +24 -0
- package/dist/generation/types.d.ts.map +1 -0
- package/dist/generation/types.js +3 -0
- package/dist/generation/types.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/llm/BedrockProvider.d.ts +8 -0
- package/dist/llm/BedrockProvider.d.ts.map +1 -0
- package/dist/llm/BedrockProvider.js +34 -0
- package/dist/llm/BedrockProvider.js.map +1 -0
- package/dist/llm/LLMFactory.d.ts +5 -0
- package/dist/llm/LLMFactory.d.ts.map +1 -0
- package/dist/llm/LLMFactory.js +21 -0
- package/dist/llm/LLMFactory.js.map +1 -0
- package/dist/llm/OpenAIProvider.d.ts +7 -0
- package/dist/llm/OpenAIProvider.d.ts.map +1 -0
- package/dist/llm/OpenAIProvider.js +34 -0
- package/dist/llm/OpenAIProvider.js.map +1 -0
- package/dist/llm/index.d.ts +5 -0
- package/dist/llm/index.d.ts.map +1 -0
- package/dist/llm/index.js +21 -0
- package/dist/llm/index.js.map +1 -0
- package/dist/llm/types.d.ts +23 -0
- package/dist/llm/types.d.ts.map +1 -0
- package/dist/llm/types.js +3 -0
- package/dist/llm/types.js.map +1 -0
- package/dist/transformation/ASTFactory.d.ts +92 -0
- package/dist/transformation/ASTFactory.d.ts.map +1 -0
- package/dist/transformation/ASTFactory.js +240 -0
- package/dist/transformation/ASTFactory.js.map +1 -0
- package/dist/transformation/ASTParser.d.ts +13 -0
- package/dist/transformation/ASTParser.d.ts.map +1 -0
- package/dist/transformation/ASTParser.js +177 -0
- package/dist/transformation/ASTParser.js.map +1 -0
- package/dist/transformation/TransformationPipeline.d.ts +31 -0
- package/dist/transformation/TransformationPipeline.d.ts.map +1 -0
- package/dist/transformation/TransformationPipeline.js +42 -0
- package/dist/transformation/TransformationPipeline.js.map +1 -0
- package/dist/transformation/index.d.ts +7 -0
- package/dist/transformation/index.d.ts.map +1 -0
- package/dist/transformation/index.js +25 -0
- package/dist/transformation/index.js.map +1 -0
- package/dist/transformation/transformers/AIComponentSplitter.d.ts +14 -0
- package/dist/transformation/transformers/AIComponentSplitter.d.ts.map +1 -0
- package/dist/transformation/transformers/AIComponentSplitter.js +71 -0
- package/dist/transformation/transformers/AIComponentSplitter.js.map +1 -0
- package/dist/transformation/transformers/AILayoutAnalyzer.d.ts +18 -0
- package/dist/transformation/transformers/AILayoutAnalyzer.d.ts.map +1 -0
- package/dist/transformation/transformers/AILayoutAnalyzer.js +125 -0
- package/dist/transformation/transformers/AILayoutAnalyzer.js.map +1 -0
- package/dist/transformation/transformers/AISemanticNamer.d.ts +12 -0
- package/dist/transformation/transformers/AISemanticNamer.d.ts.map +1 -0
- package/dist/transformation/transformers/AISemanticNamer.js +50 -0
- package/dist/transformation/transformers/AISemanticNamer.js.map +1 -0
- package/dist/transformation/transformers/ComponentExtractor.d.ts +16 -0
- package/dist/transformation/transformers/ComponentExtractor.d.ts.map +1 -0
- package/dist/transformation/transformers/ComponentExtractor.js +91 -0
- package/dist/transformation/transformers/ComponentExtractor.js.map +1 -0
- package/dist/transformation/transformers/FigmaLayerPreserver.d.ts +14 -0
- package/dist/transformation/transformers/FigmaLayerPreserver.d.ts.map +1 -0
- package/dist/transformation/transformers/FigmaLayerPreserver.js +48 -0
- package/dist/transformation/transformers/FigmaLayerPreserver.js.map +1 -0
- package/dist/transformation/transformers/FigmaStructureOptimizer.d.ts +16 -0
- package/dist/transformation/transformers/FigmaStructureOptimizer.d.ts.map +1 -0
- package/dist/transformation/transformers/FigmaStructureOptimizer.js +65 -0
- package/dist/transformation/transformers/FigmaStructureOptimizer.js.map +1 -0
- package/dist/transformation/transformers/FlattenTransformer.d.ts +15 -0
- package/dist/transformation/transformers/FlattenTransformer.d.ts.map +1 -0
- package/dist/transformation/transformers/FlattenTransformer.js +69 -0
- package/dist/transformation/transformers/FlattenTransformer.js.map +1 -0
- package/dist/transformation/transformers/LayoutOptimizer.d.ts +15 -0
- package/dist/transformation/transformers/LayoutOptimizer.d.ts.map +1 -0
- package/dist/transformation/transformers/LayoutOptimizer.js +63 -0
- package/dist/transformation/transformers/LayoutOptimizer.js.map +1 -0
- package/dist/transformation/transformers/SemanticNamer.d.ts +14 -0
- package/dist/transformation/transformers/SemanticNamer.d.ts.map +1 -0
- package/dist/transformation/transformers/SemanticNamer.js +65 -0
- package/dist/transformation/transformers/SemanticNamer.js.map +1 -0
- package/dist/transformation/transformers/SimplifyTransformer.d.ts +23 -0
- package/dist/transformation/transformers/SimplifyTransformer.d.ts.map +1 -0
- package/dist/transformation/transformers/SimplifyTransformer.js +138 -0
- package/dist/transformation/transformers/SimplifyTransformer.js.map +1 -0
- package/dist/transformation/types.d.ts +129 -0
- package/dist/transformation/types.d.ts.map +1 -0
- package/dist/transformation/types.js +7 -0
- package/dist/transformation/types.js.map +1 -0
- package/dist/validation/index.d.ts +6 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +7 -0
- package/dist/validation/index.js.map +1 -0
- package/package.json +81 -0
- package/test-app/test-react/index.html +19 -0
- package/test-app/test-react/package.json +16 -0
- package/test-app/test-react/src/Component.jsx +146 -0
- package/test-app/test-react/src/Component.module.css +572 -0
- package/test-app/test-react/src/Homepage.jsx +53 -0
- package/test-app/test-react/src/Homepage.module.css +218 -0
- package/test-app/test-react/src/ProductPage.jsx +74 -0
- package/test-app/test-react/src/ProductPage.module.css +357 -0
- package/test-app/test-react/src/ShoppingCart.jsx +151 -0
- package/test-app/test-react/src/ShoppingCart.module.css +739 -0
- package/test-app/test-react/src/main.jsx +9 -0
- package/test-app/test-react/vite.config.js +6 -0
- package/test-app/test-vue/index.html +19 -0
- package/test-app/test-vue/package.json +15 -0
- package/test-app/test-vue/src/App.vue +16 -0
- package/test-app/test-vue/src/Component.vue +723 -0
- package/test-app/test-vue/src/Homepage.vue +271 -0
- package/test-app/test-vue/src/ProductPage.vue +431 -0
- package/test-app/test-vue/src/ShoppingCart.vue +890 -0
- package/test-app/test-vue/src/main.js +4 -0
- package/test-app/test-vue/vite.config.js +6 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.3.0] - 2026-02-12
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- `--preview` flag: generate and instantly preview in browser via built-in Vite test app
|
|
7
|
+
- `--node` parameter: convert specific Figma node/layer instead of entire file
|
|
8
|
+
- Responsive scaling: root container auto-fits viewport width, no horizontal scrollbar
|
|
9
|
+
- Figma mask support: filter `isMask` nodes, containers inherit parent background
|
|
10
|
+
- Invisible fill filtering: skip `opacity: 0` fills (transparent placeholders)
|
|
11
|
+
- Hidden layer filtering: `visible: false` nodes excluded from output
|
|
12
|
+
- Vector icon detection: pure vector containers auto-exported as PNG images
|
|
13
|
+
- Image local cache fallback: skip API calls when images already exist on disk
|
|
14
|
+
- High-res image export: `scale: 2` for retina displays
|
|
15
|
+
- Full typography CSS: font-family, font-size, font-weight, line-height, letter-spacing, text-align, color
|
|
16
|
+
- `white-space: nowrap` for single-line text nodes
|
|
17
|
+
- Google Fonts and CSS reset in test-app HTML templates
|
|
18
|
+
- Non-ASCII filename sanitization (Chinese characters stripped from output filenames)
|
|
19
|
+
- 4 example projects with original vs generated screenshots
|
|
20
|
+
|
|
21
|
+
### Changed
|
|
22
|
+
- Default TypeScript output changed to `false` (use `--typescript` to enable)
|
|
23
|
+
- Root container uses `transform: scale()` with JS resize listener instead of fixed `max-width`
|
|
24
|
+
- `overflow: hidden` containers now get `background-color: inherit` to match Figma compositing
|
|
25
|
+
- Replaced `SimplifyTransformer`/`FlattenTransformer` with `FigmaStructureOptimizer`
|
|
26
|
+
- `ComponentExtractor` preserves Text/Image/Shape types (no longer converts to Component)
|
|
27
|
+
- Image imports only generated for local paths (avoids broken imports from failed downloads)
|
|
28
|
+
- CSS class names truncated for long text nodes
|
|
29
|
+
|
|
30
|
+
### Fixed
|
|
31
|
+
- Duplicate method definitions in generators
|
|
32
|
+
- Button text centering with flex layout for single-text containers
|
|
33
|
+
- Image rendering with ES module imports (`import img from './assets/...'`)
|
|
34
|
+
- Rate limit handling with automatic retry and backoff
|
|
35
|
+
|
|
36
|
+
## [0.2.0] - 2026-02-11
|
|
37
|
+
|
|
38
|
+
### Added
|
|
39
|
+
- LLM integration: AWS Bedrock, OpenAI, and Anthropic providers
|
|
40
|
+
- AI semantic naming, component splitting, and code optimization
|
|
41
|
+
- CLI options: `--llm-provider`, `--llm-model`, `--ai-naming`, `--ai-splitting`, `--ai-optimization`
|
|
42
|
+
|
|
43
|
+
### Changed
|
|
44
|
+
- `TransformationPipeline` supports async transformers
|
|
45
|
+
- `FigmaToCodeAgent` conditionally registers AI transformers
|
|
46
|
+
|
|
47
|
+
## [0.1.0] - 2026-02-11
|
|
48
|
+
|
|
49
|
+
### Initial Release
|
|
50
|
+
- Figma API and MCP protocol integration
|
|
51
|
+
- AST parsing and transformation pipeline
|
|
52
|
+
- React and Vue code generation
|
|
53
|
+
- CSS Modules, Tailwind, and plain CSS support
|
|
54
|
+
- TypeScript support
|
|
55
|
+
- 129 tests
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 lewiscutey
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/QUICKSTART.md
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# Quick Start
|
|
2
|
+
|
|
3
|
+
## 1. Install & Build
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
git clone https://github.com/lewiscutey/figma-to-code-agent.git
|
|
7
|
+
cd figma-to-code-agent
|
|
8
|
+
npm install
|
|
9
|
+
npm run build
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## 2. Get Your Figma Token
|
|
13
|
+
|
|
14
|
+
1. Go to [Figma API Settings](https://www.figma.com/developers/api#access-tokens)
|
|
15
|
+
2. Click "Get personal access token"
|
|
16
|
+
3. Copy the token
|
|
17
|
+
|
|
18
|
+
## 3. Get File Key & Node ID
|
|
19
|
+
|
|
20
|
+
From your Figma URL:
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
https://www.figma.com/design/ABC123DEF456/My-Design?node-id=100-200
|
|
24
|
+
^^^^^^^^^^^^^^ ^^^^^^^
|
|
25
|
+
File Key Node ID
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Node ID format: `100-200` (use hyphen, not colon)
|
|
29
|
+
|
|
30
|
+
## 4. Generate Code
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# React component
|
|
34
|
+
node dist/cli.js \
|
|
35
|
+
--token YOUR_FIGMA_TOKEN \
|
|
36
|
+
--file ABC123DEF456 \
|
|
37
|
+
--node 100-200 \
|
|
38
|
+
--framework react \
|
|
39
|
+
--output ./output
|
|
40
|
+
|
|
41
|
+
# Vue component
|
|
42
|
+
node dist/cli.js \
|
|
43
|
+
--token YOUR_FIGMA_TOKEN \
|
|
44
|
+
--file ABC123DEF456 \
|
|
45
|
+
--node 100-200 \
|
|
46
|
+
--framework vue \
|
|
47
|
+
--output ./output
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Or use environment variable:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
export FIGMA_TOKEN="your-token"
|
|
54
|
+
node dist/cli.js --file ABC123DEF456 --node 100-200 --output ./output
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## 5. Preview in Browser
|
|
58
|
+
|
|
59
|
+
Add `--preview` to instantly open the result in your browser:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
node dist/cli.js \
|
|
63
|
+
--token YOUR_FIGMA_TOKEN \
|
|
64
|
+
--file ABC123DEF456 \
|
|
65
|
+
--node 100-200 \
|
|
66
|
+
--framework react \
|
|
67
|
+
--output ./output \
|
|
68
|
+
--preview
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
This copies the generated files to the built-in test app, starts a Vite dev server, and opens your browser. Press `Ctrl+C` to stop — temporary files are cleaned up automatically.
|
|
72
|
+
|
|
73
|
+
## 6. TypeScript Output (Optional)
|
|
74
|
+
|
|
75
|
+
By default, generates `.jsx` / `.vue`. Add `--typescript` for `.tsx` output:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
node dist/cli.js \
|
|
79
|
+
--token YOUR_FIGMA_TOKEN \
|
|
80
|
+
--file ABC123DEF456 \
|
|
81
|
+
--node 100-200 \
|
|
82
|
+
--framework react \
|
|
83
|
+
--typescript \
|
|
84
|
+
--output ./output
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Generated Output
|
|
88
|
+
|
|
89
|
+
For a React component, you'll get:
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
output/
|
|
93
|
+
├── Component.jsx # React component
|
|
94
|
+
├── Component.module.css # CSS Module styles
|
|
95
|
+
└── assets/ # Downloaded images
|
|
96
|
+
├── hero-image.png
|
|
97
|
+
└── icon-close.png
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
For Vue, a single `.vue` file with `<template>`, `<script setup>`, and `<style scoped>`.
|
|
101
|
+
|
|
102
|
+
## What Gets Generated
|
|
103
|
+
|
|
104
|
+
- Pixel-accurate layout using absolute positioning
|
|
105
|
+
- Responsive scaling — auto-fits viewport width, no horizontal scrollbar
|
|
106
|
+
- Full typography (font family, size, weight, line-height, letter-spacing, color)
|
|
107
|
+
- Images exported at 2x resolution for retina displays
|
|
108
|
+
- Vector icons auto-detected and exported as PNG
|
|
109
|
+
- Hidden layers and mask shapes filtered out
|
|
110
|
+
- CSS Modules with scoped class names
|
|
111
|
+
|
|
112
|
+
## CLI Reference
|
|
113
|
+
|
|
114
|
+
| Option | Description | Default |
|
|
115
|
+
|--------|-------------|---------|
|
|
116
|
+
| `--token <token>` | Figma API token (or `FIGMA_TOKEN` env) | — |
|
|
117
|
+
| `--file <key>` | Figma file key | — |
|
|
118
|
+
| `--node <id>` | Target node ID | root |
|
|
119
|
+
| `--framework` | `react` or `vue` | `react` |
|
|
120
|
+
| `--style` | `css-modules`, `tailwind`, or `css` | `css-modules` |
|
|
121
|
+
| `--typescript` | Enable TypeScript output | `false` |
|
|
122
|
+
| `--output <dir>` | Output directory | `./output` |
|
|
123
|
+
| `--preview` | Preview in browser after generation | — |
|
|
124
|
+
|
|
125
|
+
## AI Enhancements (Optional)
|
|
126
|
+
|
|
127
|
+
For smarter component naming and code optimization, add an LLM provider:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
# AWS Bedrock
|
|
131
|
+
node dist/cli.js \
|
|
132
|
+
--token YOUR_FIGMA_TOKEN \
|
|
133
|
+
--file ABC123DEF456 \
|
|
134
|
+
--framework react \
|
|
135
|
+
--llm-provider bedrock \
|
|
136
|
+
--llm-model anthropic.claude-3-5-sonnet-20241022-v2:0 \
|
|
137
|
+
--ai-naming \
|
|
138
|
+
--output ./output
|
|
139
|
+
|
|
140
|
+
# OpenAI
|
|
141
|
+
LLM_API_KEY="sk-..." node dist/cli.js \
|
|
142
|
+
--token YOUR_FIGMA_TOKEN \
|
|
143
|
+
--file ABC123DEF456 \
|
|
144
|
+
--framework react \
|
|
145
|
+
--llm-provider openai \
|
|
146
|
+
--llm-model gpt-4o \
|
|
147
|
+
--ai-naming \
|
|
148
|
+
--output ./output
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
| AI Option | What It Does |
|
|
152
|
+
|-----------|-------------|
|
|
153
|
+
| `--ai-naming` | Renames `Frame123` → `UserProfileCard` |
|
|
154
|
+
| `--ai-splitting` | Splits large designs into sub-components |
|
|
155
|
+
| `--ai-optimization` | Improves code quality and accessibility |
|
|
156
|
+
|
|
157
|
+
## Troubleshooting
|
|
158
|
+
|
|
159
|
+
**"Figma token is required"**
|
|
160
|
+
Set `--token` or `export FIGMA_TOKEN="your-token"`
|
|
161
|
+
|
|
162
|
+
**"Rate limited" / 429 errors**
|
|
163
|
+
The tool auto-retries with backoff. If persistent, wait a minute and try again. Downloaded images are cached locally to avoid repeated API calls.
|
|
164
|
+
|
|
165
|
+
**Images not showing**
|
|
166
|
+
Check that the `assets/` folder was generated alongside your component. Image paths are relative imports.
|
|
167
|
+
|
|
168
|
+
**Preview not working**
|
|
169
|
+
Make sure `test-app/test-react/node_modules` (or `test-vue`) exists. Run `npm install` inside the test-app directory first:
|
|
170
|
+
```bash
|
|
171
|
+
cd test-app/test-react && npm install
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Try the Pre-built Examples
|
|
175
|
+
|
|
176
|
+
The `test-app/` directory contains pre-generated components from the 4 examples shown in the README. You can preview them directly without a Figma token:
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
# React examples
|
|
180
|
+
cd test-app/test-react
|
|
181
|
+
npm install
|
|
182
|
+
npx vite --open
|
|
183
|
+
|
|
184
|
+
# Vue examples
|
|
185
|
+
cd test-app/test-vue
|
|
186
|
+
npm install
|
|
187
|
+
npx vite --open
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Edit `src/main.jsx` (or `src/main.js` for Vue) to switch between components:
|
|
191
|
+
|
|
192
|
+
```jsx
|
|
193
|
+
// Try different pages:
|
|
194
|
+
import { Component } from './Component.jsx' // MIUI12
|
|
195
|
+
import { Homepage } from './Homepage.jsx' // World Peas Homepage
|
|
196
|
+
import { ShoppingCart } from './ShoppingCart.jsx' // Shopping Cart
|
|
197
|
+
import { ProductPage } from './ProductPage.jsx' // Product Page
|
|
198
|
+
```
|
package/README.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Figma-to-Code Agent
|
|
2
|
+
|
|
3
|
+
Convert Figma designs to production-ready React/Vue components. Supports CSS Modules, Tailwind, and plain CSS.
|
|
4
|
+
|
|
5
|
+
> 📖 New here? Check the [Quick Start Guide](QUICKSTART.md) for step-by-step setup.
|
|
6
|
+
>
|
|
7
|
+
> 🖥️ Want to see it in action? Run the examples in [test-app/](test-app/) — pre-generated React and Vue components ready to preview.
|
|
8
|
+
|
|
9
|
+
## Examples
|
|
10
|
+
|
|
11
|
+
### Example 1 — MIUI12 官网 (全局导航)
|
|
12
|
+
|
|
13
|
+
| Original Design | React | Vue |
|
|
14
|
+
|:-:|:-:|:-:|
|
|
15
|
+
|  |  |  |
|
|
16
|
+
|
|
17
|
+
### Example 2 — Homepage (World Peas)
|
|
18
|
+
|
|
19
|
+
| Original Design | React | Vue |
|
|
20
|
+
|:-:|:-:|:-:|
|
|
21
|
+
|  |  |  |
|
|
22
|
+
|
|
23
|
+
### Example 3 — Shopping Cart
|
|
24
|
+
|
|
25
|
+
| Original Design | React | Vue |
|
|
26
|
+
|:-:|:-:|:-:|
|
|
27
|
+
|  |  |  |
|
|
28
|
+
|
|
29
|
+
### Example 4 — Product Page
|
|
30
|
+
|
|
31
|
+
| Original Design | React | Vue |
|
|
32
|
+
|:-:|:-:|:-:|
|
|
33
|
+
|  |  |  |
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
- 🎨 Extract designs from Figma API with caching and rate-limit handling
|
|
38
|
+
- ⚛️ Generate React (.jsx/.tsx) and Vue (.vue) components
|
|
39
|
+
- 🎭 CSS Modules, Tailwind, or plain CSS
|
|
40
|
+
- 📐 Absolute positioning with responsive scaling (auto-fits viewport)
|
|
41
|
+
- 🖼️ Image export at 2x resolution, vector icon auto-detection and PNG export
|
|
42
|
+
- 🔤 Full typography support (font family, size, weight, line-height, letter-spacing, color)
|
|
43
|
+
- 👁️ Figma layer filtering (hidden layers, mask shapes)
|
|
44
|
+
- 🤖 Optional AI enhancements (semantic naming, component splitting, code optimization)
|
|
45
|
+
|
|
46
|
+
## Quick Start
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm install
|
|
50
|
+
npm run build
|
|
51
|
+
|
|
52
|
+
# Generate React component
|
|
53
|
+
node dist/cli.js \
|
|
54
|
+
--token YOUR_FIGMA_TOKEN \
|
|
55
|
+
--file FILE_KEY \
|
|
56
|
+
--node NODE_ID \
|
|
57
|
+
--framework react \
|
|
58
|
+
--output ./output
|
|
59
|
+
|
|
60
|
+
# Generate and preview in browser
|
|
61
|
+
node dist/cli.js \
|
|
62
|
+
--token YOUR_FIGMA_TOKEN \
|
|
63
|
+
--file FILE_KEY \
|
|
64
|
+
--node NODE_ID \
|
|
65
|
+
--framework react \
|
|
66
|
+
--output ./output \
|
|
67
|
+
--preview
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## How It Works
|
|
71
|
+
|
|
72
|
+
1. Fetches the Figma file data via API (with local caching)
|
|
73
|
+
2. Parses the design tree into an intermediate AST
|
|
74
|
+
3. Filters invisible layers, mask shapes, and transparent fills
|
|
75
|
+
4. Detects vector-only containers and exports them as PNG icons
|
|
76
|
+
5. Downloads images at 2x resolution
|
|
77
|
+
6. Generates framework-specific components with CSS Modules
|
|
78
|
+
7. Wraps output in a responsive scale container (no horizontal scrollbar)
|
|
79
|
+
|
|
80
|
+
## CLI Options
|
|
81
|
+
|
|
82
|
+
| Option | Description | Default |
|
|
83
|
+
|--------|-------------|---------|
|
|
84
|
+
| `--token <token>` | Figma API token (or `FIGMA_TOKEN` env) | — |
|
|
85
|
+
| `--file <key>` | Figma file key | — |
|
|
86
|
+
| `--node <id>` | Target node ID (e.g. `1502-102`) | root |
|
|
87
|
+
| `--framework` | `react` or `vue` | `react` |
|
|
88
|
+
| `--style` | `css-modules`, `tailwind`, or `css` | `css-modules` |
|
|
89
|
+
| `--typescript` | Enable TypeScript output | `false` |
|
|
90
|
+
| `--output <dir>` | Output directory | `./output` |
|
|
91
|
+
| `--preview` | Preview in browser after generation | — |
|
|
92
|
+
|
|
93
|
+
### AI Options (optional)
|
|
94
|
+
|
|
95
|
+
| Option | Description |
|
|
96
|
+
|--------|-------------|
|
|
97
|
+
| `--llm-provider` | `bedrock`, `openai`, or `anthropic` |
|
|
98
|
+
| `--llm-model` | Model name |
|
|
99
|
+
| `--ai-naming` | AI-powered semantic component naming |
|
|
100
|
+
| `--ai-splitting` | AI-powered component splitting |
|
|
101
|
+
| `--ai-optimization` | AI-powered code optimization |
|
|
102
|
+
|
|
103
|
+
## Programmatic API
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { FigmaToCodeAgent } from 'figma-to-code-agent'
|
|
107
|
+
|
|
108
|
+
const agent = new FigmaToCodeAgent({
|
|
109
|
+
figmaToken: 'your-token',
|
|
110
|
+
fileKey: 'your-file-key',
|
|
111
|
+
framework: 'react',
|
|
112
|
+
styleMode: 'css-modules',
|
|
113
|
+
typescript: false,
|
|
114
|
+
outputDir: './output',
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
const files = await agent.convert()
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Project Structure
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
src/
|
|
124
|
+
├── extraction/ # Figma API client, MCP protocol, caching
|
|
125
|
+
├── transformation/ # AST parsing, layout optimization, transformers
|
|
126
|
+
│ └── transformers/ # Component extraction, semantic naming, etc.
|
|
127
|
+
├── generation/ # React and Vue code generators
|
|
128
|
+
├── llm/ # LLM providers (Bedrock, OpenAI)
|
|
129
|
+
├── validation/ # Visual validation
|
|
130
|
+
└── cli.ts # CLI entry point
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Development
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
npm run build # Compile TypeScript
|
|
137
|
+
npm test # Run all tests (129 tests)
|
|
138
|
+
npm run lint # ESLint
|
|
139
|
+
npm run format # Prettier
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Requirements
|
|
143
|
+
|
|
144
|
+
- Node.js 18+
|
|
145
|
+
- npm
|
|
146
|
+
|
|
147
|
+
## License
|
|
148
|
+
|
|
149
|
+
MIT
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { GeneratedFile } from './generation/types';
|
|
2
|
+
export interface AgentConfig {
|
|
3
|
+
figmaToken: string;
|
|
4
|
+
fileKey: string;
|
|
5
|
+
nodeIds?: string[];
|
|
6
|
+
framework: 'react' | 'vue';
|
|
7
|
+
styleMode: 'css-modules' | 'tailwind' | 'css';
|
|
8
|
+
typescript: boolean;
|
|
9
|
+
outputDir: string;
|
|
10
|
+
llm?: {
|
|
11
|
+
provider: 'bedrock' | 'openai' | 'anthropic';
|
|
12
|
+
model: string;
|
|
13
|
+
region?: string;
|
|
14
|
+
apiKey?: string;
|
|
15
|
+
enableAINaming?: boolean;
|
|
16
|
+
enableAISplitting?: boolean;
|
|
17
|
+
enableAIOptimization?: boolean;
|
|
18
|
+
enableAILayout?: boolean;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export declare class FigmaToCodeAgent {
|
|
22
|
+
private config;
|
|
23
|
+
private figmaClient;
|
|
24
|
+
private parser;
|
|
25
|
+
private pipeline;
|
|
26
|
+
private llm?;
|
|
27
|
+
private aiOptimizer?;
|
|
28
|
+
constructor(config: AgentConfig);
|
|
29
|
+
convert(): Promise<GeneratedFile[]>;
|
|
30
|
+
/**
|
|
31
|
+
* Download images for all Image nodes in the AST
|
|
32
|
+
*/
|
|
33
|
+
private downloadImages;
|
|
34
|
+
private collectImageNodes;
|
|
35
|
+
/**
|
|
36
|
+
* Find containers whose children are all Shape/Vector nodes (icons).
|
|
37
|
+
* These should be exported as images since CSS can't render SVG paths.
|
|
38
|
+
*/
|
|
39
|
+
/**
|
|
40
|
+
* Find containers whose children are all Shape/Vector nodes (icons).
|
|
41
|
+
* These should be exported as images since CSS can't render SVG paths.
|
|
42
|
+
* Only matches small containers (< 200px) to avoid exporting large sections.
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* Find containers whose descendants are all Shape/Vector nodes.
|
|
46
|
+
* These should be exported as images since CSS can't render SVG paths.
|
|
47
|
+
* Skips containers that also have Text or Image children (mixed content).
|
|
48
|
+
*/
|
|
49
|
+
private collectIconNodes;
|
|
50
|
+
private hasShapeDescendant;
|
|
51
|
+
private hasTextOrImageDescendant;
|
|
52
|
+
private sanitizeFileName;
|
|
53
|
+
private findNodeById;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=FigmaToCodeAgent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FigmaToCodeAgent.d.ts","sourceRoot":"","sources":["../src/FigmaToCodeAgent.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAmB,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAMzE,MAAM,WAAW,WAAW;IAC1B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,SAAS,EAAE,OAAO,GAAG,KAAK,CAAC;IAC3B,SAAS,EAAE,aAAa,GAAG,UAAU,GAAG,KAAK,CAAC;IAC9C,UAAU,EAAE,OAAO,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,CAAC,EAAE;QACJ,QAAQ,EAAE,SAAS,GAAG,QAAQ,GAAG,WAAW,CAAC;QAC7C,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,cAAc,CAAC,EAAE,OAAO,CAAC;QACzB,iBAAiB,CAAC,EAAE,OAAO,CAAC;QAC5B,oBAAoB,CAAC,EAAE,OAAO,CAAC;QAC/B,cAAc,CAAC,EAAE,OAAO,CAAC;KAC1B,CAAC;CACH;AAED,qBAAa,gBAAgB;IAOf,OAAO,CAAC,MAAM;IAN1B,OAAO,CAAC,WAAW,CAAiB;IACpC,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,QAAQ,CAAyB;IACzC,OAAO,CAAC,GAAG,CAAC,CAAc;IAC1B,OAAO,CAAC,WAAW,CAAC,CAAkB;gBAElB,MAAM,EAAE,WAAW;IAwCjC,OAAO,IAAI,OAAO,CAAC,aAAa,EAAE,CAAC;IA8CzC;;OAEG;YACW,cAAc;IAiE5B,OAAO,CAAC,iBAAiB;IAUzB;;;OAGG;IACH;;;;OAIG;IACH;;;;OAIG;IACH,OAAO,CAAC,gBAAgB;IAexB,OAAO,CAAC,kBAAkB;IAK1B,OAAO,CAAC,wBAAwB;IAKhC,OAAO,CAAC,gBAAgB;IAYxB,OAAO,CAAC,YAAY;CAUrB"}
|