@terrazzo/plugin-js 0.0.0 → 0.0.2
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 +11 -0
- package/LICENSE +2 -2
- package/README.md +38 -0
- package/package.json +19 -14
- package/biome.json +0 -15
- package/src/build.ts +0 -67
- package/src/index.ts +0 -66
- package/src/lib.ts +0 -40
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# @terrazzo/plugin-js
|
|
2
|
+
|
|
3
|
+
## 0.0.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#285](https://github.com/terrazzoapp/terrazzo/pull/285) [`e8a0df1`](https://github.com/terrazzoapp/terrazzo/commit/e8a0df1f3b50cf7cb292bcc475aae271feae4569) Thanks [@drwpow](https://github.com/drwpow)! - Add support for multiple token files
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`e8a0df1`](https://github.com/terrazzoapp/terrazzo/commit/e8a0df1f3b50cf7cb292bcc475aae271feae4569)]:
|
|
10
|
+
- @terrazzo/token-tools@0.0.6
|
|
11
|
+
- @terrazzo/cli@0.0.12
|
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2024 Drew Powers
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# ⛋ @terrazzo/plugin-js
|
|
2
|
+
|
|
3
|
+
Generate JavaScript, TypeScript, and JSON from DTCG tokens.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
Requires [Node.js 18 or later](https://nodejs.org). With that installed, run:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm i -D @terrazzo/cli @terrazzo/plugin-js
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Add a `terrazzo.config.js` to the root of your project with:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { defineConfig } from "@terrazzo/cli";
|
|
17
|
+
import js from "@terrazzo/plugin-js";
|
|
18
|
+
|
|
19
|
+
export default defineConfig({
|
|
20
|
+
outDir: "./tokens/",
|
|
21
|
+
plugins: [
|
|
22
|
+
js({
|
|
23
|
+
js: "index.js",
|
|
24
|
+
// json: "tokens.json",
|
|
25
|
+
}),
|
|
26
|
+
],
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Lastly, run:
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
npx tz build
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
And you’ll see a `./tokens/index.js` file generated in your project.
|
|
37
|
+
|
|
38
|
+
[Full Documentation](https://terrazzo.app/docs/integrations/js)
|
package/package.json
CHANGED
|
@@ -1,40 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrazzo/plugin-js",
|
|
3
|
+
"version": "0.0.2",
|
|
3
4
|
"description": "Generate JS, TS, and JSON from your design tokens schema (requires @terrazzo/cli)",
|
|
4
|
-
"
|
|
5
|
+
"type": "module",
|
|
5
6
|
"author": {
|
|
6
7
|
"name": "Drew Powers",
|
|
7
8
|
"email": "drew@pow.rs"
|
|
8
9
|
},
|
|
9
10
|
"keywords": [
|
|
10
11
|
"design tokens",
|
|
11
|
-
"design tokens community group",
|
|
12
|
-
"design tokens format module",
|
|
13
12
|
"design system",
|
|
14
13
|
"dtcg",
|
|
15
|
-
"w3c
|
|
14
|
+
"w3c",
|
|
15
|
+
"ts",
|
|
16
|
+
"typescript"
|
|
16
17
|
],
|
|
17
|
-
"license": "MIT",
|
|
18
|
-
"type": "module",
|
|
19
18
|
"main": "./dist/index.js",
|
|
19
|
+
"homepage": "https://terrazzoapp.com/docs/cli/integrations/js",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/terrazzoapp/terrazzo.git",
|
|
23
|
+
"directory": "./packages/plugin-js/"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
20
26
|
"peerDependencies": {
|
|
21
|
-
"@terrazzo/cli": "^0.0.
|
|
27
|
+
"@terrazzo/cli": "^0.0.12"
|
|
22
28
|
},
|
|
23
29
|
"dependencies": {
|
|
24
|
-
"@terrazzo/token-tools": "^0.0.
|
|
30
|
+
"@terrazzo/token-tools": "^0.0.6"
|
|
25
31
|
},
|
|
26
32
|
"devDependencies": {
|
|
27
|
-
"@terrazzo/
|
|
28
|
-
"@terrazzo/
|
|
33
|
+
"@terrazzo/cli": "^0.0.12",
|
|
34
|
+
"@terrazzo/parser": "^0.0.12"
|
|
29
35
|
},
|
|
30
36
|
"scripts": {
|
|
31
|
-
"build": "pnpm run build:clean && pnpm run build:ts && pnpm run
|
|
32
|
-
"build:clean": "del dist",
|
|
37
|
+
"build": "pnpm run build:clean && pnpm run build:ts && pnpm -w run inject-license @terrazzo/plugin-js dist/index.js",
|
|
38
|
+
"build:clean": "del-cli dist",
|
|
33
39
|
"build:ts": "tsc -p tsconfig.build.json",
|
|
34
|
-
"build:license": "node ../../scripts/inject-license.js @terrazzo/plugin-js dist/index.js",
|
|
35
40
|
"dev": "tsc -p tsconfig.build.json -w",
|
|
36
41
|
"lint": "biome check .",
|
|
37
|
-
"test": "
|
|
42
|
+
"test": "pnpm --filter @terrazzo/plugin-js run \"/^test:*/\"",
|
|
38
43
|
"test:js": "vitest run",
|
|
39
44
|
"test:ts": "tsc --noEmit"
|
|
40
45
|
}
|
package/biome.json
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
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/src/build.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
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
|
-
};
|