soma-core 1.3.3 → 1.3.5
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 +7 -6
- package/app/SomaPageClient.tsx +2 -72
- package/app/soma/page/route.ts +17 -2
- package/bin/soma-lsp.cjs +1 -4
- package/core/build/compiler/full-manifest-builder.ts +17 -26
- package/core/build/compiler/html-include-expander.ts +56 -0
- package/core/build/compiler/page-assets.ts +26 -0
- package/core/build/compiler/page-declaration-compiler.ts +85 -0
- package/core/build/compiler/rollup-plugin-soma.ts +4 -19
- package/core/build/compiler/route-builder.ts +0 -2
- package/core/build/compiler/rspack-plugin-soma.js +1 -10
- package/core/build/compiler/soma-manifest-webpack-plugin.js +54 -13
- package/core/build/compiler/soma-precompile.js +74 -0
- package/core/build/compiler/soma-precompile.ts +13 -0
- package/core/build/compiler/soma-rspack-yaml-loader.js +5 -11
- package/core/build/compiler/soma-webpack-loader.js +6 -21
- package/core/build/compiler/vite-plugin-soma.ts +9 -31
- package/core/build/component/pack-from-dir.ts +58 -0
- package/core/build/component/package-loader.ts +158 -12
- package/core/build/eject/plan-eject.ts +15 -21
- package/core/build/lsp/cross-file-diagnostics.cjs +82 -113
- package/core/build/lsp/extra-completion.cjs +14 -8
- package/core/build/lsp/lsp-navigation.cjs +50 -43
- package/core/build/lsp/soma-declaration.cjs +85 -0
- package/core/build/lsp/ui-yaml-completion.cjs +98 -104
- package/core/build/v2/block-compile.ts +200 -0
- package/core/build/v2/block-loader.ts +33 -0
- package/core/build/v2/html-index.ts +120 -0
- package/core/build/v2/migrate-v1.ts +370 -0
- package/core/build/v2/soma-file-loader.ts +216 -0
- package/core/client/component/ai-catalog.ts +15 -0
- package/core/client/component/component-pack.ts +205 -31
- package/core/client/component/contract-compat.ts +132 -0
- package/core/client/component/example-v2.ts +132 -0
- package/core/client/engine/dom-renderer.ts +14 -8
- package/core/client/engine/reactive-proxy.ts +8 -7
- package/core/client/engine/soma-bootstrap.ts +23 -5
- package/core/client/parser/v2/namespace.ts +182 -0
- package/core/client/parser/v2/page-parser.ts +359 -0
- package/core/client/parser/v2/use-parser.ts +412 -0
- package/core/client/parser/value-lang.ts +340 -0
- package/core/client/pipes/pipe-registry.ts +1 -1
- package/core/client/registry/declaration-renderer.ts +17 -15
- package/core/client/registry/page-lifecycle.ts +1 -1
- package/core/client/security/attribute-presence.ts +16 -1
- package/core/client/theme/harmony.ts +307 -0
- package/core/client/theme/scene-tokens.ts +134 -0
- package/core/client/theme/theme-axes.ts +228 -0
- package/core/client/theme/theme-engine.ts +333 -73
- package/core/client/theme/tone-solver.ts +118 -0
- package/core/index.ts +74 -2
- package/core/server/host/published-page-assets.ts +35 -0
- package/core/server/host/server-page-assets.ts +33 -16
- package/core/server/host/soma-page-compile.ts +33 -0
- package/core/server/index.ts +15 -0
- package/core/server/registry/declaration-precompiler.ts +24 -54
- package/core/server/registry/page-registry-store.ts +39 -10
- package/core/server/registry/services/pages/Get.ts +19 -9
- package/core/server/registry/validation/compile-gate.ts +14 -31
- package/core/server/registry/validation/cross-check.ts +7 -17
- package/core/server/registry/validation/declaration-safety-scan.ts +2 -2
- package/core/shared/types/component-contract.types.ts +10 -0
- package/dist/core/index.cjs +2144 -155
- package/dist/types/build/compiler/full-manifest-builder.d.ts +4 -4
- package/dist/types/build/compiler/html-include-expander.d.ts +19 -0
- package/dist/types/build/compiler/page-assets.d.ts +3 -0
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +33 -0
- package/dist/types/build/compiler/rollup-plugin-soma.d.ts +0 -3
- package/dist/types/build/compiler/route-builder.d.ts +0 -1
- package/dist/types/build/compiler/soma-precompile.d.ts +8 -0
- package/dist/types/build/compiler/vite-plugin-soma.d.ts +2 -10
- package/dist/types/build/component/pack-from-dir.d.ts +17 -0
- package/dist/types/build/component/package-loader.d.ts +33 -2
- package/dist/types/build/v2/block-compile.d.ts +28 -0
- package/dist/types/build/v2/block-loader.d.ts +9 -0
- package/dist/types/build/v2/html-index.d.ts +37 -0
- package/dist/types/build/v2/migrate-v1.d.ts +32 -0
- package/dist/types/build/v2/soma-file-loader.d.ts +45 -0
- package/dist/types/client/component/component-pack.d.ts +23 -3
- package/dist/types/client/component/contract-compat.d.ts +16 -0
- package/dist/types/client/component/example-v2.d.ts +9 -0
- package/dist/types/client/engine/dom-renderer.d.ts +2 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +3 -1
- package/dist/types/client/parser/v2/namespace.d.ts +52 -0
- package/dist/types/client/parser/v2/page-parser.d.ts +21 -0
- package/dist/types/client/parser/v2/use-parser.d.ts +72 -0
- package/dist/types/client/parser/value-lang.d.ts +63 -0
- package/dist/types/client/registry/declaration-renderer.d.ts +1 -2
- package/dist/types/client/theme/harmony.d.ts +77 -0
- package/dist/types/client/theme/scene-tokens.d.ts +25 -0
- package/dist/types/client/theme/theme-axes.d.ts +81 -0
- package/dist/types/client/theme/theme-engine.d.ts +29 -1
- package/dist/types/client/theme/tone-solver.d.ts +47 -0
- package/dist/types/index.d.ts +13 -2
- package/dist/types/server/host/published-page-assets.d.ts +19 -0
- package/dist/types/server/host/server-page-assets.d.ts +1 -4
- package/dist/types/server/host/soma-page-compile.d.ts +7 -0
- package/dist/types/server/index.d.ts +3 -1
- package/dist/types/server/registry/declaration-precompiler.d.ts +3 -3
- package/dist/types/server/registry/page-registry-store.d.ts +5 -5
- package/dist/types/server/registry/validation/compile-gate.d.ts +2 -0
- package/dist/types/server/registry/validation/cross-check.d.ts +4 -4
- package/dist/types/shared/types/component-contract.types.d.ts +10 -0
- package/editor/vscode/package.json +1 -24
- package/global.d.ts +1 -11
- package/next.config.js +32 -50
- package/package.json +3 -2
- package/tailwind.config.ts +0 -1
- package/tsconfig.json +20 -7
- package/core/build/lsp/event-yaml-completion.cjs +0 -106
- package/core/client/host-bridge/empty-host/data/README.md +0 -17
- package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +0 -30
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// 色调求解(REQ-007 S2):明度是**解出来的**,不是写死的。
|
|
2
|
+
//
|
|
3
|
+
// 现状引擎里同一段 WCAG 循环出现了五次(主色、info、secondary/accent/neutral 的
|
|
4
|
+
// ensureBrandTone、successFg、warningFg),每次都是"不达标就推明度、最多 16 轮"。
|
|
5
|
+
// 本模块把那个循环提炼成一个原语 `pushLightnessUntil`,让五处共用一份实现——
|
|
6
|
+
// 五份拷贝里改对四份是这类代码的经典事故。
|
|
7
|
+
//
|
|
8
|
+
// 更重要的是方向反转:现状是"先写死明度、不达标再兜底修正",
|
|
9
|
+
// 本模块提供"给定目标对比度、正向解出明度"(Adobe Leonardo 的路线)。
|
|
10
|
+
// 这让 contrast 轴的三档成为**目标对比度的三组值**,而不是三组写死的明度常量。
|
|
11
|
+
//
|
|
12
|
+
// 纯函数模块:零全局、零缓存(memo 归编排者,沿用既有 onSoftMemo 的位置)。
|
|
13
|
+
|
|
14
|
+
import type { SomaAxisParams, SomaContrastTargets } from './theme-axes';
|
|
15
|
+
|
|
16
|
+
export interface Oklch { l: number; c: number; h: number }
|
|
17
|
+
|
|
18
|
+
/** 求解需要的色彩原语由编排者注入——本模块不 import theme-engine,避免循环依赖 */
|
|
19
|
+
export interface ToneSolverDeps {
|
|
20
|
+
oklchToHex(c: Oklch): string;
|
|
21
|
+
hexToOklch(hex: string): Oklch;
|
|
22
|
+
contrastRatio(a: string, b: string): number;
|
|
23
|
+
bestFg(bg: string): string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 与现状五处循环一致的上限:推 16 轮仍不达标就交出当前最优 */
|
|
27
|
+
export const MAX_SOLVE_ITER = 16;
|
|
28
|
+
/** 与现状五处循环一致的步长 */
|
|
29
|
+
const STEP = 0.04;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 沿明度方向推色直到谓词满足。
|
|
33
|
+
*
|
|
34
|
+
* **幂等**:谓词一开始就满足时原值返回——这条性质让"S3 推过色相后回跑 S2"
|
|
35
|
+
* 成为无脑安全的操作(未被推动的语气原样穿过,不会因为多跑一次而漂移)。
|
|
36
|
+
*/
|
|
37
|
+
export function pushLightnessUntil(
|
|
38
|
+
color: Oklch,
|
|
39
|
+
satisfied: (candidate: Oklch) => boolean,
|
|
40
|
+
direction: 'lighten' | 'darken',
|
|
41
|
+
maxIter: number = MAX_SOLVE_ITER,
|
|
42
|
+
): Oklch {
|
|
43
|
+
let current = color;
|
|
44
|
+
const delta = direction === 'lighten' ? STEP : -STEP;
|
|
45
|
+
for (let i = 0; i < maxIter && !satisfied(current); i += 1) {
|
|
46
|
+
current = { ...current, l: current.l + delta };
|
|
47
|
+
}
|
|
48
|
+
return current;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 现状五处循环的等价封装:不达 AA 就推明度,推的方向按当前明暗自适应。
|
|
53
|
+
*
|
|
54
|
+
* 保留"方向按 l > 0.55 判定"这个既有行为——它不是随手写的:
|
|
55
|
+
* 亮色推更亮、暗色推更暗,才不会把一个中亮度的色推穿到对面去。
|
|
56
|
+
*/
|
|
57
|
+
export function ensureContrast(
|
|
58
|
+
bg: string,
|
|
59
|
+
deps: ToneSolverDeps,
|
|
60
|
+
minRatio = 4.5,
|
|
61
|
+
maxIter = MAX_SOLVE_ITER,
|
|
62
|
+
forceDirection?: 'lighten' | 'darken',
|
|
63
|
+
): { bg: string; fg: string } {
|
|
64
|
+
let current = bg;
|
|
65
|
+
let fg = deps.bestFg(current);
|
|
66
|
+
for (let i = 0; i < maxIter && deps.contrastRatio(current, fg) < minRatio; i += 1) {
|
|
67
|
+
const cur = deps.hexToOklch(current);
|
|
68
|
+
// 缺省的方向启发(亮色推更亮、暗色推更暗)在**分离场景下必须被覆盖**:
|
|
69
|
+
// 分离闸把一个语气推离品牌明度之后,若求解器按自己的启发把它又拉回去,
|
|
70
|
+
// 两个约束就在打架,结果是闸"推了但没推动"(terminalHacker 的绿品牌
|
|
71
|
+
// 实测停在 ΔL=0.105,差 0.015 达不到阈值)。有分离意图时方向由闸说了算。
|
|
72
|
+
const lighten = forceDirection ? forceDirection === 'lighten' : cur.l > 0.55;
|
|
73
|
+
current = deps.oklchToHex({ ...cur, l: lighten ? cur.l + STEP : cur.l - STEP });
|
|
74
|
+
fg = deps.bestFg(current);
|
|
75
|
+
}
|
|
76
|
+
return { bg: current, fg };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 解一个语气的实心底:命中 toneOnPage 目标。
|
|
81
|
+
*
|
|
82
|
+
* 前景(-fg)仍由既有 ④ 系代码解——那部分是实测校准过的 AA 保障,不在此重复。
|
|
83
|
+
*
|
|
84
|
+
* **裁决**:粉彩带约束与对比度目标冲突时**对比度赢**(AA 底线不随 contrast 档
|
|
85
|
+
* 移动,是《主题属性设计》§3 原则 4 的承诺)。带被突破时通过返回值旁路上报,
|
|
86
|
+
* 由编排者收进 meta,供观测——不报错,因为"带"是气质偏好不是硬约束。
|
|
87
|
+
*/
|
|
88
|
+
export function solveTone(
|
|
89
|
+
seed: Oklch,
|
|
90
|
+
pageHex: string,
|
|
91
|
+
target: SomaContrastTargets,
|
|
92
|
+
band: readonly [number, number] | null,
|
|
93
|
+
deps: ToneSolverDeps,
|
|
94
|
+
): { tone: Oklch; bandBroken: boolean } {
|
|
95
|
+
const meets = (candidate: Oklch): boolean =>
|
|
96
|
+
deps.contrastRatio(deps.oklchToHex(candidate), pageHex) >= target.toneOnPage;
|
|
97
|
+
|
|
98
|
+
// 带存在时先把种子夹进带内,再朝"离页面底更远"的方向推到达标
|
|
99
|
+
let start = seed;
|
|
100
|
+
let bandBroken = false;
|
|
101
|
+
if (band) {
|
|
102
|
+
start = { ...seed, l: Math.min(band[1], Math.max(band[0], seed.l)) };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const pageL = deps.hexToOklch(pageHex).l;
|
|
106
|
+
const direction = start.l > pageL ? 'lighten' : 'darken';
|
|
107
|
+
const solved = pushLightnessUntil(start, meets, direction);
|
|
108
|
+
|
|
109
|
+
if (band && (solved.l < band[0] - 1e-6 || solved.l > band[1] + 1e-6)) {
|
|
110
|
+
bandBroken = true;
|
|
111
|
+
}
|
|
112
|
+
return { tone: solved, bandBroken };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 彩度个性:把 chroma 轴的乘数应用到一个种子彩度上(下限护栏防止推成死灰) */
|
|
116
|
+
export function applyChroma(baseChroma: number, params: SomaAxisParams): number {
|
|
117
|
+
return Math.max(0.02, baseChroma * params.chromaMul);
|
|
118
|
+
}
|
package/core/index.ts
CHANGED
|
@@ -64,7 +64,14 @@ export {
|
|
|
64
64
|
contrastRatio,
|
|
65
65
|
THEME_SWITCHING_CLASS,
|
|
66
66
|
} from './client/theme/theme-engine';
|
|
67
|
-
export type { SomaThemeInput, SomaDerivedTheme } from './client/theme/theme-engine';
|
|
67
|
+
export type { SomaThemeInput, SomaDerivedTheme, SomaThemeMeta } from './client/theme/theme-engine';
|
|
68
|
+
export { themeRootAttributes } from './client/theme/theme-engine';
|
|
69
|
+
// 主题轴(REQ-007):宿主与 soma-ui 的装配层要按名构造轴位,类型必须在包根可达
|
|
70
|
+
export type {
|
|
71
|
+
SomaPaletteAxes, SomaSceneAxes, SomaResolvedAxes,
|
|
72
|
+
SomaHarmony, SomaSemantics, SomaChroma, SomaContrast,
|
|
73
|
+
SomaField, SomaIntensity, SomaMaterial,
|
|
74
|
+
} from './client/theme/theme-axes';
|
|
68
75
|
|
|
69
76
|
// ── 中文排版基线(F-05 / 契约 C2)──
|
|
70
77
|
// soma-ui 基座引用而不复制:复制即第二份事实。
|
|
@@ -231,7 +238,7 @@ export {
|
|
|
231
238
|
} from './client/registry/index';
|
|
232
239
|
|
|
233
240
|
// 组件包(业务拓展组件库):合成、查重、目录、自检。
|
|
234
|
-
// 接入指南见
|
|
241
|
+
// 接入指南见 external-docs/soma-组件包接入指南.md。
|
|
235
242
|
export {
|
|
236
243
|
composeComponentPacks,
|
|
237
244
|
generatePackCatalog,
|
|
@@ -241,8 +248,73 @@ export {
|
|
|
241
248
|
type ComposePacksOptions,
|
|
242
249
|
type SomaComponentPack,
|
|
243
250
|
type SomaComponentPackStyles,
|
|
251
|
+
type PackOverrideEvent,
|
|
244
252
|
} from './client/component/component-pack';
|
|
245
253
|
export { verifyComponentPack, type VerifyPackOptions } from './client/component/verify-pack';
|
|
254
|
+
|
|
255
|
+
// 值语言(DSL v2 · REQ-005 F4):算术六词元 + 管道链的唯一值规则。
|
|
256
|
+
// 规格见 doc/需求/REQ-005-DSL体验强化/UI业务组件DSL规格书.md §7;
|
|
257
|
+
// v2 其余部件未施工前,本模块可被 v1 路径独立复用(纯函数、无依赖)。
|
|
258
|
+
export {
|
|
259
|
+
parseValueSegment,
|
|
260
|
+
evaluateValueNode,
|
|
261
|
+
isArithSegment,
|
|
262
|
+
isPlainPath,
|
|
263
|
+
describeNode,
|
|
264
|
+
SomaValueLangError,
|
|
265
|
+
type ValueNode,
|
|
266
|
+
type PipedValue,
|
|
267
|
+
type ValuePosition,
|
|
268
|
+
type HasVarFn,
|
|
269
|
+
type EvalOptions,
|
|
270
|
+
} from './client/parser/value-lang';
|
|
271
|
+
|
|
272
|
+
// SOMA DSL 容器与块装载(外置块发现在 soma-core/server)。
|
|
273
|
+
export {
|
|
274
|
+
loadSomaFile,
|
|
275
|
+
SomaV2ParseError,
|
|
276
|
+
PAGE_BLOCK_ID,
|
|
277
|
+
type RawBlock,
|
|
278
|
+
type SomaFileLoadInput,
|
|
279
|
+
} from './build/v2/soma-file-loader';
|
|
280
|
+
export {
|
|
281
|
+
buildHtmlAnchorIndex,
|
|
282
|
+
type HtmlAnchorIndex,
|
|
283
|
+
type HtmlAnchorInfo,
|
|
284
|
+
} from './build/v2/html-index';
|
|
285
|
+
// 资产级入口:拿着原始 v2 声明的一方(服务端装载层/测试替身/预览器)先编译,
|
|
286
|
+
// 再把 v1 等价产物交给 mount——运行时只认 v1(两级编译、单一运行时)。
|
|
287
|
+
export { compilePageAssets } from './build/compiler/page-assets';
|
|
288
|
+
export {
|
|
289
|
+
compileSomaV2,
|
|
290
|
+
type CompiledSomaV2,
|
|
291
|
+
type CompileSomaV2Input,
|
|
292
|
+
} from './build/v2/block-compile';
|
|
293
|
+
export {
|
|
294
|
+
parsePageBlock,
|
|
295
|
+
type PageBlockAst,
|
|
296
|
+
} from './client/parser/v2/page-parser';
|
|
297
|
+
export {
|
|
298
|
+
parseUseBlock,
|
|
299
|
+
type UseBlockAst,
|
|
300
|
+
type DataObjectDecl,
|
|
301
|
+
type FieldDecl,
|
|
302
|
+
type ActionDecl,
|
|
303
|
+
} from './client/parser/v2/use-parser';
|
|
304
|
+
export {
|
|
305
|
+
buildBlockScope,
|
|
306
|
+
resolvePath,
|
|
307
|
+
resolveApiRef,
|
|
308
|
+
expandPath,
|
|
309
|
+
scopeHasVar,
|
|
310
|
+
SomaScopeError,
|
|
311
|
+
type BlockScope,
|
|
312
|
+
} from './client/parser/v2/namespace';
|
|
313
|
+
export {
|
|
314
|
+
checkOverrideCompat,
|
|
315
|
+
type ContractCompatIssue,
|
|
316
|
+
type ContractCompatIssueKind,
|
|
317
|
+
} from './client/component/contract-compat';
|
|
246
318
|
export {
|
|
247
319
|
attachPackStyles,
|
|
248
320
|
ensurePackBaseStyles,
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// 发布页路径接线:Registry canonical 声明 → 服务端内存编译 → Host 运行时资产。
|
|
2
|
+
|
|
3
|
+
import { compilePageDeclaration } from '../../build/compiler/page-declaration-compiler';
|
|
4
|
+
import { getPageRegistryRepository } from '../registry/page-registry-repository';
|
|
5
|
+
|
|
6
|
+
const PUBLISHED_PAGE_PREFIX = '/pages/';
|
|
7
|
+
|
|
8
|
+
/** `/pages/<registry-name>` 是发布声明的稳定浏览路径;更深层路径仍归普通 Host 页面。 */
|
|
9
|
+
export function registryPageNameFromPath(pagePath: string): string | null {
|
|
10
|
+
if (!pagePath.startsWith(PUBLISHED_PAGE_PREFIX)) return null;
|
|
11
|
+
const name = pagePath.slice(PUBLISHED_PAGE_PREFIX.length);
|
|
12
|
+
return name !== '' && !name.includes('/') ? name : null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Registry 声明只在服务端编译,浏览器收到的仍是既有运行时资产。 */
|
|
16
|
+
export async function loadPublishedRegistryPageAssets(pagePath: string) {
|
|
17
|
+
const name = registryPageNameFromPath(pagePath);
|
|
18
|
+
if (!name) return null;
|
|
19
|
+
|
|
20
|
+
const record = await getPageRegistryRepository().get(name);
|
|
21
|
+
const compiled = compilePageDeclaration(record.declaration);
|
|
22
|
+
return {
|
|
23
|
+
base: record.name,
|
|
24
|
+
sourceType: 'registry' as const,
|
|
25
|
+
sourcePackage: null,
|
|
26
|
+
html: record.declaration.html,
|
|
27
|
+
ui: compiled.runtimeAssets.uiYaml,
|
|
28
|
+
event: compiled.runtimeAssets.eventYaml,
|
|
29
|
+
styleYaml: '',
|
|
30
|
+
...(compiled.runtimeAssets.dataYaml ? { dataYaml: compiled.runtimeAssets.dataYaml } : {}),
|
|
31
|
+
localeYaml: '',
|
|
32
|
+
appConfigYaml: '',
|
|
33
|
+
registryPage: { name: record.name, version: record.version },
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
getSomaAssetSources,
|
|
12
12
|
type SomaAssetSource,
|
|
13
13
|
} from './source-packages';
|
|
14
|
+
import { compileSomaPageAssets } from './soma-page-compile';
|
|
14
15
|
|
|
15
16
|
export type SomaPageAssetLoadProfile = 'render' | 'headless';
|
|
16
17
|
|
|
@@ -22,10 +23,7 @@ export interface SomaServerPageAssets {
|
|
|
22
23
|
ui: string;
|
|
23
24
|
event: string;
|
|
24
25
|
styleYaml: string;
|
|
25
|
-
/**
|
|
26
|
-
* 数据对象层声明(*_data.yaml,五件套可选件)。
|
|
27
|
-
* 按约定发现 data/<page>_data.yaml(回退 ui/ 同目录);无文件时该字段缺省,零影响。
|
|
28
|
-
*/
|
|
26
|
+
/** 构建期从统一声明编译出的运行时数据对象资产。 */
|
|
29
27
|
dataYaml?: string;
|
|
30
28
|
localeYaml: string;
|
|
31
29
|
appConfigYaml: string;
|
|
@@ -147,7 +145,23 @@ export function loadSomaServerPageAssets(options: {
|
|
|
147
145
|
const { source, base } = resolvedPage;
|
|
148
146
|
const htmlPath = assertContainedHtmlPath(source, base);
|
|
149
147
|
try {
|
|
150
|
-
const
|
|
148
|
+
const uiDir = join(source.assetRoot, 'ui');
|
|
149
|
+
const declarationPath = join(uiDir, base + '_ui.yaml');
|
|
150
|
+
const forbiddenSidecars = [
|
|
151
|
+
join(source.assetRoot, 'event', base + '_event.yaml'),
|
|
152
|
+
join(uiDir, base + '_style.yaml'),
|
|
153
|
+
join(source.assetRoot, 'data', base + '_data.yaml'),
|
|
154
|
+
join(uiDir, base + '_data.yaml'),
|
|
155
|
+
].filter(existsSync);
|
|
156
|
+
if (forbiddenSidecars.length > 0) {
|
|
157
|
+
throw new SomaPageAssetError(
|
|
158
|
+
'SOMA_PAGE_SIDECAR_FORBIDDEN',
|
|
159
|
+
422,
|
|
160
|
+
'页面侧车已退役;请把事件、样式状态与数据对象并入同一份 SOMA DSL 声明:'
|
|
161
|
+
+ forbiddenSidecars.map((file) => relative(source.assetRoot, file)).join(', '),
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
const ui = cache.tryReadFile(declarationPath);
|
|
151
165
|
const localeYaml = cache.tryReadFile(join(hostConfigRoot, 'locale.yaml'));
|
|
152
166
|
const hostAppConfigPath = resolveHostAppConfigPath(hostDir);
|
|
153
167
|
const hostAppConfigYaml = hostAppConfigPath
|
|
@@ -161,35 +175,38 @@ export function loadSomaServerPageAssets(options: {
|
|
|
161
175
|
? hostAppConfigYaml
|
|
162
176
|
: composeAppConfigYaml(sourceAppConfigYaml, hostAppConfigYaml);
|
|
163
177
|
|
|
178
|
+
const html = cache.readFile(htmlPath);
|
|
179
|
+
const includes = collectIncludes(sources);
|
|
180
|
+
|
|
181
|
+
// 唯一页面声明在服务端编译;浏览器只接收运行时资产,不解析源 YAML。
|
|
182
|
+
const compiled = compileSomaPageAssets(ui, html, uiDir, base, includes);
|
|
183
|
+
|
|
164
184
|
if (profile === 'headless') {
|
|
165
185
|
return {
|
|
166
186
|
base,
|
|
167
187
|
sourceType: source.type,
|
|
168
188
|
sourcePackage: source.packageName ?? null,
|
|
169
|
-
ui,
|
|
189
|
+
ui: compiled.ui,
|
|
170
190
|
event: '',
|
|
171
191
|
styleYaml: '',
|
|
192
|
+
...(compiled.dataYaml ? { dataYaml: compiled.dataYaml } : {}),
|
|
172
193
|
localeYaml,
|
|
173
194
|
appConfigYaml,
|
|
174
195
|
};
|
|
175
196
|
}
|
|
176
197
|
|
|
177
|
-
// 数据对象层(可选件):约定优先 data/<page>_data.yaml,回退 ui/ 同目录
|
|
178
|
-
const dataYaml = cache.tryReadFile(join(source.assetRoot, 'data', base + '_data.yaml'))
|
|
179
|
-
|| cache.tryReadFile(join(source.assetRoot, 'ui', base + '_data.yaml'));
|
|
180
|
-
|
|
181
198
|
return {
|
|
182
199
|
base,
|
|
183
200
|
sourceType: source.type,
|
|
184
201
|
sourcePackage: source.packageName ?? null,
|
|
185
|
-
html
|
|
186
|
-
ui,
|
|
187
|
-
event:
|
|
188
|
-
styleYaml:
|
|
189
|
-
...(dataYaml ? { dataYaml } : {}),
|
|
202
|
+
html,
|
|
203
|
+
ui: compiled.ui,
|
|
204
|
+
event: compiled.event,
|
|
205
|
+
styleYaml: '',
|
|
206
|
+
...(compiled.dataYaml ? { dataYaml: compiled.dataYaml } : {}),
|
|
190
207
|
localeYaml,
|
|
191
208
|
appConfigYaml,
|
|
192
|
-
includes
|
|
209
|
+
includes,
|
|
193
210
|
};
|
|
194
211
|
} catch (error) {
|
|
195
212
|
if (error instanceof SomaPageAssetError) throw error;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// SOMA DSL 页面接入运行路径:源声明只在服务端编译,浏览器只消费运行时资产。
|
|
2
|
+
|
|
3
|
+
import { compilePageDeclaration } from '../../build/compiler/page-declaration-compiler';
|
|
4
|
+
import { loadExternalBlocks } from '../../build/v2/block-loader';
|
|
5
|
+
|
|
6
|
+
export interface SomaPageCompileResult {
|
|
7
|
+
ui: string;
|
|
8
|
+
event: string;
|
|
9
|
+
dataYaml?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** 外置块约定 `ui/{base}.blocks/*.yaml`,与页面声明同目录。 */
|
|
13
|
+
export function compileSomaPageAssets(
|
|
14
|
+
uiRaw: string,
|
|
15
|
+
html: string,
|
|
16
|
+
uiDir: string,
|
|
17
|
+
base: string,
|
|
18
|
+
includes?: Readonly<Record<string, string>>,
|
|
19
|
+
): SomaPageCompileResult {
|
|
20
|
+
const externalBlocks = loadExternalBlocks(uiDir, base);
|
|
21
|
+
const compiled = compilePageDeclaration({
|
|
22
|
+
html,
|
|
23
|
+
uiYaml: uiRaw,
|
|
24
|
+
...(externalBlocks.size > 0 ? { externalBlocks } : {}),
|
|
25
|
+
...(includes ? { includes } : {}),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
ui: compiled.runtimeAssets.uiYaml,
|
|
30
|
+
event: compiled.runtimeAssets.eventYaml,
|
|
31
|
+
...(compiled.runtimeAssets.dataYaml ? { dataYaml: compiled.runtimeAssets.dataYaml } : {}),
|
|
32
|
+
};
|
|
33
|
+
}
|
package/core/server/index.ts
CHANGED
|
@@ -59,9 +59,24 @@ export {
|
|
|
59
59
|
export {
|
|
60
60
|
loadComponentPackage,
|
|
61
61
|
loadComponentPackages,
|
|
62
|
+
scanComponentPackages,
|
|
62
63
|
SomaComponentPackageError,
|
|
64
|
+
type LoadPackagesOptions,
|
|
65
|
+
type PackScanWarning,
|
|
63
66
|
} from '../build/component/package-loader';
|
|
64
67
|
|
|
68
|
+
export {
|
|
69
|
+
loadComponentPackFromDir,
|
|
70
|
+
type LoadPackFromDirOptions,
|
|
71
|
+
} from '../build/component/pack-from-dir';
|
|
72
|
+
|
|
73
|
+
// DSL v2 容器(REQ-005):开关探测、HTML 锚点索引、块装载、外置块发现。
|
|
74
|
+
// 用 node:fs 的只有 block-loader,其余纯函数(可在浏览器侧复用)。
|
|
75
|
+
export {
|
|
76
|
+
loadExternalBlocks,
|
|
77
|
+
blocksDirOf,
|
|
78
|
+
} from '../build/v2/block-loader';
|
|
79
|
+
|
|
65
80
|
export {
|
|
66
81
|
setSomaDb,
|
|
67
82
|
getSomaDb,
|
|
@@ -1,25 +1,18 @@
|
|
|
1
1
|
// 声明态页面的发布期预编译(P-a)。
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// 而声明一旦发布就是不可变的,这份解析结果完全可以在**落库时算一次**。
|
|
3
|
+
// 页面声明一旦发布就是不可变的,统一 SOMA DSL 应在服务端落库时编译一次,
|
|
4
|
+
// 浏览器只接收运行时配置对象,不保留源声明解析回退。
|
|
6
5
|
//
|
|
7
6
|
// 三条纪律,第一条最重要:
|
|
8
7
|
// 1. **预编译是缓存,不是真理**。parser 一旦升级,库里躺着的产物就可能与新语义不符。
|
|
9
|
-
// 所以每份产物带 `compilerVersion
|
|
10
|
-
// 宁可慢一点,也不能拿旧编译器的产物渲染新语义。
|
|
8
|
+
// 所以每份产物带 `compilerVersion`,服务端版本对不上就从统一源声明重新编译。
|
|
11
9
|
// 2. **编译失败不阻断发布**。声明本身已经过校验闸门;预编译只是加速,
|
|
12
10
|
// 它挂了就当没有这份缓存——绝不能让一个性能优化变成发布的新失败面。
|
|
13
11
|
// 3. **只编译纯数据部分**。html 不编译(它本来就是字符串),
|
|
14
12
|
// locale / appConfig 归宿主而不归声明。
|
|
15
13
|
|
|
16
14
|
import { createRequire } from 'node:module';
|
|
17
|
-
import {
|
|
18
|
-
compileDataConfig,
|
|
19
|
-
compileEventConfig,
|
|
20
|
-
compileStyleConfig,
|
|
21
|
-
compileUiConfig,
|
|
22
|
-
} from '../../build/compiler/soma-precompile';
|
|
15
|
+
import { compilePageDeclaration } from '../../build/compiler/page-declaration-compiler';
|
|
23
16
|
import type { PageRegistryDeclaration } from './page-registry-store';
|
|
24
17
|
|
|
25
18
|
/**
|
|
@@ -46,7 +39,7 @@ function readCoreVersion(): string {
|
|
|
46
39
|
return `unknown-${Math.random().toString(36).slice(2, 10)}`;
|
|
47
40
|
}
|
|
48
41
|
|
|
49
|
-
/**
|
|
42
|
+
/** 落库的完整运行时产物。 */
|
|
50
43
|
export interface CompiledDeclaration {
|
|
51
44
|
compilerVersion: string;
|
|
52
45
|
uiConfig?: unknown;
|
|
@@ -58,56 +51,33 @@ export interface CompiledDeclaration {
|
|
|
58
51
|
}
|
|
59
52
|
|
|
60
53
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
54
|
+
* 把统一声明一次编译成运行时可直接消费的完整配置对象。
|
|
55
|
+
* 编译失败返回 null,不产生可被误用的半成品。
|
|
63
56
|
*/
|
|
64
57
|
export function precompileDeclaration(
|
|
65
58
|
declaration: PageRegistryDeclaration,
|
|
66
59
|
): CompiledDeclaration | null {
|
|
67
|
-
const compiled: CompiledDeclaration = { compilerVersion: DECLARATION_COMPILER_VERSION };
|
|
68
|
-
let any = false;
|
|
69
|
-
|
|
70
60
|
try {
|
|
71
|
-
|
|
72
|
-
|
|
61
|
+
const page = compilePageDeclaration({
|
|
62
|
+
html: declaration.html,
|
|
63
|
+
uiYaml: declaration.uiYaml,
|
|
64
|
+
...(declaration.eventYaml ? { eventYaml: declaration.eventYaml } : {}),
|
|
65
|
+
...(declaration.styleYaml ? { styleYaml: declaration.styleYaml } : {}),
|
|
66
|
+
...(declaration.dataYaml ? { dataYaml: declaration.dataYaml } : {}),
|
|
67
|
+
});
|
|
68
|
+
return {
|
|
69
|
+
compilerVersion: DECLARATION_COMPILER_VERSION,
|
|
70
|
+
uiConfig: page.uiConfig,
|
|
71
|
+
eventConfig: page.eventConfig,
|
|
72
|
+
triggerConfig: page.triggers,
|
|
73
|
+
timerConfig: page.timers,
|
|
74
|
+
...(page.styleConfig ? { styleConfig: page.styleConfig } : {}),
|
|
75
|
+
...(page.dataConfig ? { dataConfig: page.dataConfig } : {}),
|
|
76
|
+
};
|
|
73
77
|
} catch (error) {
|
|
74
|
-
console.warn(`[soma-core][注册中心]
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
try {
|
|
78
|
-
const events = compileEventConfig(declaration.eventYaml ?? '');
|
|
79
|
-
compiled.eventConfig = events.events;
|
|
80
|
-
compiled.triggerConfig = events.triggers;
|
|
81
|
-
compiled.timerConfig = events.timers;
|
|
82
|
-
any = true;
|
|
83
|
-
} catch (error) {
|
|
84
|
-
console.warn(`[soma-core][注册中心] eventYaml 预编译失败,回落运行时解析:${message(error)}`);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (declaration.styleYaml?.trim()) {
|
|
88
|
-
try {
|
|
89
|
-
compiled.styleConfig = compileStyleConfig(declaration.styleYaml);
|
|
90
|
-
any = true;
|
|
91
|
-
} catch (error) {
|
|
92
|
-
console.warn(`[soma-core][注册中心] styleYaml 预编译失败:${message(error)}`);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (declaration.dataYaml?.trim()) {
|
|
97
|
-
try {
|
|
98
|
-
compiled.dataConfig = compileDataConfig(declaration.dataYaml);
|
|
99
|
-
any = true;
|
|
100
|
-
} catch (error) {
|
|
101
|
-
console.warn(`[soma-core][注册中心] dataYaml 预编译失败:${message(error)}`);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// uiConfig 与 eventConfig 缺一,运行时就走不了预编译分支(asset-resolver 要求两者齐备),
|
|
106
|
-
// 这时整份产物没有意义,不如不存——省得给消费端留个半成品去判断。
|
|
107
|
-
if (!any || compiled.uiConfig === undefined || compiled.eventConfig === undefined) {
|
|
78
|
+
console.warn(`[soma-core][注册中心] SOMA DSL 预编译失败:${message(error)}`);
|
|
108
79
|
return null;
|
|
109
80
|
}
|
|
110
|
-
return compiled;
|
|
111
81
|
}
|
|
112
82
|
|
|
113
83
|
/**
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
// - 生命周期三态:published / draft / archived(归档 = 追加一条 archived 墓碑记录,
|
|
11
11
|
// 缺省 list 消失、缺省 get 404,但全部历史仍可按版本查;rollback 旧版即恢复上架);
|
|
12
12
|
// - 页面元数据 meta(title/描述/来源需求文本/建议路由)随版本记录存储;
|
|
13
|
-
// - declaration
|
|
13
|
+
// - declaration 只接受 HTML + 一份统一 SOMA DSL;事件、状态样式与数据对象均内嵌;
|
|
14
14
|
// - 多应用租户预留:记录带 app(应用命名空间,缺省 'default')与 tags,
|
|
15
15
|
// P-C 多应用来时存量数据零迁移;
|
|
16
16
|
// - list 走分页协议 { pages, total },支持 app / 关键词过滤与 limit/offset;
|
|
@@ -26,6 +26,7 @@ import { scanDeclarationSafety } from './validation/declaration-safety-scan';
|
|
|
26
26
|
import { crossCheckDeclaredApis } from './validation/cross-check';
|
|
27
27
|
import { dryRunCompile } from './validation/compile-gate';
|
|
28
28
|
import { scanUnknownComponents } from './validation/component-existence';
|
|
29
|
+
import { compilePageDeclaration } from '../../build/compiler/page-declaration-compiler';
|
|
29
30
|
|
|
30
31
|
/** 页面名约束:小写字母 / 数字 / 连字符,1~64 位(可直接用作文件名与 URL 片段) */
|
|
31
32
|
export const PAGE_NAME_PATTERN = /^[a-z0-9-]{1,64}$/;
|
|
@@ -46,7 +47,7 @@ export const PAGE_APP_PATTERN = /^[a-z0-9-]{1,32}$/;
|
|
|
46
47
|
* notes 塞满一样会撑爆响应。
|
|
47
48
|
*
|
|
48
49
|
* 128KiB 这个数的依据(实测,2026-08-12):
|
|
49
|
-
* -
|
|
50
|
+
* - 历史基线中**最大的真实 Host 声明**是 gallery 页(html 13,826 + ui 4,407 + event 1,805)
|
|
50
51
|
* = 20,038 字节。128KiB / 20,038 ≈ 6.5×,即人手写得最复杂的整站页面还能再翻 6.5 倍。
|
|
51
52
|
* - 声明仓库里 119 份 AI 生成版本的实测分布:中位 1,777 / p90 2,305 / 最大 2,995 字节,
|
|
52
53
|
* 离上限有 43 倍余量——正常生成永远撞不到这条线。
|
|
@@ -132,9 +133,9 @@ export interface PageVersionRecord {
|
|
|
132
133
|
operator?: string;
|
|
133
134
|
/**
|
|
134
135
|
* 发布期预编译产物(P-a,可选):把 uiYaml/eventYaml 等的解析结果落库,
|
|
135
|
-
|
|
136
|
+
* 取页时客户端直接消费;浏览器不解析源声明。
|
|
136
137
|
* **它是缓存不是真理**——带 compilerVersion,消费端版本对不上就整份丢弃、
|
|
137
|
-
*
|
|
138
|
+
* 由服务端从统一源声明重新编译(见 DeclarationPrecompiler.isCompiledUsable)。
|
|
138
139
|
*/
|
|
139
140
|
compiled?: CompiledDeclaration;
|
|
140
141
|
}
|
|
@@ -757,7 +758,7 @@ function normalizeMeta(meta: unknown): PageRegistryMeta {
|
|
|
757
758
|
return normalized;
|
|
758
759
|
}
|
|
759
760
|
|
|
760
|
-
/** 声明归一化:
|
|
761
|
+
/** 声明归一化:HTML + 统一 SOMA DSL 必填;页面侧车字段只允许缺省或空串。 */
|
|
761
762
|
export function normalizeDeclaration(declaration: unknown): PageRegistryDeclaration {
|
|
762
763
|
if (!declaration || typeof declaration !== 'object' || Array.isArray(declaration)) {
|
|
763
764
|
throw new PageRegistryError(
|
|
@@ -782,12 +783,18 @@ export function normalizeDeclaration(declaration: unknown): PageRegistryDeclarat
|
|
|
782
783
|
);
|
|
783
784
|
}
|
|
784
785
|
}
|
|
786
|
+
const retiredSidecars = (['eventYaml', 'styleYaml', 'dataYaml'] as const)
|
|
787
|
+
.filter((field) => typeof source[field] === 'string' && (source[field] as string).trim() !== '');
|
|
788
|
+
if (retiredSidecars.length > 0) {
|
|
789
|
+
throw new PageRegistryError(
|
|
790
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
791
|
+
`页面侧车字段已退役:${retiredSidecars.join('、')};事件、样式状态与数据对象必须写进 uiYaml 的统一 SOMA DSL 声明`,
|
|
792
|
+
);
|
|
793
|
+
}
|
|
785
794
|
const normalized: PageRegistryDeclaration = {
|
|
786
795
|
html: source.html as string,
|
|
787
796
|
uiYaml: source.uiYaml as string,
|
|
788
|
-
eventYaml:
|
|
789
|
-
styleYaml: (source.styleYaml as string | undefined) || undefined,
|
|
790
|
-
dataYaml: (source.dataYaml as string | undefined) || undefined,
|
|
797
|
+
eventYaml: '',
|
|
791
798
|
notes: (source.notes as string | undefined) || undefined,
|
|
792
799
|
};
|
|
793
800
|
|
|
@@ -828,7 +835,10 @@ export function normalizeDeclaration(declaration: unknown): PageRegistryDeclarat
|
|
|
828
835
|
// 两表交叉校验:页面绑定的数据接口必须已在 API 注册表登记。
|
|
829
836
|
// 与安全扫描同一个咽喉、同一种失败方式——白名单才有牙齿:
|
|
830
837
|
// 没登记过的接口,AI 生成的页面在物理上就绑不上。
|
|
831
|
-
|
|
838
|
+
// 编译闸门已经证明可编译;这里再取同一编译器的 dataConfig 做 API 白名单交叉校验,
|
|
839
|
+
// 不另开 dataYaml 解析入口。
|
|
840
|
+
const compiled = compilePageDeclaration(normalized);
|
|
841
|
+
const unregistered = crossCheckDeclaredApis(compiled.dataConfig);
|
|
832
842
|
if (unregistered.length > 0) {
|
|
833
843
|
throw new PageRegistryError(
|
|
834
844
|
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
@@ -866,7 +876,7 @@ function assertDeclarationSize(declaration: PageRegistryDeclaration): void {
|
|
|
866
876
|
);
|
|
867
877
|
}
|
|
868
878
|
|
|
869
|
-
/** 存量 JSON
|
|
879
|
+
/** 存量 JSON 记录归一化:只补元数据;旧页面语法必须离线迁移,不在读取时自动转换。 */
|
|
870
880
|
export function normalizeStoredRecord(record: Record<string, unknown>): PageVersionRecord {
|
|
871
881
|
const normalized = { ...record } as unknown as PageVersionRecord;
|
|
872
882
|
if (typeof normalized.app !== 'string' || !PAGE_APP_PATTERN.test(normalized.app)) {
|
|
@@ -877,9 +887,28 @@ export function normalizeStoredRecord(record: Record<string, unknown>): PageVers
|
|
|
877
887
|
} else {
|
|
878
888
|
normalized.tags = normalized.tags.filter((tag): tag is string => typeof tag === 'string');
|
|
879
889
|
}
|
|
890
|
+
assertStoredDeclarationCanonical(normalized.declaration);
|
|
880
891
|
return normalized;
|
|
881
892
|
}
|
|
882
893
|
|
|
894
|
+
function assertStoredDeclarationCanonical(declaration: unknown): void {
|
|
895
|
+
if (!declaration || typeof declaration !== 'object' || Array.isArray(declaration)) {
|
|
896
|
+
throw new Error('[PageRegistryStore] 存量页面记录缺少 declaration;请修复或离线迁移该记录');
|
|
897
|
+
}
|
|
898
|
+
const source = declaration as Record<string, unknown>;
|
|
899
|
+
const uiYaml = typeof source.uiYaml === 'string' ? source.uiYaml : '';
|
|
900
|
+
const hasRetiredSidecar = ['eventYaml', 'styleYaml', 'dataYaml']
|
|
901
|
+
.some((field) => typeof source[field] === 'string' && (source[field] as string).trim() !== '');
|
|
902
|
+
if (!/^\s*(?:#[^\n]*\n\s*)*\{\{/u.test(uiYaml)
|
|
903
|
+
|| !/\}\}\s*$/u.test(uiYaml)
|
|
904
|
+
|| hasRetiredSidecar) {
|
|
905
|
+
throw new Error(
|
|
906
|
+
'[PageRegistryStore] 检测到旧格式存量页面声明;请使用离线迁移工具转换为 HTML + 单一 SOMA DSL 后再启动。'
|
|
907
|
+
+ '运行时不会自动转换旧 bind/show 或 event/style/data 侧车。',
|
|
908
|
+
);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
|
|
883
912
|
/** 对外返回深拷贝,避免调用方改动污染内存缓存 */
|
|
884
913
|
export function cloneRecord(record: PageVersionRecord): PageVersionRecord {
|
|
885
914
|
return JSON.parse(JSON.stringify(record)) as PageVersionRecord;
|