-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathbuild.js
578 lines (513 loc) · 15 KB
/
build.js
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
const glob = require('fast-glob')
const StyleDictionary = require('style-dictionary')
const {fileHeader, formattedVariables} = StyleDictionary.formatHelpers
//-----
// functions to be extracted
// TODO: extract to a separate files
const pathToKebabCase = token => token.path.join('-')
const pathToDotNotation = token => token.path.join('.')
const capitalize = string => string[0].toUpperCase() + string.slice(1)
const pathToPascalCase = token => token.path.map(tokenPathItems => capitalize(tokenPathItems)).join('')
// REGISTER THE CUSTOM TRANFORMS
/**
* transform: scss variable names
* example: `$namespace-item-variant-property-modifier`
*/
StyleDictionary.registerTransform({
name: 'name/scss',
type: 'name',
transformer: pathToKebabCase,
})
/**
* transform: css variable names
* example: `--namespace-item-variant-property-modifier`
*/
StyleDictionary.registerTransform({
name: 'name/css',
type: 'name',
transformer: pathToKebabCase,
})
/**
* transform: js variable names
* example: `namespace.item.variant.property.modifier`
*/
StyleDictionary.registerTransform({
name: 'name/js',
type: 'name',
transformer: pathToDotNotation,
})
/**
* transform: js es6 variable names
* example: `NamespaceItemVariantPropertyModifier`
*/
StyleDictionary.registerTransform({
name: 'name/js/es6',
type: 'name',
transformer: pathToPascalCase,
})
// find values with px unit
function isPx(value) {
return /[\d.]+px$/.test(value)
}
// transform: px to rem
StyleDictionary.registerTransform({
name: 'pxToRem',
type: 'value',
transformer: token => {
if (isPx(token.value)) {
const baseFontSize = 16
const floatValue = parseFloat(token.value.replace('px', ''))
if (isNaN(floatValue)) {
return token.value
}
if (floatValue === 0) {
return '0'
}
if (token.name.includes('lineHeight')) {
return `${floatValue / baseFontSize}`
}
return `${floatValue / baseFontSize}rem`
}
return token.value
},
})
//-----
// ts output
StyleDictionary.registerTransform({
name: 'attribute/typescript',
type: 'attribute',
transformer: token => {
return {
typescript: {
// these transforms will need to match the ones you use for typescript
// or you can "chain" the transforms and use token.name and token.value
// like scss and less transforms do.
name: token.path.slice(1).join('.'),
value: token.value,
},
}
},
})
// css output
StyleDictionary.registerTransform({
name: 'attribute/css',
type: 'attribute',
transformer: token => {
const tokenName = token.path.slice(1).join('-')
return {
css: {
name: `--${tokenName}`,
value: token.value,
},
}
},
})
// transform: composite typography to shorthands
StyleDictionary.registerTransform({
name: 'typography/shorthand',
type: 'value',
transitive: true,
matcher: token => token.type === 'typography',
transformer: token => {
const {value} = token
// if lineHeight has value, include in shorthand
if (value.lineHeight) {
return `${value.fontWeight} ${value.fontSize}/${value.lineHeight} ${value.fontFamily}`
}
return `${value.fontWeight} ${value.fontSize} ${value.fontFamily}`
},
})
// REGISTER THE CUSTOM TRANFORM GROUPS
StyleDictionary.registerTransformGroup({
name: 'css',
transforms: ['name/css', 'pxToRem', 'typography/shorthand'],
})
StyleDictionary.registerTransformGroup({
name: 'scss',
transforms: ['name/scss', 'pxToRem', 'typography/shorthand'],
})
// REGISTER A CUSTOM FORMAT
// wrap mobile tokens in media query
StyleDictionary.registerFormat({
name: 'css/touch-target-mobile',
formatter({dictionary, file, options}) {
const {outputReferences} = options
return `${fileHeader({file})}
@media (pointer: coarse) { :root {\n${formattedVariables({
format: 'css',
dictionary,
outputReferences,
})}\n}}\n`
},
})
// wrap desktop tokens in media query
StyleDictionary.registerFormat({
name: 'css/touch-target-desktop',
formatter({dictionary, file, options}) {
const {outputReferences} = options
return `${fileHeader({file})}
@media (pointer: fine) { :root {\n${formattedVariables({
format: 'css',
dictionary,
outputReferences,
})}\n}}\n`
},
})
StyleDictionary.registerFormat({
name: 'custom/format/custom-media',
formatter({dictionary}) {
return dictionary.allProperties
.map(prop => {
const {value, name} = prop
return `@custom-media --${name}-viewport ${value};`
})
.join('\n')
},
})
// format docs
StyleDictionary.registerFormat({
name: 'json/docs',
formatter({dictionary}) {
const groupedTokens = groupBy(dictionary.allProperties, 'filePath')
return JSON.stringify(groupedTokens, null, 2)
},
})
/**
* Replacement format for javascript/module
*/
StyleDictionary.registerFormat({
name: 'javascript/module-v2',
formatter({dictionary, file}) {
const recursiveleyFlattenDictionary = obj => {
const tree = {}
if (typeof obj !== 'object' || Array.isArray(obj)) {
return obj
}
if (obj.hasOwnProperty('value')) {
return obj.value
} else {
for (const name in obj) {
if (obj.hasOwnProperty(name)) {
tree[name] = recursiveleyFlattenDictionary(obj[name])
}
}
}
return tree
}
return `${fileHeader({file})}
module.exports = ${JSON.stringify(recursiveleyFlattenDictionary(dictionary.tokens), null, 2)}`
},
})
/**
* Replacement format for typescript/module-declarations
* Type schema corresponds to javascript/module-v2 format
*/
StyleDictionary.registerFormat({
name: 'typescript/module-declarations-v2',
formatter({dictionary, options, file}) {
const {moduleName = `tokens`} = options
const getType = value => {
switch (typeof value) {
case 'string':
return 'string'
case 'number':
return 'number'
default:
return 'any'
}
}
const recursiveTypeGeneration = obj => {
const tree = {}
const shortHandSizes = ['large', 'medium', 'small']
if (typeof obj !== 'object' || Array.isArray(obj)) {
return obj
}
if (obj.hasOwnProperty('value') && typeof obj.value === 'string') {
return getType(obj.value)
} else {
for (const name in obj) {
if ((obj.hasOwnProperty(name) && obj.name === 'shorthand') || shortHandSizes.includes(obj.name)) {
for (const shorthandKey in obj.value) {
tree[shorthandKey] = getType(obj.value[shorthandKey])
}
return tree
} else if (obj.hasOwnProperty(name)) {
tree[name] = recursiveTypeGeneration(obj[name])
}
}
}
return tree
}
const output = `${fileHeader({file})}
declare const ${moduleName}: ${JSON.stringify(recursiveTypeGeneration(dictionary.tokens), null, 2)}
export default ${moduleName};`
return output
.replace(/"any"/g, 'any')
.replace(/"string"/g, 'string')
.replace(/"number"/g, 'number')
},
})
/**
* @name groupBy
* @description Equivalent to lodash _.groupBy, to avoid creating another package dependency for users
* @param {Array|Object} The collection to iterate over.
* @param {Function} The iteratee to transform keys.
* @returns {Object} Returns the composed aggregate object.
*/
function groupBy(collection, iteratee = x => x) {
const current = typeof iteratee === 'function' ? iteratee : ({[iteratee]: prop}) => prop
const array = Array.isArray(collection) ? collection : Object.values(collection)
return array.reduce((acc, item) => {
const value = current(item)
acc[value] = acc[value] || []
acc[value].push(item)
return acc
}, {})
}
/**
* @name build
* @description
* Used to generate design tokens programmatically using StyleDictionary
* Called internally to build primitives and exported for self-serve use
*
* @param {Object} options
* @param {string} options.source glob or file path to a JSON object of tokens
* @param {string} options.outputPath location to write the output files
* @param {string} options.namespace a custom namespace to use for the output files
* @param {Platform} options.platforms add custom platform configurations to style-dictionary
* @example
* buildPrimitives({
* source: [`src/colors.json`],
* outputPath: 'dist',
* namespace: 'primer',
* platforms: {...}
* })
*/
function buildPrimitives(
{source, outputPath = 'tokens-v2-private', include, platforms, namespace = 'primer'},
_StyleDictionary = StyleDictionary,
) {
// eslint-disable-next-line no-console
console.log('Build started...')
// eslint-disable-next-line no-console
console.log('\n==============================================')
const customParseConfig = {
parsers: [
{
pattern: /\.json$/,
parse: ({contents, filePath}) => {
try {
let mutableContent = JSON.stringify(contents)
if (filePath.includes('/functional/')) {
mutableContent = mutableContent.replace(/<namespace>/g, namespace)
}
const parsed = JSON.parse(mutableContent)
return JSON.parse(parsed)
} catch (error) {
// eslint-disable-next-line no-console
console.log(error)
}
},
},
],
}
const getConfig = files => {
const defaultPlatformConfig = {
css: {
buildPath: `${outputPath}/css/`,
transformGroup: 'css',
// map the array of token file paths to style dictionary output files
files: files.map(filePath => {
return {
format: `css/variables`,
destination: filePath.replace(`.json`, `.css`),
filter: token => token.filePath === filePath,
options: {
outputReferences: true,
},
}
}),
},
cssViewport: {
buildPath: `${outputPath}/css/tokens/functional/size/`,
transformGroup: 'css',
files: [
{
filter: token => token.filePath.includes('viewport'),
format: 'custom/format/custom-media',
destination: 'viewport.css',
},
],
},
scss: {
buildPath: `${outputPath}/scss/`,
transformGroup: 'scss',
// map the array of token file paths to style dictionary output files
files: files.map(filePath => {
return {
format: `scss/variables`,
destination: filePath.replace(`.json`, `.scss`),
filter: token => token.filePath === filePath,
options: {
outputReferences: true,
},
}
}),
},
js: {
buildPath: `${outputPath}/js/`,
transforms: ['name/js/es6', 'pxToRem'],
// map the array of token file paths to style dictionary output files
files: files.map(filePath => {
return {
format: `javascript/es6`,
destination: filePath.replace(`.json`, `.js`),
filter: token => token.filePath === filePath,
}
}),
},
jsModule: {
buildPath: `${outputPath}/js/module/`,
transforms: ['pxToRem'],
// map the array of token file paths to style dictionary output files
files: files.map(filePath => {
return {
format: `javascript/module`,
destination: filePath.replace(`.json`, `.js`),
filter: token => token.filePath === filePath,
}
}),
},
tsTypes: {
buildPath: `${outputPath}/ts/`,
transforms: ['pxToRem'],
// map the array of token file paths to style dictionary output files
files: files.map(filePath => {
return {
format: `typescript/module-declarations-v2`,
destination: filePath.replace(`.json`, `.d.ts`),
filter: token => token.filePath === filePath,
}
}),
},
ts: {
buildPath: `${outputPath}/ts/`,
transforms: ['pxToRem'],
// map the array of token file paths to style dictionary output files
files: files.map(filePath => {
return {
format: `javascript/module-v2`,
destination: filePath.replace(`.json`, `.js`),
filter: token => token.filePath === filePath,
}
}),
},
}
const config = {
...{include: include ? [include] : undefined},
source: files,
...customParseConfig,
platforms: platforms ? platforms : defaultPlatformConfig,
}
return config
}
//build all tokens
_StyleDictionary
.extend({
...getConfig(glob.sync([...source])),
})
.buildAllPlatforms()
}
/**
* @name init
* @description
* Triggers the build for @primer/primitive default tokens
* from an npm script. Internal use only. Use `build` for self-serve.
* @private
*/
function _init() {
const outputPath = 'tokens-v2-private'
//build all tokens
buildPrimitives({
source: [`tokens/**/*.json`, `!tokens/**/size-*.json`, `!tokens/**/color/*.json`, `!tokens/**/shadow/*.json`],
outputPath,
})
//build size fine
buildPrimitives({
source: [`tokens/functional/size/size-fine.json`, `tokens/base/size/size.json`],
outputPath,
})
//build size coarse
buildPrimitives({
source: [`tokens/functional/size/size-coarse.json`, `tokens/base/size/size.json`],
outputPath,
})
buildPrimitives({
source: [`tokens/base/size/size.json`, `tokens/functional/size/size-fine.json`],
outputPath,
platforms: {
css: {
buildPath: `${outputPath}/css/`,
transformGroup: 'css',
files: [
{
destination: `tokens/functional/size/size-fine.css`,
format: `css/touch-target-desktop`,
filter: token => token.filePath.includes('fine'),
options: {
outputReferences: true,
},
},
],
},
},
})
buildPrimitives({
source: [`tokens/base/size/size.json`, `tokens/functional/size/size-coarse.json`],
platforms: {
css: {
buildPath: `${outputPath}/css/`,
transformGroup: 'css',
files: [
{
destination: `tokens/functional/size/size-coarse.css`,
format: `css/touch-target-mobile`,
filter: token => token.filePath.includes('coarse'),
options: {
outputReferences: true,
},
},
],
},
},
})
//build docs data
buildPrimitives({
source: [`tokens/**/*.json`, `!tokens/**/color/*.json`, `!tokens/**/shadow/*.json`],
outputPath,
platforms: {
docs: {
buildPath: `${outputPath}/docs/`,
transformGroup: 'css',
files: [
{
format: 'json/docs',
destination: 'docValues.json',
options: {
outputReferences: false,
},
},
],
},
},
})
}
module.exports = {
buildPrimitives,
_init,
StyleDictionary,
pathToKebabCase,
pathToDotNotation,
capitalize,
pathToPascalCase,
}