Skip to content

Commit 256ad14

Browse files
committed
Feat treeshaking mode
1 parent 195b8bb commit 256ad14

File tree

1 file changed

+65
-36
lines changed

1 file changed

+65
-36
lines changed

src/commands/devup/export-devup.ts

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,34 @@ export async function buildDevupConfig(
222222
styleMetaById[style.id] = meta
223223
}
224224

225+
// Collect bound variable IDs from document (includes library vars)
226+
const usedVarIds = new Set<string>()
227+
const collectBoundVarsFromNode = (node: SceneNode) => {
228+
const bv =
229+
'boundVariables' in node
230+
? (node.boundVariables as
231+
| Record<string, { id: string } | { id: string }[]>
232+
| undefined)
233+
: undefined
234+
if (!bv) return
235+
for (const val of Object.values(bv)) {
236+
if (Array.isArray(val)) {
237+
for (const item of val) {
238+
if (item?.id) usedVarIds.add(item.id)
239+
}
240+
} else if (val?.id) {
241+
usedVarIds.add(val.id)
242+
}
243+
}
244+
}
245+
const walkBoundVars = (node: SceneNode) => {
246+
collectBoundVarsFromNode(node)
247+
if (!('children' in node)) return
248+
for (const child of node.children) {
249+
walkBoundVars(child)
250+
}
251+
}
252+
225253
const tTypo = perfStart()
226254
const typography: Record<string, (null | DevupTypography)[]> = {}
227255
if (treeshaking) {
@@ -257,34 +285,6 @@ export async function buildDevupConfig(
257285
}
258286
}
259287

260-
// Collect bound variable IDs for length treeshaking (includes library vars)
261-
const usedVarIds = new Set<string>()
262-
const collectBoundVarsFromNode = (node: SceneNode) => {
263-
const bv =
264-
'boundVariables' in node
265-
? (node.boundVariables as
266-
| Record<string, { id: string } | { id: string }[]>
267-
| undefined)
268-
: undefined
269-
if (!bv) return
270-
for (const val of Object.values(bv)) {
271-
if (Array.isArray(val)) {
272-
for (const item of val) {
273-
if (item?.id) usedVarIds.add(item.id)
274-
}
275-
} else if (val?.id) {
276-
usedVarIds.add(val.id)
277-
}
278-
}
279-
}
280-
const walkBoundVars = (node: SceneNode) => {
281-
collectBoundVarsFromNode(node)
282-
if (!('children' in node)) return
283-
for (const child of node.children) {
284-
walkBoundVars(child)
285-
}
286-
}
287-
288288
const processSubtree = (node: SceneNode) => {
289289
collectBoundVarsFromNode(node)
290290
if (node.type === 'TEXT') {
@@ -355,9 +355,43 @@ export async function buildDevupConfig(
355355
for (const key of Object.keys(usedTypographyKeys)) {
356356
typography[key] = [...(typographyByKey[key] ?? [])]
357357
}
358+
} else {
359+
for (const [key, values] of Object.entries(typographyByKey)) {
360+
typography[key] = [...values]
361+
}
358362

359-
// Build length values from used FLOAT bound variables
360-
const tLength = perfStart()
363+
// Scan document for bound variables (picks up library vars)
364+
const prevSkip = figma.skipInvisibleInstanceChildren
365+
figma.skipInvisibleInstanceChildren = true
366+
367+
const rootPages = Array.isArray(figma.root.children)
368+
? figma.root.children
369+
: []
370+
if (rootPages.length > 0) {
371+
const currentPageId = figma.currentPage.id
372+
const orderedPages: PageNode[] = [
373+
figma.currentPage,
374+
...rootPages.filter((page) => page.id !== currentPageId),
375+
]
376+
for (const page of orderedPages) {
377+
if (page.id !== currentPageId) {
378+
const tPageLoad = perfStart()
379+
await page.loadAsync()
380+
perfEnd('exportDevup.load', tPageLoad)
381+
}
382+
for (const child of page.children) {
383+
walkBoundVars(child)
384+
}
385+
}
386+
}
387+
388+
figma.skipInvisibleInstanceChildren = prevSkip
389+
}
390+
perfEnd('exportDevup.typography', tTypo)
391+
392+
// Build length values from used FLOAT bound variables
393+
const tLength = perfStart()
394+
if (usedVarIds.size > 0) {
361395
const collectionCache = new Map<string, VariableCollection | null>()
362396
const lengthThemeName = resolveThemeName(devup)
363397

@@ -410,13 +444,8 @@ export async function buildDevupConfig(
410444
if (Object.keys(devup.theme.length).length === 0) {
411445
delete devup.theme.length
412446
}
413-
perfEnd('exportDevup.length', tLength)
414-
} else {
415-
for (const [key, values] of Object.entries(typographyByKey)) {
416-
typography[key] = [...values]
417-
}
418447
}
419-
perfEnd('exportDevup.typography', tTypo)
448+
perfEnd('exportDevup.length', tLength)
420449

421450
if (Object.keys(typography).length > 0) {
422451
devup.theme ??= {}

0 commit comments

Comments
 (0)