-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
139 lines (132 loc) · 3.23 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
const PLATFORMS = ["css", "scss"];
const StyleDictionary = require("style-dictionary").extend({
source: ["src/tokens/index.js", "src/components/index.js"],
platforms: {
css: {
transformGroup: "css",
buildPath: "css/",
files: [
{
destination: "variables.css",
format: "css/variables",
},
{
destination: `variables-dark.css`,
format: `css/dark`,
filter: (token) => token.darkValue,
},
],
},
scss: {
transformGroup: "scss",
buildPath: "scss/",
files: [
{
destination: "variables.scss",
format: "scss/map-deep",
},
],
},
js: {
transformGroup: "js",
files: [
{
format: "javascript/module",
destination: "lib/variables.js",
},
],
},
mjs: {
transformGroup: "js",
files: [
{
format: "javascript/esm",
destination: "lib/variables.esm.js",
},
],
},
json: {
transformGroup: "js",
files: [
{
format: "json/nested",
destination: "lib/variables.json",
},
],
},
},
});
function minifyDictionary(obj) {
if (typeof obj !== "object" || Array.isArray(obj)) {
return obj;
}
var toRet = {};
if (obj.hasOwnProperty("value")) {
return obj.value;
} else {
for (var name in obj) {
if (obj.hasOwnProperty(name)) {
toRet[name] = minifyDictionary(obj[name]);
}
}
}
return toRet;
}
StyleDictionary.registerFormat({
name: "javascript/esm",
formatter: function (dictionary, options) {
const minified = minifyDictionary(dictionary.properties);
const tokens = Object.keys(minified).map((name) => {
const value = JSON.stringify(minified[name], null, 2);
return `export const ${name} = ${value};`;
});
return [
`/**`,
` * Do not edit directly`,
` * Generated on ${new Date().toUTCString()}`,
` **/`,
...tokens,
].join("\n");
},
});
StyleDictionary.registerFormat({
name: "javascript/module",
formatter: function (dictionary, options) {
const tokens = JSON.stringify(
minifyDictionary(dictionary.properties),
null,
2
);
return [
`/**`,
` * Do not edit directly`,
` * Generated on ${new Date().toUTCString()}`,
` **/`,
`module.exports = ${tokens};`,
].join("\n");
},
});
function cssFormatWrapper(property) {
return function (args) {
const dictionary = Object.assign({}, args.dictionary);
// Override each token's `value` with `property` value
dictionary.allProperties = dictionary.allProperties.map((token) => {
const propValue = token[property];
if (propValue) {
return Object.assign({}, token, {
value: propValue,
});
} else {
return token;
}
});
// Use the built-in format but with our customized dictionary object
// so it will output the hcValue instead of the value
return StyleDictionary.format["css/variables"]({ ...args, dictionary });
};
}
StyleDictionary.registerFormat({
name: "css/dark",
formatter: cssFormatWrapper("darkValue"),
});
StyleDictionary.buildAllPlatforms();