-
Notifications
You must be signed in to change notification settings - Fork 3
/
style-dictionary.config.js
179 lines (167 loc) · 4.8 KB
/
style-dictionary.config.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
const resolveConfig = require("tailwindcss/resolveConfig");
const tailwindConfig = require("./tailwind.config.js");
const _ = require("lodash");
const { transform } = require("@divriots/style-dictionary-to-figma");
// const StyleDictionary = require("style-dictionary");
// StyleDictionary.registerFormat({
// name: "figmaTokensPlugin",
// formatter: ({ dictionary }) => {
// const transformedTokens = transform(dictionary.tokens);
// return JSON.stringify(transformedTokens, null, 2);
// },
// });
// Grab just the theme data from the Tailwind config.
const { theme } = resolveConfig(tailwindConfig);
// Create an empty object to hold our transformed tokens data.
const tokens = {};
// A helper function that uses Lodash's setWidth method to
// insert things into an object at the right point in the
// structure, and to create the right structure for us
// if it doesn't already exist.
const addToTokensObject = function (position, value, attr = null) {
_.setWith(tokens, position, { value: value, ...attr }, Object);
};
// Loop over the theme data…
_.forEach(theme, function (value, key) {
switch (key) {
case "fontFamily":
// Font family data is in an array, so we use join to
// turn the font families into a single string.
_.forEach(theme["fontFamily"], function (value, key) {
addToTokensObject(
["fontFamily", key],
theme["fontFamily"][key].join(",")
);
});
break;
case "fontSize":
// Font size data contains both the font size (makes
// sense!) but also a recommended line-length, so we
// create two tokens for every font size, one for the
// font-size value and one for the line-height.
_.forEach(theme["fontSize"], function (value, key) {
addToTokensObject(["fontSize", key], value[0]);
addToTokensObject(
["fontSize", `${key}--lineHeight`],
value[1]["lineHeight"]
);
});
break;
case "lineHeight":
_.forEach(theme["lineHeight"], function (value, key) {
// convert rem to px
const valuePx = parseFloat(value) * 16;
addToTokensObject(["lineHeights", key], valuePx, {
type: "lineHeights",
});
});
break;
default:
_.forEach(value, function (value, secondLevelKey) {
if (!_.isObject(value)) {
// For non-objects (simple key/value pairs) we can
// add them straight into our tokens object.
addToTokensObject([key, secondLevelKey], value);
} else {
// Skip 'raw' CSS media queries.
if (!_.isUndefined(value["raw"])) {
return;
}
// For objects (like color shades) we need to do a
// final forOwn loop to make sure we add everything
// in the right format.
_.forEach(value, function (value, thirdLevelKey) {
addToTokensObject([key, secondLevelKey, thirdLevelKey], value);
});
}
});
break;
}
});
const limitedFilter = (token) =>
["colors", "spacing", "fontFamily"].includes(token.attributes.category);
const fullFilter = (token) =>
[
"screens",
"colors",
"spacing",
"opacity",
"borderRadius",
"borderWidth",
"boxShadow",
"fontFamily",
"fontSize",
"fontWeight",
"letterSpacing",
"lineHeights",
"maxWidth",
"zIndex",
"scale",
"transitionProperty",
"transitionTimingFunction",
"transitionDuration",
"transitionDelay",
"animation",
].includes(token.attributes.category);
module.exports = {
tokens,
source: ["**/*.tokens.json"],
format: {
figmaTokensPlugin: ({ dictionary }) => {
const transformedTokens = transform(dictionary.tokens);
return JSON.stringify(transformedTokens, null, 2);
},
},
platforms: {
json: {
transformGroup: "js",
buildPath: "tokens/",
files: [
{
format: "figmaTokensPlugin",
destination: "tokens.json",
filter: fullFilter,
options: {
outputReferences: true,
},
},
],
},
js: {
transformGroup: "js",
buildPath: "dist/js/",
files: [
{
format: "javascript/module",
destination: "tokens.js",
filter: fullFilter,
options: {
outputReferences: true,
},
},
],
},
css: {
transformGroup: "css",
buildPath: "dist/css/",
files: [
{
format: "css/variables",
destination: "variables.css",
filter: fullFilter,
},
],
},
scss: {
transformGroup: "scss",
buildPath: "src/scss/",
files: [
{
destination: "_variables.scss",
format: "scss/variables",
filter: limitedFilter,
},
],
},
},
};