@terrazzo/plugin-tailwind 0.0.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 ADDED
@@ -0,0 +1,10 @@
1
+ # @terrazzo/plugin-tailwind
2
+
3
+ ## 0.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#411](https://github.com/terrazzoapp/terrazzo/pull/411) [`2ad079c`](https://github.com/terrazzoapp/terrazzo/commit/2ad079c06dcdb3b0241e678b1625f202a4ec92b1) Thanks [@drwpow](https://github.com/drwpow)! - Fix bug in pnpm workspaces
8
+
9
+ - Updated dependencies [[`7e3d513`](https://github.com/terrazzoapp/terrazzo/commit/7e3d513e5bcde5e613cde35d367d49c6a46293a1), [`2ad079c`](https://github.com/terrazzoapp/terrazzo/commit/2ad079c06dcdb3b0241e678b1625f202a4ec92b1)]:
10
+ - @terrazzo/plugin-css@0.3.2
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 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/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # ⛋ @terrazzo/plugin-tailwind
2
+
3
+ Generate a Tailwind v4 theme from DTCG tokens.
4
+
5
+ ## Setup
6
+
7
+ Requires [Node.js 20 or later](https://nodejs.org). With that installed, run:
8
+
9
+ ```sh
10
+ npm i -D @terrazzo/cli @terrazzo/plugin-css @terrazzo/plugin-tailwind
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 css from "@terrazzo/plugin-css";
18
+ import tailwind from "@terrazzo/plugin-tailwind";
19
+
20
+ export default defineConfig({
21
+ outDir: "./tokens/",
22
+ plugins: [
23
+ css({
24
+ filename: "tokens.css",
25
+ }),
26
+ tailwind({
27
+ filename: "tailwind.js",
28
+ theme: {
29
+ /** @see https://tailwindcss.com/docs/configuration#theme */
30
+ colors: {
31
+ blue: {
32
+ 100: "color.blue.100",
33
+ 200: "color.blue.200",
34
+ // …
35
+ },
36
+ },
37
+ fontFamily: {
38
+ sans: "typography.family.base",
39
+ // …
40
+ },
41
+ extend: {
42
+ spacing: {
43
+ 1: "token.size.s.space",
44
+ 2: "token.size.m.space",
45
+ // …
46
+ },
47
+ borderRadius: {
48
+ m: "token.size.m.borderRadius",
49
+ // …
50
+ },
51
+ },
52
+ },
53
+ }),
54
+ ],
55
+ });
56
+ ```
57
+
58
+ Lastly, run:
59
+
60
+ ```sh
61
+ npx tz build
62
+ ```
63
+
64
+ And you’ll see a `./tokens/tailwind-theme.css` file generated in your project.
65
+
66
+ [Full Documentation](https://terrazzo.app/docs/integrations/taiwlind)
@@ -0,0 +1,6 @@
1
+ import type { Plugin } from '@terrazzo/parser';
2
+ import { type TailwindPluginOptions } from './lib.js';
3
+ export declare const FORMAT_ID = "tailwind";
4
+ export * from './lib.js';
5
+ export default function pluginTailwind(options: TailwindPluginOptions): Plugin;
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,EAAE,KAAK,qBAAqB,EAAmB,MAAM,UAAU,CAAC;AAEvE,eAAO,MAAM,SAAS,aAAa,CAAC;AAEpC,cAAc,UAAU,CAAC;AAEzB,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,OAAO,EAAE,qBAAqB,GAAG,MAAM,CAgE7E"}
package/dist/index.js ADDED
@@ -0,0 +1,88 @@
1
+ /**
2
+ * @module @terrazzo/plugin-tailwind
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 { FORMAT_ID as FORMAT_CSS } from '@terrazzo/plugin-css';
26
+ import { flattenThemeObj } from './lib.js';
27
+ export const FORMAT_ID = 'tailwind';
28
+ export * from './lib.js';
29
+ export default function pluginTailwind(options) {
30
+ const filename = options?.filename ?? options?.fileName ?? 'tailwind-theme.css';
31
+ return {
32
+ name: '@terrazzo/plugin-tailwind',
33
+ enforce: 'post', // ensure this comes after @terrazzo/plugin-css
34
+ config(config) {
35
+ if (!config.plugins.some((p) => p.name === '@terrazzo/plugin-css')) {
36
+ throw new Error('@terrazzo/plugin-css missing! Please install and add to the plugins array to use the Tailwind plugin.');
37
+ }
38
+ if (!options || !options.theme) {
39
+ throw new Error('Missing Tailwind `theme` option.');
40
+ }
41
+ },
42
+ async transform({ getTransforms, setTransform }) {
43
+ const variants = [{ variant: '.', mode: '.' }, ...(options?.modeVariants ?? [])];
44
+ for (const { variant, mode } of variants) {
45
+ const flatTheme = flattenThemeObj(options.theme);
46
+ for (const { path, value } of flatTheme) {
47
+ for (const token of getTransforms({
48
+ format: FORMAT_CSS,
49
+ id: value,
50
+ mode,
51
+ })) {
52
+ let relName = token.id.split('.').at(-1);
53
+ for (const subgroup of [...(Array.isArray(value) ? value : [value])]) {
54
+ const match = subgroup.replace(/\*.*/, '');
55
+ relName = token.id.replace(match, '');
56
+ }
57
+ setTransform(token.id, {
58
+ format: FORMAT_ID,
59
+ localID: `--${path.join('-')}-${relName.replace(/\./g, '-')}`,
60
+ value: typeof token.value === 'object' ? token.value['.'] : token.value,
61
+ mode: variant, // ! <- not the original mode!
62
+ });
63
+ }
64
+ }
65
+ }
66
+ },
67
+ async build({ getTransforms, outputFile }) {
68
+ const output = ['@import "tailwind";', ''];
69
+ const variants = { '.': [] };
70
+ for (const token of getTransforms({ format: FORMAT_ID })) {
71
+ const { localID, value, mode } = token;
72
+ if (!variants[mode]) {
73
+ variants[mode] = [];
74
+ }
75
+ variants[mode].push(`${localID}: ${value};`);
76
+ }
77
+ for (const [variant, values] of Object.entries(variants)) {
78
+ output.push(variant === '.' ? '@theme {' : `@variant ${variant} {`);
79
+ for (const value of values) {
80
+ output.push(` ${value}`);
81
+ }
82
+ output.push('}', '');
83
+ }
84
+ outputFile(filename, output.join('\n'));
85
+ },
86
+ };
87
+ }
88
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,IAAI,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,EAA8B,eAAe,EAAE,MAAM,UAAU,CAAC;AAEvE,MAAM,CAAC,MAAM,SAAS,GAAG,UAAU,CAAC;AAEpC,cAAc,UAAU,CAAC;AAEzB,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,OAA8B;IACnE,MAAM,QAAQ,GAAG,OAAO,EAAE,QAAQ,IAAK,OAAe,EAAE,QAAQ,IAAI,oBAAoB,CAAC;IAEzF,OAAO;QACL,IAAI,EAAE,2BAA2B;QACjC,OAAO,EAAE,MAAM,EAAE,+CAA+C;QAChE,MAAM,CAAC,MAAM;YACX,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,sBAAsB,CAAC,EAAE,CAAC;gBACnE,MAAM,IAAI,KAAK,CACb,uGAAuG,CACxG,CAAC;YACJ,CAAC;YAED,IAAI,CAAC,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;gBAC/B,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;YACtD,CAAC;QACH,CAAC;QACD,KAAK,CAAC,SAAS,CAAC,EAAE,aAAa,EAAE,YAAY,EAAE;YAC7C,MAAM,QAAQ,GAAG,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE,GAAG,CAAC,OAAO,EAAE,YAAY,IAAI,EA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* Filename to output.
4
+ * @default "tailwind-theme.css"
5
+ */
6
+ filename?: string;
7
+ /** @see https://tailwindcss.com/docs/theme */
8
+ theme: Record<string, any>;
9
+ /** Array of mapping variants to DTCG modes. */
10
+ modeVariants?: {
11
+ variant: string;
12
+ mode: string;
13
+ }[];
14
+ }
15
+ /** Flatten an arbitrarily-nested object */
16
+ export declare function flattenThemeObj(themeObj: Record<string, unknown>): {
17
+ path: string[];
18
+ value: string | string[];
19
+ }[];
20
+ //# sourceMappingURL=lib.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8CAA8C;IAC9C,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,+CAA+C;IAC/C,YAAY,CAAC,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CACpD;AAED,2CAA2C;AAC3C,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IAAE,IAAI,EAAE,MAAM,EAAE,CAAC;IAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CAAE,EAAE,CAqBjH"}
package/dist/lib.js ADDED
@@ -0,0 +1,21 @@
1
+ /** Flatten an arbitrarily-nested object */
2
+ export function flattenThemeObj(themeObj) {
3
+ const result = [];
4
+ function traverse(obj, path) {
5
+ for (const [key, value] of Object.entries(obj)) {
6
+ const newPath = [...path, key];
7
+ if (typeof value === 'string' || Array.isArray(value)) {
8
+ if (Array.isArray(value) && (value.length === 0 || value.some((v) => typeof v !== 'string'))) {
9
+ throw new Error(`Invalid value at path "${newPath.join('.')}": expected a string or an array of strings, but got ${JSON.stringify(value)}`);
10
+ }
11
+ result.push({ path: newPath, value });
12
+ }
13
+ else if (typeof value === 'object' && value !== null) {
14
+ traverse(value, newPath);
15
+ }
16
+ }
17
+ }
18
+ traverse(themeObj, []);
19
+ return result;
20
+ }
21
+ //# sourceMappingURL=lib.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lib.js","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAYA,2CAA2C;AAC3C,MAAM,UAAU,eAAe,CAAC,QAAiC;IAC/D,MAAM,MAAM,GAAmD,EAAE,CAAC;IAElE,SAAS,QAAQ,CAAC,GAA4B,EAAE,IAAc;QAC5D,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YAC/C,MAAM,OAAO,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,CAAC,CAAC;YAC/B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;gBACtD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,EAAE,CAAC;oBAC7F,MAAM,IAAI,KAAK,CACb,0BAA0B,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,wDAAwD,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAC3H,CAAC;gBACJ,CAAC;gBACD,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;YACxC,CAAC;iBAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBACvD,QAAQ,CAAC,KAAgC,EAAE,OAAO,CAAC,CAAC;YACtD,CAAC;QACH,CAAC;IACH,CAAC;IAED,QAAQ,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IACvB,OAAO,MAAM,CAAC;AAChB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@terrazzo/plugin-tailwind",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "author": {
6
+ "name": "Drew Powers",
7
+ "email": "drew@pow.rs"
8
+ },
9
+ "keywords": [
10
+ "design tokens",
11
+ "design system",
12
+ "dtcg",
13
+ "w3c",
14
+ "tailwind",
15
+ "css"
16
+ ],
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/terrazzoapp/terrazzo.git",
21
+ "directory": "packages/plugin-tailwind"
22
+ },
23
+ "main": "./dist/index.js",
24
+ "peerDependencies": {
25
+ "@terrazzo/cli": "^0.7.0",
26
+ "@terrazzo/plugin-css": "^0.7.0",
27
+ "tailwindcss": "^4.0.0"
28
+ },
29
+ "dependencies": {
30
+ "@terrazzo/token-tools": "^0.7.1"
31
+ },
32
+ "devDependencies": {
33
+ "tailwindcss": "^4.1.5",
34
+ "@terrazzo/cli": "^0.7.1",
35
+ "@terrazzo/parser": "^0.7.1",
36
+ "@terrazzo/plugin-css": "^0.7.1"
37
+ },
38
+ "scripts": {
39
+ "build": "pnpm run build:ts && pnpm -w run inject-license @terrazzo/plugin-tailwind dist/index.js",
40
+ "build:ts": "tsc -p tsconfig.build.json",
41
+ "format": "biome check --fix --unsafe .",
42
+ "dev": "tsc -p tsconfig.build.json -w",
43
+ "lint": "pnpm --filter @terrazzo/plugin-tailwind run \"/^lint:.*/\"",
44
+ "lint:js": "biome check .",
45
+ "lint:ts": "tsc --noEmit",
46
+ "test": "vitest run"
47
+ }
48
+ }