-
Notifications
You must be signed in to change notification settings - Fork 161
/
build.js
172 lines (146 loc) · 4.61 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
import StyleDictionary from 'style-dictionary';
console.log('Build started...');
console.log('\n==============================================');
const arrayTocamelCase = (arr) =>
arr
.map((item, i) => (i === 0 ? item : item[0].toUpperCase() + item.slice(1)))
.join('');
const encodeJson = (data) =>
JSON.stringify(data, null, 2).replace(
/[\u007f-\uffff]/g,
(c) => `\\u${`0000${c.charCodeAt(0).toString(16)}`.slice(-4)}`,
);
const toScssIdentifier = (string) =>
string.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase();
const toScssValue = (chunk) => {
if (typeof chunk === 'boolean' || typeof chunk === 'number') {
return chunk;
}
if (typeof chunk === 'string') {
return /\s/.test(chunk) ? encodeJson(chunk) : chunk;
}
if (chunk === undefined || chunk === null) {
return 'null';
}
if (Array.isArray(chunk)) {
return `(${chunk.map(toScssValue).join(',')})`;
}
return `(${Object.entries(chunk)
.map(([key, value]) => `${toScssIdentifier(key)}:${toScssValue(value)}`)
.join(',')})`;
};
StyleDictionary.registerTransformGroup({
name: 'custom/mjs',
transforms: ['name/camel'],
});
StyleDictionary.registerFormat({
name: 'custom/colors-json',
async format({ dictionary }) {
return `{${dictionary.allTokens.map(
(token) =>
`\n\t${encodeJson(token.path[1])}: ${encodeJson(token.original.value)}`,
)}\n}`;
},
});
StyleDictionary.registerFormat({
name: 'custom/breakpoints-json',
async format({ dictionary }) {
return `[${dictionary.allTokens.map(
(token) => `\n\t${encodeJson(token.original.value)}`,
)}\n]`;
},
});
StyleDictionary.registerFormat({
name: 'cjsmodule',
async format({ dictionary }) {
return `module.exports = {${dictionary.allTokens.map(
(token) =>
`\n\t${encodeJson(token.name)}: ${encodeJson(token.original.value)}`,
)}\n};`;
},
});
StyleDictionary.registerFormat({
name: 'camelCase',
async format({ dictionary }) {
// Get group name through folder name ./src/******
const exp = /[a-z]+\/([a-z]+)\/[a-z]+.json/i;
const [, group] = dictionary.allTokens[0].filePath.match(exp);
return `module.exports = {${dictionary.allTokens.map((token) => {
const name =
group === 'colors'
? encodeJson(token.path[1])
: encodeJson(arrayTocamelCase(token.path));
return `\n\t${name}: ${encodeJson(token.value)}`;
})}\n};`;
},
});
StyleDictionary.registerFormat({
name: 'custom/mjs',
async format({ dictionary }) {
return `export default {${dictionary.allTokens.map(
(token) =>
`\n\t${encodeJson(token.name)}: ${encodeJson(token.original.value)}`,
)}\n};`;
},
});
StyleDictionary.registerFormat({
name: 'custom/colors-mjs',
async format({ dictionary }) {
return `export default {${dictionary.allTokens.map(
(token) =>
`\n\t${encodeJson(token.path[1])}: ${encodeJson(token.original.value)}`,
)}\n};`;
},
});
StyleDictionary.registerFormat({
name: 'custom/scss',
async format({ dictionary }) {
// Get group name through folder name ./src/******
const exp = /[a-z]+\/([a-z]+)\/[a-z]+.json/i;
const [, group] = dictionary.allTokens[0].filePath.match(exp);
const newPaletteGroup = [
'badge',
'background',
'surface',
'stroke',
'shadow',
'button',
'font',
'status',
'statusBullet',
];
if (newPaletteGroup.includes(group)) {
const subGroup = dictionary.allTokens[0].name.split('-')[0];
return `$${subGroup}: (${dictionary.allTokens.map((token) => {
const nameArray = token.name.split('-');
nameArray.shift();
const tokenName = nameArray.join('-');
return `\n${toScssIdentifier(tokenName)}:${toScssValue(token.value)}`;
})}\n);`;
}
return `$${group}: (${dictionary.allTokens
.map(
(token) =>
`\n${toScssIdentifier(
group === 'colors' ? token.path[1] : token.name,
)}:${toScssValue(token.value)},`,
)
.join('')})`;
},
});
StyleDictionary.registerFormat({
name: 'custom/typography-scss',
async format({ dictionary }) {
return `${dictionary.allTokens
.map((token) => `$${token.name}: \n${toScssValue(token.value)};`)
.join('')}`;
},
});
// APPLY THE CONFIGURATION
// needs to be done _before_ applying the configuration
const StyleDictionaryExtended = new StyleDictionary('./config.js');
await StyleDictionaryExtended.hasInitialized;
// Build all platforms
await StyleDictionaryExtended.buildAllPlatforms();
console.log('\n==============================================');
console.log('\nBuild completed!');