soma-core 1.4.0 → 1.4.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/README.md +1 -1
- package/core/build/blocks/block-compile.ts +31 -2
- package/core/build/blocks/block-container.ts +2 -11
- package/core/build/compiler/page-declaration-compiler.ts +30 -2
- package/core/build/compiler/soma-precompile.js +2041 -19
- package/core/build/component/component-graph.ts +208 -0
- package/core/build/eject/component-eject.ts +473 -0
- package/core/build/eject/component-upgrade.ts +359 -0
- package/core/build/layout/constraint-solver.ts +235 -0
- package/core/build/layout/css-emitter.ts +116 -0
- package/core/build/layout/page-layout.ts +85 -0
- package/core/build/pack/pack-init.ts +265 -0
- package/core/client/base/SomaPageRootBase.ts +61 -0
- package/core/client/component/ai-catalog.ts +79 -4
- package/core/client/component/component-defaults.ts +100 -0
- package/core/client/component/component-definition.types.ts +39 -0
- package/core/client/component/component-manager.ts +79 -4
- package/core/client/component/component-pack.ts +126 -2
- package/core/client/component/keyboard-matrix.ts +144 -0
- package/core/client/component/pack-styles.ts +59 -1
- package/core/client/component/style-features.ts +131 -0
- package/core/client/component/theme-component-tokens.ts +83 -0
- package/core/client/data/data-object-resolver.ts +407 -4
- package/core/client/data/reaction-engine.ts +176 -0
- package/core/client/data/window-engine.ts +477 -0
- package/core/client/engine/event-engine.ts +98 -0
- package/core/client/engine/soma-bootstrap.ts +265 -5
- package/core/client/engine/step-runner.ts +134 -0
- package/core/client/parser/blocks/page-parser.ts +15 -1
- package/core/client/parser/blocks/use-parser.ts +5 -12
- package/core/client/parser/data-config-parser.ts +316 -8
- package/core/client/parser/duration.ts +16 -0
- package/core/client/parser/layout-parser.ts +157 -0
- package/core/client/parser/page-state-parser.ts +166 -0
- package/core/client/parser/pipeline-parser.ts +76 -1
- package/core/client/parser/reaction-parser.ts +459 -0
- package/core/client/parser/reaction-sugar.ts +47 -0
- package/core/client/parser/step-parser.ts +359 -0
- package/core/client/parser/ui-config-parser.ts +59 -0
- package/core/client/parser/window-parser.ts +294 -0
- package/core/client/theme/theme-engine.ts +102 -2
- package/core/index.ts +163 -0
- package/core/shared/block-errors.ts +11 -0
- package/core/shared/calc/coerce.ts +29 -0
- package/core/shared/calc/errors.ts +43 -0
- package/core/shared/calc/evaluator.ts +101 -0
- package/core/shared/calc/index.ts +12 -0
- package/core/shared/calc/lexer.ts +194 -0
- package/core/shared/calc/parser.ts +204 -0
- package/core/shared/calc/typer.ts +184 -0
- package/core/shared/path-algebra.ts +169 -0
- package/core/shared/predicates/index.ts +527 -0
- package/core/shared/types/component-contract.types.ts +128 -2
- package/core/shared/types/data-config.types.ts +120 -1
- package/core/shared/types/domain.types.ts +13 -0
- package/core/shared/types/event.types.ts +22 -2
- package/core/shared/types/layout-constraint.types.ts +73 -0
- package/core/shared/types/page-state.types.ts +60 -0
- package/core/shared/types/reaction.types.ts +144 -0
- package/core/shared/types/step.types.ts +91 -0
- package/core/shared/types/ui-config.types.ts +18 -0
- package/core/shared/types/window.types.ts +177 -0
- package/dist/cli/soma.js +13 -1
- package/dist/core/index.cjs +4684 -349
- package/dist/types/build/blocks/block-container.d.ts +1 -6
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -0
- package/dist/types/build/component/component-graph.d.ts +85 -0
- package/dist/types/build/eject/component-eject.d.ts +121 -0
- package/dist/types/build/eject/component-upgrade.d.ts +43 -0
- package/dist/types/build/layout/constraint-solver.d.ts +36 -0
- package/dist/types/build/layout/css-emitter.d.ts +18 -0
- package/dist/types/build/layout/page-layout.d.ts +24 -0
- package/dist/types/build/pack/pack-init.d.ts +8 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +28 -0
- package/dist/types/client/component/ai-catalog.d.ts +24 -1
- package/dist/types/client/component/component-defaults.d.ts +15 -0
- package/dist/types/client/component/component-definition.types.d.ts +38 -0
- package/dist/types/client/component/component-manager.d.ts +36 -0
- package/dist/types/client/component/component-pack.d.ts +32 -2
- package/dist/types/client/component/keyboard-matrix.d.ts +24 -0
- package/dist/types/client/component/pack-styles.d.ts +21 -0
- package/dist/types/client/component/style-features.d.ts +26 -0
- package/dist/types/client/component/theme-component-tokens.d.ts +14 -0
- package/dist/types/client/data/data-object-resolver.d.ts +106 -1
- package/dist/types/client/data/reaction-engine.d.ts +21 -0
- package/dist/types/client/data/window-engine.d.ts +27 -0
- package/dist/types/client/engine/event-engine.d.ts +15 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +70 -0
- package/dist/types/client/engine/step-runner.d.ts +24 -0
- package/dist/types/client/parser/blocks/use-parser.d.ts +2 -2
- package/dist/types/client/parser/duration.d.ts +1 -0
- package/dist/types/client/parser/layout-parser.d.ts +9 -0
- package/dist/types/client/parser/page-state-parser.d.ts +32 -0
- package/dist/types/client/parser/reaction-parser.d.ts +16 -0
- package/dist/types/client/parser/reaction-sugar.d.ts +13 -0
- package/dist/types/client/parser/step-parser.d.ts +23 -0
- package/dist/types/client/parser/ui-config-parser.d.ts +5 -0
- package/dist/types/client/parser/window-parser.d.ts +25 -0
- package/dist/types/client/theme/theme-engine.d.ts +26 -0
- package/dist/types/index.d.ts +32 -2
- package/dist/types/shared/block-errors.d.ts +5 -0
- package/dist/types/shared/calc/coerce.d.ts +4 -0
- package/dist/types/shared/calc/errors.d.ts +14 -0
- package/dist/types/shared/calc/evaluator.d.ts +2 -0
- package/dist/types/shared/calc/index.d.ts +6 -0
- package/dist/types/shared/calc/lexer.d.ts +20 -0
- package/dist/types/shared/calc/parser.d.ts +49 -0
- package/dist/types/shared/calc/typer.d.ts +16 -0
- package/dist/types/shared/path-algebra.d.ts +56 -0
- package/dist/types/shared/predicates/index.d.ts +79 -0
- package/dist/types/shared/types/component-contract.types.d.ts +112 -0
- package/dist/types/shared/types/data-config.types.d.ts +115 -1
- package/dist/types/shared/types/domain.types.d.ts +12 -0
- package/dist/types/shared/types/event.types.d.ts +8 -2
- package/dist/types/shared/types/layout-constraint.types.d.ts +52 -0
- package/dist/types/shared/types/page-state.types.d.ts +49 -0
- package/dist/types/shared/types/reaction.types.d.ts +115 -0
- package/dist/types/shared/types/step.types.d.ts +76 -0
- package/dist/types/shared/types/ui-config.types.d.ts +18 -0
- package/dist/types/shared/types/window.types.d.ts +171 -0
- package/package.json +2 -1
|
@@ -15,10 +15,28 @@
|
|
|
15
15
|
// 这样热更新重复执行也不会堆出十几张同样的 <style>)。
|
|
16
16
|
|
|
17
17
|
import type { SomaComponentDefinition } from './component-definition.types';
|
|
18
|
+
import type { SomaComponentContract } from '../../shared/types/component-contract.types';
|
|
19
|
+
import { componentTokenPrefix, componentTokenVar } from '../../shared/types/component-contract.types';
|
|
18
20
|
import type { ComposedComponentPacks } from './component-pack';
|
|
19
21
|
|
|
20
22
|
const injected = new Set<string>();
|
|
21
23
|
|
|
24
|
+
/**
|
|
25
|
+
* 组件令牌规则(M-1):把契约声明的可调旋钮写成一条挂在组件根类上的规则。
|
|
26
|
+
*
|
|
27
|
+
* 为什么是规则不是内联样式:一个组件一条,与实例数无关;
|
|
28
|
+
* 且 `ThemeScope` 或主题 `components` 段写出的 `--soma-<comp>-*` 层级更高,
|
|
29
|
+
* 靠 CSS 级联天然覆盖,不需要引擎逐实例改写。
|
|
30
|
+
*/
|
|
31
|
+
export function renderComponentTokenRule(contract: SomaComponentContract): string {
|
|
32
|
+
const tokens = contract.tokens;
|
|
33
|
+
if (!tokens || Object.keys(tokens).length === 0) return '';
|
|
34
|
+
const decls = Object.entries(tokens)
|
|
35
|
+
.map(([key, token]) => `${componentTokenVar(contract.name, key)}:${token.default};`)
|
|
36
|
+
.join('');
|
|
37
|
+
return `.soma-${componentTokenPrefix(contract.name)}{${decls}}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
22
40
|
export interface PackStyleOptions {
|
|
23
41
|
/** CSP nonce;宿主启用严格 CSP 时必须给 */
|
|
24
42
|
nonce?: string | null;
|
|
@@ -29,6 +47,14 @@ export function resetPackStyleInjection(): void {
|
|
|
29
47
|
injected.clear();
|
|
30
48
|
}
|
|
31
49
|
|
|
50
|
+
/**
|
|
51
|
+
* 幂等注入一段 CSS(组件分片、特性分片与页面级布局产物共用同一个入口)。
|
|
52
|
+
* 同一个 key 只落一张 <style>;热更新重复执行不会堆出十几张同样的样式表。
|
|
53
|
+
*/
|
|
54
|
+
export function injectStyleOnce(key: string, css: string, options: PackStyleOptions): void {
|
|
55
|
+
injectOnce(key, css, options);
|
|
56
|
+
}
|
|
57
|
+
|
|
32
58
|
function injectOnce(key: string, css: string, options: PackStyleOptions): void {
|
|
33
59
|
if (!css.trim() || injected.has(key)) return;
|
|
34
60
|
if (typeof document === 'undefined') return;
|
|
@@ -56,10 +82,19 @@ export function attachPackStyles(
|
|
|
56
82
|
const { components, styles } = composed;
|
|
57
83
|
const result: Record<string, SomaComponentDefinition> = {};
|
|
58
84
|
|
|
85
|
+
const patchesByTarget = new Map<string, Array<{ patchedBy: string; css: string }>>();
|
|
86
|
+
for (const patch of composed.patches ?? []) {
|
|
87
|
+
const list = patchesByTarget.get(patch.target) ?? [];
|
|
88
|
+
list.push({ patchedBy: patch.patchedBy, css: patch.css });
|
|
89
|
+
patchesByTarget.set(patch.target, list);
|
|
90
|
+
}
|
|
91
|
+
|
|
59
92
|
for (const [name, definition] of Object.entries(components) as Array<[string, SomaComponentDefinition]>) {
|
|
60
93
|
const own = styles.components[name];
|
|
61
94
|
const deps = styles.deps[name] ?? [];
|
|
62
|
-
|
|
95
|
+
const tokenRule = renderComponentTokenRule(definition.contract);
|
|
96
|
+
const patches = patchesByTarget.get(name) ?? [];
|
|
97
|
+
if (!own && deps.length === 0 && !tokenRule && patches.length === 0) {
|
|
63
98
|
result[name] = definition;
|
|
64
99
|
continue;
|
|
65
100
|
}
|
|
@@ -71,7 +106,15 @@ export function attachPackStyles(
|
|
|
71
106
|
const shared = styles.shared[dep];
|
|
72
107
|
if (shared) injectOnce(`shared:${dep}`, shared, options);
|
|
73
108
|
}
|
|
109
|
+
// 令牌规则必须排在组件分片**之前**:分片里的声明要能覆盖旋钮的缺省值,
|
|
110
|
+
// 反过来注入会让组件自己的特化失效。
|
|
111
|
+
if (tokenRule) injectOnce(`tokens:${name}`, tokenRule, options);
|
|
74
112
|
if (own) injectOnce(`component:${name}`, own, options);
|
|
113
|
+
// 补丁最后:它的全部意义就是覆盖官方分片(M-4)。
|
|
114
|
+
// 特性分片在这两者之间,由 component-manager 按 prop 注入。
|
|
115
|
+
for (const patch of patches) {
|
|
116
|
+
injectOnce(`patch:${patch.patchedBy}→${name}`, patch.css, options);
|
|
117
|
+
}
|
|
75
118
|
return inner ? inner(ctx) : {};
|
|
76
119
|
},
|
|
77
120
|
};
|
|
@@ -92,3 +135,18 @@ export function ensurePackBaseStyles(
|
|
|
92
135
|
injectOnce(`base:${index}`, css, options);
|
|
93
136
|
});
|
|
94
137
|
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 注入一片特性分片(M-3)。由 component-manager 在 prop 首次为真值时调用。
|
|
141
|
+
*
|
|
142
|
+
* key 用 `feature:<组件>#<特性>`,与 `component:<组件>` 同一个 injected 集合,
|
|
143
|
+
* 所以「注入一次」的保证是同一套,不需要组件自己记账。
|
|
144
|
+
*/
|
|
145
|
+
export function injectStyleFeature(
|
|
146
|
+
componentName: string,
|
|
147
|
+
featureName: string,
|
|
148
|
+
css: string,
|
|
149
|
+
options: PackStyleOptions = {},
|
|
150
|
+
): void {
|
|
151
|
+
injectOnce(`feature:${componentName}#${featureName}`, css, options);
|
|
152
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// 特性分片的装配(REQ-009 M-3)。
|
|
2
|
+
//
|
|
3
|
+
// 「按需装配」此前只细到组件一档:组件一实例化就付它全部 CSS。
|
|
4
|
+
// 这一档在 DataTable 上不够用——核心分片贴着 7,600 上限,而排队的表格特性有五个。
|
|
5
|
+
// 抬上限只装得下一个;正确的杠杆是再细一档:**用到这个特性的页面才为它付钱**。
|
|
6
|
+
//
|
|
7
|
+
// 判定刻意做得很窄:`when` 只能指向契约里的 **prop**,不能指向控制器的派生态。
|
|
8
|
+
// 派生态要等实例跑起来才知道,而"要不要注入 CSS"必须在渲染第一帧之前定下来——
|
|
9
|
+
// 否则用户会看到一帧无样式的内容。
|
|
10
|
+
//
|
|
11
|
+
// 两种开关形态:boolean/array 的真值门控,以及 enum 的按值门控
|
|
12
|
+
// (`{ prop, equals }`)。后者是施工中补的:首批两个消费者都卡在同一处——
|
|
13
|
+
// 真正决定"要不要这段样式"的是一个枚举值而不是布尔开关,只有布尔一种写法时
|
|
14
|
+
// 它们只能退而按一个恒为真的数组 prop 开门,于是所有页面都付这笔钱,机制等于白建。
|
|
15
|
+
|
|
16
|
+
import type {
|
|
17
|
+
SomaComponentDefinition,
|
|
18
|
+
SomaStyleFeatureWhen,
|
|
19
|
+
} from './component-definition.types';
|
|
20
|
+
import { injectStyleFeature, type PackStyleOptions } from './pack-styles';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 真值门控:`true`、非空数组,或**非空字符串**。`0` / `''` / `[]` 一律不算启用。
|
|
24
|
+
*
|
|
25
|
+
* 字符串这一路是后补的:`detail_api` / `submit_api` 这类"声明了接口就要这段样式"的
|
|
26
|
+
* prop 是自治组件里最常见的开关形态——给了路径就意味着这段界面会出现。
|
|
27
|
+
* 不认它的话,这类特性只能退回"所有页面都付这笔钱",机制等于白建
|
|
28
|
+
* (与枚举门控当初的处境完全一样)。
|
|
29
|
+
*/
|
|
30
|
+
export function isFeatureActive(value: unknown): boolean {
|
|
31
|
+
if (Array.isArray(value)) return value.length > 0;
|
|
32
|
+
if (typeof value === 'string') return value.trim() !== '';
|
|
33
|
+
return value === true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 开关名(两种形态共用的取 prop 名) */
|
|
37
|
+
export function featurePropOf(when: SomaStyleFeatureWhen): string {
|
|
38
|
+
return typeof when === 'string' ? when : when.prop;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 按当前 props 判定某个特性是否启用 */
|
|
42
|
+
export function isFeatureEnabled(when: SomaStyleFeatureWhen, props: Record<string, unknown>): boolean {
|
|
43
|
+
if (typeof when === 'string') return isFeatureActive(props[when]);
|
|
44
|
+
const value = props[when.prop];
|
|
45
|
+
// 逐值比较用 Object.is 而不是 ==:枚举值可能是数字,'2' 与 2 不该互相命中
|
|
46
|
+
return when.equals.some((candidate) => Object.is(candidate, value));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 装配期校验:`when` 必须指向契约里真实存在的 prop,且形态要对得上——
|
|
51
|
+
* 真值门控只认 boolean / array,按值门控只认 enum 且取值必须在枚举内。
|
|
52
|
+
* 返回问题清单(空数组 = 通过),由 bootstrap 汇总后抛。
|
|
53
|
+
*/
|
|
54
|
+
export function validateStyleFeatures(definition: SomaComponentDefinition): string[] {
|
|
55
|
+
const features = definition.styleFeatures;
|
|
56
|
+
if (!features) return [];
|
|
57
|
+
const issues: string[] = [];
|
|
58
|
+
const props = definition.contract.props ?? {};
|
|
59
|
+
const componentName = definition.contract.name;
|
|
60
|
+
|
|
61
|
+
for (const [featureName, feature] of Object.entries(features)) {
|
|
62
|
+
if (!feature || typeof feature.css !== 'string' || !feature.css.trim()) {
|
|
63
|
+
issues.push(`"${componentName}" 的特性分片 "${featureName}" 缺少 css`);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
const propName = featurePropOf(feature.when);
|
|
67
|
+
const prop = props[propName];
|
|
68
|
+
if (!prop) {
|
|
69
|
+
issues.push(
|
|
70
|
+
`"${componentName}" 的特性分片 "${featureName}" 的 when 指向 "${propName}",`
|
|
71
|
+
+ '但契约里没有这个 prop(when 只能指向契约 prop,不能指向控制器派生态——'
|
|
72
|
+
+ '派生态要等实例跑起来才知道,而"要不要注入 CSS"必须在第一帧之前定下来)',
|
|
73
|
+
);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (typeof feature.when === 'string') {
|
|
78
|
+
if (prop.type !== 'boolean' && prop.type !== 'array' && prop.type !== 'string') {
|
|
79
|
+
issues.push(
|
|
80
|
+
`"${componentName}" 的特性分片 "${featureName}" 的 when 指向 "${propName}",`
|
|
81
|
+
+ `但它是 ${prop.type} 类型——真值开关只能是 boolean / array / string`
|
|
82
|
+
+ '(string 按"非空即启用"判,用于 detail_api 这类"声明了就要这段样式"的口子)。'
|
|
83
|
+
+ `若想按取值开门,写成 { prop: '${propName}', equals: [...] }`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (prop.type !== 'enum') {
|
|
90
|
+
issues.push(
|
|
91
|
+
`"${componentName}" 的特性分片 "${featureName}" 用了按值门控,`
|
|
92
|
+
+ `但 "${propName}" 是 ${prop.type} 类型——{ prop, equals } 只对 enum 有意义`,
|
|
93
|
+
);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (!Array.isArray(feature.when.equals) || feature.when.equals.length === 0) {
|
|
97
|
+
issues.push(`"${componentName}" 的特性分片 "${featureName}" 的 equals 必须是非空数组`);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
// 取值不在枚举内 = 这个特性永远不会被注入。它不报错、只是"设了没用",
|
|
101
|
+
// 所以必须在装配期拦下——运行期没有任何迹象能让人发现它。
|
|
102
|
+
const allowed = new Set<unknown>(prop.values ?? []);
|
|
103
|
+
const stray = feature.when.equals.filter((value) => !allowed.has(value));
|
|
104
|
+
if (allowed.size > 0 && stray.length > 0) {
|
|
105
|
+
issues.push(
|
|
106
|
+
`"${componentName}" 的特性分片 "${featureName}" 的 equals 含枚举外取值 `
|
|
107
|
+
+ `${stray.map((v) => JSON.stringify(v)).join(' / ')}——`
|
|
108
|
+
+ `"${propName}" 的合法取值是 [${(prop.values ?? []).join(' | ')}]。`
|
|
109
|
+
+ '取不到的值等于这段样式永远不注入,而运行期没有任何迹象能发现它',
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return issues;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 按当前 props 注入该启用的特性分片。实例创建与每次 setProps 后调用。
|
|
118
|
+
* 注入是幂等的(pack-styles 的 injected 集合负责),所以这里不用自己记账。
|
|
119
|
+
*/
|
|
120
|
+
export function ensureStyleFeatures(
|
|
121
|
+
definition: SomaComponentDefinition,
|
|
122
|
+
props: Record<string, unknown>,
|
|
123
|
+
options: PackStyleOptions = {},
|
|
124
|
+
): void {
|
|
125
|
+
const features = definition.styleFeatures;
|
|
126
|
+
if (!features) return;
|
|
127
|
+
for (const [featureName, feature] of Object.entries(features)) {
|
|
128
|
+
if (!isFeatureEnabled(feature.when, props)) continue;
|
|
129
|
+
injectStyleFeature(definition.contract.name, featureName, feature.css, options);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// 主题输入里的组件令牌 × 契约声明的对账(REQ-009 M-1)。
|
|
2
|
+
//
|
|
3
|
+
// 分工的理由:主题引擎不认识组件(它只认令牌名,且刻意不可插拔),
|
|
4
|
+
// 而契约住在组件包里。两者相遇的唯一地点是装配层——也就是这里。
|
|
5
|
+
//
|
|
6
|
+
// 为什么必须对账而不是随便写:`components: { Buton: { radius: … } }` 这样一个拼写错误,
|
|
7
|
+
// 在没有对账时的表现是「改了没反应」——变量确实写进了 :root,只是没有任何组件读它。
|
|
8
|
+
// 这类错误的排查成本远高于报错。
|
|
9
|
+
|
|
10
|
+
import type { SomaComponentDefinition } from './component-definition.types';
|
|
11
|
+
import { COMPONENT_TOKEN_KEY_PATTERN } from '../../shared/types/component-contract.types';
|
|
12
|
+
|
|
13
|
+
export class SomaThemeComponentTokenError extends Error {
|
|
14
|
+
constructor(message: string) {
|
|
15
|
+
super(`[SomaThemeComponentTokens] ${message}`);
|
|
16
|
+
this.name = 'SomaThemeComponentTokenError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 校验主题输入的 `components` / `darkComponents` 段是否与契约对得上。
|
|
22
|
+
* Host 在调 `applyTheme` 之前调用;不给组件表时(纯预览场景)跳过。
|
|
23
|
+
*/
|
|
24
|
+
export function validateThemeComponentTokens(
|
|
25
|
+
input: {
|
|
26
|
+
components?: Record<string, Record<string, string>>;
|
|
27
|
+
darkComponents?: Record<string, Record<string, string>>;
|
|
28
|
+
},
|
|
29
|
+
components: Record<string, SomaComponentDefinition>,
|
|
30
|
+
): void {
|
|
31
|
+
const issues: string[] = [];
|
|
32
|
+
for (const segment of [input.components, input.darkComponents]) {
|
|
33
|
+
if (!segment) continue;
|
|
34
|
+
for (const [componentName, tokens] of Object.entries(segment)) {
|
|
35
|
+
const definition = components[componentName];
|
|
36
|
+
if (!definition) {
|
|
37
|
+
issues.push(`未知组件 "${componentName}"`);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const declared = definition.contract.tokens ?? {};
|
|
41
|
+
for (const key of Object.keys(tokens ?? {})) {
|
|
42
|
+
if (!(key in declared)) {
|
|
43
|
+
const available = Object.keys(declared);
|
|
44
|
+
issues.push(
|
|
45
|
+
`"${componentName}" 没有声明可调令牌 "${key}"`
|
|
46
|
+
+ (available.length > 0
|
|
47
|
+
? `(它声明的是:${available.join(' / ')})`
|
|
48
|
+
: '(它一个可调令牌都没声明——组件令牌是组件自己的承诺,'
|
|
49
|
+
+ '不能由外面凭空指定)'),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (issues.length > 0) {
|
|
56
|
+
throw new SomaThemeComponentTokenError(
|
|
57
|
+
`主题的组件令牌段有 ${issues.length} 处问题:\n${issues.map((i) => ` · ${i}`).join('\n')}`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 契约自检:令牌键名格式 + default 必须引用语义令牌。返回问题清单 */
|
|
63
|
+
export function validateContractTokens(definition: SomaComponentDefinition): string[] {
|
|
64
|
+
const tokens = definition.contract.tokens;
|
|
65
|
+
if (!tokens) return [];
|
|
66
|
+
const issues: string[] = [];
|
|
67
|
+
const name = definition.contract.name;
|
|
68
|
+
for (const [key, token] of Object.entries(tokens)) {
|
|
69
|
+
if (!COMPONENT_TOKEN_KEY_PATTERN.test(key)) {
|
|
70
|
+
issues.push(`"${name}" 的令牌键 "${key}" 必须是 kebab-case`);
|
|
71
|
+
}
|
|
72
|
+
if (!token || typeof token.default !== 'string' || !token.default.includes('var(--soma-')) {
|
|
73
|
+
issues.push(
|
|
74
|
+
`"${name}.${key}" 的 default 必须引用语义令牌(形如 var(--soma-…))——`
|
|
75
|
+
+ '组件令牌是语义令牌的别名与收窄,不是新的真源',
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
if (!token?.desc?.trim()) {
|
|
79
|
+
issues.push(`"${name}.${key}" 缺少 desc(可调令牌要告诉使用者它调的是什么)`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return issues;
|
|
83
|
+
}
|