-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.js
99 lines (80 loc) · 2.63 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
const StyleDictionaryPackage = require('style-dictionary');
// HAVE THE STYLE DICTIONARY CONFIG DYNAMICALLY GENERATED
StyleDictionaryPackage.registerFormat({
name: 'css/variables',
formatter: function (dictionary, config) {
return `${this.selector} {
${dictionary.allProperties.map(prop => ` --${prop.name}: ${prop.value};`).join('\n')}
}`
}
});
//
function kebabIt(str) {
return str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.join('-')
.toLowerCase();
}
function getBasePxFontSize(options) {
return (options && options.basePxFontSize) || 16;
}
function fontPxToRem(token, options) {
const baseFont = getBasePxFontSize(options);
const floatVal = parseFloat(token.value);
if (isNaN(floatVal)) {
console.log('NaN error', token.name, token.value, 'rem');
}
if (floatVal === 0) {
return '0';
}
return `${floatVal / baseFont}rem`;
}
StyleDictionaryPackage.registerTransform({
name: 'size/pxToRem',
type: 'value',
matcher: (token) => ['fontSizes'].includes(token.type),
transformer: (token, options) => fontPxToRem(token, options)
})
//
StyleDictionaryPackage.registerTransform({
name: 'sizes/px',
type: 'value',
matcher: function(prop) {
// You can be more specific here if you only want 'em' units for font sizes
return ["fontSize", "spacing", "borderRadius", "borderWidth", "sizing"].includes(prop.attributes.category);
},
transformer: function(prop) {
// You can also modify the value here if you want to convert pixels to ems
return parseFloat(prop.original.value) + 'px';
}
});
function getStyleDictionaryConfig(theme) {
return {
"source": [
`input/${theme}.json`,
],
"platforms": {
"web": {
"transforms":
["attribute/cti", "name/cti/kebab", "sizes/px", "size/pxToRem"],
"buildPath": `output/`,
"files": [{
"destination": `${theme}.css`,
"format": "css/variables",
"selector": `.${theme}-theme`
}]
}
}
};
}
console.log('Build started...');
// PROCESS THE DESIGN TOKENS FOR THE DIFFEREN BRANDS AND PLATFORMS
['base', 'brand-a', 'brand-b'].map(function (theme) {
console.log('\n==============================================');
console.log(`\nProcessing: [${theme}]`);
const StyleDictionary = StyleDictionaryPackage.extend(getStyleDictionaryConfig(theme));
StyleDictionary.buildPlatform('web');
console.log('\nEnd processing');
})
console.log('\n==============================================');
console.log('\nBuild completed!');