-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexportPagesAndComponents.ts
More file actions
503 lines (435 loc) · 16 KB
/
exportPagesAndComponents.ts
File metadata and controls
503 lines (435 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
import JSZip from 'jszip'
import {
Codegen,
getGlobalAssetNodes,
resetGlobalAssetNodes,
} from '../codegen/Codegen'
import { ResponsiveCodegen } from '../codegen/responsive/ResponsiveCodegen'
import { checkAssetNode } from '../codegen/utils/check-asset-node'
import {
perfEnd,
perfReport,
perfReset,
perfStart,
} from '../codegen/utils/perf'
import { wrapComponent } from '../codegen/utils/wrap-component'
import { getComponentName } from '../utils'
import { downloadFile } from '../utils/download-file'
import { toPascal } from '../utils/to-pascal'
import { buildDevupConfig } from './devup'
const NOTIFY_TIMEOUT = 3000
// Figma throttles >4 concurrent exportAsync calls for large PNGs (screenshots).
// SVG/asset exports are lightweight and scale better with higher concurrency.
const SCREENSHOT_BATCH_SIZE = 4
const ASSET_BATCH_SIZE = 8
const ZIP_TEXT_FILE_OPTIONS = { compression: 'DEFLATE' as const }
const ZIP_BINARY_FILE_OPTIONS = { binary: true, compression: 'STORE' as const }
const ZIP_GENERATE_OPTIONS = {
type: 'uint8array' as const,
compression: 'DEFLATE' as const,
compressionOptions: { level: 1 },
streamFiles: true,
}
export const DEVUP_COMPONENTS = [
'Center',
'VStack',
'Flex',
'Grid',
'Box',
'Text',
'Image',
]
const DEVUP_COMPONENT_SET = new Set(DEVUP_COMPONENTS)
const DEVUP_COMPONENT_PATTERNS = DEVUP_COMPONENTS.map(
(component) => [component, new RegExp(`<${component}[\\s/>]`)] as const,
)
const CUSTOM_COMPONENT_USAGE_REGEX = /<([A-Z][a-zA-Z0-9]*)/g
function getCombinedCode(
componentsCodes: ReadonlyArray<readonly [string, string]>,
): string {
let allCode = ''
for (let i = 0; i < componentsCodes.length; i++) {
if (i > 0) allCode += '\n'
allCode += componentsCodes[i][1]
}
return allCode
}
export function extractImports(
componentsCodes: ReadonlyArray<readonly [string, string]>,
): string[] {
const allCode = getCombinedCode(componentsCodes)
const imports = new Set<string>()
for (const [component, regex] of DEVUP_COMPONENT_PATTERNS) {
if (regex.test(allCode)) {
imports.add(component)
}
}
if (/\bkeyframes\s*(\(|`)/.test(allCode)) {
imports.add('keyframes')
}
return Array.from(imports).sort()
}
export function extractCustomComponentImports(
componentsCodes: ReadonlyArray<readonly [string, string]>,
): string[] {
const allCode = getCombinedCode(componentsCodes)
const customImports = new Set<string>()
CUSTOM_COMPONENT_USAGE_REGEX.lastIndex = 0
for (const match of allCode.matchAll(CUSTOM_COMPONENT_USAGE_REGEX)) {
const componentName = match[1]
if (!DEVUP_COMPONENT_SET.has(componentName)) {
customImports.add(componentName)
}
}
return Array.from(customImports).sort()
}
export function generateImportStatements(
componentsCodes: ReadonlyArray<readonly [string, string]>,
): string {
const devupImports = extractImports(componentsCodes)
const customImports = extractCustomComponentImports(componentsCodes)
const statements: string[] = []
if (devupImports.length > 0) {
statements.push(
`import { ${devupImports.join(', ')} } from '@devup-ui/react'`,
)
}
for (const componentName of customImports) {
statements.push(
`import { ${componentName} } from '@/components/${componentName}'`,
)
}
return statements.length > 0 ? `${statements.join('\n')}\n\n` : ''
}
type AnnotatedNode = SceneNode & {
annotations: ReadonlyArray<{ label?: string; labelMarkdown?: string }>
}
/**
* Collect notes from a section: standalone TEXT children (annotations by convention)
* and Figma dev-mode annotations on descendant nodes.
* Returns the combined text, or empty string if none found.
*/
export function collectSectionNotes(section: SectionNode): string {
const lines: string[] = []
// 1. Direct TEXT children are treated as page annotations
for (const child of section.children) {
if (child.type === 'TEXT') {
const text = (child as TextNode).characters.trim()
if (text) lines.push(text)
}
}
// 2. Figma dev-mode annotations on any descendant node
const annotatedNodes = section.findAll(
(node) =>
'annotations' in node && (node as AnnotatedNode).annotations.length > 0,
)
for (const node of annotatedNodes) {
for (const annotation of (node as AnnotatedNode).annotations) {
const text = (annotation.label ?? annotation.labelMarkdown ?? '').trim()
if (text) lines.push(`[${node.name}] ${text}`)
}
}
return lines.join('\n')
}
interface ScreenshotTarget {
node: SceneNode
folder: JSZip
fileName: string
}
/**
* Recursively collect asset nodes (SVGs and PNGs) from a Figma node tree.
* Uses checkAssetNode to identify assets. Deduplicates by type + name.
* Does not descend into children of asset nodes (they are leaf nodes in codegen).
*
* Pass a `visited` set to skip already-walked subtrees when collecting
* from multiple overlapping roots (e.g. top-level nodes + external component sets).
*/
export function collectAssetNodes(
node: SceneNode,
assets: Map<string, { node: SceneNode; type: 'svg' | 'png' }>,
visited?: Set<string>,
): void {
if (!node.visible) return
if (visited) {
const id = (node as SceneNode & { id?: string }).id
if (id) {
if (visited.has(id)) return
visited.add(id)
}
}
const assetType = checkAssetNode(node)
if (assetType) {
const key = `${assetType}/${node.name}`
if (!assets.has(key)) {
assets.set(key, { node, type: assetType })
}
return
}
if ('children' in node) {
for (const child of (node as SceneNode & ChildrenMixin).children) {
collectAssetNodes(child, assets, visited)
}
}
}
export async function exportPagesAndComponents() {
let notificationHandler = figma.notify('Preparing export...', {
timeout: Infinity,
})
try {
perfReset()
const tTotal = perfStart()
const zip = new JSZip()
const componentsFolder = zip.folder('components')
const pagesFolder = zip.folder('pages')
const iconsFolder = zip.folder('icons')
const imagesFolder = zip.folder('images')
let componentCount = 0
let pageCount = 0
// Deferred work collectors
const screenshotTargets: ScreenshotTarget[] = []
// Fire devup config build early — runs in parallel with codegen
const devupConfigPromise = buildDevupConfig()
// Reset global asset registry so buildTree() populates it fresh
resetGlobalAssetNodes()
// Track processed COMPONENT_SETs to avoid duplicates
const processedComponentSets = new Set<string>()
// Use selection if available, otherwise use all top-level children of current page
const nodes = Array.from(
figma.currentPage.selection.length > 0
? figma.currentPage.selection
: figma.currentPage.children,
)
const totalNodes = nodes.length
let processedNodes = 0
// Throttled notification — avoids cancel/recreate churn on rapid progress
let lastNotifyTime = 0
function updateProgress(message: string, force = false) {
const now = Date.now()
if (!force && now - lastNotifyTime < 200) return
lastNotifyTime = now
const percent = Math.round((processedNodes / totalNodes) * 100)
notificationHandler.cancel()
notificationHandler = figma.notify(`[${percent}%] ${message}`, {
timeout: Infinity,
})
}
// Helper function to process a COMPONENT_SET
async function processComponentSet(componentSet: ComponentSetNode) {
if (processedComponentSets.has(componentSet.id)) return
processedComponentSets.add(componentSet.id)
const componentName = getComponentName(componentSet)
updateProgress(`Processing component: ${componentName}`)
let t = perfStart()
const responsiveCodes =
await ResponsiveCodegen.generateVariantResponsiveComponents(
componentSet,
componentName,
)
perfEnd(`responsiveCodegen(${componentName})`, t)
t = perfStart()
for (const [name, code] of responsiveCodes) {
const importStatement = generateImportStatements([[name, code]])
const fullCode = importStatement + code
componentsFolder?.file(`${name}.tsx`, fullCode, ZIP_TEXT_FILE_OPTIONS)
componentCount++
}
perfEnd('writeComponentFiles', t)
// Defer screenshot capture
if (componentsFolder) {
screenshotTargets.push({
node: componentSet,
folder: componentsFolder,
fileName: `${componentName}.png`,
})
}
}
// Process each node
const tCodegen = perfStart()
for (const node of nodes) {
processedNodes++
// 1. Handle COMPONENT_SET directly
if (node.type === 'COMPONENT_SET') {
await processComponentSet(node as ComponentSetNode)
continue
}
// 2. Handle COMPONENT - check if parent is COMPONENT_SET
if (node.type === 'COMPONENT' && node.parent?.type === 'COMPONENT_SET') {
await processComponentSet(node.parent as ComponentSetNode)
continue
}
updateProgress(`Processing: ${node.name}`)
// 3. Extract components using Codegen for other node types
let t = perfStart()
const codegen = new Codegen(node)
await codegen.run()
perfEnd(`codegen(${node.name})`, t)
t = perfStart()
const componentsCodes = codegen.getComponentsCodes()
const componentNodes = codegen.getComponentNodes()
// Add component files
for (const [name, code] of componentsCodes) {
const importStatement = generateImportStatements([[name, code]])
const fullCode = importStatement + code
componentsFolder?.file(`${name}.tsx`, fullCode, ZIP_TEXT_FILE_OPTIONS)
componentCount++
}
perfEnd('writeComponentFiles', t)
// 4. Generate responsive codes for COMPONENT_SET components found inside
for (const componentNode of componentNodes) {
if (
componentNode.type === 'COMPONENT' &&
componentNode.parent?.type === 'COMPONENT_SET'
) {
await processComponentSet(componentNode.parent as ComponentSetNode)
}
}
// 5. Check if node is Section or has parent Section for page generation
const isNodeSection = ResponsiveCodegen.canGenerateResponsive(node)
const parentSection = ResponsiveCodegen.hasParentSection(node)
const sectionNode = isNodeSection ? (node as SectionNode) : parentSection
if (sectionNode) {
const isParentSection = !isNodeSection && parentSection !== null
updateProgress(`Generating page: ${sectionNode.name}`)
t = perfStart()
const responsiveCodegen = new ResponsiveCodegen(sectionNode)
const responsiveCode = await responsiveCodegen.generateResponsiveCode()
const baseName = toPascal(sectionNode.name)
const pageName = isParentSection ? `${baseName}Page` : baseName
const wrappedCode = wrapComponent(pageName, responsiveCode, {
exportDefault: isParentSection,
})
const pageCodeEntry: ReadonlyArray<readonly [string, string]> = [
[pageName, wrappedCode],
]
const importStatement = generateImportStatements(pageCodeEntry)
const fullCode = importStatement + wrappedCode
pagesFolder?.file(`${pageName}.tsx`, fullCode, ZIP_TEXT_FILE_OPTIONS)
perfEnd(`responsivePage(${pageName})`, t)
// Collect section notes (standalone TEXT children + annotations)
const notes = collectSectionNotes(sectionNode)
if (notes && pagesFolder) {
pagesFolder.file(`${pageName}.txt`, notes, ZIP_TEXT_FILE_OPTIONS)
}
// Defer screenshot capture
if (pagesFolder) {
screenshotTargets.push({
node: sectionNode,
folder: pagesFolder,
fileName: `${pageName}.png`,
})
}
pageCount++
}
}
perfEnd('phase1.codegen', tCodegen)
// Check if we have anything to export
if (componentCount === 0 && pageCount === 0) {
notificationHandler.cancel()
figma.notify('No components or pages found')
return
}
// Await devup config (started in parallel with codegen) and add to ZIP
const devupConfig = await devupConfigPromise
zip.file('devup.json', JSON.stringify(devupConfig), ZIP_TEXT_FILE_OPTIONS)
// Asset nodes were already collected by Codegen's buildTree() into the
// global registry — no need to re-walk the Figma node tree via IPC.
const assetNodes = getGlobalAssetNodes()
// Phase 2: Batch export screenshots and assets in parallel
const tExport = perfStart()
const totalExports = screenshotTargets.length + assetNodes.size
let completedExports = 0
function updateExportProgress(label: string) {
const now = Date.now()
if (now - lastNotifyTime < 200) return
lastNotifyTime = now
const percent = Math.round((completedExports / totalExports) * 100)
notificationHandler.cancel()
notificationHandler = figma.notify(`Exporting [${percent}%] ${label}`, {
timeout: Infinity,
})
}
// Export screenshots in parallel batches
const tScreenshots = perfStart()
for (let i = 0; i < screenshotTargets.length; i += SCREENSHOT_BATCH_SIZE) {
const batch = screenshotTargets.slice(i, i + SCREENSHOT_BATCH_SIZE)
updateExportProgress(`screenshots (${i + 1}/${screenshotTargets.length})`)
await Promise.all(
batch.map(async ({ node, folder, fileName }) => {
try {
const t = perfStart()
const imageData = await node.exportAsync({
format: 'PNG',
constraint: { type: 'SCALE', value: 1 },
})
folder.file(fileName, imageData, ZIP_BINARY_FILE_OPTIONS)
perfEnd('exportAsync(screenshot)', t)
completedExports++
} catch (e) {
console.error(`Failed to capture screenshot for ${fileName}:`, e)
completedExports++
}
}),
)
}
perfEnd('phase2.screenshots', tScreenshots)
// Export asset files in parallel batches (SVGs → icons/, PNGs → images/)
const tAssets = perfStart()
const assetEntries = [...assetNodes.values()]
let assetCount = 0
for (let i = 0; i < assetEntries.length; i += ASSET_BATCH_SIZE) {
const batch = assetEntries.slice(i, i + ASSET_BATCH_SIZE)
updateExportProgress(`assets (${i + 1}/${assetEntries.length})`)
await Promise.all(
batch.map(async ({ node, type }) => {
try {
const fileName = `${node.name}.${type}`
const t = perfStart()
if (type === 'svg') {
const svgData = await node.exportAsync({ format: 'SVG' })
iconsFolder?.file(fileName, svgData, ZIP_BINARY_FILE_OPTIONS)
} else {
const pngData = await node.exportAsync({
format: 'PNG',
constraint: { type: 'SCALE', value: 2 },
})
imagesFolder?.file(fileName, pngData, ZIP_BINARY_FILE_OPTIONS)
}
perfEnd(`exportAsync(${type})`, t)
assetCount++
completedExports++
} catch (e) {
console.error(`Failed to export asset ${node.name}:`, e)
completedExports++
}
}),
)
}
perfEnd('phase2.assets', tAssets)
perfEnd('phase2.export', tExport)
notificationHandler.cancel()
notificationHandler = figma.notify('[100%] Creating zip file...', {
timeout: Infinity,
})
const tZip = perfStart()
await downloadFile(
`${figma.currentPage.name}-export.zip`,
await zip.generateAsync(ZIP_GENERATE_OPTIONS),
)
perfEnd('phase3.zip', tZip)
perfEnd('exportPagesAndComponents()', tTotal)
console.info(perfReport())
notificationHandler.cancel()
const parts = [`${componentCount} components`, `${pageCount} pages`]
if (assetCount > 0) {
parts.push(`${assetCount} assets`)
}
figma.notify(`Exported ${parts.join(', ')}`, { timeout: NOTIFY_TIMEOUT })
} catch (error) {
console.error(error)
notificationHandler.cancel()
figma.notify('Error exporting pages and components', {
timeout: NOTIFY_TIMEOUT,
error: true,
})
}
}