@apexhub/apex-ui-design-token-cli 0.0.0-stage → 1.0.0
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/LICENSE +21 -0
- package/README.md +94 -2
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +99 -0
- package/dist/commands/build.d.ts +11 -0
- package/dist/commands/build.js +118 -0
- package/dist/commands/theme.d.ts +12 -0
- package/dist/commands/theme.js +68 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +17 -0
- package/dist/lib/build-report.d.ts +44 -0
- package/dist/lib/build-report.js +111 -0
- package/dist/lib/css-value.d.ts +6 -0
- package/dist/lib/css-value.js +11 -0
- package/dist/lib/emit-cheatsheet.d.ts +13 -0
- package/dist/lib/emit-cheatsheet.js +53 -0
- package/dist/lib/emit-component-scoped.d.ts +20 -0
- package/dist/lib/emit-component-scoped.js +140 -0
- package/dist/lib/emit-css.d.ts +25 -0
- package/dist/lib/emit-css.js +95 -0
- package/dist/lib/emit-dts.d.ts +13 -0
- package/dist/lib/emit-dts.js +42 -0
- package/dist/lib/flatten.d.ts +8 -0
- package/dist/lib/flatten.js +35 -0
- package/dist/lib/merge-modes.d.ts +17 -0
- package/dist/lib/merge-modes.js +66 -0
- package/dist/lib/parse-component-path.d.ts +27 -0
- package/dist/lib/parse-component-path.js +61 -0
- package/dist/lib/parse-css-snapshot.d.ts +18 -0
- package/dist/lib/parse-css-snapshot.js +59 -0
- package/dist/lib/path-to-var.d.ts +31 -0
- package/dist/lib/path-to-var.js +56 -0
- package/dist/lib/read-tokens.d.ts +33 -0
- package/dist/lib/read-tokens.js +107 -0
- package/dist/lib/resolve-refs.d.ts +22 -0
- package/dist/lib/resolve-refs.js +232 -0
- package/dist/lib/selector-parent-map.d.ts +7 -0
- package/dist/lib/selector-parent-map.js +36 -0
- package/dist/lib/types.d.ts +84 -0
- package/dist/lib/types.js +2 -0
- package/package.json +43 -3
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { DEFAULT_NAMESPACE, pathToVar } from './path-to-var.js';
|
|
2
|
+
/**
|
|
3
|
+
* 业务方可覆盖 token 的速查表。按 setKey 分组列表。
|
|
4
|
+
*
|
|
5
|
+
* 输出格式:markdown,便于直接 `cat` 或在 IDE 预览。
|
|
6
|
+
*/
|
|
7
|
+
export function emitCheatsheet(merged, options = {}) {
|
|
8
|
+
const namespace = options.namespace ?? DEFAULT_NAMESPACE;
|
|
9
|
+
const lines = [];
|
|
10
|
+
lines.push('# Apex UI Tokens — Cheatsheet');
|
|
11
|
+
lines.push('');
|
|
12
|
+
if (options.header) {
|
|
13
|
+
lines.push(`> ${options.header}`);
|
|
14
|
+
lines.push('');
|
|
15
|
+
}
|
|
16
|
+
lines.push('业务方可覆盖以下 CSS 变量来定制主题。覆盖方式:');
|
|
17
|
+
lines.push('');
|
|
18
|
+
lines.push(`1. 在自己的 CSS 中写 \`:root { --${namespace}-xxx: ... }\``);
|
|
19
|
+
lines.push(`2. 在 inline style 中:\`style={{ "--${namespace}-xxx": "..." } as React.CSSProperties}\``);
|
|
20
|
+
lines.push('3. 使用 `data-theme="dark"` 触发暗色 Mode(若导出包含 Mode 2 及以上)');
|
|
21
|
+
lines.push('');
|
|
22
|
+
// 按 setKey 分组
|
|
23
|
+
const groups = new Map();
|
|
24
|
+
for (const t of merged.base) {
|
|
25
|
+
const arr = groups.get(t.setKey);
|
|
26
|
+
if (arr)
|
|
27
|
+
arr.push(t);
|
|
28
|
+
else
|
|
29
|
+
groups.set(t.setKey, [t]);
|
|
30
|
+
}
|
|
31
|
+
for (const [setKey, group] of groups) {
|
|
32
|
+
lines.push(`## ${setKey}`);
|
|
33
|
+
lines.push('');
|
|
34
|
+
lines.push('| CSS 变量 | 默认值 | Figma 路径 | 说明 |');
|
|
35
|
+
lines.push('|---------|--------|-----------|------|');
|
|
36
|
+
const sorted = group.slice().sort((a, b) => a.figmaPath.localeCompare(b.figmaPath));
|
|
37
|
+
for (const t of sorted) {
|
|
38
|
+
const varName = pathToVar(t.figmaPath, namespace);
|
|
39
|
+
const desc = t.description ?? '';
|
|
40
|
+
lines.push(`| \`${varName}\` | \`${t.value}\` | \`${t.figmaPath}\` | ${desc} |`);
|
|
41
|
+
}
|
|
42
|
+
lines.push('');
|
|
43
|
+
}
|
|
44
|
+
if (merged.alts.length > 0) {
|
|
45
|
+
lines.push('## Alt Modes');
|
|
46
|
+
lines.push('');
|
|
47
|
+
for (const alt of merged.alts) {
|
|
48
|
+
lines.push(`### ${alt.modeName}(${alt.tokens.length} 个 token 与 base 不同)`);
|
|
49
|
+
lines.push('');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return lines.join('\n');
|
|
53
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { MergedTokens } from './types.js';
|
|
2
|
+
interface EmitComponentScopedOptions {
|
|
3
|
+
header?: string;
|
|
4
|
+
filterSetKey?: string | string[];
|
|
5
|
+
/** class / var 前缀,默认 {@link DEFAULT_NAMESPACE}("apex") */
|
|
6
|
+
namespace?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 生成组件作用域 token CSS(嵌套 + 局部变量覆盖)。
|
|
10
|
+
*
|
|
11
|
+
* ```css
|
|
12
|
+
* .apex-button {
|
|
13
|
+
* &.apex-main-default-button {
|
|
14
|
+
* --button-text-default: var(--apex-text-neutral-primary);
|
|
15
|
+
* }
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function emitComponentScoped(merged: MergedTokens, options?: EmitComponentScopedOptions): string;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { tokenCssValue } from './css-value.js';
|
|
2
|
+
import { componentBlockClass, componentLocalVar, parseComponentPath, variantScopeClass, } from './parse-component-path.js';
|
|
3
|
+
import { DEFAULT_NAMESPACE } from './path-to-var.js';
|
|
4
|
+
/**
|
|
5
|
+
* 生成组件作用域 token CSS(嵌套 + 局部变量覆盖)。
|
|
6
|
+
*
|
|
7
|
+
* ```css
|
|
8
|
+
* .apex-button {
|
|
9
|
+
* &.apex-main-default-button {
|
|
10
|
+
* --button-text-default: var(--apex-text-neutral-primary);
|
|
11
|
+
* }
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function emitComponentScoped(merged, options = {}) {
|
|
16
|
+
const namespace = options.namespace ?? DEFAULT_NAMESPACE;
|
|
17
|
+
const filter = normaliseFilter(options.filterSetKey);
|
|
18
|
+
const filterFn = (t) => filter ? filter.has(t.setKey) : t.setKey === 'component' || t.setKey === 'component usage';
|
|
19
|
+
const buckets = groupTokens(merged.base.filter(filterFn));
|
|
20
|
+
const lines = [];
|
|
21
|
+
lines.push('/* ==========================================================================');
|
|
22
|
+
lines.push(' Apex UI Component Tokens — Auto-generated (scoped)');
|
|
23
|
+
if (options.header)
|
|
24
|
+
lines.push(` ${options.header}`);
|
|
25
|
+
lines.push(' DO NOT EDIT MANUALLY. Run `apex-ui theme` / `apex-ui build` to regenerate.');
|
|
26
|
+
lines.push(' ========================================================================== */');
|
|
27
|
+
lines.push('');
|
|
28
|
+
const componentNames = [...buckets.keys()].sort();
|
|
29
|
+
for (const component of componentNames) {
|
|
30
|
+
const bucket = buckets.get(component);
|
|
31
|
+
emitComponentBlock(lines, component, bucket, namespace);
|
|
32
|
+
lines.push('');
|
|
33
|
+
}
|
|
34
|
+
// alt modes:每个 mode 一块,内部结构同 base
|
|
35
|
+
for (const alt of merged.alts) {
|
|
36
|
+
const altBuckets = groupTokens(alt.tokens.filter(filterFn));
|
|
37
|
+
if (altBuckets.size === 0)
|
|
38
|
+
continue;
|
|
39
|
+
const selector = altSelector(alt.modeName);
|
|
40
|
+
lines.push(`${selector} {`);
|
|
41
|
+
for (const component of [...altBuckets.keys()].sort()) {
|
|
42
|
+
emitComponentBlock(lines, component, altBuckets.get(component), namespace, ' ');
|
|
43
|
+
}
|
|
44
|
+
lines.push('}');
|
|
45
|
+
lines.push('');
|
|
46
|
+
}
|
|
47
|
+
return lines.join('\n').replace(/\n{3,}/g, '\n\n');
|
|
48
|
+
}
|
|
49
|
+
function normaliseFilter(filter) {
|
|
50
|
+
if (!filter)
|
|
51
|
+
return undefined;
|
|
52
|
+
if (Array.isArray(filter))
|
|
53
|
+
return new Set(filter);
|
|
54
|
+
return new Set([filter]);
|
|
55
|
+
}
|
|
56
|
+
function groupTokens(tokens) {
|
|
57
|
+
const buckets = new Map();
|
|
58
|
+
const unparsed = [];
|
|
59
|
+
for (const t of tokens) {
|
|
60
|
+
const parsed = parseComponentPath(t.figmaPath, t.setKey);
|
|
61
|
+
if (!parsed) {
|
|
62
|
+
unparsed.push(t);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const bucket = getBucket(buckets, parsed.component);
|
|
66
|
+
// component usage:有变体(尺寸/形态)进变体 scope;无变体(container/content 结构特例)进 root
|
|
67
|
+
if (t.setKey === 'component usage') {
|
|
68
|
+
if (parsed.variant) {
|
|
69
|
+
const vb = bucket.variants.get(parsed.variant) ?? { tokens: [] };
|
|
70
|
+
vb.tokens.push(t);
|
|
71
|
+
bucket.variants.set(parsed.variant, vb);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
bucket.root.push(t);
|
|
75
|
+
}
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (parsed.variant) {
|
|
79
|
+
const vb = bucket.variants.get(parsed.variant) ?? { tokens: [] };
|
|
80
|
+
vb.tokens.push(t);
|
|
81
|
+
bucket.variants.set(parsed.variant, vb);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
bucket.root.push(t);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (unparsed.length > 0) {
|
|
88
|
+
process.stderr.write(`[apex-ui] warn: ${unparsed.length} 个 component token 无法解析为作用域结构,已跳过\n`);
|
|
89
|
+
}
|
|
90
|
+
return buckets;
|
|
91
|
+
}
|
|
92
|
+
function getBucket(buckets, component) {
|
|
93
|
+
let b = buckets.get(component);
|
|
94
|
+
if (!b) {
|
|
95
|
+
b = { root: [], variants: new Map() };
|
|
96
|
+
buckets.set(component, b);
|
|
97
|
+
}
|
|
98
|
+
return b;
|
|
99
|
+
}
|
|
100
|
+
function emitComponentBlock(lines, component, bucket, namespace, indent = '') {
|
|
101
|
+
const blockClass = componentBlockClass(component, namespace);
|
|
102
|
+
lines.push(`${indent}.${blockClass} {`);
|
|
103
|
+
for (const t of bucket.root) {
|
|
104
|
+
const parsed = parseComponentPath(t.figmaPath, t.setKey);
|
|
105
|
+
if (!parsed)
|
|
106
|
+
continue;
|
|
107
|
+
lines.push(`${indent} ${componentLocalVar(parsed.component, parsed.localName)}: ${tokenCssValue(t, namespace)};`);
|
|
108
|
+
}
|
|
109
|
+
const variantNames = [...bucket.variants.keys()].sort();
|
|
110
|
+
for (const variant of variantNames) {
|
|
111
|
+
const { tokens } = bucket.variants.get(variant);
|
|
112
|
+
const scopeClass = variantScopeClass(component, variant, namespace);
|
|
113
|
+
lines.push(`${indent} &.${scopeClass} {`);
|
|
114
|
+
emitVariantTokens(lines, component, tokens, namespace, `${indent} `);
|
|
115
|
+
lines.push(`${indent} }`);
|
|
116
|
+
}
|
|
117
|
+
lines.push(`${indent}}`);
|
|
118
|
+
}
|
|
119
|
+
function emitVariantTokens(lines, component, tokens, namespace, indent) {
|
|
120
|
+
const sorted = [...tokens].sort((a, b) => {
|
|
121
|
+
const pa = parseComponentPath(a.figmaPath, a.setKey);
|
|
122
|
+
const pb = parseComponentPath(b.figmaPath, b.setKey);
|
|
123
|
+
return (pa?.localName ?? a.figmaPath).localeCompare(pb?.localName ?? b.figmaPath);
|
|
124
|
+
});
|
|
125
|
+
for (const t of sorted) {
|
|
126
|
+
const parsed = parseComponentPath(t.figmaPath, t.setKey);
|
|
127
|
+
if (!parsed)
|
|
128
|
+
continue;
|
|
129
|
+
lines.push(`${indent}${componentLocalVar(component, parsed.localName)}: ${tokenCssValue(t, namespace)};`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function altSelector(modeName) {
|
|
133
|
+
if (/dark|night|mode\s*2/i.test(modeName))
|
|
134
|
+
return '[data-theme="dark"]';
|
|
135
|
+
const slug = modeName
|
|
136
|
+
.toLowerCase()
|
|
137
|
+
.replace(/\s+/g, '-')
|
|
138
|
+
.replace(/[^a-z0-9-]/g, '');
|
|
139
|
+
return `[data-theme="${slug}"]`;
|
|
140
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { MergedTokens } from './types.js';
|
|
2
|
+
/** 全局 token 默认作用域:普通文档用 :root,Shadow DOM / Wujie 子应用用 :host。 */
|
|
3
|
+
export declare const DEFAULT_GLOBAL_TOKEN_SCOPE = ":root, :host";
|
|
4
|
+
interface EmitCssOptions {
|
|
5
|
+
/** 主 Mode 的 CSS 选择器,默认 {@link DEFAULT_GLOBAL_TOKEN_SCOPE} */
|
|
6
|
+
scope: string;
|
|
7
|
+
/** 暗色等 alt Mode 的选择器(兼容 Tokens Studio 单一 mode 名)。 */
|
|
8
|
+
darkSelector: string;
|
|
9
|
+
/** 输出顶部注释里写的时间戳/来源(便于排查) */
|
|
10
|
+
header?: string;
|
|
11
|
+
/** 仅输出指定 setKey 的 token(build 命令按层分文件时用) */
|
|
12
|
+
filterSetKey?: string | string[];
|
|
13
|
+
/** CSS 变量前缀,默认 {@link DEFAULT_NAMESPACE}("apex") */
|
|
14
|
+
namespace?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 把合并后的 token 渲染为一段 CSS 文本。
|
|
18
|
+
*
|
|
19
|
+
* - base → `--scope { ... }`
|
|
20
|
+
* - 每个 alt → `--darkSelector` 或 `[data-theme="<mode-slug>"] { ... }`
|
|
21
|
+
*
|
|
22
|
+
* 输出格式追求"git diff 友好":按 setKey / 路径分组排序,组间空行。
|
|
23
|
+
*/
|
|
24
|
+
export declare function emitCss(merged: MergedTokens, options: EmitCssOptions): string;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { tokenCssValue } from './css-value.js';
|
|
2
|
+
import { DEFAULT_NAMESPACE, pathToVar } from './path-to-var.js';
|
|
3
|
+
/** 全局 token 默认作用域:普通文档用 :root,Shadow DOM / Wujie 子应用用 :host。 */
|
|
4
|
+
export const DEFAULT_GLOBAL_TOKEN_SCOPE = ':root, :host';
|
|
5
|
+
/**
|
|
6
|
+
* 把合并后的 token 渲染为一段 CSS 文本。
|
|
7
|
+
*
|
|
8
|
+
* - base → `--scope { ... }`
|
|
9
|
+
* - 每个 alt → `--darkSelector` 或 `[data-theme="<mode-slug>"] { ... }`
|
|
10
|
+
*
|
|
11
|
+
* 输出格式追求"git diff 友好":按 setKey / 路径分组排序,组间空行。
|
|
12
|
+
*/
|
|
13
|
+
export function emitCss(merged, options) {
|
|
14
|
+
const namespace = options.namespace ?? DEFAULT_NAMESPACE;
|
|
15
|
+
const filter = normaliseFilter(options.filterSetKey);
|
|
16
|
+
const filterFn = (t) => (filter ? filter.has(t.setKey) : true);
|
|
17
|
+
const lines = [];
|
|
18
|
+
lines.push('/* ==========================================================================');
|
|
19
|
+
lines.push(' Apex UI Design Tokens — Auto-generated');
|
|
20
|
+
if (options.header)
|
|
21
|
+
lines.push(` ${options.header}`);
|
|
22
|
+
lines.push(' DO NOT EDIT MANUALLY. Run `apex-ui theme` / `apex-ui build` to regenerate.');
|
|
23
|
+
lines.push(' ========================================================================== */');
|
|
24
|
+
lines.push('');
|
|
25
|
+
const baseTokens = merged.base.filter(filterFn);
|
|
26
|
+
if (baseTokens.length > 0) {
|
|
27
|
+
lines.push(`${options.scope} {`);
|
|
28
|
+
appendTokenBlock(lines, baseTokens, namespace);
|
|
29
|
+
lines.push('}');
|
|
30
|
+
lines.push('');
|
|
31
|
+
}
|
|
32
|
+
for (const alt of merged.alts) {
|
|
33
|
+
const altTokens = alt.tokens.filter(filterFn);
|
|
34
|
+
if (altTokens.length === 0)
|
|
35
|
+
continue;
|
|
36
|
+
const selector = altSelector(alt.modeName, options.darkSelector);
|
|
37
|
+
lines.push(`${selector} {`);
|
|
38
|
+
appendTokenBlock(lines, altTokens, namespace);
|
|
39
|
+
lines.push('}');
|
|
40
|
+
lines.push('');
|
|
41
|
+
}
|
|
42
|
+
return lines.join('\n');
|
|
43
|
+
}
|
|
44
|
+
function normaliseFilter(filter) {
|
|
45
|
+
if (!filter)
|
|
46
|
+
return undefined;
|
|
47
|
+
if (Array.isArray(filter))
|
|
48
|
+
return new Set(filter);
|
|
49
|
+
return new Set([filter]);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Tokens Studio 单 Mode 配置 + 我们「暗色专用 selector」的兼容:
|
|
53
|
+
* - 只有 1 个 alt:用 darkSelector
|
|
54
|
+
* - 多 alt:用 [data-theme="<mode-slug>"]
|
|
55
|
+
*/
|
|
56
|
+
function altSelector(modeName, darkSelector) {
|
|
57
|
+
// 模糊匹配 "Mode 2" / "Dark" 这种暗色约定
|
|
58
|
+
if (/dark|night|mode\s*2/i.test(modeName))
|
|
59
|
+
return darkSelector;
|
|
60
|
+
const slug = modeName
|
|
61
|
+
.toLowerCase()
|
|
62
|
+
.replace(/\s+/g, '-')
|
|
63
|
+
.replace(/[^a-z0-9-]/g, '');
|
|
64
|
+
return `[data-theme="${slug}"]`;
|
|
65
|
+
}
|
|
66
|
+
function appendTokenBlock(lines, tokens, namespace) {
|
|
67
|
+
// 按 setKey 分组、组内按 figmaPath 字典序排
|
|
68
|
+
const groups = new Map();
|
|
69
|
+
for (const t of tokens) {
|
|
70
|
+
const existing = groups.get(t.setKey);
|
|
71
|
+
if (existing)
|
|
72
|
+
existing.push(t);
|
|
73
|
+
else
|
|
74
|
+
groups.set(t.setKey, [t]);
|
|
75
|
+
}
|
|
76
|
+
let first = true;
|
|
77
|
+
for (const [setKey, group] of groups) {
|
|
78
|
+
if (!first)
|
|
79
|
+
lines.push('');
|
|
80
|
+
first = false;
|
|
81
|
+
lines.push(` /* ── ${setKey} ${separator(setKey.length)} */`);
|
|
82
|
+
group.sort((a, b) => a.figmaPath.localeCompare(b.figmaPath));
|
|
83
|
+
for (const t of group) {
|
|
84
|
+
const varName = pathToVar(t.figmaPath, namespace);
|
|
85
|
+
if (t.description)
|
|
86
|
+
lines.push(` /* ${t.description} */`);
|
|
87
|
+
lines.push(` ${varName}: ${tokenCssValue(t, namespace)};`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function separator(used) {
|
|
92
|
+
const total = 60;
|
|
93
|
+
const len = Math.max(3, total - used);
|
|
94
|
+
return '─'.repeat(len);
|
|
95
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { MergedTokens } from './types.js';
|
|
2
|
+
interface EmitDtsOptions {
|
|
3
|
+
header?: string;
|
|
4
|
+
/** CSS 变量前缀,默认 {@link DEFAULT_NAMESPACE}("apex") */
|
|
5
|
+
namespace?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* 生成 csstype 模块扩展,让业务方在 `<div style={{ '--apex-...': ... }}>` 时获得补全。
|
|
9
|
+
*
|
|
10
|
+
* 业务方 tsconfig 的 `include` 覆盖到该文件即可,无需手动 import。
|
|
11
|
+
*/
|
|
12
|
+
export declare function emitDts(merged: MergedTokens, options?: EmitDtsOptions): string;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { DEFAULT_NAMESPACE, pathToVar } from './path-to-var.js';
|
|
2
|
+
/**
|
|
3
|
+
* 生成 csstype 模块扩展,让业务方在 `<div style={{ '--apex-...': ... }}>` 时获得补全。
|
|
4
|
+
*
|
|
5
|
+
* 业务方 tsconfig 的 `include` 覆盖到该文件即可,无需手动 import。
|
|
6
|
+
*/
|
|
7
|
+
export function emitDts(merged, options = {}) {
|
|
8
|
+
const namespace = options.namespace ?? DEFAULT_NAMESPACE;
|
|
9
|
+
// 汇总所有出现过的 token(base + alts),按变量名去重
|
|
10
|
+
const seen = new Map();
|
|
11
|
+
for (const t of merged.base)
|
|
12
|
+
seen.set(pathToVar(t.figmaPath, namespace), t);
|
|
13
|
+
for (const alt of merged.alts) {
|
|
14
|
+
for (const t of alt.tokens) {
|
|
15
|
+
const varName = pathToVar(t.figmaPath, namespace);
|
|
16
|
+
if (!seen.has(varName))
|
|
17
|
+
seen.set(varName, t);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const sorted = [...seen.entries()].sort(([a], [b]) => a.localeCompare(b));
|
|
21
|
+
const lines = [];
|
|
22
|
+
lines.push('// AUTO-GENERATED. DO NOT EDIT.');
|
|
23
|
+
if (options.header)
|
|
24
|
+
lines.push(`// ${options.header}`);
|
|
25
|
+
lines.push('// 业务方覆盖 Apex UI token 时使用 — 提供 inline style 自动补全。');
|
|
26
|
+
lines.push('');
|
|
27
|
+
lines.push("import 'csstype'");
|
|
28
|
+
lines.push('');
|
|
29
|
+
lines.push("declare module 'csstype' {");
|
|
30
|
+
lines.push(' interface Properties {');
|
|
31
|
+
for (const [varName, token] of sorted) {
|
|
32
|
+
lines.push(` /** Figma: ${token.figmaPath}${token.description ? ` — ${token.description}` : ''} */`);
|
|
33
|
+
lines.push(` '${varName}'?: string`);
|
|
34
|
+
}
|
|
35
|
+
lines.push(' }');
|
|
36
|
+
lines.push('}');
|
|
37
|
+
lines.push('');
|
|
38
|
+
// 确保是模块文件
|
|
39
|
+
lines.push('export {}');
|
|
40
|
+
lines.push('');
|
|
41
|
+
return lines.join('\n');
|
|
42
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RawFlatToken, TokenSet } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* 把一个 set 的 tree 扁平化为 RawFlatToken 列表。
|
|
4
|
+
* 路径以 Figma 内部约定的 "." 连接(与引用语法 `{a.b.c}` 对齐)。
|
|
5
|
+
*
|
|
6
|
+
* @param prefix 仅在递归时使用
|
|
7
|
+
*/
|
|
8
|
+
export declare function flattenSet(set: TokenSet, prefix?: string): RawFlatToken[];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** 判断节点是否是叶子(含 $value 即叶子)。 */
|
|
2
|
+
function isLeaf(node) {
|
|
3
|
+
return typeof node === 'object' && node !== null && '$value' in node;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* 把一个 set 的 tree 扁平化为 RawFlatToken 列表。
|
|
7
|
+
* 路径以 Figma 内部约定的 "." 连接(与引用语法 `{a.b.c}` 对齐)。
|
|
8
|
+
*
|
|
9
|
+
* @param prefix 仅在递归时使用
|
|
10
|
+
*/
|
|
11
|
+
export function flattenSet(set, prefix = '') {
|
|
12
|
+
return flattenTree(set.content, prefix, set.setKey, set.modeName);
|
|
13
|
+
}
|
|
14
|
+
function flattenTree(tree, prefix, setKey, modeName) {
|
|
15
|
+
const out = [];
|
|
16
|
+
for (const [key, value] of Object.entries(tree)) {
|
|
17
|
+
if (key.startsWith('$'))
|
|
18
|
+
continue;
|
|
19
|
+
const figmaPath = prefix ? `${prefix}.${key}` : key;
|
|
20
|
+
if (isLeaf(value)) {
|
|
21
|
+
out.push({
|
|
22
|
+
figmaPath,
|
|
23
|
+
rawValue: value.$value,
|
|
24
|
+
type: value.$type,
|
|
25
|
+
description: value.$description,
|
|
26
|
+
setKey,
|
|
27
|
+
modeName,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
else if (typeof value === 'object' && value !== null) {
|
|
31
|
+
out.push(...flattenTree(value, figmaPath, setKey, modeName));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type FlatToken, type MergedTokens } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* 规范化 mode 顺序:light / Mode 1 作为 base,dark / Mode 2 作为 alt。
|
|
4
|
+
* 保证 `:root` 始终是亮色,`[data-theme="dark"]` 是暗色覆盖。
|
|
5
|
+
*/
|
|
6
|
+
export declare function orderModeNames(modeNames: string[]): string[];
|
|
7
|
+
/**
|
|
8
|
+
* 按 Mode 把扁平 token 列表分组、计算差异,得到 base + alts 结构。
|
|
9
|
+
*
|
|
10
|
+
* 策略:
|
|
11
|
+
* 1. DEFAULT_MODE(无 mode 标记)的 token 视为对所有 mode 都生效
|
|
12
|
+
* 2. modeNames[0] 作为 base mode(通常是 light / Mode 1)
|
|
13
|
+
* 3. 其余 mode 仅记录"与同 path 的 base 值有差异"的 token(落到 alts)
|
|
14
|
+
*
|
|
15
|
+
* 当 modeNames 为空(即所有 set 都没有多 Mode)时,所有 token 全部塞进 base,alts 为空。
|
|
16
|
+
*/
|
|
17
|
+
export declare function mergeModes(tokens: FlatToken[], modeNames: string[]): MergedTokens;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { DEFAULT_MODE } from './types.js';
|
|
2
|
+
const LIGHT_MODE = /^(light|mode\s*1)$/i;
|
|
3
|
+
const DARK_MODE = /^(dark|night|mode\s*2)$/i;
|
|
4
|
+
/**
|
|
5
|
+
* 规范化 mode 顺序:light / Mode 1 作为 base,dark / Mode 2 作为 alt。
|
|
6
|
+
* 保证 `:root` 始终是亮色,`[data-theme="dark"]` 是暗色覆盖。
|
|
7
|
+
*/
|
|
8
|
+
export function orderModeNames(modeNames) {
|
|
9
|
+
const seen = new Set();
|
|
10
|
+
const light = [];
|
|
11
|
+
const other = [];
|
|
12
|
+
const dark = [];
|
|
13
|
+
for (const name of modeNames) {
|
|
14
|
+
if (seen.has(name))
|
|
15
|
+
continue;
|
|
16
|
+
seen.add(name);
|
|
17
|
+
if (LIGHT_MODE.test(name))
|
|
18
|
+
light.push(name);
|
|
19
|
+
else if (DARK_MODE.test(name))
|
|
20
|
+
dark.push(name);
|
|
21
|
+
else
|
|
22
|
+
other.push(name);
|
|
23
|
+
}
|
|
24
|
+
return [...light, ...other, ...dark];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 按 Mode 把扁平 token 列表分组、计算差异,得到 base + alts 结构。
|
|
28
|
+
*
|
|
29
|
+
* 策略:
|
|
30
|
+
* 1. DEFAULT_MODE(无 mode 标记)的 token 视为对所有 mode 都生效
|
|
31
|
+
* 2. modeNames[0] 作为 base mode(通常是 light / Mode 1)
|
|
32
|
+
* 3. 其余 mode 仅记录"与同 path 的 base 值有差异"的 token(落到 alts)
|
|
33
|
+
*
|
|
34
|
+
* 当 modeNames 为空(即所有 set 都没有多 Mode)时,所有 token 全部塞进 base,alts 为空。
|
|
35
|
+
*/
|
|
36
|
+
export function mergeModes(tokens, modeNames) {
|
|
37
|
+
const ordered = orderModeNames(modeNames);
|
|
38
|
+
// 没有具体 mode → 全 base
|
|
39
|
+
if (ordered.length === 0) {
|
|
40
|
+
return { base: tokens.slice(), alts: [] };
|
|
41
|
+
}
|
|
42
|
+
const baseMode = ordered[0];
|
|
43
|
+
const altModes = ordered.slice(1);
|
|
44
|
+
const defaultModeTokens = tokens.filter((t) => t.modeName === DEFAULT_MODE);
|
|
45
|
+
const baseModeTokens = tokens.filter((t) => t.modeName === baseMode);
|
|
46
|
+
const base = [...defaultModeTokens, ...baseModeTokens];
|
|
47
|
+
// 建 base path → token,方便 alt 比对
|
|
48
|
+
const basePathMap = new Map();
|
|
49
|
+
for (const t of base)
|
|
50
|
+
basePathMap.set(t.figmaPath, t);
|
|
51
|
+
const alts = [];
|
|
52
|
+
for (const mode of altModes) {
|
|
53
|
+
const modeTokens = tokens.filter((t) => t.modeName === mode);
|
|
54
|
+
const diff = [];
|
|
55
|
+
for (const t of modeTokens) {
|
|
56
|
+
const baseTok = basePathMap.get(t.figmaPath);
|
|
57
|
+
if (!baseTok || baseTok.value !== t.value) {
|
|
58
|
+
diff.push(t);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (diff.length > 0) {
|
|
62
|
+
alts.push({ modeName: mode, tokens: diff });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return { base, alts };
|
|
66
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 把 component / component usage 的 figmaPath 解析为「组件块 + 变体 + 局部变量名」。
|
|
3
|
+
*
|
|
4
|
+
* Color(component,四层):组件大类 / 组件变体 / 结构 / 状态
|
|
5
|
+
* button.main-default.fill.default → { component: button, variant: main-default, localName: fill-default }
|
|
6
|
+
* dropdown.default.fill-container.default → { component: dropdown, variant: default, localName: fill-container-default }
|
|
7
|
+
*
|
|
8
|
+
* Number(component usage,三层):组件大类 / 尺寸或形态变体 / 具体结构
|
|
9
|
+
* input.sm.height → { component: input, variant: sm, localName: height }
|
|
10
|
+
* button.main-md.gap → { component: button, variant: main-md, localName: gap }
|
|
11
|
+
*
|
|
12
|
+
* 结构特例(usage 第二段为 container|content):无变体 class,localName 含结构前缀
|
|
13
|
+
* menu1.container.padding-x → { component: menu1, localName: container-padding-x }
|
|
14
|
+
*/
|
|
15
|
+
export interface ParsedComponentPath {
|
|
16
|
+
component: string;
|
|
17
|
+
variant?: string;
|
|
18
|
+
/** 局部变量后缀,完整名为 --{component}-{localName} */
|
|
19
|
+
localName: string;
|
|
20
|
+
}
|
|
21
|
+
export declare function parseComponentPath(figmaPath: string, setKey: string): ParsedComponentPath | null;
|
|
22
|
+
/** 组件块 class:button → apex-button(默认 namespace) */
|
|
23
|
+
export declare function componentBlockClass(component: string, namespace?: string): string;
|
|
24
|
+
/** 变体 class:main-default + button → apex-main-default-button(默认 namespace) */
|
|
25
|
+
export declare function variantScopeClass(component: string, variant: string, namespace?: string): string;
|
|
26
|
+
/** 局部 CSS 变量名:button + fill-default → --button-fill-default */
|
|
27
|
+
export declare function componentLocalVar(component: string, localName: string): string;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { DEFAULT_NAMESPACE, joinTokenSegments, tokenPathSegment } from './path-to-var.js';
|
|
2
|
+
export function parseComponentPath(figmaPath, setKey) {
|
|
3
|
+
const parts = figmaPath.split('.').filter(Boolean);
|
|
4
|
+
if (parts.length === 0)
|
|
5
|
+
return null;
|
|
6
|
+
const component = tokenPathSegment(parts[0]);
|
|
7
|
+
if (setKey === 'component') {
|
|
8
|
+
if (parts.length >= 4) {
|
|
9
|
+
return {
|
|
10
|
+
component,
|
|
11
|
+
variant: tokenPathSegment(parts[1]),
|
|
12
|
+
localName: joinTokenSegments(parts[2], parts[3]),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
if (parts.length === 1) {
|
|
16
|
+
return { component, localName: 'value' };
|
|
17
|
+
}
|
|
18
|
+
if (parts.length === 2) {
|
|
19
|
+
return { component, localName: joinTokenSegments(parts[1]) };
|
|
20
|
+
}
|
|
21
|
+
if (parts.length === 3) {
|
|
22
|
+
return {
|
|
23
|
+
component,
|
|
24
|
+
variant: tokenPathSegment(parts[1]),
|
|
25
|
+
localName: joinTokenSegments(parts[2]),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
if (setKey === 'component usage') {
|
|
31
|
+
// menu1.container.padding-x → --menu1-container-padding-x(无变体 class;layout/menu1/menu2 仍用此形态)
|
|
32
|
+
const structural = new Set(['container', 'content']);
|
|
33
|
+
if (parts.length === 3 && structural.has(parts[1])) {
|
|
34
|
+
return { component, localName: joinTokenSegments(parts[1], parts[2]) };
|
|
35
|
+
}
|
|
36
|
+
if (parts.length >= 3) {
|
|
37
|
+
return {
|
|
38
|
+
component,
|
|
39
|
+
variant: tokenPathSegment(parts[1]),
|
|
40
|
+
localName: joinTokenSegments(...parts.slice(2)),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (parts.length === 2) {
|
|
44
|
+
return { component, localName: joinTokenSegments(parts[1]) };
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
/** 组件块 class:button → apex-button(默认 namespace) */
|
|
51
|
+
export function componentBlockClass(component, namespace = DEFAULT_NAMESPACE) {
|
|
52
|
+
return `${namespace}-${component}`;
|
|
53
|
+
}
|
|
54
|
+
/** 变体 class:main-default + button → apex-main-default-button(默认 namespace) */
|
|
55
|
+
export function variantScopeClass(component, variant, namespace = DEFAULT_NAMESPACE) {
|
|
56
|
+
return `${namespace}-${variant}-${component}`;
|
|
57
|
+
}
|
|
58
|
+
/** 局部 CSS 变量名:button + fill-default → --button-fill-default */
|
|
59
|
+
export function componentLocalVar(component, localName) {
|
|
60
|
+
return `--${component}-${localName}`;
|
|
61
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse generated theme CSS into comparable snapshots (for build --report).
|
|
3
|
+
*/
|
|
4
|
+
/** Flat custom properties from :root (primitives / aliases). */
|
|
5
|
+
export type RootVarMap = Record<string, string>;
|
|
6
|
+
/** Per selector (e.g. apex-button, apex-main-button) → local --vars. */
|
|
7
|
+
export type ComponentBlockMap = Record<string, Record<string, string>>;
|
|
8
|
+
/** Exported for selector-parent-map (single source of truth). */
|
|
9
|
+
export declare const BLOCK_OPEN_RE: RegExp;
|
|
10
|
+
export declare const NESTED_OPEN_RE: RegExp;
|
|
11
|
+
/**
|
|
12
|
+
* Extract all `--name: value` declarations (primitives / aliases are flat under :root).
|
|
13
|
+
*/
|
|
14
|
+
export declare function parseRootLayerCss(css: string): RootVarMap;
|
|
15
|
+
/**
|
|
16
|
+
* Parse component-tokens.css nested blocks.
|
|
17
|
+
*/
|
|
18
|
+
export declare function parseComponentScopedCss(css: string): ComponentBlockMap;
|