@mp3wizard/figma-console-mcp 1.25.1 → 1.28.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/README.md +53 -35
- package/dist/apps/design-system-dashboard/mcp-app.html +78 -78
- package/dist/apps/token-browser/mcp-app.html +60 -59
- package/dist/cloudflare/core/config.js +0 -8
- package/dist/cloudflare/core/console-monitor.js +3 -3
- package/dist/cloudflare/core/diagnose-tool.js +96 -0
- package/dist/cloudflare/core/figma-tools.js +69 -229
- package/dist/cloudflare/core/identity.js +96 -0
- package/dist/cloudflare/core/tokens/alias-resolver.js +135 -0
- package/dist/cloudflare/core/tokens/config.js +284 -0
- package/dist/cloudflare/core/tokens/figma-converter.js +195 -0
- package/dist/cloudflare/core/tokens/formatters/css-vars.js +329 -0
- package/dist/cloudflare/core/tokens/formatters/dtcg.js +300 -0
- package/dist/cloudflare/core/tokens/formatters/index.js +45 -0
- package/dist/cloudflare/core/tokens/formatters/json.js +187 -0
- package/dist/cloudflare/core/tokens/formatters/less.js +4 -0
- package/dist/cloudflare/core/tokens/formatters/scss.js +252 -0
- package/dist/cloudflare/core/tokens/formatters/stubs.js +13 -0
- package/dist/cloudflare/core/tokens/formatters/style-dictionary-v3.js +207 -0
- package/dist/cloudflare/core/tokens/formatters/tailwind-v3.js +237 -0
- package/dist/cloudflare/core/tokens/formatters/tailwind-v4.js +330 -0
- package/dist/cloudflare/core/tokens/formatters/tokens-studio.js +250 -0
- package/dist/cloudflare/core/tokens/formatters/ts-module.js +198 -0
- package/dist/cloudflare/core/tokens/index.js +15 -0
- package/dist/cloudflare/core/tokens/parsers/css-vars.js +4 -0
- package/dist/cloudflare/core/tokens/parsers/dtcg.js +253 -0
- package/dist/cloudflare/core/tokens/parsers/index.js +138 -0
- package/dist/cloudflare/core/tokens/parsers/json.js +7 -0
- package/dist/cloudflare/core/tokens/parsers/scss.js +4 -0
- package/dist/cloudflare/core/tokens/parsers/stubs.js +20 -0
- package/dist/cloudflare/core/tokens/parsers/style-dictionary-v3.js +4 -0
- package/dist/cloudflare/core/tokens/parsers/tailwind-v3.js +4 -0
- package/dist/cloudflare/core/tokens/parsers/tailwind-v4.js +4 -0
- package/dist/cloudflare/core/tokens/parsers/tokens-studio.js +4 -0
- package/dist/cloudflare/core/tokens/schemas.js +148 -0
- package/dist/cloudflare/core/tokens/transforms/color.js +12 -0
- package/dist/cloudflare/core/tokens/transforms/index.js +29 -0
- package/dist/cloudflare/core/tokens/transforms/size.js +7 -0
- package/dist/cloudflare/core/tokens/types.js +18 -0
- package/dist/cloudflare/core/tokens-tools.js +859 -0
- package/dist/cloudflare/core/websocket-server.js +5 -55
- package/dist/cloudflare/index.js +37 -26
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +0 -8
- package/dist/core/config.js.map +1 -1
- package/dist/core/console-monitor.d.ts +2 -2
- package/dist/core/console-monitor.d.ts.map +1 -1
- package/dist/core/console-monitor.js +3 -3
- package/dist/core/console-monitor.js.map +1 -1
- package/dist/core/diagnose-tool.d.ts +33 -0
- package/dist/core/diagnose-tool.d.ts.map +1 -0
- package/dist/core/diagnose-tool.js +97 -0
- package/dist/core/diagnose-tool.js.map +1 -0
- package/dist/core/figma-connector.d.ts +1 -1
- package/dist/core/figma-connector.d.ts.map +1 -1
- package/dist/core/figma-tools.d.ts +1 -2
- package/dist/core/figma-tools.d.ts.map +1 -1
- package/dist/core/figma-tools.js +69 -229
- package/dist/core/figma-tools.js.map +1 -1
- package/dist/core/identity.d.ts +41 -0
- package/dist/core/identity.d.ts.map +1 -0
- package/dist/core/identity.js +97 -0
- package/dist/core/identity.js.map +1 -0
- package/dist/core/tokens/alias-resolver.d.ts +55 -0
- package/dist/core/tokens/alias-resolver.d.ts.map +1 -0
- package/dist/core/tokens/alias-resolver.js +136 -0
- package/dist/core/tokens/alias-resolver.js.map +1 -0
- package/dist/core/tokens/config.d.ts +352 -0
- package/dist/core/tokens/config.d.ts.map +1 -0
- package/dist/core/tokens/config.js +285 -0
- package/dist/core/tokens/config.js.map +1 -0
- package/dist/core/tokens/figma-converter.d.ts +81 -0
- package/dist/core/tokens/figma-converter.d.ts.map +1 -0
- package/dist/core/tokens/figma-converter.js +196 -0
- package/dist/core/tokens/figma-converter.js.map +1 -0
- package/dist/core/tokens/formatters/css-vars.d.ts +24 -0
- package/dist/core/tokens/formatters/css-vars.d.ts.map +1 -0
- package/dist/core/tokens/formatters/css-vars.js +330 -0
- package/dist/core/tokens/formatters/css-vars.js.map +1 -0
- package/dist/core/tokens/formatters/dtcg.d.ts +28 -0
- package/dist/core/tokens/formatters/dtcg.d.ts.map +1 -0
- package/dist/core/tokens/formatters/dtcg.js +301 -0
- package/dist/core/tokens/formatters/dtcg.js.map +1 -0
- package/dist/core/tokens/formatters/index.d.ts +30 -0
- package/dist/core/tokens/formatters/index.d.ts.map +1 -0
- package/dist/core/tokens/formatters/index.js +46 -0
- package/dist/core/tokens/formatters/index.js.map +1 -0
- package/dist/core/tokens/formatters/json.d.ts +37 -0
- package/dist/core/tokens/formatters/json.d.ts.map +1 -0
- package/dist/core/tokens/formatters/json.js +188 -0
- package/dist/core/tokens/formatters/json.js.map +1 -0
- package/dist/core/tokens/formatters/less.d.ts +4 -0
- package/dist/core/tokens/formatters/less.d.ts.map +1 -0
- package/dist/core/tokens/formatters/less.js +5 -0
- package/dist/core/tokens/formatters/less.js.map +1 -0
- package/dist/core/tokens/formatters/scss.d.ts +26 -0
- package/dist/core/tokens/formatters/scss.d.ts.map +1 -0
- package/dist/core/tokens/formatters/scss.js +253 -0
- package/dist/core/tokens/formatters/scss.js.map +1 -0
- package/dist/core/tokens/formatters/stubs.d.ts +9 -0
- package/dist/core/tokens/formatters/stubs.d.ts.map +1 -0
- package/dist/core/tokens/formatters/stubs.js +14 -0
- package/dist/core/tokens/formatters/stubs.js.map +1 -0
- package/dist/core/tokens/formatters/style-dictionary-v3.d.ts +45 -0
- package/dist/core/tokens/formatters/style-dictionary-v3.d.ts.map +1 -0
- package/dist/core/tokens/formatters/style-dictionary-v3.js +208 -0
- package/dist/core/tokens/formatters/style-dictionary-v3.js.map +1 -0
- package/dist/core/tokens/formatters/tailwind-v3.d.ts +37 -0
- package/dist/core/tokens/formatters/tailwind-v3.d.ts.map +1 -0
- package/dist/core/tokens/formatters/tailwind-v3.js +238 -0
- package/dist/core/tokens/formatters/tailwind-v3.js.map +1 -0
- package/dist/core/tokens/formatters/tailwind-v4.d.ts +41 -0
- package/dist/core/tokens/formatters/tailwind-v4.d.ts.map +1 -0
- package/dist/core/tokens/formatters/tailwind-v4.js +331 -0
- package/dist/core/tokens/formatters/tailwind-v4.js.map +1 -0
- package/dist/core/tokens/formatters/tokens-studio.d.ts +44 -0
- package/dist/core/tokens/formatters/tokens-studio.d.ts.map +1 -0
- package/dist/core/tokens/formatters/tokens-studio.js +251 -0
- package/dist/core/tokens/formatters/tokens-studio.js.map +1 -0
- package/dist/core/tokens/formatters/ts-module.d.ts +35 -0
- package/dist/core/tokens/formatters/ts-module.d.ts.map +1 -0
- package/dist/core/tokens/formatters/ts-module.js +199 -0
- package/dist/core/tokens/formatters/ts-module.js.map +1 -0
- package/dist/core/tokens/index.d.ts +17 -0
- package/dist/core/tokens/index.d.ts.map +1 -0
- package/dist/core/tokens/index.js +16 -0
- package/dist/core/tokens/index.js.map +1 -0
- package/dist/core/tokens/parsers/css-vars.d.ts +3 -0
- package/dist/core/tokens/parsers/css-vars.d.ts.map +1 -0
- package/dist/core/tokens/parsers/css-vars.js +5 -0
- package/dist/core/tokens/parsers/css-vars.js.map +1 -0
- package/dist/core/tokens/parsers/dtcg.d.ts +21 -0
- package/dist/core/tokens/parsers/dtcg.d.ts.map +1 -0
- package/dist/core/tokens/parsers/dtcg.js +254 -0
- package/dist/core/tokens/parsers/dtcg.js.map +1 -0
- package/dist/core/tokens/parsers/index.d.ts +37 -0
- package/dist/core/tokens/parsers/index.d.ts.map +1 -0
- package/dist/core/tokens/parsers/index.js +139 -0
- package/dist/core/tokens/parsers/index.js.map +1 -0
- package/dist/core/tokens/parsers/json.d.ts +4 -0
- package/dist/core/tokens/parsers/json.d.ts.map +1 -0
- package/dist/core/tokens/parsers/json.js +8 -0
- package/dist/core/tokens/parsers/json.js.map +1 -0
- package/dist/core/tokens/parsers/scss.d.ts +3 -0
- package/dist/core/tokens/parsers/scss.d.ts.map +1 -0
- package/dist/core/tokens/parsers/scss.js +5 -0
- package/dist/core/tokens/parsers/scss.js.map +1 -0
- package/dist/core/tokens/parsers/stubs.d.ts +15 -0
- package/dist/core/tokens/parsers/stubs.d.ts.map +1 -0
- package/dist/core/tokens/parsers/stubs.js +21 -0
- package/dist/core/tokens/parsers/stubs.js.map +1 -0
- package/dist/core/tokens/parsers/style-dictionary-v3.d.ts +3 -0
- package/dist/core/tokens/parsers/style-dictionary-v3.d.ts.map +1 -0
- package/dist/core/tokens/parsers/style-dictionary-v3.js +5 -0
- package/dist/core/tokens/parsers/style-dictionary-v3.js.map +1 -0
- package/dist/core/tokens/parsers/tailwind-v3.d.ts +3 -0
- package/dist/core/tokens/parsers/tailwind-v3.d.ts.map +1 -0
- package/dist/core/tokens/parsers/tailwind-v3.js +5 -0
- package/dist/core/tokens/parsers/tailwind-v3.js.map +1 -0
- package/dist/core/tokens/parsers/tailwind-v4.d.ts +3 -0
- package/dist/core/tokens/parsers/tailwind-v4.d.ts.map +1 -0
- package/dist/core/tokens/parsers/tailwind-v4.js +5 -0
- package/dist/core/tokens/parsers/tailwind-v4.js.map +1 -0
- package/dist/core/tokens/parsers/tokens-studio.d.ts +3 -0
- package/dist/core/tokens/parsers/tokens-studio.d.ts.map +1 -0
- package/dist/core/tokens/parsers/tokens-studio.js +5 -0
- package/dist/core/tokens/parsers/tokens-studio.js.map +1 -0
- package/dist/core/tokens/schemas.d.ts +152 -0
- package/dist/core/tokens/schemas.d.ts.map +1 -0
- package/dist/core/tokens/schemas.js +149 -0
- package/dist/core/tokens/schemas.js.map +1 -0
- package/dist/core/tokens/transforms/color.d.ts +9 -0
- package/dist/core/tokens/transforms/color.d.ts.map +1 -0
- package/dist/core/tokens/transforms/color.js +13 -0
- package/dist/core/tokens/transforms/color.js.map +1 -0
- package/dist/core/tokens/transforms/index.d.ts +36 -0
- package/dist/core/tokens/transforms/index.d.ts.map +1 -0
- package/dist/core/tokens/transforms/index.js +30 -0
- package/dist/core/tokens/transforms/index.js.map +1 -0
- package/dist/core/tokens/transforms/size.d.ts +7 -0
- package/dist/core/tokens/transforms/size.d.ts.map +1 -0
- package/dist/core/tokens/transforms/size.js +8 -0
- package/dist/core/tokens/transforms/size.js.map +1 -0
- package/dist/core/tokens/types.d.ts +228 -0
- package/dist/core/tokens/types.d.ts.map +1 -0
- package/dist/core/tokens/types.js +19 -0
- package/dist/core/tokens/types.js.map +1 -0
- package/dist/core/tokens-tools.d.ts +42 -0
- package/dist/core/tokens-tools.d.ts.map +1 -0
- package/dist/core/tokens-tools.js +860 -0
- package/dist/core/tokens-tools.js.map +1 -0
- package/dist/core/types/index.d.ts +0 -8
- package/dist/core/types/index.d.ts.map +1 -1
- package/dist/core/websocket-connector.d.ts +1 -1
- package/dist/core/websocket-connector.d.ts.map +1 -1
- package/dist/core/websocket-server.d.ts +4 -3
- package/dist/core/websocket-server.d.ts.map +1 -1
- package/dist/core/websocket-server.js +5 -55
- package/dist/core/websocket-server.js.map +1 -1
- package/dist/local.d.ts +0 -12
- package/dist/local.d.ts.map +1 -1
- package/dist/local.js +959 -3406
- package/dist/local.js.map +1 -1
- package/figma-desktop-bridge/code.js +11 -63
- package/figma-desktop-bridge/ui.html +72 -11
- package/package.json +27 -12
- package/figma-desktop-bridge/ui-full.html +0 -1353
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tailwind v3 config formatter.
|
|
3
|
+
*
|
|
4
|
+
* Output shape:
|
|
5
|
+
*
|
|
6
|
+
* // tailwind.tokens.js
|
|
7
|
+
* module.exports = {
|
|
8
|
+
* colors: {
|
|
9
|
+
* primary: "#4085F2",
|
|
10
|
+
* brand: { primary: "#FF00AA" }
|
|
11
|
+
* },
|
|
12
|
+
* spacing: {
|
|
13
|
+
* md: "16px"
|
|
14
|
+
* }
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
* Designed to be spread into `tailwind.config.js`:
|
|
18
|
+
*
|
|
19
|
+
* const tokens = require('./src/styles/generated/tailwind.tokens.js');
|
|
20
|
+
* module.exports = { theme: { extend: tokens } };
|
|
21
|
+
*
|
|
22
|
+
* Token-to-namespace mapping mirrors Tailwind v3's theme keys:
|
|
23
|
+
*
|
|
24
|
+
* color/* → colors.*
|
|
25
|
+
* spacing/* → spacing.*
|
|
26
|
+
* font/* → fontFamily.*
|
|
27
|
+
* text/* → fontSize.*
|
|
28
|
+
* radius/* → borderRadius.*
|
|
29
|
+
*
|
|
30
|
+
* Multi-mode tokens flatten to the primary mode (Tailwind v3 doesn't have
|
|
31
|
+
* a native multi-mode model — dark-mode variants come from `tailwindcss`'s
|
|
32
|
+
* `darkMode: 'class'` + separate CSS variable files).
|
|
33
|
+
*/
|
|
34
|
+
import { buildTokenIndex, resolveAliasChain } from "../alias-resolver.js";
|
|
35
|
+
export function formatTailwindV3(doc, opts) {
|
|
36
|
+
const warnings = [];
|
|
37
|
+
const files = [];
|
|
38
|
+
const splitByCollection = opts.target.splitByCollection ?? false;
|
|
39
|
+
// Tailwind v3 config is consumed by Tailwind's build step, which needs
|
|
40
|
+
// literal values — no runtime alias resolution. Build a full-document
|
|
41
|
+
// token index so we can resolve alias chains at export time. Even when
|
|
42
|
+
// splitByCollection writes one file per set, aliases may target tokens
|
|
43
|
+
// in OTHER sets (e.g. semantic → primitives) so the index must span
|
|
44
|
+
// every set, not just the file's own.
|
|
45
|
+
const tokenIndex = buildTokenIndex(doc);
|
|
46
|
+
if (splitByCollection) {
|
|
47
|
+
for (const set of doc.sets) {
|
|
48
|
+
files.push({
|
|
49
|
+
path: filenameFor(opts, set),
|
|
50
|
+
content: renderConfigFile([set], opts, tokenIndex, warnings),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
files.push({
|
|
56
|
+
path: filenameFor(opts),
|
|
57
|
+
content: renderConfigFile(doc.sets, opts, tokenIndex, warnings),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return { files, warnings };
|
|
61
|
+
}
|
|
62
|
+
function filenameFor(opts, set) {
|
|
63
|
+
if (opts.target.filename)
|
|
64
|
+
return opts.target.filename;
|
|
65
|
+
if (set)
|
|
66
|
+
return `${slugify(set.name)}.tailwind.tokens.js`;
|
|
67
|
+
return "tailwind.tokens.js";
|
|
68
|
+
}
|
|
69
|
+
function slugify(s) {
|
|
70
|
+
return s
|
|
71
|
+
.trim()
|
|
72
|
+
.toLowerCase()
|
|
73
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
74
|
+
.replace(/^-+|-+$/g, "");
|
|
75
|
+
}
|
|
76
|
+
function jsKey(segment) {
|
|
77
|
+
if (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(segment))
|
|
78
|
+
return segment;
|
|
79
|
+
return JSON.stringify(segment);
|
|
80
|
+
}
|
|
81
|
+
function renderConfigFile(sets, opts, tokenIndex, warnings) {
|
|
82
|
+
const lines = [];
|
|
83
|
+
lines.push("/**");
|
|
84
|
+
lines.push(" * Generated by figma-console-mcp — do not edit by hand.");
|
|
85
|
+
lines.push(" * Spread this into your tailwind.config.js theme.extend block:");
|
|
86
|
+
lines.push(" *");
|
|
87
|
+
lines.push(" * const tokens = require('./path/to/this-file');");
|
|
88
|
+
lines.push(" * module.exports = { theme: { extend: tokens } };");
|
|
89
|
+
lines.push(" */");
|
|
90
|
+
lines.push("");
|
|
91
|
+
// Group tokens by Tailwind theme namespace.
|
|
92
|
+
const namespaces = {};
|
|
93
|
+
for (const set of sets) {
|
|
94
|
+
const primaryMode = pickPrimaryMode(set.modes);
|
|
95
|
+
for (const token of set.tokens) {
|
|
96
|
+
const value = token.values[primaryMode];
|
|
97
|
+
if (!value)
|
|
98
|
+
continue;
|
|
99
|
+
const formattedValue = formatTokenValue(value, token, primaryMode, tokenIndex, warnings);
|
|
100
|
+
if (formattedValue === null)
|
|
101
|
+
continue;
|
|
102
|
+
const { namespace, subPath } = mapToNamespace(token);
|
|
103
|
+
if (!namespaces[namespace])
|
|
104
|
+
namespaces[namespace] = {};
|
|
105
|
+
writeIntoTree(namespaces[namespace], subPath, formattedValue);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
lines.push("module.exports = {");
|
|
109
|
+
const namespaceEntries = Object.entries(namespaces).sort(([a], [b]) => a.localeCompare(b));
|
|
110
|
+
for (let i = 0; i < namespaceEntries.length; i++) {
|
|
111
|
+
const [ns, tree] = namespaceEntries[i];
|
|
112
|
+
lines.push(` ${jsKey(ns)}: ${renderTree(tree, 1)}${i < namespaceEntries.length - 1 ? "," : ""}`);
|
|
113
|
+
}
|
|
114
|
+
lines.push("};");
|
|
115
|
+
lines.push("");
|
|
116
|
+
return lines.join("\n");
|
|
117
|
+
}
|
|
118
|
+
function pickPrimaryMode(modes) {
|
|
119
|
+
return modes.find((m) => /^(default|light|value)$/i.test(m)) ?? modes[0];
|
|
120
|
+
}
|
|
121
|
+
function mapToNamespace(token) {
|
|
122
|
+
const slug = token.path.map(slugify);
|
|
123
|
+
if (slug.length === 0)
|
|
124
|
+
return { namespace: "extend", subPath: [] };
|
|
125
|
+
// First-segment aliases that should be re-namespaced to Tailwind theme keys.
|
|
126
|
+
const NAMESPACE_MAP = {
|
|
127
|
+
color: "colors",
|
|
128
|
+
colors: "colors",
|
|
129
|
+
spacing: "spacing",
|
|
130
|
+
space: "spacing",
|
|
131
|
+
radius: "borderRadius",
|
|
132
|
+
"border-radius": "borderRadius",
|
|
133
|
+
rounded: "borderRadius",
|
|
134
|
+
font: "fontFamily",
|
|
135
|
+
"font-family": "fontFamily",
|
|
136
|
+
text: "fontSize",
|
|
137
|
+
"font-size": "fontSize",
|
|
138
|
+
"font-weight": "fontWeight",
|
|
139
|
+
shadow: "boxShadow",
|
|
140
|
+
"box-shadow": "boxShadow",
|
|
141
|
+
opacity: "opacity",
|
|
142
|
+
"z-index": "zIndex",
|
|
143
|
+
"line-height": "lineHeight",
|
|
144
|
+
leading: "lineHeight",
|
|
145
|
+
tracking: "letterSpacing",
|
|
146
|
+
"letter-spacing": "letterSpacing",
|
|
147
|
+
};
|
|
148
|
+
if (NAMESPACE_MAP[slug[0]]) {
|
|
149
|
+
return { namespace: NAMESPACE_MAP[slug[0]], subPath: slug.slice(1) };
|
|
150
|
+
}
|
|
151
|
+
// Heuristic by type when no first-segment match.
|
|
152
|
+
if (token.type === "color")
|
|
153
|
+
return { namespace: "colors", subPath: slug };
|
|
154
|
+
if (token.type === "dimension")
|
|
155
|
+
return { namespace: "spacing", subPath: slug };
|
|
156
|
+
if (token.type === "fontFamily")
|
|
157
|
+
return { namespace: "fontFamily", subPath: slug };
|
|
158
|
+
if (token.type === "fontWeight")
|
|
159
|
+
return { namespace: "fontWeight", subPath: slug };
|
|
160
|
+
// Anything else: dump under extend.{path}
|
|
161
|
+
return { namespace: "extend", subPath: slug };
|
|
162
|
+
}
|
|
163
|
+
function writeIntoTree(tree, path, value) {
|
|
164
|
+
if (path.length === 0)
|
|
165
|
+
return;
|
|
166
|
+
let cursor = tree;
|
|
167
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
168
|
+
const segment = path[i];
|
|
169
|
+
const next = cursor[segment];
|
|
170
|
+
if (!next || typeof next === "string") {
|
|
171
|
+
const newNode = {};
|
|
172
|
+
cursor[segment] = newNode;
|
|
173
|
+
cursor = newNode;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
cursor = next;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const leafKey = path[path.length - 1];
|
|
180
|
+
cursor[leafKey] = value;
|
|
181
|
+
}
|
|
182
|
+
function renderTree(tree, indent) {
|
|
183
|
+
const ind = " ".repeat(indent);
|
|
184
|
+
const innerInd = " ".repeat(indent + 1);
|
|
185
|
+
const entries = [];
|
|
186
|
+
const sortedKeys = Object.keys(tree).sort();
|
|
187
|
+
for (const key of sortedKeys) {
|
|
188
|
+
const value = tree[key];
|
|
189
|
+
const k = jsKey(key);
|
|
190
|
+
if (typeof value === "string") {
|
|
191
|
+
entries.push(`${innerInd}${k}: ${value}`);
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
entries.push(`${innerInd}${k}: ${renderTree(value, indent + 1)}`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (entries.length === 0)
|
|
198
|
+
return "{}";
|
|
199
|
+
return `{\n${entries.join(",\n")},\n${ind}}`;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Format a token value as a JavaScript literal (quoted string for most
|
|
203
|
+
* things). Resolves alias chains to their literal target value via the
|
|
204
|
+
* document-wide token index — Tailwind v3 config is read at Tailwind's
|
|
205
|
+
* build time and needs concrete values (no runtime cascade).
|
|
206
|
+
*
|
|
207
|
+
* Returns null only for cross-library aliases or genuinely empty values,
|
|
208
|
+
* after emitting a warning explaining what got skipped.
|
|
209
|
+
*/
|
|
210
|
+
function formatTokenValue(value, token, mode, tokenIndex, warnings) {
|
|
211
|
+
let effective = value;
|
|
212
|
+
if (value.reference) {
|
|
213
|
+
effective = resolveAliasChain(value, mode, tokenIndex);
|
|
214
|
+
if (!effective) {
|
|
215
|
+
const bare = value.reference.replace(/^\{|\}$/g, "");
|
|
216
|
+
const reason = bare.startsWith("__library:") || bare === "unknown"
|
|
217
|
+
? "cross-library alias"
|
|
218
|
+
: "alias target not found in any set";
|
|
219
|
+
warnings.push(`Skipped ${token.path.join(".")} in Tailwind v3 — ${reason}: ${value.reference}.`);
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (!effective || effective.literal === undefined || effective.literal === null) {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
if (typeof effective.literal === "number") {
|
|
227
|
+
if (token.type === "dimension")
|
|
228
|
+
return JSON.stringify(`${effective.literal}px`);
|
|
229
|
+
return String(effective.literal);
|
|
230
|
+
}
|
|
231
|
+
if (typeof effective.literal === "string")
|
|
232
|
+
return JSON.stringify(effective.literal);
|
|
233
|
+
if (typeof effective.literal === "boolean")
|
|
234
|
+
return String(effective.literal);
|
|
235
|
+
// Composite: emit as JSON object literal
|
|
236
|
+
return JSON.stringify(effective.literal);
|
|
237
|
+
}
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tailwind v4 `@theme` block formatter.
|
|
3
|
+
*
|
|
4
|
+
* Tailwind v4 introduced CSS-first configuration: instead of a JS
|
|
5
|
+
* `tailwind.config.js`, you write tokens directly into your CSS with an
|
|
6
|
+
* `@theme inline { ... }` block. Tailwind processes that block to generate
|
|
7
|
+
* utility classes (`bg-primary`, `text-foreground`, etc.) at build time.
|
|
8
|
+
*
|
|
9
|
+
* Output shape:
|
|
10
|
+
*
|
|
11
|
+
* @theme inline {
|
|
12
|
+
* --color-primary: #4085F2;
|
|
13
|
+
* --spacing-md: 16px;
|
|
14
|
+
* --font-family-sans: "Inter", sans-serif;
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* .dark {
|
|
18
|
+
* --color-primary: #5599FF;
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* Token-to-Tailwind-namespace mapping follows the canonical Tailwind v4
|
|
22
|
+
* convention (see https://tailwindcss.com/docs/theme):
|
|
23
|
+
*
|
|
24
|
+
* color/* → --color-* (maps to bg-*, text-*, border-*, etc.)
|
|
25
|
+
* spacing/* → --spacing-*
|
|
26
|
+
* radius/* → --radius-* (maps to rounded-*)
|
|
27
|
+
* font/* → --font-* (font-family namespace)
|
|
28
|
+
* text/* → --text-* (font-size namespace)
|
|
29
|
+
* font-weight/* → --font-weight-*
|
|
30
|
+
* tracking/* → --tracking-*
|
|
31
|
+
* leading/* → --leading-*
|
|
32
|
+
* shadow/* → --shadow-*
|
|
33
|
+
*
|
|
34
|
+
* Tokens that don't fit a known Tailwind namespace get emitted with their
|
|
35
|
+
* path verbatim (with prefix if configured). They're still valid CSS vars
|
|
36
|
+
* — they just don't generate Tailwind utility classes.
|
|
37
|
+
*/
|
|
38
|
+
export function formatTailwindV4(doc, opts) {
|
|
39
|
+
const warnings = [];
|
|
40
|
+
const files = [];
|
|
41
|
+
const splitByMode = opts.target.splitByMode ?? false;
|
|
42
|
+
const splitByCollection = opts.target.splitByCollection ?? false;
|
|
43
|
+
const prefix = opts.target.prefix ?? "";
|
|
44
|
+
if (splitByMode && splitByCollection) {
|
|
45
|
+
for (const set of doc.sets) {
|
|
46
|
+
for (const mode of set.modes) {
|
|
47
|
+
files.push({
|
|
48
|
+
path: filenameFor(opts, set, mode),
|
|
49
|
+
content: renderTailwindFile(doc.sets.filter((s) => s.name === set.name), [mode], prefix, warnings),
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else if (splitByMode) {
|
|
55
|
+
const allModes = new Set();
|
|
56
|
+
for (const set of doc.sets)
|
|
57
|
+
for (const m of set.modes)
|
|
58
|
+
allModes.add(m);
|
|
59
|
+
for (const mode of allModes) {
|
|
60
|
+
files.push({
|
|
61
|
+
path: filenameFor(opts, undefined, mode),
|
|
62
|
+
content: renderTailwindFile(doc.sets, [mode], prefix, warnings),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else if (splitByCollection) {
|
|
67
|
+
for (const set of doc.sets) {
|
|
68
|
+
files.push({
|
|
69
|
+
path: filenameFor(opts, set),
|
|
70
|
+
content: renderTailwindFile([set], set.modes, prefix, warnings),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
// Single file with everything.
|
|
76
|
+
const allModes = new Set();
|
|
77
|
+
for (const set of doc.sets)
|
|
78
|
+
for (const m of set.modes)
|
|
79
|
+
allModes.add(m);
|
|
80
|
+
files.push({
|
|
81
|
+
path: filenameFor(opts),
|
|
82
|
+
content: renderTailwindFile(doc.sets, [...allModes], prefix, warnings),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return { files, warnings };
|
|
86
|
+
}
|
|
87
|
+
function filenameFor(opts, set, mode) {
|
|
88
|
+
if (opts.target.filename)
|
|
89
|
+
return opts.target.filename;
|
|
90
|
+
const parts = [];
|
|
91
|
+
if (set)
|
|
92
|
+
parts.push(slugify(set.name));
|
|
93
|
+
if (mode)
|
|
94
|
+
parts.push(slugify(mode));
|
|
95
|
+
if (parts.length === 0)
|
|
96
|
+
parts.push("tailwind.theme");
|
|
97
|
+
return `${parts.join(".")}.css`;
|
|
98
|
+
}
|
|
99
|
+
function slugify(s) {
|
|
100
|
+
return s
|
|
101
|
+
.trim()
|
|
102
|
+
.toLowerCase()
|
|
103
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
104
|
+
.replace(/^-+|-+$/g, "");
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Render a Tailwind v4 CSS file. The "primary" mode (first in the list)
|
|
108
|
+
* goes into `@theme inline { ... }` — this is what Tailwind reads to
|
|
109
|
+
* generate utility classes. Additional modes go under standard CSS
|
|
110
|
+
* selectors (`.dark`, `[data-theme="..."]`) so the user can swap modes
|
|
111
|
+
* at runtime.
|
|
112
|
+
*/
|
|
113
|
+
function renderTailwindFile(sets, modes, prefix, warnings) {
|
|
114
|
+
const lines = [];
|
|
115
|
+
lines.push("/* Generated by figma-console-mcp — do not edit by hand */");
|
|
116
|
+
lines.push("");
|
|
117
|
+
// Decide which mode is the "primary" (lives inside @theme).
|
|
118
|
+
const primaryMode = modes.find((m) => /^(default|light|value)$/i.test(m)) ?? modes[0];
|
|
119
|
+
// @theme inline { ... } block for the primary mode.
|
|
120
|
+
lines.push("@theme inline {");
|
|
121
|
+
for (const set of sets) {
|
|
122
|
+
if (!set.modes.includes(primaryMode))
|
|
123
|
+
continue;
|
|
124
|
+
for (const token of set.tokens) {
|
|
125
|
+
const value = token.values[primaryMode];
|
|
126
|
+
if (!value)
|
|
127
|
+
continue;
|
|
128
|
+
emitTailwindTokenLines(token, value, prefix, lines, warnings);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
lines.push("}");
|
|
132
|
+
lines.push("");
|
|
133
|
+
// Mode variants under standard CSS selectors.
|
|
134
|
+
for (const mode of modes) {
|
|
135
|
+
if (mode === primaryMode)
|
|
136
|
+
continue;
|
|
137
|
+
lines.push(`${selectorFor(mode)} {`);
|
|
138
|
+
for (const set of sets) {
|
|
139
|
+
if (!set.modes.includes(mode))
|
|
140
|
+
continue;
|
|
141
|
+
for (const token of set.tokens) {
|
|
142
|
+
const value = token.values[mode];
|
|
143
|
+
if (!value)
|
|
144
|
+
continue;
|
|
145
|
+
emitTailwindTokenLines(token, value, prefix, lines, warnings);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
lines.push("}");
|
|
149
|
+
lines.push("");
|
|
150
|
+
}
|
|
151
|
+
return lines.join("\n");
|
|
152
|
+
}
|
|
153
|
+
function selectorFor(mode) {
|
|
154
|
+
const lower = mode.toLowerCase();
|
|
155
|
+
if (lower === "dark")
|
|
156
|
+
return ".dark";
|
|
157
|
+
return `[data-theme="${slugify(mode)}"]`;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Emit one or more CSS custom property declarations for a token, using
|
|
161
|
+
* Tailwind v4's namespace conventions where possible.
|
|
162
|
+
*/
|
|
163
|
+
function emitTailwindTokenLines(token, value, prefix, out, warnings) {
|
|
164
|
+
const cssName = `--${prefix}${pathToTailwindName(token.path, token.type)}`;
|
|
165
|
+
if (value.reference) {
|
|
166
|
+
// Cross-library alias: skip with a comment.
|
|
167
|
+
const bareRef = value.reference.replace(/^\{|\}$/g, "");
|
|
168
|
+
const libMatch = bareRef.match(/^__library:(.+)$/);
|
|
169
|
+
if (libMatch || bareRef === "unknown") {
|
|
170
|
+
const originalId = libMatch ? libMatch[1] : "unknown";
|
|
171
|
+
warnings.push(`Skipped ${token.path.join(".")} in Tailwind v4 — references cross-library variable ${originalId}.`);
|
|
172
|
+
out.push(` /* ${cssName}: skipped — cross-library alias to ${originalId} */`);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
// Local alias → var() reference using the same namespace mapping.
|
|
176
|
+
const refPath = bareRef.split(".");
|
|
177
|
+
const targetName = pathToTailwindName(refPath, token.type);
|
|
178
|
+
out.push(` ${cssName}: var(--${prefix}${targetName});`);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (value.literal === undefined || value.literal === null) {
|
|
182
|
+
warnings.push(`Token ${token.path.join(".")} has no value — emitting nothing.`);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
// Composite tokens: typography expands, shadow renders as CSS string.
|
|
186
|
+
if (token.type === "typography" && typeof value.literal === "object") {
|
|
187
|
+
const t = value.literal;
|
|
188
|
+
for (const subField of ["fontFamily", "fontSize", "fontWeight", "lineHeight", "letterSpacing"]) {
|
|
189
|
+
if (t[subField] !== undefined) {
|
|
190
|
+
out.push(` ${cssName}-${kebab(subField)}: ${formatValue(t[subField], token.type)};`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (token.type === "shadow" && typeof value.literal === "object") {
|
|
196
|
+
const css = renderShadow(value.literal);
|
|
197
|
+
if (css)
|
|
198
|
+
out.push(` ${cssName}: ${css};`);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
out.push(` ${cssName}: ${formatValue(value.literal, token.type)};`);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Map a token path to a Tailwind v4 CSS custom property name.
|
|
205
|
+
*
|
|
206
|
+
* Strategy:
|
|
207
|
+
* 1. If the path's leading segment matches a Tailwind namespace
|
|
208
|
+
* (color, spacing, radius, font, text, etc.), emit it as-is — that
|
|
209
|
+
* tells Tailwind to generate the corresponding utility class family.
|
|
210
|
+
* 2. Otherwise, prepend our type-based namespace heuristically.
|
|
211
|
+
* 3. Always slugify each segment for valid CSS identifiers.
|
|
212
|
+
*
|
|
213
|
+
* Examples:
|
|
214
|
+
* ["color", "primary"] → "color-primary" (bg-primary works)
|
|
215
|
+
* ["spacing", "md"] → "spacing-md" (p-md works)
|
|
216
|
+
* ["radius", "lg"] → "radius-lg" (rounded-lg works)
|
|
217
|
+
* ["text", "base", "font-size"] → "text-base-font-size"
|
|
218
|
+
* ["semantic", "bg"] (color type) → "color-semantic-bg" (heuristic prepend)
|
|
219
|
+
*/
|
|
220
|
+
function pathToTailwindName(path, type) {
|
|
221
|
+
// Tailwind v4's known namespaces (https://tailwindcss.com/docs/theme).
|
|
222
|
+
const KNOWN = new Set([
|
|
223
|
+
"color",
|
|
224
|
+
"colors",
|
|
225
|
+
"spacing",
|
|
226
|
+
"radius",
|
|
227
|
+
"rounded",
|
|
228
|
+
"shadow",
|
|
229
|
+
"font",
|
|
230
|
+
"text",
|
|
231
|
+
"font-weight",
|
|
232
|
+
"tracking",
|
|
233
|
+
"leading",
|
|
234
|
+
"blur",
|
|
235
|
+
"ease",
|
|
236
|
+
"animate",
|
|
237
|
+
"breakpoint",
|
|
238
|
+
"container",
|
|
239
|
+
"aspect",
|
|
240
|
+
]);
|
|
241
|
+
// Aliases that normalize to a canonical namespace.
|
|
242
|
+
const NORMALIZE = {
|
|
243
|
+
colors: "color",
|
|
244
|
+
rounded: "radius",
|
|
245
|
+
};
|
|
246
|
+
const slug = path.map(slugify);
|
|
247
|
+
if (slug.length === 0)
|
|
248
|
+
return "token";
|
|
249
|
+
// First-segment match: easy case, normalize plural → singular and emit.
|
|
250
|
+
if (KNOWN.has(slug[0])) {
|
|
251
|
+
const head = NORMALIZE[slug[0]] ?? slug[0];
|
|
252
|
+
return [head, ...slug.slice(1)].join("-");
|
|
253
|
+
}
|
|
254
|
+
// Determine the target Tailwind namespace from token type.
|
|
255
|
+
const ns = type === "color"
|
|
256
|
+
? "color"
|
|
257
|
+
: type === "dimension" || type === "number"
|
|
258
|
+
? "spacing"
|
|
259
|
+
: type === "fontFamily"
|
|
260
|
+
? "font"
|
|
261
|
+
: type === "fontWeight"
|
|
262
|
+
? "font-weight"
|
|
263
|
+
: null;
|
|
264
|
+
if (!ns)
|
|
265
|
+
return slug.join("-");
|
|
266
|
+
// Dedup: if the path already contains the target namespace (e.g. a path
|
|
267
|
+
// like `theme.color.header.background` for a color token would otherwise
|
|
268
|
+
// produce `color-theme-color-header-background`), strip the duplicate
|
|
269
|
+
// segment so the result reads cleanly. Match both the canonical name
|
|
270
|
+
// and any alias that normalizes to it.
|
|
271
|
+
const dupAliases = new Set([ns, ...Object.entries(NORMALIZE)
|
|
272
|
+
.filter(([, target]) => target === ns)
|
|
273
|
+
.map(([alias]) => alias)]);
|
|
274
|
+
const dedupedTail = slug.filter((s) => !dupAliases.has(s));
|
|
275
|
+
return [ns, ...dedupedTail].join("-");
|
|
276
|
+
}
|
|
277
|
+
function kebab(s) {
|
|
278
|
+
return s.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
|
|
279
|
+
}
|
|
280
|
+
function formatValue(value, type) {
|
|
281
|
+
if (typeof value === "number") {
|
|
282
|
+
if (type === "dimension")
|
|
283
|
+
return `${value}px`;
|
|
284
|
+
return String(value);
|
|
285
|
+
}
|
|
286
|
+
if (typeof value === "string") {
|
|
287
|
+
if (type === "color")
|
|
288
|
+
return value;
|
|
289
|
+
if (type === "fontFamily" || type === "string") {
|
|
290
|
+
return needsQuoting(value) ? JSON.stringify(value) : value;
|
|
291
|
+
}
|
|
292
|
+
return value;
|
|
293
|
+
}
|
|
294
|
+
if (typeof value === "boolean")
|
|
295
|
+
return String(value);
|
|
296
|
+
return JSON.stringify(value);
|
|
297
|
+
}
|
|
298
|
+
function needsQuoting(s) {
|
|
299
|
+
if (/^["']/.test(s))
|
|
300
|
+
return false;
|
|
301
|
+
if (/^[\d.]+([a-z%]+)?$/.test(s))
|
|
302
|
+
return false;
|
|
303
|
+
if (/^[a-zA-Z_][a-zA-Z0-9_-]*$/.test(s))
|
|
304
|
+
return false;
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
function renderShadow(shadow) {
|
|
308
|
+
if (Array.isArray(shadow)) {
|
|
309
|
+
return shadow.map(renderShadow).filter(Boolean).join(", ");
|
|
310
|
+
}
|
|
311
|
+
if (!shadow || typeof shadow !== "object")
|
|
312
|
+
return null;
|
|
313
|
+
const s = shadow;
|
|
314
|
+
const inset = s.inset ? "inset " : "";
|
|
315
|
+
const x = withPx(s.offsetX);
|
|
316
|
+
const y = withPx(s.offsetY);
|
|
317
|
+
const blur = withPx(s.blur);
|
|
318
|
+
const spread = s.spread !== undefined ? ` ${withPx(s.spread)}` : "";
|
|
319
|
+
const color = s.color;
|
|
320
|
+
if (!x || !y || !blur || typeof color !== "string")
|
|
321
|
+
return null;
|
|
322
|
+
return `${inset}${x} ${y} ${blur}${spread} ${color}`;
|
|
323
|
+
}
|
|
324
|
+
function withPx(v) {
|
|
325
|
+
if (typeof v === "number")
|
|
326
|
+
return `${v}px`;
|
|
327
|
+
if (typeof v === "string")
|
|
328
|
+
return v;
|
|
329
|
+
return "";
|
|
330
|
+
}
|