-
Notifications
You must be signed in to change notification settings - Fork 0
/
style-dictionary.build.js
199 lines (182 loc) · 5.53 KB
/
style-dictionary.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
/** @format */
const StyleDictionaryPackage = require('style-dictionary');
const tailwindThemeColorFormat = require('./formats/tailwindColorFormat');
const reactThemeColorFormat = require('./formats/reactColorFormat');
StyleDictionaryPackage.registerFormat({
name: 'css/variables',
formatter: function (dictionary, config) {
return `${this.selector} {\n${dictionary.allProperties
.map((prop) => ` --${prop.name}: ${prop.value};`)
.join('\n')}\n}`;
},
});
// Cleans rgb from inside rgba().This is to fix an issue with rgba and variables coming from Figma
// Examples
// rgba(rgb(244, 63, 133), 0.5) -> rgba(244, 63, 133, 0.5)
// linear-gradient(180deg, rgba(rgb(0, 0, 0), 0.02) 2%, rgba(rgb(0, 0, 0), 0.10) 100%) ->
// linear-gradient(180deg, rgba(0, 0, 0, 0.02) 2%, rgba(0, 0, 0, 0.10) 100%)
const cleanRgbFromValue = (value) => {
const rgx = /rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/;
const cleanValue = value.replace(rgx, '$1,$2,$3');
if (cleanValue.match(rgx)) {
return cleanRgbFromValue(cleanValue);
} else {
return cleanValue;
}
};
StyleDictionaryPackage.registerTransform({
type: `value`,
transitive: true,
name: `css/duplicate-rgb`,
matcher: ({ value }) => value.includes('rgba(rgb('),
transformer: ({ value }) => cleanRgbFromValue(value),
});
const twSpectrumConfig = {
format: {
// Transforming colors to a tailwind.config.js color Object
tailwindThemeColorFormat,
},
source: ['tokens/spectrum.json'],
platforms: {
tailwind: {
buildPath: 'src/output/',
transformGroup: 'js',
transforms: ['attribute/cti', 'name/cti/kebab'],
files: [
{
filter: function (token) {
return token.filePath === `tokens/spectrum.json`;
},
destination: 'colors.spectrum.tailwind.js',
format: 'tailwindThemeColorFormat',
options: {
cssVariable: false,
},
},
],
},
},
};
const twThemeConfig = {
format: {
// Transforming colors to a tailwind.config.js color Object
tailwindThemeColorFormat,
},
source: ['tokens/spectrum.json', `tokens/light.json`],
platforms: {
tailwind: {
buildPath: 'src/output/',
transformGroup: 'js',
transforms: ['attribute/cti', 'name/cti/kebab', 'color/css'],
files: [
{
filter: function (token) {
return token.filePath === `tokens/light.json`;
},
destination: `colors.theme.tailwind.js`,
format: 'tailwindThemeColorFormat',
options: {
cssVariable: true,
},
},
],
},
},
};
const reactSpectrumConfig = {
format: {
reactThemeColorFormat,
},
source: ['tokens/spectrum.json'],
platforms: {
react: {
buildPath: 'src/output/',
transformGroup: 'js',
transforms: ['name/cti/camel', 'size/object', 'color/css'],
files: [
{
filter: function (token) {
return token.filePath === `tokens/spectrum.json`;
},
destination: `colors.spectrum.react.js`,
format: 'reactThemeColorFormat',
options: {
theme: true,
},
},
],
},
},
};
const reactThemeConfig = (theme) => ({
format: {
reactThemeColorFormat,
},
source: ['tokens/spectrum.json', `tokens/${theme}.json`],
platforms: {
react: {
buildPath: 'src/output/',
transformGroup: 'js',
transforms: ['name/cti/camel', 'size/object', 'color/css'],
files: [
{
filter: function (token) {
return token.filePath === `tokens/${theme}.json`;
},
destination: `colors.theme.${theme}.react.js`,
format: 'reactThemeColorFormat',
options: {
theme: true,
},
},
],
},
},
});
const themeConfig = (theme) => {
return {
source: ['tokens/spectrum.json', `tokens/${theme}.json`],
platforms: {
theme: {
transforms: [
'attribute/cti',
'name/cti/kebab',
'color/rgb',
'css/duplicate-rgb',
],
buildPath: `src/output/`,
files: [
{
filter: function (token) {
return token.filePath === `tokens/${theme}.json`;
},
destination: `theme-${theme}.css`,
format: 'css/variables',
selector: `.theme-${theme}`,
},
],
},
},
};
};
console.log('\n==============================================');
console.log(`\nProcessing: Spectrum`);
const dictionaryTailwindColors =
StyleDictionaryPackage.extend(twSpectrumConfig);
dictionaryTailwindColors.buildPlatform('tailwind');
console.log(`\nProcessing: Theme`);
const dictionaryTailwindTheme = StyleDictionaryPackage.extend(twThemeConfig);
dictionaryTailwindTheme.buildPlatform('tailwind');
const dictionaryReactLightTheme = StyleDictionaryPackage.extend(reactThemeConfig('light'));
dictionaryReactLightTheme.buildPlatform('react');
const dictionaryReactDarkTheme = StyleDictionaryPackage.extend(reactThemeConfig('dark'));
dictionaryReactDarkTheme.buildPlatform('react');
const dictionaryReactSpectrum = StyleDictionaryPackage.extend(reactSpectrumConfig);
dictionaryReactSpectrum.buildPlatform('react');
console.log(`\nProcessing: Light`);
const dictionaryLight = StyleDictionaryPackage.extend(themeConfig('light'));
dictionaryLight.buildPlatform('theme');
console.log(`\nProcessing: Dark`);
const dictionaryDark = StyleDictionaryPackage.extend(themeConfig('dark'));
dictionaryDark.buildPlatform('theme');
console.log('\nEnd processing');