@terrazzo/plugin-js 0.10.3 → 2.0.0-alpha.1
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/CHANGELOG.md +15 -0
- package/dist/index.d.ts +0 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -10
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @terrazzo/plugin-js
|
|
2
2
|
|
|
3
|
+
## 2.0.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#530](https://github.com/terrazzoapp/terrazzo/pull/530) [`370ed7b`](https://github.com/terrazzoapp/terrazzo/commit/370ed7b0f578a64824124145d7f4936536b37bb3) Thanks [@drwpow](https://github.com/drwpow)! - ⚠️ Breaking change: lint on plugins no longer runs on individual files, rather, the full set once merged.
|
|
8
|
+
|
|
9
|
+
If your lint plugin is not using the `src` context value, no changes are needed. If it is, you’ll need to instead read from the `sources` array, and look up sources with a token’s `source.loc` filename manually. This change was because lint rules now run on all files in one pass, essentially.
|
|
10
|
+
|
|
11
|
+
- [#530](https://github.com/terrazzoapp/terrazzo/pull/530) [`370ed7b`](https://github.com/terrazzoapp/terrazzo/commit/370ed7b0f578a64824124145d7f4936536b37bb3) Thanks [@drwpow](https://github.com/drwpow)! - ⚠️ [Plugin API] Minor breaking change: token.originalValue may be undefined for tokens created with $ref. This shouldn’t affect any tokens or plugins not using $refs. But going forward this value will be missing if the token was created dynamically via $ref.
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- [#530](https://github.com/terrazzoapp/terrazzo/pull/530) [`370ed7b`](https://github.com/terrazzoapp/terrazzo/commit/370ed7b0f578a64824124145d7f4936536b37bb3) Thanks [@drwpow](https://github.com/drwpow)! - Validation moved to lint rules, which means token validation can be individually configured, and optionally extended.
|
|
16
|
+
- [#553](https://github.com/terrazzoapp/terrazzo/pull/553) [`e63a627`](https://github.com/terrazzoapp/terrazzo/commit/e63a6277f61282fb608744a8348689b16f977076) Thanks [@Sidnioulz](https://github.com/Sidnioulz)! - Add support for the Token Listing format
|
|
17
|
+
|
|
3
18
|
## 0.10.3
|
|
4
19
|
|
|
5
20
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -25,7 +25,6 @@ interface JSPluginOptions {
|
|
|
25
25
|
}
|
|
26
26
|
declare const FILE_HEADER = "/** ------------------------------------------\n * Autogenerated by \u26CB Terrazzo. DO NOT EDIT!\n * ------------------------------------------- */";
|
|
27
27
|
declare const TYPE_MAP: Record<Token['$type'], string>;
|
|
28
|
-
//# sourceMappingURL=lib.d.ts.map
|
|
29
28
|
//#endregion
|
|
30
29
|
//#region src/build.d.ts
|
|
31
30
|
declare function buildJS({
|
|
@@ -38,12 +37,9 @@ declare function buildDTS({
|
|
|
38
37
|
}: {
|
|
39
38
|
getTransforms: BuildHookOptions['getTransforms'];
|
|
40
39
|
}): string;
|
|
41
|
-
//# sourceMappingURL=build.d.ts.map
|
|
42
40
|
//#endregion
|
|
43
41
|
//#region src/index.d.ts
|
|
44
42
|
declare function pluginJS(options?: JSPluginOptions): Plugin;
|
|
45
|
-
//# sourceMappingURL=index.d.ts.map
|
|
46
|
-
|
|
47
43
|
//#endregion
|
|
48
44
|
export { FILE_HEADER, FORMAT_DTS_ID, FORMAT_JS_ID, JSPluginOptions, TYPE_MAP, buildDTS, buildJS, pluginJS as default };
|
|
49
45
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/lib.ts","../src/build.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;cAEa,YAAA;cACA,aAAA;AADA,UAGI,eAAA,CAHQ;EACZ;AAEb;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/lib.ts","../src/build.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;cAEa,YAAA;cACA,aAAA;AADA,UAGI,eAAA,CAHQ;EACZ;AAEb;;;KAkBwD,EAAA,OAAA,GAAA,MAAA;EAAgB;AAGxE;AAIA;EAiBC,IAAA,CAAA,EAAA,OAAA,GAAA,MAAA;;SAjBsB,CAAA,EAAA,MAAA,EAAA;EAAM;;;;EC3Bb,IAAA,CAAA,EAAA,OAAO;EAAA;WAAG,CAAA,EAAA,CAAA,KAAA,EDoBJ,eCpBI,EAAA,IAAA,EAAA,MAAA,EAAA,GDoB8B,gBCpB9B,CAAA,OAAA,CAAA;;AAAkD,cDuB/D,WAAA,GCvB+D,uJAAA;AAmC5D,cDRH,QCQW,EDRD,MCQC,CDRM,KCQN,CAAA,OAAA,CAAA,EAAA,MAAA,CAAA;;;iBAnCR,OAAA;;ADDhB;iBCC4D;ADD5D,CAAA,CAAA,EAAa,MAAA;AACA,iBCmCG,QAAA,CDnCU;EAAA;CAAA,EAAA;EAET,aAAA,ECiC4C,gBDjC7B,CAAA,eAAA,CAAA;CAAA,CAAA,EAAA,MAAA;;;AAFnB,iBEKW,QAAA,CFLE,OAAA,CAAA,EEKiB,eFLjB,CAAA,EEKmC,MFLnC"}
|
package/dist/index.js
CHANGED
|
@@ -68,10 +68,7 @@ function buildDTS({ getTransforms }) {
|
|
|
68
68
|
mode: "."
|
|
69
69
|
}).map((t) => {
|
|
70
70
|
importDeps.add(TYPE_MAP[t.token.$type]);
|
|
71
|
-
if (t.type === "MULTI_VALUE") {
|
|
72
|
-
const description = t.value.description ? ` /** ${t.value.description} */\n` : "";
|
|
73
|
-
return `${description} "${t.token.id}": ${t.value.value};`;
|
|
74
|
-
}
|
|
71
|
+
if (t.type === "MULTI_VALUE") return `${t.value.description ? ` /** ${t.value.description} */\n` : ""} "${t.token.id}": ${t.value.value};`;
|
|
75
72
|
return `"${t.token.id}": ${t.value};`;
|
|
76
73
|
});
|
|
77
74
|
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]["."];
|
|
@@ -86,17 +83,16 @@ function pluginJS(options) {
|
|
|
86
83
|
return {
|
|
87
84
|
name: "@terrazzo/plugin-js",
|
|
88
85
|
async transform({ tokens, getTransforms, setTransform }) {
|
|
89
|
-
|
|
86
|
+
if (getTransforms({
|
|
90
87
|
format: FORMAT_JS_ID,
|
|
91
88
|
id: "*",
|
|
92
89
|
mode: "."
|
|
93
|
-
});
|
|
94
|
-
if (jsTokens.length) return;
|
|
90
|
+
}).length) return;
|
|
95
91
|
for (const [id, token] of Object.entries(tokens)) {
|
|
96
92
|
setTransform(id, {
|
|
97
93
|
format: FORMAT_DTS_ID,
|
|
98
94
|
value: {
|
|
99
|
-
description: token.$description,
|
|
95
|
+
description: token.$description ? String(token.$description) : "",
|
|
100
96
|
value: `Record<"${Object.keys(token.mode).join("\" | \"")}", ${TYPE_MAP[token.$type]}["$value"]>`
|
|
101
97
|
},
|
|
102
98
|
mode: "."
|
|
@@ -108,7 +104,8 @@ function pluginJS(options) {
|
|
|
108
104
|
setTransform(id, {
|
|
109
105
|
format: FORMAT_JS_ID,
|
|
110
106
|
value: transformedValue$1,
|
|
111
|
-
mode
|
|
107
|
+
mode,
|
|
108
|
+
meta: { "token-listing": { name: `tokens.${id}` } }
|
|
112
109
|
});
|
|
113
110
|
continue;
|
|
114
111
|
}
|
|
@@ -120,7 +117,8 @@ function pluginJS(options) {
|
|
|
120
117
|
if (transformedValue !== void 0) setTransform(id, {
|
|
121
118
|
format: FORMAT_JS_ID,
|
|
122
119
|
value: transformedValue,
|
|
123
|
-
mode
|
|
120
|
+
mode,
|
|
121
|
+
meta: { "token-listing": { name: `tokens.${id}` } }
|
|
124
122
|
});
|
|
125
123
|
}
|
|
126
124
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["TYPE_MAP: Record<Token['$type'], string>","output: string[]","tokenVals: Record<string, Record<string, string>>","options?: JSPluginOptions","transformedValue"],"sources":["../../token-tools/dist/js.js","../src/lib.ts","../src/build.ts","../src/index.ts"],"sourcesContent":["//#region src/js/index.ts\n/**\n* Convert token value to a JS string via acorn/astring.\n*/\nfunction transformJSValue(token, { mode, indent = 2, startingIndent = 0 }) {\n\tif (!(mode in token.mode)) return;\n\tconst indentStart = startingIndent > 0 ? \" \".repeat(startingIndent ?? 2) : \"\";\n\treturn JSON.stringify(token.mode[mode].$value, void 0, indent).replace(/\\n/g, `\\n${indentStart}`);\n}\n\n//#endregion\nexport { transformJSValue };\n//# sourceMappingURL=js.js.map","import type { Token, TokenNormalized, TokenTransformed } from '@terrazzo/parser';\n\nexport const FORMAT_JS_ID = 'js';\nexport const FORMAT_DTS_ID = 'd.ts';\n\nexport interface JSPluginOptions {\n /**\n * Output JS?\n * @default true\n */\n js?: boolean | string;\n /** Output JSON?\n * @default false\n */\n json?: boolean | string;\n /** Exclude token IDs from output? */\n exclude?: string[];\n /**\n * Return deeply-nested values?\n * @default: false\n */\n deep?: boolean;\n /** Override certain token values */\n transform?: (token: TokenNormalized, mode: string) => TokenTransformed['value'];\n}\n\nexport const FILE_HEADER = `/** ------------------------------------------\n * Autogenerated by ⛋ Terrazzo. DO NOT EDIT!\n * ------------------------------------------- */`;\n\nexport const TYPE_MAP: Record<Token['$type'], string> = {\n boolean: 'BooleanTokenNormalized',\n border: 'BorderTokenNormalized',\n color: 'ColorTokenNormalized',\n cubicBezier: 'CubicBezierTokenNormalized',\n dimension: 'DimensionTokenNormalized',\n duration: 'DurationTokenNormalized',\n fontFamily: 'FontFamilyTokenNormalized',\n fontWeight: 'FontWeightTokenNormalized',\n gradient: 'GradientTokenNormalized',\n link: 'LinkTokenNormalized',\n number: 'NumberTokenNormalized',\n shadow: 'ShadowTokenNormalized',\n string: 'StringTokenNormalized',\n strokeStyle: 'StrokeStyleTokenNormalized',\n typography: 'TypographyTokenNormalized',\n transition: 'TransitionTokenNormalized',\n};\n","import type { BuildHookOptions } from '@terrazzo/parser';\nimport { FILE_HEADER, FORMAT_DTS_ID, FORMAT_JS_ID, TYPE_MAP } from './lib.js';\n\nexport function buildJS({ getTransforms }: { getTransforms: BuildHookOptions['getTransforms'] }): string {\n const output: string[] = [FILE_HEADER, ''];\n\n // gather vals\n const tokenVals: Record<string, Record<string, string>> = {};\n for (const token of getTransforms({ format: FORMAT_JS_ID, id: '*' })) {\n if (!tokenVals[token.token.id]) {\n tokenVals[token.token.id] = {};\n }\n tokenVals[token.token.id]![token.mode] = token.value as string;\n }\n\n // body\n output.push('export const tokens = {');\n for (const [id, tokenValue] of Object.entries(tokenVals)) {\n output.push(` \"${id}\": {`);\n for (const [mode, modeValue] of Object.entries(tokenValue)) {\n output.push(` \"${mode}\": ${modeValue},`);\n }\n output.push(' },');\n }\n output.push('};', '');\n\n // footer\n output.push(\n `/** Get individual token */\nexport function token(tokenID, modeName = \".\") {\n return tokens[tokenID]?.[modeName];\n}`,\n '',\n );\n\n return output.join('\\n');\n}\n\nexport function buildDTS({ getTransforms }: { getTransforms: BuildHookOptions['getTransforms'] }): string {\n const output: string[] = [FILE_HEADER, ''];\n\n const importDeps = new Set<string>();\n const types = getTransforms({ format: FORMAT_DTS_ID, id: '*', mode: '.' }).map((t) => {\n importDeps.add(TYPE_MAP[t.token.$type]); // collect only necessary types\n if (t.type === 'MULTI_VALUE') {\n const description = t.value.description ? ` /** ${t.value.description} */\\n` : '';\n return `${description} \"${t.token.id}\": ${t.value.value};`;\n }\n return `\"${t.token.id}\": ${t.value};`;\n });\n\n output.push(\n 'import type {',\n ...[...importDeps].sort((a, b) => a.localeCompare(b)).map((dep) => ` ${dep},`),\n '} from \"@terrazzo/parser\";',\n '',\n 'export declare const tokens: {',\n ...types,\n '};',\n '',\n `export declare function token<K extends keyof typeof tokens>(tokenID: K, modeName?: never): (typeof tokens)[K][\".\"];\nexport declare function token<K extends keyof typeof tokens, M extends keyof (typeof tokens)[K]>(tokenID: K, modeName: M): (typeof tokens)[K][M];`,\n '',\n );\n\n return output.join('\\n');\n}\n","import type { Plugin } from '@terrazzo/parser';\nimport { transformJSValue } from '@terrazzo/token-tools/js';\nimport { buildDTS, buildJS } from './build.js';\nimport { FORMAT_DTS_ID, FORMAT_JS_ID, type JSPluginOptions, TYPE_MAP } from './lib.js';\n\nexport * from './build.js';\nexport * from './lib.js';\n\nexport default function pluginJS(options?: JSPluginOptions): Plugin {\n const customTransform = options?.transform;\n\n return {\n name: '@terrazzo/plugin-js',\n async transform({ tokens, getTransforms, setTransform }) {\n // skip work if another .js plugin has already run\n const jsTokens = getTransforms({ format: FORMAT_JS_ID, id: '*', mode: '.' });\n if (jsTokens.length) {\n return;\n }\n\n for (const [id, token] of Object.entries(tokens)) {\n // .d.ts (only default \".\" mode needed)\n setTransform(id, {\n format: FORMAT_DTS_ID,\n value: {\n description: token.$description!,\n value: `Record<\"${Object.keys(token.mode).join('\" | \"')}\", ${TYPE_MAP[token.$type]}[\"$value\"]>`,\n },\n mode: '.',\n });\n\n // .js (all modes)\n for (const mode of Object.keys(token.mode)) {\n if (customTransform) {\n const transformedValue = customTransform(token, mode);\n if (transformedValue !== undefined && transformedValue !== null) {\n setTransform(id, { format: FORMAT_JS_ID, value: transformedValue, mode });\n continue;\n }\n }\n const transformedValue = transformJSValue(token, { mode, startingIndent: 4 });\n if (transformedValue !== undefined) {\n setTransform(id, { format: FORMAT_JS_ID, value: transformedValue, mode });\n }\n }\n }\n },\n async build({ getTransforms, outputFile }) {\n // if (options?.json) {\n // const contents = buildJSON({ getTransforms });\n // outputFile(typeof options?.json === 'string' ? options.json : 'index.json', contents);\n // }\n if (options?.js) {\n const js = buildJS({ getTransforms });\n const jsFilename = typeof options?.js === 'string' ? options.js : 'index.js';\n outputFile(jsFilename, js);\n\n const dts = buildDTS({ getTransforms });\n const dtsFil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MAAM;GAAK,EAAC;AAC5E,OAAI,SAAS,OACX;AAGF,QAAK,MAAM,CAAC,IAAI,MAAM,IAAI,OAAO,QAAQ,OAAO,EAAE;AAEhD,iBAAa,IAAI;KACf,QAAQ;KACR,OAAO;MACL,aAAa,MAAM;MACnB,OAAO,CAAC,QAAQ,EAAE,OAAO,KAAK,MAAM,KAAK,CAAC,KAAK,UAAQ,CAAC,GAAG,EAAE,SAAS,MAAM,OAAO,WAAW,CAAC;KAChG;KACD,MAAM;IACP,EAAC;AAGF,SAAK,MAAM,QAAQ,OAAO,KAAK,MAAM,KAAK,EAAE;AAC1C,SAAI,iBAAiB;MACnB,MAAMC,qBAAmB,gBAAgB,OAAO,KAAK;AACrD,UAAIA,iCAAkCA,uBAAqB,MAAM;AAC/D,oBAAa,IAAI;QAAE,QAAQ;QAAc,OAAOA;QAAkB;OAAM,EAAC;AACzE;MACD;KACF;KACD,MAAM,mBAAmB,iBAAiB,OAAO;MAAE;MAAM,gBAAgB;KAAG,EAAC;AAC7E,SAAI,4BACF,cAAa,IAAI;MAAE,QAAQ;MAAc,OAAO;MAAkB;KAAM,EAAC;IAE5E;GACF;EACF;EACD,MAAM,MAAM,EAAE,eAAe,YAAY,EAAE;AAKzC,OAAI,SAAS,IAAI;IACf,MAAM,KAAK,QAAQ,EAAE,cAAe,EAAC;IACrC,MAAM,oBAAoB,SAAS,OAAO,WAAW,QAAQ,KAAK;AAClE,eAAW,YAAY,GAAG;IAE1B,MAAM,MAAM,SAAS,EAAE,cAAe,EAAC;IACvC,MAAM,qBAAqB,SAAS,OAAO,WAAW,QAAQ,GAAG,QAAQ,SAAS,QAAQ,GAAG;AAC7F,eAAW,aAAa,IAAI;GAC7B;EACF;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["TYPE_MAP: Record<Token['$type'], string>","output: string[]","tokenVals: Record<string, Record<string, string>>","transformedValue"],"sources":["../../token-tools/dist/js.js","../src/lib.ts","../src/build.ts","../src/index.ts"],"sourcesContent":["//#region src/js/index.ts\n/**\n* Convert token value to a JS string via acorn/astring.\n*/\nfunction transformJSValue(token, { mode, indent = 2, startingIndent = 0 }) {\n\tif (!(mode in token.mode)) return;\n\tconst indentStart = startingIndent > 0 ? \" \".repeat(startingIndent ?? 2) : \"\";\n\treturn JSON.stringify(token.mode[mode].$value, void 0, indent).replace(/\\n/g, `\\n${indentStart}`);\n}\n\n//#endregion\nexport { transformJSValue };\n//# sourceMappingURL=js.js.map","import type { Token, TokenNormalized, TokenTransformed } from '@terrazzo/parser';\n\nexport const FORMAT_JS_ID = 'js';\nexport const FORMAT_DTS_ID = 'd.ts';\n\nexport interface JSPluginOptions {\n /**\n * Output JS?\n * @default true\n */\n js?: boolean | string;\n /** Output JSON?\n * @default false\n */\n json?: boolean | string;\n /** Exclude token IDs from output? */\n exclude?: string[];\n /**\n * Return deeply-nested values?\n * @default: false\n */\n deep?: boolean;\n /** Override certain token values */\n transform?: (token: TokenNormalized, mode: string) => TokenTransformed['value'];\n}\n\nexport const FILE_HEADER = `/** ------------------------------------------\n * Autogenerated by ⛋ Terrazzo. DO NOT EDIT!\n * ------------------------------------------- */`;\n\nexport const TYPE_MAP: Record<Token['$type'], string> = {\n boolean: 'BooleanTokenNormalized',\n border: 'BorderTokenNormalized',\n color: 'ColorTokenNormalized',\n cubicBezier: 'CubicBezierTokenNormalized',\n dimension: 'DimensionTokenNormalized',\n duration: 'DurationTokenNormalized',\n fontFamily: 'FontFamilyTokenNormalized',\n fontWeight: 'FontWeightTokenNormalized',\n gradient: 'GradientTokenNormalized',\n link: 'LinkTokenNormalized',\n number: 'NumberTokenNormalized',\n shadow: 'ShadowTokenNormalized',\n string: 'StringTokenNormalized',\n strokeStyle: 'StrokeStyleTokenNormalized',\n typography: 'TypographyTokenNormalized',\n transition: 'TransitionTokenNormalized',\n};\n","import type { BuildHookOptions } from '@terrazzo/parser';\nimport { FILE_HEADER, FORMAT_DTS_ID, FORMAT_JS_ID, TYPE_MAP } from './lib.js';\n\nexport function buildJS({ getTransforms }: { getTransforms: BuildHookOptions['getTransforms'] }): string {\n const output: string[] = [FILE_HEADER, ''];\n\n // gather vals\n const tokenVals: Record<string, Record<string, string>> = {};\n for (const token of getTransforms({ format: FORMAT_JS_ID, id: '*' })) {\n if (!tokenVals[token.token.id]) {\n tokenVals[token.token.id] = {};\n }\n tokenVals[token.token.id]![token.mode] = token.value as string;\n }\n\n // body\n output.push('export const tokens = {');\n for (const [id, tokenValue] of Object.entries(tokenVals)) {\n output.push(` \"${id}\": {`);\n for (const [mode, modeValue] of Object.entries(tokenValue)) {\n output.push(` \"${mode}\": ${modeValue},`);\n }\n output.push(' },');\n }\n output.push('};', '');\n\n // footer\n output.push(\n `/** Get individual token */\nexport function token(tokenID, modeName = \".\") {\n return tokens[tokenID]?.[modeName];\n}`,\n '',\n );\n\n return output.join('\\n');\n}\n\nexport function buildDTS({ getTransforms }: { getTransforms: BuildHookOptions['getTransforms'] }): string {\n const output: string[] = [FILE_HEADER, ''];\n\n const importDeps = new Set<string>();\n const types = getTransforms({ format: FORMAT_DTS_ID, id: '*', mode: '.' }).map((t) => {\n importDeps.add(TYPE_MAP[t.token.$type]); // collect only necessary types\n if (t.type === 'MULTI_VALUE') {\n const description = t.value.description ? ` /** ${t.value.description} */\\n` : '';\n return `${description} \"${t.token.id}\": ${t.value.value};`;\n }\n return `\"${t.token.id}\": ${t.value};`;\n });\n\n output.push(\n 'import type {',\n ...[...importDeps].sort((a, b) => a.localeCompare(b)).map((dep) => ` ${dep},`),\n '} from \"@terrazzo/parser\";',\n '',\n 'export declare const tokens: {',\n ...types,\n '};',\n '',\n `export declare function token<K extends keyof typeof tokens>(tokenID: K, modeName?: never): (typeof tokens)[K][\".\"];\nexport declare function token<K extends keyof typeof tokens, M extends keyof (typeof tokens)[K]>(tokenID: K, modeName: M): (typeof tokens)[K][M];`,\n '',\n );\n\n return output.join('\\n');\n}\n","import type { Plugin } from '@terrazzo/parser';\nimport { transformJSValue } from '@terrazzo/token-tools/js';\nimport { buildDTS, buildJS } from './build.js';\nimport { FORMAT_DTS_ID, FORMAT_JS_ID, type JSPluginOptions, TYPE_MAP } from './lib.js';\n\nexport * from './build.js';\nexport * from './lib.js';\n\nexport default function pluginJS(options?: JSPluginOptions): Plugin {\n const customTransform = options?.transform;\n\n return {\n name: '@terrazzo/plugin-js',\n async transform({ tokens, getTransforms, setTransform }) {\n // skip work if another .js plugin has already run\n const jsTokens = getTransforms({ format: FORMAT_JS_ID, id: '*', mode: '.' });\n if (jsTokens.length) {\n return;\n }\n\n for (const [id, token] of Object.entries(tokens)) {\n // .d.ts (only default \".\" mode needed)\n setTransform(id, {\n format: FORMAT_DTS_ID,\n value: {\n description: token.$description ? String(token.$description) : '',\n value: `Record<\"${Object.keys(token.mode).join('\" | \"')}\", ${TYPE_MAP[token.$type]}[\"$value\"]>`,\n },\n mode: '.',\n });\n\n // .js (all modes)\n for (const mode of Object.keys(token.mode)) {\n if (customTransform) {\n const transformedValue = customTransform(token, mode);\n if (transformedValue !== undefined && transformedValue !== null) {\n setTransform(id, {\n format: FORMAT_JS_ID,\n value: transformedValue,\n mode,\n meta: { 'token-listing': { name: `tokens.${id}` } },\n });\n continue;\n }\n }\n const transformedValue = transformJSValue(token, { mode, startingIndent: 4 });\n if (transformedValue !== undefined) {\n setTransform(id, {\n format: FORMAT_JS_ID,\n value: transformedValue,\n mode,\n meta: { 'token-listing': { name: `tokens.${id}` } },\n });\n }\n }\n }\n },\n async build({ getTransforms, outputFile }) {\n // if (options?.json) {\n // const contents = buildJSON({ getTransforms });\n // outputFile(typeof options?.json === 'string' ? options.json : 'index.json', contents);\n // }\n if (options?.js) {\n const js = buildJS({ getTransforms });\n const jsFilename = typeof options?.js === 'string' ? options.js : 'index.js';\n outputFile(jsFilename, js);\n\n const dts = buildDTS({ getTransforms });\n const dtsFilename = typeof options?.js === 'string' ? options.js.replace(/\\.js$/, '.d.ts') : 'index.d.ts';\n outputFile(dtsFilename, dts);\n }\n },\n };\n}\n"],"mapp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,KAAK;AACrD,UAAIA,uBAAqB,UAAaA,uBAAqB,MAAM;AAC/D,oBAAa,IAAI;QACf,QAAQ;QACR,OAAOA;QACP;QACA,MAAM,EAAE,iBAAiB,EAAE,MAAM,UAAU,MAAM,EAAE;QACpD,CAAC;AACF;;;KAGJ,MAAM,mBAAmB,iBAAiB,OAAO;MAAE;MAAM,gBAAgB;MAAG,CAAC;AAC7E,SAAI,qBAAqB,OACvB,cAAa,IAAI;MACf,QAAQ;MACR,OAAO;MACP;MACA,MAAM,EAAE,iBAAiB,EAAE,MAAM,UAAU,MAAM,EAAE;MACpD,CAAC;;;;EAKV,MAAM,MAAM,EAAE,eAAe,cAAc;AAKzC,OAAI,SAAS,IAAI;IACf,MAAM,KAAK,QAAQ,EAAE,eAAe,CAAC;IACrC,MAAM,aAAa,OAAO,SAAS,OAAO,WAAW,QAAQ,KAAK;AAClE,eAAW,YAAY,GAAG;IAE1B,MAAM,MAAM,SAAS,EAAE,eAAe,CAAC;IACvC,MAAM,cAAc,OAAO,SAAS,OAAO,WAAW,QAAQ,GAAG,QAAQ,SAAS,QAAQ,GAAG;AAC7F,eAAW,aAAa,IAAI;;;EAGjC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrazzo/plugin-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "2.0.0-alpha.1",
|
|
4
4
|
"description": "Generate JS, TS, and JSON from your DTCG design tokens JSON.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,14 +28,15 @@
|
|
|
28
28
|
"directory": "./packages/plugin-js/"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@terrazzo/cli": "^0.
|
|
31
|
+
"@terrazzo/cli": "^2.0.0-alpha.1"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@terrazzo/token-tools": "^0.
|
|
34
|
+
"@terrazzo/token-tools": "^2.0.0-alpha.1"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"
|
|
38
|
-
"@terrazzo/parser": "^0.
|
|
37
|
+
"dtcg-examples": "^0.3.4",
|
|
38
|
+
"@terrazzo/parser": "^2.0.0-alpha.1",
|
|
39
|
+
"@terrazzo/cli": "^2.0.0-alpha.1"
|
|
39
40
|
},
|
|
40
41
|
"scripts": {
|
|
41
42
|
"build": "rolldown -c && attw --profile esm-only --pack .",
|