-
Notifications
You must be signed in to change notification settings - Fork 661
Expand file tree
/
Copy pathbuild.ts
More file actions
242 lines (207 loc) · 7.59 KB
/
build.ts
File metadata and controls
242 lines (207 loc) · 7.59 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
import fs from 'node:fs/promises'
import path from 'node:path'
import babel from '@babel/core'
import {generate} from '@babel/generator'
import {pascalCase} from 'change-case'
import data from '@primer/octicons/build/data.json' with {type: 'json'}
const {types: t} = babel
const SOURCE_DIRECTORY = path.resolve(import.meta.dirname, '../src')
const GENERATED_DIRECTORY = path.join(SOURCE_DIRECTORY, 'generated')
await fs.mkdir(GENERATED_DIRECTORY, {recursive: true})
// Each icon is around 2-3kB in size, create modules around 100kB in size
const BUCKET_SIZE = 40
const modules = partition(Object.values(data), BUCKET_SIZE).map((icons, index) => {
const filepath = path.join(GENERATED_DIRECTORY, `icons-${(index + 1).toString().padStart(2, '0')}.tsx`)
const propsImportSpecifier = t.importSpecifier(
t.identifier('OcticonReferenceProps'),
t.identifier('OcticonReferenceProps'),
)
propsImportSpecifier.importKind = 'type'
const imports = [
// import {forwardRef} from 'react'
t.importDeclaration(
[t.importSpecifier(t.identifier('forwardRef'), t.identifier('forwardRef'))],
t.stringLiteral('react'),
),
// import {Icon} from '../Icon'
t.importDeclaration([t.importSpecifier(t.identifier('Icon'), t.identifier('Icon'))], t.stringLiteral('../Icon')),
// import type {OcticonReferenceProps} from '../types'
t.importDeclaration([propsImportSpecifier], t.stringLiteral('../types')),
]
const components = icons.flatMap(icon => {
const symbolName = `${pascalCase(icon.name)}Symbol`
const symbols = Object.entries(icon.heights).map(([height, size]) => {
const id = `symbol-octicon-${icon.name}-${height}`
const jsx = t.jsxElement(
t.jsxOpeningElement(t.jsxIdentifier('symbol'), [
t.jsxAttribute(t.jsxIdentifier('id'), t.stringLiteral(id)),
t.jsxAttribute(t.jsxIdentifier('viewBox'), t.stringLiteral(`0 0 ${size.width} ${height}`)),
]),
t.jsxClosingElement(t.jsxIdentifier('symbol')),
svgToJSX(size.ast),
)
return {
id,
height,
width: size.width,
jsx,
}
})
const symbolComponent = t.functionDeclaration(
t.identifier(symbolName),
[],
t.blockStatement([
t.returnStatement(
symbols.length === 1
? symbols[0].jsx
: t.jsxFragment(
t.jsxOpeningFragment(),
t.jsxClosingFragment(),
symbols.map(symbol => symbol.jsx),
),
),
]),
)
const referenceName = `${pascalCase(icon.name)}Icon`
const forwardRef = t.callExpression(t.identifier('forwardRef'), [
t.functionExpression(
t.identifier(referenceName),
[t.identifier('props'), t.identifier('ref')],
t.blockStatement([
t.returnStatement(
t.jsxElement(
t.jsxOpeningElement(
t.jsxIdentifier('Icon'),
[
t.jsxSpreadAttribute(t.identifier('props')),
t.jsxAttribute(t.jsxIdentifier('ref'), t.jsxExpressionContainer(t.identifier('ref'))),
t.jsxAttribute(
t.jsxIdentifier('sizes'),
t.jsxExpressionContainer(
t.objectExpression(
symbols.map(symbol =>
t.objectProperty(
t.stringLiteral(symbol.height),
t.objectExpression([
t.objectProperty(t.stringLiteral('width'), t.numericLiteral(symbol.width)),
t.objectProperty(t.stringLiteral('id'), t.stringLiteral(symbol.id)),
]),
),
),
),
),
),
],
true,
),
t.jsxClosingElement(t.jsxIdentifier('Icon')),
[],
),
),
]),
),
])
forwardRef.typeParameters = t.tsTypeParameterInstantiation([
t.tsTypeReference(t.identifier('SVGSVGElement')),
t.tsTypeReference(t.identifier('OcticonReferenceProps')),
])
const reference = t.variableDeclaration('const', [t.variableDeclarator(t.identifier(referenceName), forwardRef)])
return [t.exportNamedDeclaration(symbolComponent), t.exportNamedDeclaration(reference)]
})
const body = [...imports, ...components]
const program = t.addComment(
t.program(body),
'leading',
`This file is auto-generated by 'script/build.ts'. Do not edit directly.`,
)
return {
filepath,
contents: generate(program).code,
exports: icons.flatMap(icon => [`${pascalCase(icon.name)}Icon`, `${pascalCase(icon.name)}Symbol`]),
}
})
for (const mod of modules) {
await fs.writeFile(mod.filepath, mod.contents)
}
const indexFilePath = path.join(SOURCE_DIRECTORY, 'generated', 'index.ts')
const octiconsSymbolsPropsExportSpecifier = t.exportSpecifier(
t.identifier('OcticonSymbolsProps'),
t.identifier('OcticonSymbolsProps'),
)
octiconsSymbolsPropsExportSpecifier.exportKind = 'type'
const octiconsReferencePropsExportSpecifier = t.exportSpecifier(
t.identifier('OcticonReferenceProps'),
t.identifier('OcticonReferenceProps'),
)
octiconsReferencePropsExportSpecifier.exportKind = 'type'
const index = t.addComment(
t.program([
t.exportNamedDeclaration(
null,
[
t.exportSpecifier(t.identifier('OcticonSymbols'), t.identifier('OcticonSymbols')),
octiconsSymbolsPropsExportSpecifier,
],
t.stringLiteral('../OcticonSymbols'),
),
t.exportNamedDeclaration(null, [octiconsReferencePropsExportSpecifier], t.stringLiteral('../types')),
...modules.map(mod => {
return t.exportNamedDeclaration(
null,
mod.exports.map(exportName => t.exportSpecifier(t.identifier(exportName), t.identifier(exportName))),
t.stringLiteral(`./${path.basename(mod.filepath, path.extname(mod.filepath))}`),
)
}),
]),
'leading',
`This file is auto-generated by 'script/build.ts'. Do not edit directly.`,
)
await fs.writeFile(indexFilePath, generate(index).code)
function partition<T>(items: Array<T>, size: number): Array<Array<T>> {
const result: Array<Array<T>> = []
let bucket: Array<T> = []
let count = 0
for (const item of items) {
if (count >= size) {
result.push(bucket)
bucket = []
count = 0
}
bucket.push(item)
count++
}
if (bucket.length > 0) {
result.push(bucket)
}
return result
}
type SVGASTNode = {
type: string
name: string
attributes: Record<string, string>
children: Array<SVGASTNode>
}
function svgToJSX(node: SVGASTNode): Array<babel.types.JSXElement> {
if (node.type === 'element') {
const children = node.children.map(svgToJSX)
if (node.name === 'svg') {
if (children.length === 0) {
throw new Error(`No children available for icon`)
}
return children.flat()
}
const attrs = Object.entries(node.attributes).map(([key, value]) => {
if (typeof value !== 'string') {
throw new Error(`Unknown value type: ${value}`)
}
return t.jsxAttribute(t.jsxIdentifier(key), t.stringLiteral(value))
})
const openingElement = t.jsxOpeningElement(t.jsxIdentifier(node.name), attrs, children.length === 0)
const closingElement = t.jsxClosingElement(t.jsxIdentifier(node.name))
if (children.length > 0) {
return [t.jsxElement(openingElement, closingElement, children.flat(), false)]
}
return [t.jsxElement(openingElement, closingElement, [], true)]
}
throw new Error(`Unknown type: ${node.type}`)
}