-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
style-dictionary.js
121 lines (110 loc) · 2.78 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
const StyleDictionary = require("style-dictionary");
/*
Set themes here. The SCSS code uses the default 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 = ["default", "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",
matcher: (token) => {
return !isBaseColor(token);
},
});
StyleDictionary.registerFilter({
name: "noInternals",
matcher: (token) => {
return !isInternal(token);
},
});
StyleDictionary.registerFormat({
name: "scss/mixin",
formatter: function ({ dictionary, options = {}, file }) {
const { outputReferences } = options;
return (
`@mixin tokens {\n` +
StyleDictionary.formatHelpers.formattedVariables({
format: "css",
dictionary,
outputReferences,
}) +
`\n}\n`
);
},
});
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",
},
{
destination: `tokens${theme}.scss`,
filter: "noBaseColors",
format: "scss/map-deep",
},
],
},
css: {
transformGroup: "css",
buildPath: "build/tokens/css/",
files: [
{
destination: `tokens${theme}.css`,
filter: (token) => {
return !isBaseColor(token) && !isInternal(token);
},
format: "css/variables",
},
],
},
js: {
transformGroup: "js",
buildPath: "build/tokens/js/",
files: [
{
destination: `tokens${theme}.js`,
format: "javascript/es6",
},
],
},
json: {
transformGroup: "js",
buildPath: "build/tokens/json/",
files: [
{
destination: `tokens${theme}.json`,
format: "json",
},
],
},
};
};
for (const theme of themes) {
const themeConfig = StyleDictionary.extend({
include: [`src/tokens/**/!(*.${themes.join(`|*.`)}).{json,js}`],
source: [`src/tokens/**/*.${theme}.{json,js}`],
platforms: platforms(theme),
});
themeConfig.buildAllPlatforms();
}