-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
style-dictionary.js
167 lines (152 loc) · 3.94 KB
/
style-dictionary.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
import StyleDictionary from "style-dictionary";
import { fileHeader, formattedVariables } from "style-dictionary/utils";
/*
Set themes here. The SCSS code uses the light theme to define the accessor
functions. Token files for a specific theme should be named `*.{theme}.json` or
`*.{theme}.js`. Files without the theme in the name are included in all themes.
When you add a theme, you will also need to add a theme file to `src/css/themes`
and use it in `src/css/themes/_index.scss`.
*/
const themes = ["light", "dark"];
const isInternal = (token) => {
return token.attributes.category == "internal";
};
const isBaseColor = (token) => {
return (
token.attributes.category === "color" && token.attributes.type === "base"
);
};
StyleDictionary.registerFilter({
name: "noBaseColors",
filter: (token) => {
return !isBaseColor(token);
},
});
StyleDictionary.registerFilter({
name: "noInternals",
filter: (token) => {
return !isInternal(token);
},
});
StyleDictionary.registerFormat({
name: "scss/custom-properties",
format: async function ({ dictionary, file }) {
return (
(await fileHeader({ file })) +
dictionary.allTokens
.map((prop) => `$${prop.name}: --${prop.name};`)
.join("\n")
);
},
});
StyleDictionary.registerFormat({
name: "scss/mixin",
format: async function ({ dictionary, file, options = {} }) {
const {
outputReferences,
themeable = true,
formatting,
usesDtcg,
} = options;
return (
(await fileHeader({ file })) +
`@mixin tokens {\n` +
formattedVariables({
format: "css",
dictionary,
outputReferences,
themeable,
formatting,
usesDtcg,
}) +
`\n}\n`
);
},
});
// Platforms that are built for each theme.
const platforms = (theme = "") => {
if (theme != "") {
theme = "." + theme;
}
return {
scss: {
transformGroup: "scss",
buildPath: "build/tokens/scss/",
files: [
{
destination: `_mixin${theme}.scss`,
filter: (token) => {
return !isBaseColor(token) && !isInternal(token);
},
format: "scss/mixin",
},
],
},
css: {
transformGroup: "css",
buildPath: "build/tokens/css/",
files: [
{
destination: `tokens${theme}.css`,
filter: (token) => {
return !isBaseColor(token) && !isInternal(token);
},
format: "css/variables",
},
],
},
json: {
transformGroup: "js",
buildPath: "build/tokens/json/",
files: [
{
destination: `tokens${theme}.json`,
format: "json",
},
],
},
};
};
// Platforms that are only built once for all themes.
const themeIndependentPlatforms = {
scss: {
transformGroup: "scss",
buildPath: "build/tokens/scss/",
files: [
{
destination: `_variables.scss`,
filter: (token) => {
return !isBaseColor(token) && !isInternal(token);
},
format: "scss/custom-properties",
},
{
destination: `_tokens.scss`,
filter: "noBaseColors",
format: "scss/map-deep",
options: {
formatting: {
header:
"/**\n * This output contains a map that is used to retrieve token names. Do not use\n * the tokens defined in this file directly.\n *\n",
},
},
},
],
},
};
await Promise.all(
themes.map((theme) => {
const sd = new StyleDictionary({
include: [`src/tokens/**/!(*.${themes.join(`|*.`)}).{json,js}`],
source: [`src/tokens/**/*.${theme}.{json,js}`],
platforms: platforms(theme),
});
return sd.buildAllPlatforms();
}),
);
const sd = new StyleDictionary({
include: [`src/tokens/**/!(*.${themes.join(`|*.`)}).{json,js}`],
source: [`src/tokens/**/*.${themes[0]}.{json,js}`],
platforms: themeIndependentPlatforms,
});
sd.buildAllPlatforms();