This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
269 lines (256 loc) · 7.62 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
const styleDictionary = require('style-dictionary');
const _ = require('lodash');
const yaml = require('yaml');
/* From style-dictionary/lib/common/formats.js */
function fileHeader(options, commentStyle) {
var to_ret = '';
// for backward compatibility we need to have the user explicitly hide them
var showFileHeader = options ? options.showFileHeader : true;
if (showFileHeader) {
if (commentStyle === 'short') {
to_ret += '\n';
to_ret += '// Do not edit directly\n';
to_ret += '// Generated on ' + new Date().toUTCString() + '\n';
to_ret += '\n';
} else {
to_ret += '/**\n';
to_ret += ' * Do not edit directly\n';
to_ret += ' * Generated on ' + new Date().toUTCString() + '\n';
to_ret += ' */\n\n';
}
}
return to_ret;
}
/* From style-dictionary/lib/common/formats.js */
function variablesWithPrefix(prefix, properties, commentStyle) {
return properties
.map(function (prop) {
var to_ret_prop =
prefix +
prop.name +
': ' +
(prop.attributes.category === 'asset'
? '"' + prop.value + '"'
: prop.value) +
';';
if (prop.comment) {
if (commentStyle === 'short') {
to_ret_prop = to_ret_prop.concat(' // ' + prop.comment);
} else {
to_ret_prop = to_ret_prop.concat(' /* ' + prop.comment + ' */');
}
}
return to_ret_prop;
})
.filter(function (strVal) {
return !!strVal;
})
.join('\n');
}
// Register css/dark custom format for "dark" theme tokens.
// It supports two selectors:
// - body.dark (a class selector) compatible with gatsby-plugin-dark-mode
// - [data-theme='dark'] body (data attribute) compatible with next-themes
styleDictionary.registerFormat({
name: 'css/dark',
formatter: function (dictionary) {
return (
fileHeader(this.options) +
"body.dark, [data-theme='dark'] body {\n" +
variablesWithPrefix(' --', dictionary.allProperties) +
'\n}\n'
);
},
});
// Map component properties to style-dictionary's CTI taxonomy
// (In components, the "C" and "I" are reversed).
// From the component-cti example in style-dictionary
//
const propertiesToCTI = {
width: { category: 'size', type: 'dimension' },
'min-width': { category: 'size', type: 'dimension' },
'max-width': { category: 'size', type: 'dimension' },
height: { category: 'size', type: 'dimension' },
'min-height': { category: 'size', type: 'dimension' },
'max-height': { category: 'size', type: 'dimension' },
'border-width': { category: 'size', type: 'border', item: 'width' },
'border-radius': { category: 'size', type: 'border', item: 'width' },
'border-color': { category: 'color', type: 'border' },
'background-color': { category: 'color', type: 'background' },
color: { category: 'color', type: 'font' },
'text-color': { category: 'color', type: 'font' },
padding: { category: 'size', type: 'padding' },
'padding-vertical': { category: 'size', type: 'padding' },
'padding-horizontal': { category: 'size', type: 'padding' },
icon: { category: 'content', type: 'icon' },
'font-size': { category: 'size', type: 'font' },
'line-height': { category: 'size', type: 'line-height' },
size: { category: 'size', type: 'icon' },
};
// Custom transform for components
const CTITransform = {
transformer: (prop) => {
// Only do this custom functionality in the 'component' top-level namespace.
if (prop.path[0] === 'component') {
// When defining component tokens, the key of the token is the relevant CSS property
// The key of the token is the last element in the path array
const pathLength = prop.path.length;
if (prop.path[pathLength - 2] === 'themed') {
// Ignore the themed part of the path
return propertiesToCTI[prop.path[prop.path.length - 3]];
} else {
return propertiesToCTI[prop.path[prop.path.length - 1]];
}
} else {
// Fallback to the original 'attribute/cti' transformer
return styleDictionary.transform['attribute/cti'].transformer(prop);
}
},
};
// Custom kebab naming transform that ignore "themed.xyz" from the path
styleDictionary.registerTransform({
name: 'name/rsp/themedKebab',
type: 'name',
transformer: (prop, options) => {
const pathLength = prop.path.length;
if (prop.path[pathLength - 2] === 'themed') {
return _.kebabCase(
[options.prefix].concat(prop.path.slice(0, pathLength - 2)).join(' ')
);
} else {
return _.kebabCase([options.prefix].concat(prop.path).join(' '));
}
},
});
// Filter only "dark" theme tokens
styleDictionary.registerFilter({
name: 'isDarkTheme',
matcher: (prop) => {
const pathLength = prop.path.length;
return (
pathLength >= 3 &&
prop.path[pathLength - 2] === 'themed' &&
prop.path[pathLength - 1] === 'dark'
);
},
});
// Filter only "light" or unthemed tokens
styleDictionary.registerFilter({
name: 'isDefaultTheme',
matcher: (prop) => {
const pathLength = prop.path.length;
if (prop.path[pathLength - 2] != 'themed') {
return true;
}
if (prop.path[pathLength - 1] === 'light') {
return true;
} else {
return false;
}
},
});
styleDictionary.registerTransformGroup({
name: 'css/themed',
transforms: [
'attribute/cti',
'name/rsp/themedKebab',
'time/seconds',
'content/icon',
'size/rem',
'color/css',
],
});
const config = {
parsers: [
{
pattern: /\.yaml$/,
// The parse function takes a single argument, which is an object with
// 2 attributes: contents which is a string of the file contents, and
// filePath which is the path of the file.
// The function is expected to return a plain object.
parse: ({ contents, filePath }) => yaml.parse(contents),
},
],
transform: {
// Override the built-in attribute/cti transform
'attribute/cti': CTITransform,
},
source: [`src/**/*.yaml`],
platforms: {
css: {
transformGroup: 'css/themed',
buildPath: 'dist/',
prefix: 'rsd',
files: [
{
destination: 'tokens.css',
format: 'css/variables',
filter: 'isDefaultTheme',
},
],
},
cssDark: {
transformGroup: 'css/themed',
buildPath: 'dist/',
prefix: 'rsd',
files: [
{
destination: 'tokens.dark.css',
format: 'css/dark',
filter: 'isDarkTheme',
},
],
},
js: {
transformGroup: 'js',
buildPath: 'dist/',
files: [
{
destination: 'tokens.js',
format: 'javascript/es6',
},
],
},
assetsEmbedJson: {
transforms: ['attribute/cti', 'name/cti/kebab', 'asset/base64'],
buildPath: 'dist/json/',
files: [
{
destination: 'rubin-imagotype-base64.json',
format: 'json/flat',
filter: {
attributes: {
category: 'asset',
type: 'image',
item: 'rubin-imagotype',
},
},
},
{
destination: 'rubin-favicon-base64.json',
format: 'json/flat',
filter: {
attributes: {
category: 'asset',
type: 'image',
item: 'favicon',
},
},
},
{
destination: 'rubin-watermark-base64.json',
format: 'json/flat',
filter: {
attributes: {
category: 'asset',
type: 'image',
item: 'rubin-watermark',
},
},
},
],
},
},
};
const SD = styleDictionary.extend(config);
SD.buildAllPlatforms();