@terrazzo/plugin-css 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Drew Powers
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,22 @@
1
+ import type { Plugin } from '@terrazzo/parser';
2
+ export interface ModeSelector {
3
+ /** The name of the mode to match */
4
+ mode: string;
5
+ /** (optional) Provide token IDs to match. Globs are allowed (e.g: `["color.*", "shadow.dark"]`) */
6
+ tokens?: string[];
7
+ /** Provide CSS selectors to generate. (e.g.: `["@media (prefers-color-scheme: dark)", "[data-color-theme='dark']"]` ) */
8
+ selectors: string[];
9
+ }
10
+ export interface CSSPluginOptions {
11
+ /** Where to output CSS */
12
+ filename?: string;
13
+ /** Glob patterns to exclude tokens from output */
14
+ exclude?: string[];
15
+ /** Define mode selectors as media queries or CSS classes */
16
+ modeSelectors?: ModeSelector[];
17
+ /** Control the final CSS variable name */
18
+ variableName?: (name: string) => string;
19
+ }
20
+ export declare const FORMAT_ID = "css";
21
+ export declare const FILE_PREFIX = "/* -------------------------------------------\n * Autogenerated by \uD83D\uDCA0 Terrazzo. DO NOT EDIT!\n * ------------------------------------------- */";
22
+ export default function cssPlugin({ filename, exclude, variableName, modeSelectors, }?: CSSPluginOptions): Plugin;
package/dist/index.js ADDED
@@ -0,0 +1,310 @@
1
+ /**
2
+ * @module @cobalt-ui/plugin-css
3
+ * @license MIT License
4
+ *
5
+ * Copyright (c) 2021 Drew Powers
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+ import { isTokenMatch, makeAlias } from '@terrazzo/token-tools';
26
+ import { makeCSSVar, transformBooleanValue, transformBorderValue, transformColorValue, transformCubicBezierValue, transformDimensionValue, transformDurationValue, transformFontFamilyValue, transformFontWeightValue, transformGradientValue, transformLinkValue, transformNumberValue, transformShadowValue, transformStringValue, transformStrokeStyleValue, transformTransitionValue, transformTypographyValue, } from '@terrazzo/token-tools/css';
27
+ export const FORMAT_ID = 'css';
28
+ export const FILE_PREFIX = `/* -------------------------------------------
29
+ * Autogenerated by 💠 Terrazzo. DO NOT EDIT!
30
+ * ------------------------------------------- */`;
31
+ export default function cssPlugin({ filename = './index.css', exclude, variableName, modeSelectors, } = {}) {
32
+ const transformName = (id) => variableName?.(id) || makeCSSVar(id);
33
+ const transformAlias = (id) => `var(${transformName(id)})`;
34
+ return {
35
+ name: '@terrazzo/plugin-css',
36
+ async transform({ tokens, setTransform }) {
37
+ for (const id in tokens) {
38
+ if (!Object.hasOwn(tokens, id)) {
39
+ continue;
40
+ }
41
+ const token = tokens[id];
42
+ const localID = transformName(id);
43
+ switch (token.$type) {
44
+ case 'boolean': {
45
+ for (const mode in token.mode) {
46
+ const { $value, aliasOf } = token.mode[mode];
47
+ setTransform(id, {
48
+ format: FORMAT_ID,
49
+ localID,
50
+ value: transformBooleanValue($value, { aliasOf, transformAlias }),
51
+ mode,
52
+ });
53
+ }
54
+ break;
55
+ }
56
+ case 'border': {
57
+ for (const mode in token.mode) {
58
+ const baseValue = { format: FORMAT_ID, mode };
59
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode];
60
+ const output = transformBorderValue($value, { aliasOf, partialAliasOf, transformAlias });
61
+ if (typeof output === 'string') {
62
+ setTransform(id, { ...baseValue, localID, value: output });
63
+ }
64
+ else {
65
+ const { width, color, style } = output;
66
+ setTransform(id, {
67
+ ...baseValue,
68
+ localID,
69
+ value: [`var(${localID}-width)`, `var(${localID}-color)`, `var(${localID}-style)`].join(' '),
70
+ });
71
+ setTransform(id, { ...baseValue, localID: `${localID}-width`, value: width });
72
+ setTransform(id, { ...baseValue, localID: `${localID}-color`, value: color });
73
+ setTransform(id, { ...baseValue, localID: `${localID}-style`, value: style });
74
+ }
75
+ }
76
+ break;
77
+ }
78
+ case 'color': {
79
+ for (const mode in token.mode) {
80
+ const { $value, aliasOf } = token.mode[mode];
81
+ setTransform(id, {
82
+ format: FORMAT_ID,
83
+ localID,
84
+ value: transformColorValue($value, { aliasOf, transformAlias }),
85
+ mode,
86
+ });
87
+ }
88
+ break;
89
+ }
90
+ case 'cubicBezier': {
91
+ for (const mode in token.mode) {
92
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode];
93
+ setTransform(id, {
94
+ format: FORMAT_ID,
95
+ localID,
96
+ value: transformCubicBezierValue($value, { aliasOf, partialAliasOf, transformAlias }),
97
+ mode,
98
+ });
99
+ }
100
+ break;
101
+ }
102
+ case 'dimension': {
103
+ for (const mode in token.mode) {
104
+ const currentMode = token.mode[mode];
105
+ setTransform(id, {
106
+ format: FORMAT_ID,
107
+ localID,
108
+ value: transformDimensionValue(currentMode.aliasOf ? makeAlias(currentMode.aliasOf) : currentMode.$value, { transformAlias }),
109
+ mode,
110
+ });
111
+ }
112
+ break;
113
+ }
114
+ case 'duration': {
115
+ for (const mode in token.mode) {
116
+ const { $value, aliasOf } = token.mode[mode];
117
+ setTransform(id, {
118
+ format: FORMAT_ID,
119
+ localID,
120
+ value: transformDurationValue($value, { aliasOf, transformAlias }),
121
+ mode,
122
+ });
123
+ }
124
+ break;
125
+ }
126
+ case 'fontFamily': {
127
+ for (const mode in token.mode) {
128
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode];
129
+ setTransform(id, {
130
+ format: FORMAT_ID,
131
+ localID,
132
+ value: transformFontFamilyValue($value, { aliasOf, partialAliasOf, transformAlias }),
133
+ mode,
134
+ });
135
+ }
136
+ break;
137
+ }
138
+ case 'fontWeight': {
139
+ for (const mode in token.mode) {
140
+ const { $value, aliasOf } = token.mode[mode];
141
+ setTransform(id, {
142
+ format: FORMAT_ID,
143
+ localID,
144
+ value: transformFontWeightValue($value, { aliasOf, transformAlias }),
145
+ mode,
146
+ });
147
+ }
148
+ break;
149
+ }
150
+ case 'gradient': {
151
+ for (const mode in token.mode) {
152
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode];
153
+ setTransform(id, {
154
+ format: FORMAT_ID,
155
+ localID,
156
+ value: transformGradientValue($value, { aliasOf, partialAliasOf, transformAlias }),
157
+ });
158
+ }
159
+ break;
160
+ }
161
+ case 'link': {
162
+ for (const mode in token.mode) {
163
+ const { $value, aliasOf } = token.mode[mode];
164
+ setTransform(id, {
165
+ format: FORMAT_ID,
166
+ localID,
167
+ value: transformLinkValue($value, { aliasOf, transformAlias }),
168
+ mode,
169
+ });
170
+ }
171
+ break;
172
+ }
173
+ case 'number': {
174
+ for (const mode in token.mode) {
175
+ const { $value, aliasOf } = token.mode[mode];
176
+ setTransform(id, {
177
+ format: FORMAT_ID,
178
+ localID,
179
+ value: transformNumberValue($value, { aliasOf, transformAlias }),
180
+ mode,
181
+ });
182
+ }
183
+ break;
184
+ }
185
+ case 'shadow': {
186
+ for (const mode in token.mode) {
187
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode];
188
+ setTransform(id, {
189
+ format: FORMAT_ID,
190
+ localID,
191
+ value: transformShadowValue($value, { aliasOf, partialAliasOf, transformAlias }),
192
+ mode,
193
+ });
194
+ }
195
+ break;
196
+ }
197
+ case 'string': {
198
+ for (const mode in token.mode) {
199
+ const { $value, aliasOf } = token.mode[mode];
200
+ setTransform(id, {
201
+ format: FORMAT_ID,
202
+ localID,
203
+ value: transformStringValue($value, { aliasOf, transformAlias }),
204
+ mode,
205
+ });
206
+ }
207
+ break;
208
+ }
209
+ case 'strokeStyle': {
210
+ for (const mode in token.mode) {
211
+ const { $value, aliasOf } = token.mode[mode];
212
+ setTransform(id, {
213
+ format: FORMAT_ID,
214
+ localID,
215
+ value: transformStrokeStyleValue($value, { aliasOf, transformAlias }),
216
+ mode,
217
+ });
218
+ }
219
+ break;
220
+ }
221
+ case 'transition': {
222
+ for (const mode in token.mode) {
223
+ const baseValue = { format: FORMAT_ID, mode };
224
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode];
225
+ const output = transformTransitionValue($value, { aliasOf, partialAliasOf, transformAlias });
226
+ if (typeof output === 'string') {
227
+ setTransform(id, { ...baseValue, localID, value: output });
228
+ }
229
+ else {
230
+ const { duration, delay, timingFunction } = output;
231
+ setTransform(id, {
232
+ ...baseValue,
233
+ localID,
234
+ value: [`var(${localID}-duration)`, `var(${localID}-delay)`, `var(${localID}-timingFunction)`].join(' '),
235
+ });
236
+ setTransform(id, { ...baseValue, localID: `${localID}-duration`, value: duration });
237
+ setTransform(id, { ...baseValue, localID: `${localID}-delay`, value: delay });
238
+ setTransform(id, { ...baseValue, localID: `${localID}-timingFunction`, value: timingFunction });
239
+ }
240
+ }
241
+ break;
242
+ }
243
+ case 'typography': {
244
+ for (const mode in token.mode) {
245
+ const baseValue = { format: FORMAT_ID, mode };
246
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode];
247
+ const output = transformTypographyValue($value, { aliasOf, partialAliasOf, transformAlias });
248
+ setTransform(id, { ...baseValue, localID, value: output });
249
+ }
250
+ break;
251
+ }
252
+ }
253
+ }
254
+ },
255
+ async build({ getTransforms, outputFile }) {
256
+ const output = [FILE_PREFIX, ''];
257
+ // :root
258
+ output.push(':root {');
259
+ const rootTokens = getTransforms({ format: 'css', mode: '.' });
260
+ for (const token of rootTokens) {
261
+ if (isTokenMatch(token.token.id, exclude ?? [])) {
262
+ continue;
263
+ }
264
+ const localID = token.localID ?? token.token.id;
265
+ if (token.type === 'SINGLE_VALUE') {
266
+ output.push(` ${localID}: ${token.value};`);
267
+ }
268
+ else if (token.type === 'MULTI_VALUE') {
269
+ for (const [name, value] of Object.entries(token.value)) {
270
+ output.push(` ${localID}-${name}: ${value};`);
271
+ }
272
+ }
273
+ }
274
+ output.push('}');
275
+ for (const { selectors, tokens, mode } of modeSelectors ?? []) {
276
+ const selectorTokens = getTransforms({ format: 'css', select: tokens, mode });
277
+ if (!selectorTokens.length) {
278
+ continue;
279
+ }
280
+ let indent = ' ';
281
+ for (const selector of selectors) {
282
+ output.push('', `${selector} {`);
283
+ if (selector.startsWith('@')) {
284
+ output.push(' :root {');
285
+ indent = ' ';
286
+ }
287
+ for (const token of selectorTokens) {
288
+ if (token.token.aliasOf) {
289
+ continue;
290
+ }
291
+ if (token.type === 'SINGLE_VALUE') {
292
+ output.push(`${indent}${token.localID ?? token.token.id}: ${token.value};`);
293
+ }
294
+ else {
295
+ for (const [name, subvalue] of Object.entries(token.value)) {
296
+ output.push(`${indent}${token.localID ?? token.token.id}-${name}: ${subvalue};`);
297
+ }
298
+ }
299
+ }
300
+ if (selector.startsWith('@')) {
301
+ output.push(' }');
302
+ }
303
+ output.push('}');
304
+ }
305
+ }
306
+ outputFile(filename, output.join('\n'));
307
+ },
308
+ };
309
+ }
310
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAChE,OAAO,EACL,UAAU,EACV,qBAAqB,EACrB,oBAAoB,EACpB,mBAAmB,EACnB,yBAAyB,EACzB,uBAAuB,EACvB,sBAAsB,EACtB,wBAAwB,EACxB,wBAAwB,EACxB,sBAAsB,EACtB,kBAAkB,EAClB,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,yBAAyB,EACzB,wBAAwB,EACxB,wBAAwB,GACzB,MAAM,2BAA2B,CAAC;AAsBnC,MAAM,CAAC,MAAM,SAAS,GAAG,KAAK,CAAC;AAE/B,MAAM,CAAC,MAAM,WAAW,GAAG;;kDAEuB,CAAC;AAEnD,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,EAChC,QAAQ,GAAG,aAAa,EACxB,OAAO,EACP,YAAY,EACZ,aAAa,MACO,EAAE;IACtB,MAAM,aAAa,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,YAAY,EAAE,CAAC,EAAE,CAAC,IAAI,UAAU,CAAC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C9E,CAAC;6BAAM,CAAC;4BACN,KAAK,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;gCAC3D,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,GAAG,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE,IAAI,IAAI,KAAK,QAAQ,GAAG,CAAC,CAAC;4BACnF,CAAC;wBACH,CAAC;oBACH,CAAC;oBACD,IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;wBAC7B,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;oBACrB,CAAC;oBACD,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBACnB,CAAC;YACH,CAAC;YAED,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QAC1C,CAAC;KACF,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@terrazzo/plugin-css",
3
+ "description": "Generate CSS from your design tokens schema (requires @terrazzo/cli)",
4
+ "version": "0.0.0",
5
+ "author": {
6
+ "name": "Drew Powers",
7
+ "email": "drew@pow.rs"
8
+ },
9
+ "keywords": [
10
+ "design tokens",
11
+ "design tokens community group",
12
+ "design tokens format module",
13
+ "design system",
14
+ "dtcg",
15
+ "w3c design tokens",
16
+ "css"
17
+ ],
18
+ "license": "MIT",
19
+ "type": "module",
20
+ "main": "./dist/index.js",
21
+ "peerDependencies": {
22
+ "@terrazzo/cli": "*"
23
+ },
24
+ "dependencies": {
25
+ "@types/culori": "^2.1.0",
26
+ "@types/mime": "^3.0.4",
27
+ "culori": "^3.3.0",
28
+ "mime": "^3.0.0",
29
+ "svgo": "^3.2.0",
30
+ "@terrazzo/parser": "^0.0.0",
31
+ "@terrazzo/token-tools": "^0.0.0"
32
+ },
33
+ "devDependencies": {
34
+ "yaml": "^2.4.1",
35
+ "@terrazzo/cli": "^0.0.0"
36
+ },
37
+ "scripts": {
38
+ "build": "pnpm run build:clean && pnpm run build:ts && pnpm run build:license",
39
+ "build:clean": "del dist",
40
+ "build:ts": "tsc -p tsconfig.build.json",
41
+ "build:license": "node ../../scripts/inject-license.js @terrazzo/plugin-css dist/index.js",
42
+ "dev": "tsc -p tsconfig.build.json -w",
43
+ "lint": "biome check .",
44
+ "test": "pnpm run \"/^test:.*/\"",
45
+ "test:js": "vitest run",
46
+ "test:ts": "tsc --noEmit"
47
+ }
48
+ }
package/src/index.ts ADDED
@@ -0,0 +1,340 @@
1
+ import type { Plugin } from '@terrazzo/parser';
2
+ import { isTokenMatch, makeAlias } from '@terrazzo/token-tools';
3
+ import {
4
+ makeCSSVar,
5
+ transformBooleanValue,
6
+ transformBorderValue,
7
+ transformColorValue,
8
+ transformCubicBezierValue,
9
+ transformDimensionValue,
10
+ transformDurationValue,
11
+ transformFontFamilyValue,
12
+ transformFontWeightValue,
13
+ transformGradientValue,
14
+ transformLinkValue,
15
+ transformNumberValue,
16
+ transformShadowValue,
17
+ transformStringValue,
18
+ transformStrokeStyleValue,
19
+ transformTransitionValue,
20
+ transformTypographyValue,
21
+ } from '@terrazzo/token-tools/css';
22
+
23
+ export interface ModeSelector {
24
+ /** The name of the mode to match */
25
+ mode: string;
26
+ /** (optional) Provide token IDs to match. Globs are allowed (e.g: `["color.*", "shadow.dark"]`) */
27
+ tokens?: string[];
28
+ /** Provide CSS selectors to generate. (e.g.: `["@media (prefers-color-scheme: dark)", "[data-color-theme='dark']"]` ) */
29
+ selectors: string[];
30
+ }
31
+
32
+ export interface CSSPluginOptions {
33
+ /** Where to output CSS */
34
+ filename?: string;
35
+ /** Glob patterns to exclude tokens from output */
36
+ exclude?: string[];
37
+ /** Define mode selectors as media queries or CSS classes */
38
+ modeSelectors?: ModeSelector[];
39
+ /** Control the final CSS variable name */
40
+ variableName?: (name: string) => string;
41
+ }
42
+
43
+ export const FORMAT_ID = 'css';
44
+
45
+ export const FILE_PREFIX = `/* -------------------------------------------
46
+ * Autogenerated by 💠 Terrazzo. DO NOT EDIT!
47
+ * ------------------------------------------- */`;
48
+
49
+ export default function cssPlugin({
50
+ filename = './index.css',
51
+ exclude,
52
+ variableName,
53
+ modeSelectors,
54
+ }: CSSPluginOptions = {}): Plugin {
55
+ const transformName = (id: string) => variableName?.(id) || makeCSSVar(id);
56
+ const transformAlias = (id: string) => `var(${transformName(id)})`;
57
+
58
+ return {
59
+ name: '@terrazzo/plugin-css',
60
+ async transform({ tokens, setTransform }) {
61
+ for (const id in tokens) {
62
+ if (!Object.hasOwn(tokens, id)) {
63
+ continue;
64
+ }
65
+ const token = tokens[id]!;
66
+ const localID = transformName(id);
67
+
68
+ switch (token.$type) {
69
+ case 'boolean': {
70
+ for (const mode in token.mode) {
71
+ const { $value, aliasOf } = token.mode[mode]!;
72
+ setTransform(id, {
73
+ format: FORMAT_ID,
74
+ localID,
75
+ value: transformBooleanValue($value, { aliasOf, transformAlias }),
76
+ mode,
77
+ });
78
+ }
79
+ break;
80
+ }
81
+ case 'border': {
82
+ for (const mode in token.mode) {
83
+ const baseValue = { format: FORMAT_ID, mode };
84
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode]!;
85
+ const output = transformBorderValue($value, { aliasOf, partialAliasOf, transformAlias });
86
+ if (typeof output === 'string') {
87
+ setTransform(id, { ...baseValue, localID, value: output });
88
+ } else {
89
+ const { width, color, style } = output;
90
+ setTransform(id, {
91
+ ...baseValue,
92
+ localID,
93
+ value: [`var(${localID}-width)`, `var(${localID}-color)`, `var(${localID}-style)`].join(' '),
94
+ });
95
+ setTransform(id, { ...baseValue, localID: `${localID}-width`, value: width });
96
+ setTransform(id, { ...baseValue, localID: `${localID}-color`, value: color });
97
+ setTransform(id, { ...baseValue, localID: `${localID}-style`, value: style });
98
+ }
99
+ }
100
+ break;
101
+ }
102
+ case 'color': {
103
+ for (const mode in token.mode) {
104
+ const { $value, aliasOf } = token.mode[mode]!;
105
+ setTransform(id, {
106
+ format: FORMAT_ID,
107
+ localID,
108
+ value: transformColorValue($value, { aliasOf, transformAlias }),
109
+ mode,
110
+ });
111
+ }
112
+ break;
113
+ }
114
+ case 'cubicBezier': {
115
+ for (const mode in token.mode) {
116
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode]!;
117
+ setTransform(id, {
118
+ format: FORMAT_ID,
119
+ localID,
120
+ value: transformCubicBezierValue($value, { aliasOf, partialAliasOf, transformAlias }),
121
+ mode,
122
+ });
123
+ }
124
+ break;
125
+ }
126
+ case 'dimension': {
127
+ for (const mode in token.mode) {
128
+ const currentMode = token.mode[mode]!;
129
+ setTransform(id, {
130
+ format: FORMAT_ID,
131
+ localID,
132
+ value: transformDimensionValue(
133
+ currentMode.aliasOf ? makeAlias(currentMode.aliasOf) : currentMode.$value,
134
+ { transformAlias },
135
+ ),
136
+ mode,
137
+ });
138
+ }
139
+ break;
140
+ }
141
+ case 'duration': {
142
+ for (const mode in token.mode) {
143
+ const { $value, aliasOf } = token.mode[mode]!;
144
+ setTransform(id, {
145
+ format: FORMAT_ID,
146
+ localID,
147
+ value: transformDurationValue($value, { aliasOf, transformAlias }),
148
+ mode,
149
+ });
150
+ }
151
+ break;
152
+ }
153
+ case 'fontFamily': {
154
+ for (const mode in token.mode) {
155
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode]!;
156
+ setTransform(id, {
157
+ format: FORMAT_ID,
158
+ localID,
159
+ value: transformFontFamilyValue($value, { aliasOf, partialAliasOf, transformAlias }),
160
+ mode,
161
+ });
162
+ }
163
+ break;
164
+ }
165
+ case 'fontWeight': {
166
+ for (const mode in token.mode) {
167
+ const { $value, aliasOf } = token.mode[mode]!;
168
+ setTransform(id, {
169
+ format: FORMAT_ID,
170
+ localID,
171
+ value: transformFontWeightValue($value, { aliasOf, transformAlias }),
172
+ mode,
173
+ });
174
+ }
175
+ break;
176
+ }
177
+ case 'gradient': {
178
+ for (const mode in token.mode) {
179
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode]!;
180
+ setTransform(id, {
181
+ format: FORMAT_ID,
182
+ localID,
183
+ value: transformGradientValue($value, { aliasOf, partialAliasOf, transformAlias }),
184
+ });
185
+ }
186
+ break;
187
+ }
188
+ case 'link': {
189
+ for (const mode in token.mode) {
190
+ const { $value, aliasOf } = token.mode[mode]!;
191
+ setTransform(id, {
192
+ format: FORMAT_ID,
193
+ localID,
194
+ value: transformLinkValue($value, { aliasOf, transformAlias }),
195
+ mode,
196
+ });
197
+ }
198
+ break;
199
+ }
200
+ case 'number': {
201
+ for (const mode in token.mode) {
202
+ const { $value, aliasOf } = token.mode[mode]!;
203
+ setTransform(id, {
204
+ format: FORMAT_ID,
205
+ localID,
206
+ value: transformNumberValue($value, { aliasOf, transformAlias }),
207
+ mode,
208
+ });
209
+ }
210
+ break;
211
+ }
212
+ case 'shadow': {
213
+ for (const mode in token.mode) {
214
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode]!;
215
+ setTransform(id, {
216
+ format: FORMAT_ID,
217
+ localID,
218
+ value: transformShadowValue($value, { aliasOf, partialAliasOf, transformAlias }),
219
+ mode,
220
+ });
221
+ }
222
+ break;
223
+ }
224
+ case 'string': {
225
+ for (const mode in token.mode) {
226
+ const { $value, aliasOf } = token.mode[mode]!;
227
+ setTransform(id, {
228
+ format: FORMAT_ID,
229
+ localID,
230
+ value: transformStringValue($value, { aliasOf, transformAlias }),
231
+ mode,
232
+ });
233
+ }
234
+ break;
235
+ }
236
+ case 'strokeStyle': {
237
+ for (const mode in token.mode) {
238
+ const { $value, aliasOf } = token.mode[mode]!;
239
+ setTransform(id, {
240
+ format: FORMAT_ID,
241
+ localID,
242
+ value: transformStrokeStyleValue($value, { aliasOf, transformAlias }),
243
+ mode,
244
+ });
245
+ }
246
+ break;
247
+ }
248
+ case 'transition': {
249
+ for (const mode in token.mode) {
250
+ const baseValue = { format: FORMAT_ID, mode };
251
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode]!;
252
+ const output = transformTransitionValue($value, { aliasOf, partialAliasOf, transformAlias });
253
+ if (typeof output === 'string') {
254
+ setTransform(id, { ...baseValue, localID, value: output });
255
+ } else {
256
+ const { duration, delay, timingFunction } = output;
257
+ setTransform(id, {
258
+ ...baseValue,
259
+ localID,
260
+ value: [`var(${localID}-duration)`, `var(${localID}-delay)`, `var(${localID}-timingFunction)`].join(
261
+ ' ',
262
+ ),
263
+ });
264
+ setTransform(id, { ...baseValue, localID: `${localID}-duration`, value: duration });
265
+ setTransform(id, { ...baseValue, localID: `${localID}-delay`, value: delay });
266
+ setTransform(id, { ...baseValue, localID: `${localID}-timingFunction`, value: timingFunction });
267
+ }
268
+ }
269
+ break;
270
+ }
271
+ case 'typography': {
272
+ for (const mode in token.mode) {
273
+ const baseValue = { format: FORMAT_ID, mode };
274
+ const { $value, aliasOf, partialAliasOf } = token.mode[mode]!;
275
+ const output = transformTypographyValue($value, { aliasOf, partialAliasOf, transformAlias });
276
+ setTransform(id, { ...baseValue, localID, value: output });
277
+ }
278
+ break;
279
+ }
280
+ }
281
+ }
282
+ },
283
+ async build({ getTransforms, outputFile }) {
284
+ const output: string[] = [FILE_PREFIX, ''];
285
+
286
+ // :root
287
+ output.push(':root {');
288
+ const rootTokens = getTransforms({ format: 'css', mode: '.' });
289
+ for (const token of rootTokens) {
290
+ if (isTokenMatch(token.token.id, exclude ?? [])) {
291
+ continue;
292
+ }
293
+ const localID = token.localID ?? token.token.id;
294
+ if (token.type === 'SINGLE_VALUE') {
295
+ output.push(` ${localID}: ${token.value};`);
296
+ } else if (token.type === 'MULTI_VALUE') {
297
+ for (const [name, value] of Object.entries(token.value)) {
298
+ output.push(` ${localID}-${name}: ${value};`);
299
+ }
300
+ }
301
+ }
302
+ output.push('}');
303
+
304
+ for (const { selectors, tokens, mode } of modeSelectors ?? []) {
305
+ const selectorTokens = getTransforms({ format: 'css', select: tokens, mode });
306
+ if (!selectorTokens.length) {
307
+ continue;
308
+ }
309
+
310
+ let indent = ' ';
311
+ for (const selector of selectors) {
312
+ output.push('', `${selector} {`);
313
+ if (selector.startsWith('@')) {
314
+ output.push(' :root {');
315
+ indent = ' ';
316
+ }
317
+ for (const token of selectorTokens) {
318
+ if (token.token.aliasOf) {
319
+ continue;
320
+ }
321
+
322
+ if (token.type === 'SINGLE_VALUE') {
323
+ output.push(`${indent}${token.localID ?? token.token.id}: ${token.value};`);
324
+ } else {
325
+ for (const [name, subvalue] of Object.entries(token.value)) {
326
+ output.push(`${indent}${token.localID ?? token.token.id}-${name}: ${subvalue};`);
327
+ }
328
+ }
329
+ }
330
+ if (selector.startsWith('@')) {
331
+ output.push(' }');
332
+ }
333
+ output.push('}');
334
+ }
335
+ }
336
+
337
+ outputFile(filename, output.join('\n'));
338
+ },
339
+ };
340
+ }