Skip to content

Commit 56e3f25

Browse files
committed
Add treeshaking mode
1 parent 298c912 commit 56e3f25

File tree

3 files changed

+57
-32
lines changed

3 files changed

+57
-32
lines changed

manifest.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,18 @@
2929
"name": "Export Devup",
3030
"command": "export-devup"
3131
},
32+
{
33+
"name": "Export Devup without treeshaking",
34+
"command": "export-devup-without-treeshaking"
35+
},
3236
{
3337
"name": "Export Devup Excel",
3438
"command": "export-devup-excel"
3539
},
40+
{
41+
"name": "Export Devup Excel without treeshaking",
42+
"command": "export-devup-excel-without-treeshaking"
43+
},
3644
{
3745
"name": "Import Devup",
3846
"command": "import-devup"

src/code.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ switch (figma.command) {
5353
case 'export-devup':
5454
exportDevup('json').finally(() => figma.closePlugin())
5555
break
56+
case 'export-devup-without-treeshaking':
57+
exportDevup('json', false).finally(() => figma.closePlugin())
58+
break
5659
case 'export-devup-excel':
5760
exportDevup('excel').finally(() => figma.closePlugin())
5861
break
62+
case 'export-devup-excel-without-treeshaking':
63+
exportDevup('excel', false).finally(() => figma.closePlugin())
64+
break
5965
case 'import-devup':
6066
importDevup('json').finally(() => figma.closePlugin())
6167
break

src/commands/devup/index.ts

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { optimizeHex } from '../../utils/optimize-hex'
1313
import { downloadDevupXlsx } from './utils/download-devup-xlsx'
1414
import { uploadDevupXlsx } from './utils/upload-devup-xlsx'
1515
import { textStyleToTypography } from '../../utils/text-style-to-typography'
16-
export async function exportDevup(output: "json" | "excel") {
16+
export async function exportDevup(output: "json" | "excel", treeshaking: boolean = true) {
1717
const devup: Devup = {}
1818

1919
const collection = await getDevupColorCollection()
@@ -47,7 +47,6 @@ export async function exportDevup(output: "json" | "excel") {
4747

4848
await figma.loadAllPagesAsync()
4949

50-
const texts = figma.root.findAllWithCriteria({ types: ['TEXT'] })
5150
const textStyles = await figma.getLocalTextStylesAsync()
5251
const ids = new Set()
5352
const styles: Record<string, TextStyle> = {}
@@ -57,38 +56,50 @@ export async function exportDevup(output: "json" | "excel") {
5756
}
5857

5958
const typography: Record<string, (null | DevupTypography)[]> = {}
60-
await Promise.all(
61-
texts
62-
.filter((text) => (typeof text.textStyleId === 'string' && text.textStyleId) || text.textStyleId === figma.mixed)
63-
.map(async (text) => {
64-
for (const seg of text.getStyledTextSegments([
65-
'fontName',
66-
'fontWeight',
67-
'fontSize',
68-
'textDecoration',
69-
'textCase',
70-
'lineHeight',
71-
'letterSpacing',
72-
'fills',
73-
'textStyleId',
74-
'fillStyleId',
75-
'listOptions',
76-
'indentation',
77-
'hyperlink',
78-
])) {
79-
if (seg && seg.textStyleId) {
80-
const style = await figma.getStyleByIdAsync(seg.textStyleId)
59+
if (treeshaking) {
60+
const texts = figma.root.findAllWithCriteria({ types: ['TEXT'] })
61+
await Promise.all(
62+
texts
63+
.filter((text) => (typeof text.textStyleId === 'string' && text.textStyleId) || text.textStyleId === figma.mixed)
64+
.map(async (text) => {
65+
for (const seg of text.getStyledTextSegments([
66+
'fontName',
67+
'fontWeight',
68+
'fontSize',
69+
'textDecoration',
70+
'textCase',
71+
'lineHeight',
72+
'letterSpacing',
73+
'fills',
74+
'textStyleId',
75+
'fillStyleId',
76+
'listOptions',
77+
'indentation',
78+
'hyperlink',
79+
])) {
80+
if (seg && seg.textStyleId) {
81+
const style = await figma.getStyleByIdAsync(seg.textStyleId)
8182

82-
if (!(style && ids.has(style.id))) continue
83-
const { level, name } = styleNameToTypography(style.name)
84-
const typo = textSegmentToTypography(seg)
85-
if (typography[name]&&typography[name][level]) continue
86-
typography[name] ??= [null, null, null, null, null, null]
87-
typography[name][level] = typo
83+
if (!(style && ids.has(style.id))) continue
84+
const { level, name } = styleNameToTypography(style.name)
85+
const typo = textSegmentToTypography(seg)
86+
if (typography[name]&&typography[name][level]) continue
87+
typography[name] ??= [null, null, null, null, null, null]
88+
typography[name][level] = typo
89+
}
8890
}
89-
}
90-
}),
91-
)
91+
}),
92+
)
93+
}
94+
else {
95+
for (const [styleName, style] of Object.entries(styles)) {
96+
const { level, name } = styleNameToTypography(styleName)
97+
const typo = textStyleToTypography(style)
98+
if (typography[name]&&typography[name][level]) continue
99+
typography[name] ??= [null, null, null, null, null, null]
100+
typography[name][level] = typo
101+
}
102+
}
92103

93104
for(const [name, style] of Object.entries(styles)) {
94105
const { level, name: styleName } = styleNameToTypography(name)

0 commit comments

Comments
 (0)