@terrazzo/plugin-js 2.0.0-alpha.2 → 2.0.0-alpha.6
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 +1 -10
- package/README.md +9 -6
- package/dist/index.d.ts +37 -27
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +143 -99
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
1
|
# @terrazzo/plugin-js
|
|
2
2
|
|
|
3
|
-
## 2.0.0-alpha.2
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- [#568](https://github.com/terrazzoapp/terrazzo/pull/568) [`67c75be`](https://github.com/terrazzoapp/terrazzo/commit/67c75be78978cece52b61cf258ccc3a875e6af48) Thanks [@drwpow](https://github.com/drwpow)! - Fix border tokens not cascading correctly across modes
|
|
8
|
-
|
|
9
|
-
- Updated dependencies [[`67c75be`](https://github.com/terrazzoapp/terrazzo/commit/67c75be78978cece52b61cf258ccc3a875e6af48)]:
|
|
10
|
-
- @terrazzo/cli@2.0.0-alpha.2
|
|
11
|
-
- @terrazzo/token-tools@2.0.0-alpha.2
|
|
12
|
-
|
|
13
3
|
## 2.0.0
|
|
14
4
|
|
|
15
5
|
### Minor Changes
|
|
@@ -24,6 +14,7 @@
|
|
|
24
14
|
|
|
25
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.
|
|
26
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
|
+
- [#568](https://github.com/terrazzoapp/terrazzo/pull/568) [`67c75be`](https://github.com/terrazzoapp/terrazzo/commit/67c75be78978cece52b61cf258ccc3a875e6af48) Thanks [@drwpow](https://github.com/drwpow)! - Fix border tokens not cascading correctly across modes
|
|
27
18
|
|
|
28
19
|
## 0.10.3
|
|
29
20
|
|
package/README.md
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
# ⛋ @terrazzo/plugin-js
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Use DTCG resolvers in Node.js for server-rendered applications.
|
|
4
|
+
|
|
5
|
+
> [!NOTE]
|
|
6
|
+
>
|
|
7
|
+
> Since adding support for [resolvers](https://terrazzo.app/docs/), plugin-js has added more weight and metadata than is practical for most clientside uses. You may want to consider using [plugin-css-in-js](https://github.com/terrazzoapp/terrazzo/blob/main/packages/plugin-css-in-js) instead.
|
|
4
8
|
|
|
5
9
|
## Setup
|
|
6
10
|
|
|
7
|
-
Requires [Node.js
|
|
11
|
+
Requires [Node.js](https://nodejs.org). With that installed, run:
|
|
8
12
|
|
|
9
13
|
```sh
|
|
10
14
|
npm i -D @terrazzo/cli @terrazzo/plugin-js
|
|
11
15
|
```
|
|
12
16
|
|
|
13
|
-
Add a `terrazzo.config.
|
|
17
|
+
Add a `terrazzo.config.ts` to the root of your project with:
|
|
14
18
|
|
|
15
19
|
```ts
|
|
16
20
|
import { defineConfig } from "@terrazzo/cli";
|
|
@@ -20,8 +24,7 @@ export default defineConfig({
|
|
|
20
24
|
outDir: "./tokens/",
|
|
21
25
|
plugins: [
|
|
22
26
|
js({
|
|
23
|
-
|
|
24
|
-
// json: "tokens.json",
|
|
27
|
+
filename: "tokens.js", // Note: `.d.ts` is generated too
|
|
25
28
|
}),
|
|
26
29
|
],
|
|
27
30
|
});
|
|
@@ -33,6 +36,6 @@ Lastly, run:
|
|
|
33
36
|
npx tz build
|
|
34
37
|
```
|
|
35
38
|
|
|
36
|
-
And you’ll see a `./tokens/
|
|
39
|
+
And you’ll see a `./tokens/tokens.js` file generated in your project.
|
|
37
40
|
|
|
38
41
|
[Full Documentation](https://terrazzo.app/docs/integrations/js)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,45 +1,55 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Logger, Plugin, Resolver as Resolver$1, Token, TokenNormalized, TokenNormalizedSet } from "@terrazzo/parser";
|
|
2
2
|
|
|
3
3
|
//#region src/lib.d.ts
|
|
4
|
-
declare const
|
|
5
|
-
|
|
6
|
-
interface JSPluginOptions {
|
|
4
|
+
declare const DEFAULT_PROPERTIES: Set<keyof TokenNormalized>;
|
|
5
|
+
interface JSPluginOptions<T extends Record<string, string> = Record<string, string>> {
|
|
7
6
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @default
|
|
7
|
+
* Change the output filename
|
|
8
|
+
* @default "index.js"
|
|
10
9
|
*/
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
/** Exclude token IDs from output? */
|
|
17
|
-
exclude?: string[];
|
|
18
|
-
/**
|
|
19
|
-
* Return deeply-nested values?
|
|
20
|
-
* @default: false
|
|
21
|
-
*/
|
|
22
|
-
deep?: boolean;
|
|
23
|
-
/** Override certain token values */
|
|
24
|
-
transform?: (token: TokenNormalized, mode: string) => TokenTransformed['value'];
|
|
10
|
+
filename?: string;
|
|
11
|
+
/** Only generate permutations for the specified modifiers (by default, generate all permutations) */
|
|
12
|
+
contexts?: { [key in keyof T]: T[keyof T][] };
|
|
13
|
+
/** Only include specified properties as part of token output. */
|
|
14
|
+
properties?: (keyof TokenNormalized)[];
|
|
25
15
|
}
|
|
16
|
+
type Resolver<T extends Record<string, string>> = (input: T) => TokenNormalizedSet;
|
|
26
17
|
declare const FILE_HEADER = "/** ------------------------------------------\n * Autogenerated by \u26CB Terrazzo. DO NOT EDIT!\n * ------------------------------------------- */";
|
|
27
18
|
declare const TYPE_MAP: Record<Token['$type'], string>;
|
|
28
19
|
//#endregion
|
|
29
20
|
//#region src/build.d.ts
|
|
30
21
|
declare function buildJS({
|
|
31
|
-
|
|
22
|
+
resolver,
|
|
23
|
+
properties,
|
|
24
|
+
logger,
|
|
25
|
+
contexts
|
|
32
26
|
}: {
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
resolver: Resolver$1;
|
|
28
|
+
properties: Set<keyof TokenNormalized>;
|
|
29
|
+
contexts?: Record<string, string[]>;
|
|
30
|
+
logger: Logger;
|
|
31
|
+
}): {
|
|
32
|
+
code: string;
|
|
33
|
+
typeMap: Record<string, Token['$type']>;
|
|
34
|
+
};
|
|
35
35
|
declare function buildDTS({
|
|
36
|
-
|
|
36
|
+
resolver,
|
|
37
|
+
contexts,
|
|
38
|
+
properties,
|
|
39
|
+
typeMap
|
|
37
40
|
}: {
|
|
38
|
-
|
|
41
|
+
resolver: Resolver$1;
|
|
42
|
+
contexts?: Record<string, string[]>;
|
|
43
|
+
properties: Set<keyof TokenNormalized>;
|
|
44
|
+
typeMap: Record<string, Token['$type']>;
|
|
39
45
|
}): string;
|
|
40
46
|
//#endregion
|
|
41
47
|
//#region src/index.d.ts
|
|
42
|
-
declare function pluginJS(
|
|
48
|
+
declare function pluginJS({
|
|
49
|
+
filename,
|
|
50
|
+
properties: userProperties,
|
|
51
|
+
contexts
|
|
52
|
+
}?: JSPluginOptions): Plugin;
|
|
43
53
|
//#endregion
|
|
44
|
-
export {
|
|
54
|
+
export { DEFAULT_PROPERTIES, FILE_HEADER, JSPluginOptions, Resolver, TYPE_MAP, buildDTS, buildJS, pluginJS as default };
|
|
45
55
|
//# 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,
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/lib.ts","../src/build.ts","../src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lB,cFQkB;EAAA;AAAA,CAAA,CAAA,EEN7B,eFM6B,CAAA,EENN,MFMM"}
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* Convert token value to a JS string via acorn/astring.
|
|
4
|
-
*/
|
|
5
|
-
function transformJSValue(token, { mode, indent = 2, startingIndent = 0 }) {
|
|
6
|
-
if (!(mode in token.mode)) return;
|
|
7
|
-
const indentStart = startingIndent > 0 ? " ".repeat(startingIndent ?? 2) : "";
|
|
8
|
-
return JSON.stringify(token.mode[mode].$value, void 0, indent).replace(/\n/g, `\n${indentStart}`);
|
|
9
|
-
}
|
|
1
|
+
import { calculatePermutations } from "@terrazzo/parser";
|
|
2
|
+
import { pascalCase } from "scule";
|
|
10
3
|
|
|
11
|
-
//#endregion
|
|
12
4
|
//#region src/lib.ts
|
|
13
|
-
const
|
|
14
|
-
|
|
5
|
+
const DEFAULT_PROPERTIES = new Set([
|
|
6
|
+
"$type",
|
|
7
|
+
"$value",
|
|
8
|
+
"$description",
|
|
9
|
+
"$extensions",
|
|
10
|
+
"$deprecated"
|
|
11
|
+
]);
|
|
15
12
|
const FILE_HEADER = `/** ------------------------------------------
|
|
16
13
|
* Autogenerated by ⛋ Terrazzo. DO NOT EDIT!
|
|
17
14
|
* ------------------------------------------- */`;
|
|
@@ -36,106 +33,153 @@ const TYPE_MAP = {
|
|
|
36
33
|
|
|
37
34
|
//#endregion
|
|
38
35
|
//#region src/build.ts
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
36
|
+
const RESOLVER_JSDOC_COMMENT = "/** Produce a token set from a given input. */";
|
|
37
|
+
function buildJS({ resolver, properties, logger, contexts }) {
|
|
38
|
+
const entry = {
|
|
39
|
+
group: "plugin",
|
|
40
|
+
label: "@terrazzo/plugin-js"
|
|
41
|
+
};
|
|
42
|
+
const typeMap = {};
|
|
43
|
+
let output = FILE_HEADER;
|
|
44
|
+
output += "\n";
|
|
45
|
+
const permutations = (contexts ? calculatePermutations(Object.entries(contexts)) : resolver.listPermutations()).map((value) => ({
|
|
46
|
+
value,
|
|
47
|
+
id: JSON.stringify(Object.fromEntries(Object.entries(value).sort((a, b) => a[0].localeCompare(b[0], "en-us", { numeric: true }))))
|
|
48
|
+
}));
|
|
49
|
+
output += "export const PERMUTATIONS = {\n";
|
|
50
|
+
let permutationI = 1;
|
|
51
|
+
for (const { value, id } of permutations) {
|
|
52
|
+
const start = performance.now();
|
|
53
|
+
try {
|
|
54
|
+
const tokens = resolver.apply(value);
|
|
55
|
+
output += ` ${JSON.stringify(id)}: {\n`;
|
|
56
|
+
for (const id$1 of Object.keys(tokens)) {
|
|
57
|
+
output += ` ${serializeToken(tokens[id$1], properties)},\n`;
|
|
58
|
+
if (!(id$1 in typeMap)) typeMap[id$1] = tokens[id$1].$type;
|
|
59
|
+
}
|
|
60
|
+
output += " },\n";
|
|
61
|
+
} catch (err) {
|
|
62
|
+
logger.error({
|
|
63
|
+
...entry,
|
|
64
|
+
message: err.message
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const timing = performance.now() - start;
|
|
68
|
+
logger.info({
|
|
69
|
+
...entry,
|
|
70
|
+
message: `Permutation ${permutationI}/${permutations.length} done (${id})`,
|
|
71
|
+
timing
|
|
72
|
+
});
|
|
73
|
+
permutationI++;
|
|
74
|
+
}
|
|
75
|
+
output += "};\n";
|
|
76
|
+
const inputDefaults = Object.fromEntries(resolver.source.resolutionOrder.filter((i) => i.type === "modifier" && "default" in i).map((m) => [m.name, m.default]));
|
|
77
|
+
output += `const INPUT_DEFAULTS = ${JSON.stringify(inputDefaults)};\n`;
|
|
78
|
+
output += RESOLVER_JSDOC_COMMENT;
|
|
79
|
+
output += "\n";
|
|
80
|
+
output += `export const resolver = {
|
|
81
|
+
apply(userInput) {
|
|
82
|
+
if (!userInput || typeof userInput !== "object") {
|
|
83
|
+
throw new Error(\`invalid input: \${userInput}\`);
|
|
84
|
+
}
|
|
85
|
+
const input = { ...INPUT_DEFAULTS, ...userInput };
|
|
86
|
+
const inputKey = JSON.stringify(Object.fromEntries(Object.entries(input).sort((a, b) => a[0].localeCompare(b[0], "en-us", { numeric: true }))));
|
|
87
|
+
return PERMUTATIONS[inputKey];
|
|
88
|
+
},
|
|
89
|
+
listPermutations() {
|
|
90
|
+
return [${permutations.map((p) => p.id).join(", ")}];
|
|
91
|
+
},
|
|
92
|
+
};\n`;
|
|
93
|
+
return {
|
|
94
|
+
code: output,
|
|
95
|
+
typeMap
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function buildDTS({ resolver, contexts, properties, typeMap }) {
|
|
99
|
+
const unique$type = new Set(Object.values(typeMap));
|
|
100
|
+
let output = FILE_HEADER;
|
|
101
|
+
output += "\n";
|
|
102
|
+
output += "import type {\n";
|
|
103
|
+
for (const $type of unique$type) if ($type in TYPE_MAP) output += ` ${TYPE_MAP[$type]},\n`;
|
|
104
|
+
output += "} from \"@terrazzo/parser\";";
|
|
105
|
+
output += "\n\n";
|
|
106
|
+
output += "export const PERMUTATIONS: Record<string, TokenNormalizedSet>;\n\n";
|
|
107
|
+
output += `type InputType = ${buildInputType(resolver, contexts)};\n\n`;
|
|
108
|
+
for (const $type of unique$type) {
|
|
109
|
+
if (!($type in TYPE_MAP)) continue;
|
|
110
|
+
output += `export type ${localTypeName($type)} = Pick<${TYPE_MAP[$type]}, ${[...properties].map((p) => JSON.stringify(p)).join(" | ")}>;\n`;
|
|
48
111
|
}
|
|
49
|
-
output
|
|
50
|
-
for (const [id,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
112
|
+
output += "\nexport interface Tokens {\n";
|
|
113
|
+
for (const [id, $type] of Object.entries(typeMap)) output += ` ${JSON.stringify(id)}: ${$type in TYPE_MAP ? localTypeName($type) : "any"};\n`;
|
|
114
|
+
output += "}\n";
|
|
115
|
+
output += "\n";
|
|
116
|
+
output += RESOLVER_JSDOC_COMMENT;
|
|
117
|
+
output += "\n";
|
|
118
|
+
output += `export const resolver: {
|
|
119
|
+
apply(input: InputType): Tokens;
|
|
120
|
+
listPermutations(): InputType[];
|
|
121
|
+
};
|
|
122
|
+
`;
|
|
123
|
+
return output;
|
|
124
|
+
}
|
|
125
|
+
/** Generate TypeScript definition of valid inputs given a resolver */
|
|
126
|
+
function buildInputType(resolver, contexts) {
|
|
127
|
+
const validContexts = contexts ?? {};
|
|
128
|
+
if (!Object.keys(validContexts).length) {
|
|
129
|
+
for (const [name, m] of Object.entries(resolver.source.modifiers ?? {})) validContexts[name] = Object.keys(m.contexts);
|
|
130
|
+
for (const m of resolver.source.resolutionOrder) {
|
|
131
|
+
if (m?.type !== "modifier") continue;
|
|
132
|
+
validContexts[m.name] = Object.keys(m.contexts);
|
|
133
|
+
}
|
|
54
134
|
}
|
|
55
|
-
output
|
|
56
|
-
output.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}`, "");
|
|
60
|
-
return output.join("\n");
|
|
135
|
+
let output = "{";
|
|
136
|
+
for (const [name, values] of Object.entries(validContexts)) output += `\n ${JSON.stringify(name)}: ${values.length ? values.map((v) => JSON.stringify(v)).join(" | ") : "never"};`;
|
|
137
|
+
output += "\n};";
|
|
138
|
+
return output;
|
|
61
139
|
}
|
|
62
|
-
function
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
mode: "."
|
|
69
|
-
}).map((t) => {
|
|
70
|
-
importDeps.add(TYPE_MAP[t.token.$type]);
|
|
71
|
-
if (t.type === "MULTI_VALUE") return `${t.value.description ? ` /** ${t.value.description} */\n` : ""} "${t.token.id}": ${t.value.value};`;
|
|
72
|
-
return `"${t.token.id}": ${t.value};`;
|
|
73
|
-
});
|
|
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]["."];
|
|
75
|
-
export declare function token<K extends keyof typeof tokens, M extends keyof (typeof tokens)[K]>(tokenID: K, modeName: M): (typeof tokens)[K][M];`, "");
|
|
76
|
-
return output.join("\n");
|
|
140
|
+
function localTypeName($type) {
|
|
141
|
+
return pascalCase($type);
|
|
142
|
+
}
|
|
143
|
+
/** Serialize normalized Tokens set into a string */
|
|
144
|
+
function serializeToken(token, includeProperties) {
|
|
145
|
+
return `${JSON.stringify(token.id)}:${JSON.stringify(Object.fromEntries(Object.entries(token).filter(([k]) => includeProperties.has(k))))}`;
|
|
77
146
|
}
|
|
78
147
|
|
|
79
148
|
//#endregion
|
|
80
149
|
//#region src/index.ts
|
|
81
|
-
function pluginJS(
|
|
82
|
-
const
|
|
150
|
+
function pluginJS({ filename = "index.js", properties: userProperties, contexts } = {}) {
|
|
151
|
+
const entry = {
|
|
152
|
+
group: "plugin",
|
|
153
|
+
label: "@terrazzo/plugin-js"
|
|
154
|
+
};
|
|
83
155
|
return {
|
|
84
156
|
name: "@terrazzo/plugin-js",
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}).length) return;
|
|
91
|
-
for (const [id, token] of Object.entries(tokens)) {
|
|
92
|
-
setTransform(id, {
|
|
93
|
-
format: FORMAT_DTS_ID,
|
|
94
|
-
value: {
|
|
95
|
-
description: token.$description ? String(token.$description) : "",
|
|
96
|
-
value: `Record<"${Object.keys(token.mode).join("\" | \"")}", ${TYPE_MAP[token.$type]}["$value"]>`
|
|
97
|
-
},
|
|
98
|
-
mode: "."
|
|
99
|
-
});
|
|
100
|
-
for (const mode of Object.keys(token.mode)) {
|
|
101
|
-
if (customTransform) {
|
|
102
|
-
const transformedValue$1 = customTransform(token, mode);
|
|
103
|
-
if (transformedValue$1 !== void 0 && transformedValue$1 !== null) {
|
|
104
|
-
setTransform(id, {
|
|
105
|
-
format: FORMAT_JS_ID,
|
|
106
|
-
value: transformedValue$1,
|
|
107
|
-
mode,
|
|
108
|
-
meta: { "token-listing": { name: `tokens.${id}` } }
|
|
109
|
-
});
|
|
110
|
-
continue;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
const transformedValue = transformJSValue(token, {
|
|
114
|
-
mode,
|
|
115
|
-
startingIndent: 4
|
|
116
|
-
});
|
|
117
|
-
if (transformedValue !== void 0) setTransform(id, {
|
|
118
|
-
format: FORMAT_JS_ID,
|
|
119
|
-
value: transformedValue,
|
|
120
|
-
mode,
|
|
121
|
-
meta: { "token-listing": { name: `tokens.${id}` } }
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
}
|
|
157
|
+
config(_, context) {
|
|
158
|
+
if (Array.isArray(userProperties) && !userProperties.length) context.logger.error({
|
|
159
|
+
...entry,
|
|
160
|
+
message: "properties option can’t be empty"
|
|
161
|
+
});
|
|
125
162
|
},
|
|
126
|
-
async build({
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
163
|
+
async build({ resolver, outputFile, context }) {
|
|
164
|
+
const properties = userProperties ? new Set(userProperties) : DEFAULT_PROPERTIES;
|
|
165
|
+
const { code: js, typeMap } = buildJS({
|
|
166
|
+
resolver,
|
|
167
|
+
contexts,
|
|
168
|
+
properties,
|
|
169
|
+
logger: context.logger
|
|
170
|
+
});
|
|
171
|
+
outputFile(filename, js);
|
|
172
|
+
const dts = buildDTS({
|
|
173
|
+
resolver,
|
|
174
|
+
contexts,
|
|
175
|
+
properties,
|
|
176
|
+
typeMap
|
|
177
|
+
});
|
|
178
|
+
outputFile(typeof filename === "string" ? filename.replace(/\.(c|m)?js$/, ".d.$1ts") : "index.d.ts", dts);
|
|
135
179
|
}
|
|
136
180
|
};
|
|
137
181
|
}
|
|
138
182
|
|
|
139
183
|
//#endregion
|
|
140
|
-
export {
|
|
184
|
+
export { DEFAULT_PROPERTIES, FILE_HEADER, TYPE_MAP, buildDTS, buildJS, pluginJS as default };
|
|
141
185
|
//# sourceMappingURL=index.js.map
|
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>>","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"],"mappings":";;;;AAIA,SAAS,iBAAiB,OAAO,EAAE,MAAM,SAAS,GAAG,iBAAiB,KAAK;AAC1E,KAAI,EAAE,QAAQ,MAAM,MAAO;CAC3B,MAAM,cAAc,iBAAiB,IAAI,IAAI,OAAO,kBAAkB,EAAE,GAAG;AAC3E,QAAO,KAAK,UAAU,MAAM,KAAK,MAAM,QAAQ,KAAK,GAAG,OAAO,CAAC,QAAQ,OAAO,KAAK,cAAc;;;;;ACLlG,MAAa,eAAe;AAC5B,MAAa,gBAAgB;AAuB7B,MAAa,cAAc;;;AAI3B,MAAaA,WAA2C;CACtD,SAAS;CACT,QAAQ;CACR,OAAO;CACP,aAAa;CACb,WAAW;CACX,UAAU;CACV,YAAY;CACZ,YAAY;CACZ,UAAU;CACV,MAAM;CACN,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,aAAa;CACb,YAAY;CACZ,YAAY;CACb;;;;AC5CD,SAAgB,QAAQ,EAAE,iBAA+E;CACvG,MAAMC,SAAmB,CAAC,aAAa,GAAG;CAG1C,MAAMC,YAAoD,EAAE;AAC5D,MAAK,MAAM,SAAS,cAAc;EAAE,QAAQ;EAAc,IAAI;EAAK,CAAC,EAAE;AACpE,MAAI,CAAC,UAAU,MAAM,MAAM,IACzB,WAAU,MAAM,MAAM,MAAM,EAAE;AAEhC,YAAU,MAAM,MAAM,IAAK,MAAM,QAAQ,MAAM;;AAIjD,QAAO,KAAK,0BAA0B;AACtC,MAAK,MAAM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,EAAE;AAEhD,iBAAa,IAAI;KACf,QAAQ;KACR,OAAO;MACL,aAAa,MAAM,eAAe,OAAO,MAAM,aAAa,GAAG;MAC/D,OAAO,WAAW,OAAO,KAAK,MAAM,KAAK,CAAC,KAAK,UAAQ,CAAC,KAAK,SAAS,MAAM,OAAO;MACpF;KACD,MAAM;KACP,CAAC;AAGF,SAAK,MAAM,QAAQ,OAAO,KAAK,MAAM,KAAK,EAAE;AAC1C,SAAI,iBAAiB;MACnB,MAAME,qBAAmB,gBAAgB,OAAO,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"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["DEFAULT_PROPERTIES: Set<keyof TokenNormalized>","TYPE_MAP: Record<Token['$type'], string>","typeMap: Record<string, Token['$type']>","id","validContexts: Record<string, string[]>"],"sources":["../src/lib.ts","../src/build.ts","../src/index.ts"],"sourcesContent":["import type { Token, TokenNormalized, TokenNormalizedSet } from '@terrazzo/parser';\n\nexport const DEFAULT_PROPERTIES: Set<keyof TokenNormalized> = new Set([\n '$type',\n '$value',\n '$description',\n '$extensions',\n '$deprecated',\n // 'id',\n // 'jsonID',\n // 'aliasOf',\n // 'aliasChain',\n // 'aliasedBy',\n // 'dependencies',\n // 'group',\n]);\n\nexport interface JSPluginOptions<T extends Record<string, string> = Record<string, string>> {\n /**\n * Change the output filename\n * @default \"index.js\"\n */\n filename?: string;\n /** Only generate permutations for the specified modifiers (by default, generate all permutations) */\n contexts?: { [key in keyof T]: T[keyof T][] };\n /** Only include specified properties as part of token output. */\n properties?: (keyof TokenNormalized)[];\n}\n\nexport type Resolver<T extends Record<string, string>> = (input: T) => TokenNormalizedSet;\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 {\n calculatePermutations,\n type Logger,\n type Resolver,\n type ResolverModifierNormalized,\n type Token,\n type TokenNormalized,\n} from '@terrazzo/parser';\nimport { pascalCase } from 'scule';\nimport { FILE_HEADER, TYPE_MAP } from './lib.js';\n\nconst RESOLVER_JSDOC_COMMENT = '/** Produce a token set from a given input. */';\n\nexport function buildJS({\n resolver,\n properties,\n logger,\n contexts,\n}: {\n resolver: Resolver;\n properties: Set<keyof TokenNormalized>;\n contexts?: Record<string, string[]>;\n logger: Logger;\n}): { code: string; typeMap: Record<string, Token['$type']> } {\n const entry = { group: 'plugin' as const, label: '@terrazzo/plugin-js' };\n const typeMap: Record<string, Token['$type']> = {};\n\n // here, output not being an array drastically reduces memory usage for large token sets\n let output = FILE_HEADER;\n output += '\\n';\n\n // 1. Permutations\n const permutations = (contexts ? calculatePermutations(Object.entries(contexts)) : resolver.listPermutations()).map(\n (value) => ({\n value,\n // Note: id MUST have modifiers sorted alphabetically, so we can index them shallowly\n id: JSON.stringify(\n Object.fromEntries(Object.entries(value).sort((a, b) => a[0].localeCompare(b[0], 'en-us', { numeric: true }))),\n ),\n }),\n );\n output += 'export const PERMUTATIONS = {\\n';\n let permutationI = 1;\n for (const { value, id } of permutations) {\n const start = performance.now();\n try {\n const tokens = resolver.apply(value);\n output += ` ${JSON.stringify(id)}: {\\n`;\n for (const id of Object.keys(tokens)) {\n output += ` ${serializeToken(tokens[id]!, properties)},\\n`;\n if (!(id in typeMap)) {\n typeMap[id] = tokens[id]!.$type;\n }\n }\n output += ' },\\n';\n } catch (err) {\n logger.error({ ...entry, message: (err as Error).message });\n }\n const timing = performance.now() - start;\n logger.info({\n ...entry,\n message: `Permutation ${permutationI}/${permutations.length} done (${id})`,\n timing,\n });\n permutationI++;\n }\n output += '};\\n';\n\n // 2. Input defaults\n const inputDefaults = Object.fromEntries(\n resolver.source.resolutionOrder\n .filter((i) => i.type === 'modifier' && 'default' in i)\n .map((m) => [m.name, (m as ResolverModifierNormalized).default!]),\n );\n output += `const INPUT_DEFAULTS = ${JSON.stringify(inputDefaults)};\\n`;\n\n // 3. Resolver\n output += RESOLVER_JSDOC_COMMENT;\n output += '\\n';\n output += `export const resolver = {\n apply(userInput) {\n if (!userInput || typeof userInput !== \"object\") {\n throw new Error(\\`invalid input: \\${userInput}\\`);\n }\n const input = { ...INPUT_DEFAULTS, ...userInput };\n const inputKey = JSON.stringify(Object.fromEntries(Object.entries(input).sort((a, b) => a[0].localeCompare(b[0], \"en-us\", { numeric: true }))));\n return PERMUTATIONS[inputKey];\n },\n listPermutations() {\n return [${permutations.map((p) => p.id).join(', ')}];\n },\n};\\n`;\n\n return { code: output, typeMap };\n}\n\nexport function buildDTS({\n resolver,\n contexts,\n properties,\n typeMap,\n}: {\n resolver: Resolver;\n contexts?: Record<string, string[]>;\n properties: Set<keyof TokenNormalized>;\n typeMap: Record<string, Token['$type']>;\n}): string {\n const unique$type = new Set(Object.values(typeMap));\n\n let output = FILE_HEADER;\n output += '\\n';\n output += 'import type {\\n';\n for (const $type of unique$type) {\n if ($type in TYPE_MAP) {\n output += ` ${TYPE_MAP[$type]},\\n`;\n }\n }\n output += '} from \"@terrazzo/parser\";';\n output += '\\n\\n';\n output += 'export const PERMUTATIONS: Record<string, TokenNormalizedSet>;\\n\\n';\n output += `type InputType = ${buildInputType(resolver, contexts)};\\n\\n`;\n for (const $type of unique$type) {\n if (!($type in TYPE_MAP)) {\n continue;\n }\n output += `export type ${localTypeName($type)} = Pick<${TYPE_MAP[$type]}, ${[...properties].map((p) => JSON.stringify(p)).join(' | ')}>;\\n`;\n }\n output += '\\nexport interface Tokens {\\n';\n for (const [id, $type] of Object.entries(typeMap)) {\n output += ` ${JSON.stringify(id)}: ${$type in TYPE_MAP ? localTypeName($type) : 'any'};\\n`;\n }\n output += '}\\n';\n output += '\\n';\n output += RESOLVER_JSDOC_COMMENT;\n output += '\\n';\n output += `export const resolver: {\n apply(input: InputType): Tokens;\n listPermutations(): InputType[];\n};\n`;\n return output;\n}\n\n/** Generate TypeScript definition of valid inputs given a resolver */\nfunction buildInputType(resolver: Resolver, contexts?: Record<string, string[]>): string {\n const validContexts: Record<string, string[]> = contexts ?? {};\n if (!Object.keys(validContexts).length) {\n for (const [name, m] of Object.entries(resolver.source.modifiers ?? {})) {\n validContexts[name] = Object.keys(m.contexts);\n }\n for (const m of resolver.source.resolutionOrder) {\n if (m?.type !== 'modifier') {\n continue;\n }\n validContexts[m.name] = Object.keys(m.contexts);\n }\n }\n\n let output = '{';\n for (const [name, values] of Object.entries(validContexts)) {\n output += `\\n ${JSON.stringify(name)}: ${values.length ? values.map((v) => JSON.stringify(v)).join(' | ') : 'never'};`;\n }\n output += '\\n};';\n return output;\n}\n\nfunction localTypeName($type: Token['$type']): string {\n return pascalCase($type);\n}\n\n/** Serialize normalized Tokens set into a string */\nfunction serializeToken(token: TokenNormalized, includeProperties: Set<keyof TokenNormalized>): string {\n return `${JSON.stringify(token.id)}:${JSON.stringify(Object.fromEntries(Object.entries(token).filter(([k]) => includeProperties.has(k as keyof TokenNormalized))))}`;\n}\n","import type { Plugin } from '@terrazzo/parser';\nimport { buildDTS, buildJS } from './build.js';\nimport { DEFAULT_PROPERTIES, type JSPluginOptions } from './lib.js';\n\nexport * from './build.js';\nexport * from './lib.js';\n\nexport default function pluginJS({\n filename = 'index.js',\n properties: userProperties,\n contexts,\n}: JSPluginOptions = {}): Plugin {\n const entry = { group: 'plugin' as const, label: '@terrazzo/plugin-js' };\n\n return {\n name: '@terrazzo/plugin-js',\n config(_, context) {\n if (Array.isArray(userProperties) && !userProperties.length) {\n context.logger.error({ ...entry, message: 'properties option can’t be empty' });\n }\n },\n\n // As of 2.0, this plugin no longer pre-transforms values, because every instance\n // generates a unique output.\n async build({ resolver, outputFile, context }) {\n const properties = userProperties ? new Set(userProperties) : DEFAULT_PROPERTIES;\n const { code: js, typeMap } = buildJS({\n resolver,\n contexts,\n properties,\n logger: context.logger,\n });\n outputFile(filename, js);\n\n const dts = buildDTS({ resolver, contexts, properties, typeMap });\n const dtsFilename = typeof filename === 'string' ? filename.replace(/\\.(c|m)?js$/, '.d.$1ts') : 'index.d.ts';\n outputFile(dtsFilename, dts);\n },\n };\n}\n"],"mappings":";;;;AAEA,MAAaA,qBAAiD,IAAI,IAAI;CACpE;CACA;CACA;CACA;CACA;CAQD,CAAC;AAgBF,MAAa,cAAc;;;AAI3B,MAAaC,W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;IAAY;IAAS,CAAC;AAEjE,cADoB,OAAO,aAAa,WAAW,SAAS,QAAQ,eAAe,UAAU,GAAG,cACxE,IAAI;;EAE/B"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrazzo/plugin-js",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.0.0-alpha.6",
|
|
4
|
+
"description": "Use DTCG tokens in Node.js for server-rendered applications.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"author": {
|
|
@@ -21,22 +21,22 @@
|
|
|
21
21
|
".": "./dist/index.js",
|
|
22
22
|
"./package.json": "./package.json"
|
|
23
23
|
},
|
|
24
|
-
"homepage": "https://terrazzo.app/docs/
|
|
24
|
+
"homepage": "https://terrazzo.app/docs/integrations/js",
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|
|
27
27
|
"url": "https://github.com/terrazzoapp/terrazzo.git",
|
|
28
28
|
"directory": "./packages/plugin-js/"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@terrazzo/cli": "^2.0.0-alpha.
|
|
31
|
+
"@terrazzo/cli": "^2.0.0-alpha.6",
|
|
32
|
+
"@terrazzo/parser": "^2.0.0-alpha.6"
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"
|
|
35
|
+
"scule": "^1.3.0",
|
|
36
|
+
"@terrazzo/parser": "^2.0.0-alpha.6"
|
|
35
37
|
},
|
|
36
38
|
"devDependencies": {
|
|
37
|
-
"
|
|
38
|
-
"@terrazzo/cli": "^2.0.0-alpha.2",
|
|
39
|
-
"@terrazzo/parser": "^2.0.0-alpha.2"
|
|
39
|
+
"@terrazzo/cli": "^2.0.0-alpha.6"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"build": "rolldown -c && attw --profile esm-only --pack .",
|