@terrazzo/plugin-js 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 +21 -0
- package/biome.json +15 -0
- package/dist/build.d.ts +7 -0
- package/dist/build.js +44 -0
- package/dist/build.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +83 -0
- package/dist/index.js.map +1 -0
- package/dist/lib.d.ts +17 -0
- package/dist/lib.js +24 -0
- package/dist/lib.js.map +1 -0
- package/package.json +41 -0
- package/src/build.ts +67 -0
- package/src/index.ts +66 -0
- package/src/lib.ts +40 -0
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.
|
package/biome.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://biomejs.dev/schemas/1.6.4/schema.json",
|
|
3
|
+
"extends": ["../../biome.json"],
|
|
4
|
+
"files": {
|
|
5
|
+
"include": ["./src/", "./test/"],
|
|
6
|
+
"ignore": [
|
|
7
|
+
"./test/fixtures/**/*.js",
|
|
8
|
+
"./test/fixtures/**/*.json",
|
|
9
|
+
"./test/fixtures/**/*.d.ts",
|
|
10
|
+
"./test/**/actual.*",
|
|
11
|
+
"./test/**/want.*",
|
|
12
|
+
"./test/types/"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
}
|
package/dist/build.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { BuildHookOptions } from '@terrazzo/parser';
|
|
2
|
+
export declare function buildJS({ getTransforms }: {
|
|
3
|
+
getTransforms: BuildHookOptions['getTransforms'];
|
|
4
|
+
}): string;
|
|
5
|
+
export declare function buildDTS({ getTransforms }: {
|
|
6
|
+
getTransforms: BuildHookOptions['getTransforms'];
|
|
7
|
+
}): string;
|
package/dist/build.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { FILE_HEADER, FORMAT_DTS_ID, FORMAT_JS_ID, TYPE_MAP } from './lib.js';
|
|
2
|
+
export function buildJS({ getTransforms }) {
|
|
3
|
+
const output = [FILE_HEADER, ''];
|
|
4
|
+
// gather vals
|
|
5
|
+
const tokenVals = {};
|
|
6
|
+
for (const token of getTransforms({ format: FORMAT_JS_ID, id: '*' })) {
|
|
7
|
+
if (!tokenVals[token.token.id]) {
|
|
8
|
+
tokenVals[token.token.id] = {};
|
|
9
|
+
}
|
|
10
|
+
tokenVals[token.token.id][token.mode] = token.value;
|
|
11
|
+
}
|
|
12
|
+
// body
|
|
13
|
+
output.push('export const tokens = {');
|
|
14
|
+
for (const id in tokenVals) {
|
|
15
|
+
output.push(` "${id}": {`);
|
|
16
|
+
for (const mode in tokenVals[id]) {
|
|
17
|
+
output.push(` "${mode}": ${tokenVals[id][mode]},`);
|
|
18
|
+
}
|
|
19
|
+
output.push(' },');
|
|
20
|
+
}
|
|
21
|
+
output.push('};', '');
|
|
22
|
+
// footer
|
|
23
|
+
output.push(`/** Get individual token */
|
|
24
|
+
export function token(tokenID, modeName = ".") {
|
|
25
|
+
return tokens[tokenID]?.[modeName];
|
|
26
|
+
}`, '');
|
|
27
|
+
return output.join('\n');
|
|
28
|
+
}
|
|
29
|
+
export function buildDTS({ getTransforms }) {
|
|
30
|
+
const output = [FILE_HEADER, ''];
|
|
31
|
+
const importDeps = new Set();
|
|
32
|
+
const types = getTransforms({ format: FORMAT_DTS_ID, id: '*', mode: '.' }).map((t) => {
|
|
33
|
+
importDeps.add(TYPE_MAP[t.token.$type]); // collect only necessary types
|
|
34
|
+
if (t.type === 'MULTI_VALUE') {
|
|
35
|
+
const description = t.value.description ? ` /** ${t.value.description} */\n` : '';
|
|
36
|
+
return `${description} "${t.token.id}": ${t.value.value};`;
|
|
37
|
+
}
|
|
38
|
+
return `"${t.token.id}": ${t.value};`;
|
|
39
|
+
});
|
|
40
|
+
output.push('import type {', ...[...importDeps].sort((a, b) => a.localeCompare(b)).map((dep) => ` ${dep},`), '} from "@terrazzo/parser";', '', 'export declare const tokens: {', ...types, '};', '', `export declare function token<K extends keyof typeof tokens>(tokenID: K, modeName?: never): (typeof tokens)[K]["."];
|
|
41
|
+
export declare function token<K extends keyof typeof tokens, M extends keyof (typeof tokens)[K]>(tokenID: K, modeName: M): (typeof tokens)[K][M];`, '');
|
|
42
|
+
return output.join('\n');
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=build.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build.js","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,aAA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,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC3B,CAAC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @terrazzo/plugin-js
|
|
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 { transformJSValue } from '@terrazzo/token-tools/js';
|
|
26
|
+
import { FORMAT_DTS_ID, FORMAT_JS_ID, TYPE_MAP } from './lib.js';
|
|
27
|
+
import { buildDTS, buildJS } from './build.js';
|
|
28
|
+
export * from './build.js';
|
|
29
|
+
export * from './lib.js';
|
|
30
|
+
export default function pluginJS(options) {
|
|
31
|
+
const customTransform = options?.transform;
|
|
32
|
+
return {
|
|
33
|
+
name: '@terrazzo/plugin-js',
|
|
34
|
+
async transform({ tokens, getTransforms, setTransform }) {
|
|
35
|
+
// skip work if another .js plugin has already run
|
|
36
|
+
const jsTokens = getTransforms({ format: FORMAT_JS_ID, id: '*', mode: '.' });
|
|
37
|
+
if (jsTokens.length) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
for (const id in tokens) {
|
|
41
|
+
const token = tokens[id];
|
|
42
|
+
// .d.ts (only default "." mode needed)
|
|
43
|
+
setTransform(id, {
|
|
44
|
+
format: FORMAT_DTS_ID,
|
|
45
|
+
value: {
|
|
46
|
+
description: token.$description,
|
|
47
|
+
value: `Record<"${Object.keys(token.mode).join('" | "')}", ${TYPE_MAP[token.$type]}["$value"]>`,
|
|
48
|
+
},
|
|
49
|
+
mode: '.',
|
|
50
|
+
});
|
|
51
|
+
// .js (all modes)
|
|
52
|
+
for (const mode in token.mode) {
|
|
53
|
+
if (customTransform) {
|
|
54
|
+
const transformedValue = customTransform(token, mode);
|
|
55
|
+
if (transformedValue !== undefined && transformedValue !== null) {
|
|
56
|
+
setTransform(id, { format: FORMAT_JS_ID, value: transformedValue, mode });
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const transformedValue = transformJSValue(token, { mode, startingIndent: 4 });
|
|
61
|
+
if (transformedValue !== undefined) {
|
|
62
|
+
setTransform(id, { format: FORMAT_JS_ID, value: transformedValue, mode });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
async build({ getTransforms, outputFile }) {
|
|
68
|
+
// if (options?.json) {
|
|
69
|
+
// const contents = buildJSON({ getTransforms });
|
|
70
|
+
// outputFile(typeof options?.json === 'string' ? options.json : 'index.json', contents);
|
|
71
|
+
// }
|
|
72
|
+
if (options?.js) {
|
|
73
|
+
const js = buildJS({ getTransforms });
|
|
74
|
+
const jsFilename = typeof options?.js === 'string' ? options.js : 'index.js';
|
|
75
|
+
outputFile(jsFilename, js);
|
|
76
|
+
const dts = buildDTS({ getTransforms });
|
|
77
|
+
const dtsFilename = typeof options?.js === 'string' ? options.js.replace(/\.js$/, '.d.ts') : 'index.d.ts';
|
|
78
|
+
outputFile(dtsFilename, dts);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,QAAQ,EAAwB,MAAM,UAAU,CAAC;AACvF,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE/C,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AAEzB,MAAM,CAAC,OAAO,UAAU,QAAQ,CAAC,OAAyB;IACxD,MAAM,eAAe,GAAG,OAAO,EAAE,SAAS,CAAC;IAE3C,OAAO;QACL,IAAI,EAAE,qBAAqB;QAC3B,KAAK,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,aAAa,EAAE,YAAY,EAAE;YACrD,kDAAkD;YAClD,MAAM,QAAQ,GAAG,aAAa,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC;YAC7E,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC;gBACpB,OAAO;YACT,CAAC;YAED,KAAK,MAAM,EAAE,IAAI,MAAM,EAAE,CAAC;gBACxB,MAAM,KAAK,GAAG,MAAM,CAAC,EAAE,CAAE,CAAC;gBAE1B,uCAA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E,aAAa,EAAE,CAAC,CAAC;gBACtC,MAAM,UAAU,GAAG,OAAO,OAAO,EAAE,EAAE,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC;gBAC7E,UAAU,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;gBAE3B,MAAM,GAAG,GAAG,QAAQ,CAAC,EAAE,aAAa,EAAE,CAAC,CAAC;gBACxC,MAAM,WAAW,GAAG,OAAO,OAAO,EAAE,EAAE,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC;gBAC1G,UAAU,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/lib.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Token, TokenNormalized, TokenTransformed } from '@terrazzo/parser';
|
|
2
|
+
export declare const FORMAT_JS_ID = "js";
|
|
3
|
+
export declare const FORMAT_DTS_ID = "d.ts";
|
|
4
|
+
export interface JSPluginOptions {
|
|
5
|
+
/** output JS? (default: true) */
|
|
6
|
+
js?: boolean | string;
|
|
7
|
+
/** output JSON? (default: false) */
|
|
8
|
+
json?: boolean | string;
|
|
9
|
+
/** exclude token IDs from output? */
|
|
10
|
+
exclude?: string[];
|
|
11
|
+
/** return deeply-nested values? (default: false) */
|
|
12
|
+
deep?: boolean;
|
|
13
|
+
/** Override certain token values */
|
|
14
|
+
transform?: (token: TokenNormalized, mode: string) => TokenTransformed['value'];
|
|
15
|
+
}
|
|
16
|
+
export declare const FILE_HEADER = "/** ------------------------------------------\n * Autogenerated by \u26CB Terrazzo. DO NOT EDIT!\n * ------------------------------------------- */";
|
|
17
|
+
export declare const TYPE_MAP: Record<Token['$type'], string>;
|
package/dist/lib.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const FORMAT_JS_ID = 'js';
|
|
2
|
+
export const FORMAT_DTS_ID = 'd.ts';
|
|
3
|
+
export const FILE_HEADER = `/** ------------------------------------------
|
|
4
|
+
* Autogenerated by ⛋ Terrazzo. DO NOT EDIT!
|
|
5
|
+
* ------------------------------------------- */`;
|
|
6
|
+
export const TYPE_MAP = {
|
|
7
|
+
boolean: 'BooleanTokenNormalized',
|
|
8
|
+
border: 'BorderTokenNormalized',
|
|
9
|
+
color: 'ColorTokenNormalized',
|
|
10
|
+
cubicBezier: 'CubicBezierTokenNormalized',
|
|
11
|
+
dimension: 'DimensionTokenNormalized',
|
|
12
|
+
duration: 'DurationTokenNormalized',
|
|
13
|
+
fontFamily: 'FontFamilyTokenNormalized',
|
|
14
|
+
fontWeight: 'FontWeightTokenNormalized',
|
|
15
|
+
gradient: 'GradientTokenNormalized',
|
|
16
|
+
link: 'LinkTokenNormalized',
|
|
17
|
+
number: 'NumberTokenNormalized',
|
|
18
|
+
shadow: 'ShadowTokenNormalized',
|
|
19
|
+
string: 'StringTokenNormalized',
|
|
20
|
+
strokeStyle: 'StrokeStyleTokenNormalized',
|
|
21
|
+
typography: 'TypographyTokenNormalized',
|
|
22
|
+
transition: 'TransitionTokenNormalized',
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=lib.js.map
|
package/dist/lib.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lib.js","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC;AACjC,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC;AAepC,MAAM,CAAC,MAAM,WAAW,GAAG;;kDAEuB,CAAC;AAEnD,MAAM,CAAC,MAAM,QAAQ,GAAmC;IACtD,OAAO,EAAE,wBAAwB;IACjC,MAAM,EAAE,uBAAuB;IAC/B,KAAK,EAAE,sBAAsB;IAC7B,WAAW,EAAE,4BAA4B;IACzC,SAAS,EAAE,0BAA0B;IACrC,QAAQ,EAAE,yBAAyB;IACnC,UAAU,EAAE,2BAA2B;IACvC,UAAU,EAAE,2BAA2B;IACvC,QAAQ,EAAE,yBAAyB;IACnC,IAAI,EAAE,qBAAqB;IAC3B,MAAM,EAAE,uBAAuB;IAC/B,MAAM,EAAE,uBAAuB;IAC/B,MAAM,EAAE,uBAAuB;IAC/B,WAAW,EAAE,4BAA4B;IACzC,UAAU,EAAE,2BAA2B;IACvC,UAAU,EAAE,2BAA2B;CACxC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@terrazzo/plugin-js",
|
|
3
|
+
"description": "Generate JS, TS, and JSON 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
|
+
],
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "./dist/index.js",
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@terrazzo/cli": "^0.0.6"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@terrazzo/token-tools": "^0.0.2"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@terrazzo/parser": "^0.0.6",
|
|
28
|
+
"@terrazzo/cli": "^0.0.6"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "pnpm run build:clean && pnpm run build:ts && pnpm run build:license",
|
|
32
|
+
"build:clean": "del dist",
|
|
33
|
+
"build:ts": "tsc -p tsconfig.build.json",
|
|
34
|
+
"build:license": "node ../../scripts/inject-license.js @terrazzo/plugin-js dist/index.js",
|
|
35
|
+
"dev": "tsc -p tsconfig.build.json -w",
|
|
36
|
+
"lint": "biome check .",
|
|
37
|
+
"test": "co build -c ./test/types.tokens.mjs && pnpm run \"/^test:.*/\"",
|
|
38
|
+
"test:js": "vitest run",
|
|
39
|
+
"test:ts": "tsc --noEmit"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/build.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { BuildHookOptions } from '@terrazzo/parser';
|
|
2
|
+
import { FILE_HEADER, FORMAT_DTS_ID, FORMAT_JS_ID, TYPE_MAP } from './lib.js';
|
|
3
|
+
|
|
4
|
+
export function buildJS({ getTransforms }: { getTransforms: BuildHookOptions['getTransforms'] }): string {
|
|
5
|
+
const output: string[] = [FILE_HEADER, ''];
|
|
6
|
+
|
|
7
|
+
// gather vals
|
|
8
|
+
const tokenVals: Record<string, Record<string, string>> = {};
|
|
9
|
+
for (const token of getTransforms({ format: FORMAT_JS_ID, id: '*' })) {
|
|
10
|
+
if (!tokenVals[token.token.id]) {
|
|
11
|
+
tokenVals[token.token.id] = {};
|
|
12
|
+
}
|
|
13
|
+
tokenVals[token.token.id]![token.mode] = token.value as string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// body
|
|
17
|
+
output.push('export const tokens = {');
|
|
18
|
+
for (const id in tokenVals) {
|
|
19
|
+
output.push(` "${id}": {`);
|
|
20
|
+
for (const mode in tokenVals[id]) {
|
|
21
|
+
output.push(` "${mode}": ${tokenVals[id]![mode]},`);
|
|
22
|
+
}
|
|
23
|
+
output.push(' },');
|
|
24
|
+
}
|
|
25
|
+
output.push('};', '');
|
|
26
|
+
|
|
27
|
+
// footer
|
|
28
|
+
output.push(
|
|
29
|
+
`/** Get individual token */
|
|
30
|
+
export function token(tokenID, modeName = ".") {
|
|
31
|
+
return tokens[tokenID]?.[modeName];
|
|
32
|
+
}`,
|
|
33
|
+
'',
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
return output.join('\n');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function buildDTS({ getTransforms }: { getTransforms: BuildHookOptions['getTransforms'] }): string {
|
|
40
|
+
const output: string[] = [FILE_HEADER, ''];
|
|
41
|
+
|
|
42
|
+
const importDeps = new Set<string>();
|
|
43
|
+
const types = getTransforms({ format: FORMAT_DTS_ID, id: '*', mode: '.' }).map((t) => {
|
|
44
|
+
importDeps.add(TYPE_MAP[t.token.$type]); // collect only necessary types
|
|
45
|
+
if (t.type === 'MULTI_VALUE') {
|
|
46
|
+
const description = t.value.description ? ` /** ${t.value.description} */\n` : '';
|
|
47
|
+
return `${description} "${t.token.id}": ${t.value.value};`;
|
|
48
|
+
}
|
|
49
|
+
return `"${t.token.id}": ${t.value};`;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
output.push(
|
|
53
|
+
'import type {',
|
|
54
|
+
...[...importDeps].sort((a, b) => a.localeCompare(b)).map((dep) => ` ${dep},`),
|
|
55
|
+
'} from "@terrazzo/parser";',
|
|
56
|
+
'',
|
|
57
|
+
'export declare const tokens: {',
|
|
58
|
+
...types,
|
|
59
|
+
'};',
|
|
60
|
+
'',
|
|
61
|
+
`export declare function token<K extends keyof typeof tokens>(tokenID: K, modeName?: never): (typeof tokens)[K]["."];
|
|
62
|
+
export declare function token<K extends keyof typeof tokens, M extends keyof (typeof tokens)[K]>(tokenID: K, modeName: M): (typeof tokens)[K][M];`,
|
|
63
|
+
'',
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
return output.join('\n');
|
|
67
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Plugin } from '@terrazzo/parser';
|
|
2
|
+
import { transformJSValue } from '@terrazzo/token-tools/js';
|
|
3
|
+
import { FORMAT_DTS_ID, FORMAT_JS_ID, TYPE_MAP, type JSPluginOptions } from './lib.js';
|
|
4
|
+
import { buildDTS, buildJS } from './build.js';
|
|
5
|
+
|
|
6
|
+
export * from './build.js';
|
|
7
|
+
export * from './lib.js';
|
|
8
|
+
|
|
9
|
+
export default function pluginJS(options?: JSPluginOptions): Plugin {
|
|
10
|
+
const customTransform = options?.transform;
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
name: '@terrazzo/plugin-js',
|
|
14
|
+
async transform({ tokens, getTransforms, setTransform }) {
|
|
15
|
+
// skip work if another .js plugin has already run
|
|
16
|
+
const jsTokens = getTransforms({ format: FORMAT_JS_ID, id: '*', mode: '.' });
|
|
17
|
+
if (jsTokens.length) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
for (const id in tokens) {
|
|
22
|
+
const token = tokens[id]!;
|
|
23
|
+
|
|
24
|
+
// .d.ts (only default "." mode needed)
|
|
25
|
+
setTransform(id, {
|
|
26
|
+
format: FORMAT_DTS_ID,
|
|
27
|
+
value: {
|
|
28
|
+
description: token.$description,
|
|
29
|
+
value: `Record<"${Object.keys(token.mode).join('" | "')}", ${TYPE_MAP[token.$type]}["$value"]>`,
|
|
30
|
+
},
|
|
31
|
+
mode: '.',
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// .js (all modes)
|
|
35
|
+
for (const mode in token.mode) {
|
|
36
|
+
if (customTransform) {
|
|
37
|
+
const transformedValue = customTransform(token, mode);
|
|
38
|
+
if (transformedValue !== undefined && transformedValue !== null) {
|
|
39
|
+
setTransform(id, { format: FORMAT_JS_ID, value: transformedValue, mode });
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const transformedValue = transformJSValue(token, { mode, startingIndent: 4 });
|
|
44
|
+
if (transformedValue !== undefined) {
|
|
45
|
+
setTransform(id, { format: FORMAT_JS_ID, value: transformedValue, mode });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
async build({ getTransforms, outputFile }) {
|
|
51
|
+
// if (options?.json) {
|
|
52
|
+
// const contents = buildJSON({ getTransforms });
|
|
53
|
+
// outputFile(typeof options?.json === 'string' ? options.json : 'index.json', contents);
|
|
54
|
+
// }
|
|
55
|
+
if (options?.js) {
|
|
56
|
+
const js = buildJS({ getTransforms });
|
|
57
|
+
const jsFilename = typeof options?.js === 'string' ? options.js : 'index.js';
|
|
58
|
+
outputFile(jsFilename, js);
|
|
59
|
+
|
|
60
|
+
const dts = buildDTS({ getTransforms });
|
|
61
|
+
const dtsFilename = typeof options?.js === 'string' ? options.js.replace(/\.js$/, '.d.ts') : 'index.d.ts';
|
|
62
|
+
outputFile(dtsFilename, dts);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
package/src/lib.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Token, TokenNormalized, TokenTransformed } from '@terrazzo/parser';
|
|
2
|
+
|
|
3
|
+
export const FORMAT_JS_ID = 'js';
|
|
4
|
+
export const FORMAT_DTS_ID = 'd.ts';
|
|
5
|
+
|
|
6
|
+
export interface JSPluginOptions {
|
|
7
|
+
/** output JS? (default: true) */
|
|
8
|
+
js?: boolean | string;
|
|
9
|
+
/** output JSON? (default: false) */
|
|
10
|
+
json?: boolean | string;
|
|
11
|
+
/** exclude token IDs from output? */
|
|
12
|
+
exclude?: string[];
|
|
13
|
+
/** return deeply-nested values? (default: false) */
|
|
14
|
+
deep?: boolean;
|
|
15
|
+
/** Override certain token values */
|
|
16
|
+
transform?: (token: TokenNormalized, mode: string) => TokenTransformed['value'];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const FILE_HEADER = `/** ------------------------------------------
|
|
20
|
+
* Autogenerated by ⛋ Terrazzo. DO NOT EDIT!
|
|
21
|
+
* ------------------------------------------- */`;
|
|
22
|
+
|
|
23
|
+
export const TYPE_MAP: Record<Token['$type'], string> = {
|
|
24
|
+
boolean: 'BooleanTokenNormalized',
|
|
25
|
+
border: 'BorderTokenNormalized',
|
|
26
|
+
color: 'ColorTokenNormalized',
|
|
27
|
+
cubicBezier: 'CubicBezierTokenNormalized',
|
|
28
|
+
dimension: 'DimensionTokenNormalized',
|
|
29
|
+
duration: 'DurationTokenNormalized',
|
|
30
|
+
fontFamily: 'FontFamilyTokenNormalized',
|
|
31
|
+
fontWeight: 'FontWeightTokenNormalized',
|
|
32
|
+
gradient: 'GradientTokenNormalized',
|
|
33
|
+
link: 'LinkTokenNormalized',
|
|
34
|
+
number: 'NumberTokenNormalized',
|
|
35
|
+
shadow: 'ShadowTokenNormalized',
|
|
36
|
+
string: 'StringTokenNormalized',
|
|
37
|
+
strokeStyle: 'StrokeStyleTokenNormalized',
|
|
38
|
+
typography: 'TypographyTokenNormalized',
|
|
39
|
+
transition: 'TransitionTokenNormalized',
|
|
40
|
+
};
|