soma-core 1.3.3 → 1.3.4

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.
Files changed (106) hide show
  1. package/README.md +7 -6
  2. package/app/SomaPageClient.tsx +2 -72
  3. package/bin/soma-lsp.cjs +1 -4
  4. package/core/build/compiler/full-manifest-builder.ts +17 -26
  5. package/core/build/compiler/html-include-expander.ts +56 -0
  6. package/core/build/compiler/page-assets.ts +26 -0
  7. package/core/build/compiler/page-declaration-compiler.ts +85 -0
  8. package/core/build/compiler/rollup-plugin-soma.ts +4 -19
  9. package/core/build/compiler/route-builder.ts +0 -2
  10. package/core/build/compiler/rspack-plugin-soma.js +1 -10
  11. package/core/build/compiler/soma-manifest-webpack-plugin.js +54 -13
  12. package/core/build/compiler/soma-precompile.js +74 -0
  13. package/core/build/compiler/soma-precompile.ts +13 -0
  14. package/core/build/compiler/soma-rspack-yaml-loader.js +5 -11
  15. package/core/build/compiler/soma-webpack-loader.js +6 -21
  16. package/core/build/compiler/vite-plugin-soma.ts +9 -31
  17. package/core/build/component/pack-from-dir.ts +58 -0
  18. package/core/build/component/package-loader.ts +158 -12
  19. package/core/build/eject/plan-eject.ts +15 -21
  20. package/core/build/lsp/cross-file-diagnostics.cjs +82 -113
  21. package/core/build/lsp/extra-completion.cjs +14 -8
  22. package/core/build/lsp/lsp-navigation.cjs +50 -43
  23. package/core/build/lsp/soma-declaration.cjs +85 -0
  24. package/core/build/lsp/ui-yaml-completion.cjs +98 -104
  25. package/core/build/v2/block-compile.ts +200 -0
  26. package/core/build/v2/block-loader.ts +33 -0
  27. package/core/build/v2/html-index.ts +120 -0
  28. package/core/build/v2/migrate-v1.ts +370 -0
  29. package/core/build/v2/soma-file-loader.ts +216 -0
  30. package/core/client/component/ai-catalog.ts +15 -0
  31. package/core/client/component/component-pack.ts +205 -31
  32. package/core/client/component/contract-compat.ts +132 -0
  33. package/core/client/component/example-v2.ts +132 -0
  34. package/core/client/engine/dom-renderer.ts +14 -8
  35. package/core/client/engine/reactive-proxy.ts +8 -7
  36. package/core/client/engine/soma-bootstrap.ts +23 -5
  37. package/core/client/parser/v2/namespace.ts +182 -0
  38. package/core/client/parser/v2/page-parser.ts +359 -0
  39. package/core/client/parser/v2/use-parser.ts +412 -0
  40. package/core/client/parser/value-lang.ts +340 -0
  41. package/core/client/pipes/pipe-registry.ts +1 -1
  42. package/core/client/registry/declaration-renderer.ts +17 -15
  43. package/core/client/theme/harmony.ts +307 -0
  44. package/core/client/theme/scene-tokens.ts +134 -0
  45. package/core/client/theme/theme-axes.ts +228 -0
  46. package/core/client/theme/theme-engine.ts +333 -73
  47. package/core/client/theme/tone-solver.ts +118 -0
  48. package/core/index.ts +74 -2
  49. package/core/server/host/server-page-assets.ts +33 -16
  50. package/core/server/host/soma-page-compile.ts +33 -0
  51. package/core/server/index.ts +15 -0
  52. package/core/server/registry/declaration-precompiler.ts +24 -54
  53. package/core/server/registry/page-registry-store.ts +38 -9
  54. package/core/server/registry/services/pages/Get.ts +19 -9
  55. package/core/server/registry/validation/compile-gate.ts +14 -31
  56. package/core/server/registry/validation/cross-check.ts +7 -17
  57. package/core/server/registry/validation/declaration-safety-scan.ts +2 -2
  58. package/core/shared/types/component-contract.types.ts +10 -0
  59. package/dist/core/index.cjs +2138 -154
  60. package/dist/types/build/compiler/full-manifest-builder.d.ts +4 -4
  61. package/dist/types/build/compiler/html-include-expander.d.ts +19 -0
  62. package/dist/types/build/compiler/page-assets.d.ts +3 -0
  63. package/dist/types/build/compiler/page-declaration-compiler.d.ts +33 -0
  64. package/dist/types/build/compiler/rollup-plugin-soma.d.ts +0 -3
  65. package/dist/types/build/compiler/route-builder.d.ts +0 -1
  66. package/dist/types/build/compiler/soma-precompile.d.ts +8 -0
  67. package/dist/types/build/compiler/vite-plugin-soma.d.ts +2 -10
  68. package/dist/types/build/component/pack-from-dir.d.ts +17 -0
  69. package/dist/types/build/component/package-loader.d.ts +33 -2
  70. package/dist/types/build/v2/block-compile.d.ts +28 -0
  71. package/dist/types/build/v2/block-loader.d.ts +9 -0
  72. package/dist/types/build/v2/html-index.d.ts +37 -0
  73. package/dist/types/build/v2/migrate-v1.d.ts +32 -0
  74. package/dist/types/build/v2/soma-file-loader.d.ts +45 -0
  75. package/dist/types/client/component/component-pack.d.ts +23 -3
  76. package/dist/types/client/component/contract-compat.d.ts +16 -0
  77. package/dist/types/client/component/example-v2.d.ts +9 -0
  78. package/dist/types/client/engine/dom-renderer.d.ts +2 -0
  79. package/dist/types/client/engine/soma-bootstrap.d.ts +3 -1
  80. package/dist/types/client/parser/v2/namespace.d.ts +52 -0
  81. package/dist/types/client/parser/v2/page-parser.d.ts +21 -0
  82. package/dist/types/client/parser/v2/use-parser.d.ts +72 -0
  83. package/dist/types/client/parser/value-lang.d.ts +63 -0
  84. package/dist/types/client/registry/declaration-renderer.d.ts +1 -2
  85. package/dist/types/client/theme/harmony.d.ts +77 -0
  86. package/dist/types/client/theme/scene-tokens.d.ts +25 -0
  87. package/dist/types/client/theme/theme-axes.d.ts +81 -0
  88. package/dist/types/client/theme/theme-engine.d.ts +29 -1
  89. package/dist/types/client/theme/tone-solver.d.ts +47 -0
  90. package/dist/types/index.d.ts +13 -2
  91. package/dist/types/server/host/server-page-assets.d.ts +1 -4
  92. package/dist/types/server/host/soma-page-compile.d.ts +7 -0
  93. package/dist/types/server/index.d.ts +3 -1
  94. package/dist/types/server/registry/declaration-precompiler.d.ts +3 -3
  95. package/dist/types/server/registry/page-registry-store.d.ts +4 -4
  96. package/dist/types/server/registry/validation/compile-gate.d.ts +2 -0
  97. package/dist/types/server/registry/validation/cross-check.d.ts +4 -4
  98. package/dist/types/shared/types/component-contract.types.d.ts +10 -0
  99. package/editor/vscode/package.json +1 -24
  100. package/global.d.ts +1 -11
  101. package/next.config.js +32 -50
  102. package/package.json +3 -2
  103. package/tailwind.config.ts +0 -1
  104. package/core/build/lsp/event-yaml-completion.cjs +0 -106
  105. package/core/client/host-bridge/empty-host/data/README.md +0 -17
  106. package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +0 -30
package/README.md CHANGED
@@ -11,12 +11,13 @@ SOMA 运行时引擎:页面 = 四件套声明文件(html/_ui.yaml/_event.yaml/_s
11
11
  + triggers/timers + confirm 段;格式化管道;作用域清单树与增量刷新
12
12
  (`core/client/engine`、`core/client/runtime`)。
13
13
  - **组件运行时**:实例 = 迷你运行时(作用域隔离/props/emit 路由/插槽/自治生命周期),
14
- `data-soma-component` 声明接入;文件态组件包加载器 `loadComponentPackages(dir)`
15
- (contract.yaml + template.html + ui.yaml + event.yaml,AI 自产组件落盘即用)。
14
+ `data-soma-component` 声明接入;文件态组件包由 `loadComponentPackFromDir(dir, { name, version })`
15
+ 完整装载定义、样式与 provenance,`loadComponentPackages` 仅保留为无样式定义表兼容入口。
16
16
  - **数据对象层**:`*_data.yaml` 解析器(多接口 DAG 依赖编排/字段字典/类型双态映射/速记列/
17
17
  mock 兜底),解析结果注入 `page.data`,支持 `reloadData`(`core/client/data`)。
18
- - **主题引擎**:54+ 业界标准令牌,brand 一行 → OKLCH 派生亮暗双套,WCAG AA 硬保障,
19
- overrides 精调,`#soma-ui-styles` 整张换皮(`core/client/theme`)。
18
+ - **主题引擎**:54+ 业界标准令牌;brand + palette 五轴 + scene 三项派生亮暗双套,
19
+ 语义分离与逐配对 WCAG AA 硬保障;省略新轴保持历史字节兼容,overrides 可精调
20
+ (`core/client/theme`)。
20
21
  - **宿主与 CLI**:Next Host(`app/`,含静态 public/ 托管)+ `soma` CLI
21
22
  (`dev/build/start/db generate|push`、mock 服务器、LSP、codegen,见 `bin/`)。
22
23
 
@@ -27,8 +28,8 @@ SOMA 运行时引擎:页面 = 四件套声明文件(html/_ui.yaml/_event.yaml/_s
27
28
  - `soma 运行时引擎详设.md` — 编译管线、组件运行时、作用域、emit 路由、services 白名单
28
29
  - `soma 设计令牌与主题.md` — 令牌总表、派生算法、换肤协议
29
30
  - `soma 数据对象层.md` — `*_data.yaml` 语法与解析器
30
- - `soma 组件dsl扩展.md` / `soma 作用域清单树.md` — 页面侧组件用法与运行时机制
31
- - 其余:`SOMA-HOST-CLI.md`、`soma 事件引擎.md`、`soma 渲染引擎.md`、`mock-data.md` 等
31
+ - `soma DSL 语法全书.md` / `soma 作用域清单树.md` — 页面侧组件用法与运行时机制
32
+ - 其余:`SOMA-HOST-CLI.md`、`soma 渲染引擎.md`、`mock-data.md` 等
32
33
 
33
34
  ## 测试
34
35
 
@@ -51,28 +51,9 @@ type PrecompiledAssets = Pick<
51
51
  'uiConfig' | 'eventConfig' | 'triggerConfig' | 'timerConfig' | 'styleConfig' | 'locale' | 'appConfig' | 'dataConfig'
52
52
  >;
53
53
 
54
- function normalizeEventBundle(raw: unknown): {
55
- events: PrecompiledAssets['eventConfig'];
56
- triggers: NonNullable<PrecompiledAssets['triggerConfig']>;
57
- timers: NonNullable<PrecompiledAssets['timerConfig']>;
58
- } {
59
- if (Array.isArray(raw)) {
60
- return { events: raw, triggers: [], timers: [] };
61
- }
62
- if (raw && typeof raw === 'object' && 'events' in raw) {
63
- const bundle = raw as { events?: unknown; triggers?: unknown; timers?: unknown };
64
- return {
65
- events: (bundle.events ?? []) as NonNullable<PrecompiledAssets['eventConfig']>,
66
- triggers: (bundle.triggers ?? []) as NonNullable<PrecompiledAssets['triggerConfig']>,
67
- timers: (bundle.timers ?? []) as NonNullable<PrecompiledAssets['timerConfig']>,
68
- };
69
- }
70
- return { events: [], triggers: [], timers: [] };
71
- }
72
-
73
54
  const pageAssetCache = new Map<string, PagePayload>();
74
55
 
75
- async function loadPrecompiledFromManifest(base: string, path: string): Promise<PrecompiledAssets | null> {
56
+ async function loadPrecompiledFromManifest(path: string): Promise<PrecompiledAssets | null> {
76
57
  try {
77
58
  const res = await fetch(`/soma/manifest?path=${encodeURIComponent(path)}`);
78
59
  if (!res.ok) return null;
@@ -92,56 +73,6 @@ async function loadPrecompiledFromManifest(base: string, path: string): Promise<
92
73
  }
93
74
  }
94
75
 
95
- async function loadPrecompiledAssets(base: string, urlPath = `/${base}`): Promise<PrecompiledAssets | null> {
96
- try {
97
- const [uiMod, eventMod, styleMod, localeMod, configMod, dataMod] = await Promise.all([
98
- import(
99
- /* webpackInclude: /\/ui\/.*_ui\.yaml$/ */
100
- `@soma-host/ui/${base}_ui.yaml`
101
- ).catch(() => null),
102
- import(
103
- /* webpackInclude: /\/event\/.*_event\.yaml$/ */
104
- `@soma-host/event/${base}_event.yaml`
105
- ).catch(() => null),
106
- import(
107
- /* webpackInclude: /\/ui\/.*_style\.yaml$/ */
108
- `@soma-host/ui/${base}_style.yaml`
109
- ).catch(() => null),
110
- import(
111
- /* webpackInclude: /\/config\/locale\.yaml$/ */
112
- '@soma-host/config/locale.yaml'
113
- ).catch(() => null),
114
- import(
115
- /* webpackInclude: /\/config\/app\.config\.yaml$/ */
116
- '@soma-host/config/app.config.yaml'
117
- ).catch(() => null),
118
- import(
119
- /* webpackInclude: /\/data\/.*_data\.yaml$/ */
120
- `@soma-host/data/${base}_data.yaml`
121
- ).catch(() => null),
122
- ]);
123
-
124
- if (!uiMod?.default || !eventMod?.default) {
125
- return null;
126
- }
127
-
128
- const eventBundle = normalizeEventBundle(eventMod.default);
129
-
130
- return {
131
- uiConfig: uiMod.default,
132
- eventConfig: eventBundle.events,
133
- triggerConfig: eventBundle.triggers,
134
- timerConfig: eventBundle.timers,
135
- styleConfig: styleMod?.default ?? undefined,
136
- locale: (localeMod?.default ?? {}) as Record<string, unknown>,
137
- appConfig: configMod?.default ?? null,
138
- dataConfig: dataMod?.default ?? undefined,
139
- };
140
- } catch {
141
- return null;
142
- }
143
- }
144
-
145
76
  function SomaPageRenderer({
146
77
  pagePath,
147
78
  routerPath,
@@ -265,8 +196,7 @@ function SomaPageRenderer({
265
196
  const routeAssets = { routerPath };
266
197
 
267
198
  if (!isDev) {
268
- const fromManifest = await loadPrecompiledFromManifest(base, pagePath);
269
- const precompiled = fromManifest ?? await loadPrecompiledAssets(base, pagePath);
199
+ const precompiled = await loadPrecompiledFromManifest(pagePath);
270
200
  if (precompiled) {
271
201
  await mountInto(
272
202
  DomainClass,
package/bin/soma-lsp.cjs CHANGED
@@ -1,12 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * SOMA Language Server — 跨文件锚点诊断(html ↔ ui ↔ event)
3
+ * SOMA Language Server — HTML ↔ 唯一 SOMA DSL 声明诊断与导航
4
4
  */
5
5
  const fs = require('node:fs');
6
6
  const path = require('node:path');
7
7
  const { lintAnchorConsistency } = require('../core/build/lsp/cross-file-diagnostics.cjs');
8
8
  const { getUiYamlCompletions } = require('../core/build/lsp/ui-yaml-completion.cjs');
9
- const { getEventYamlCompletions } = require('../core/build/lsp/event-yaml-completion.cjs');
10
9
  const { getHover, getDefinition } = require('../core/build/lsp/lsp-navigation.cjs');
11
10
  const { getLocaleYamlCompletions, getHtmlAnchorCompletions } = require('../core/build/lsp/extra-completion.cjs');
12
11
 
@@ -126,8 +125,6 @@ function handleMessage(msg) {
126
125
  if (resolved) {
127
126
  if (fileName.endsWith('_ui.yaml')) {
128
127
  items = getUiYamlCompletions(resolved, uri, text, pos);
129
- } else if (fileName.endsWith('_event.yaml')) {
130
- items = getEventYamlCompletions(resolved, uri, text, pos);
131
128
  } else if (fileName === 'locale.yaml') {
132
129
  items = getLocaleYamlCompletions(resolved, uri, text, pos);
133
130
  } else if (fileName.endsWith('.html')) {
@@ -7,9 +7,7 @@ import type { PropertyTrigger } from '../../shared/types/trigger.types';
7
7
  import type { ParsedTimer } from '../../shared/types/timer.types';
8
8
  import type { StyleConfig } from '../../shared/types/style-config.types';
9
9
  import type { SomaDataConfig } from '../../shared/types/data-config.types';
10
- import { compileUiConfig, compileStyleConfig, compileDataConfig, precompileYaml } from '@core/build/compiler/soma-precompile';
11
- import { parseEventConfig } from '../../client/parser/pipeline-parser';
12
- import { splitEventConfigRaw } from '../../client/parser/trigger-parser';
10
+ import { compilePageDeclaration } from './page-declaration-compiler';
13
11
  import { buildRouteTable, type SomaRouteEntry } from './route-builder';
14
12
  import yaml from 'js-yaml';
15
13
  import {
@@ -17,18 +15,6 @@ import {
17
15
  parseAppConfigYaml,
18
16
  } from '../../server/host/app-config-composer';
19
17
 
20
- function compileEventBundle(eventYaml: string) {
21
- if (!eventYaml.trim()) {
22
- return { events: [], triggers: [] as PropertyTrigger[], timers: [] as ParsedTimer[] };
23
- }
24
- const { eventsRaw, triggers, timers } = splitEventConfigRaw(yaml.load(eventYaml));
25
- return {
26
- events: parseEventConfig(eventsRaw as any),
27
- triggers,
28
- timers,
29
- };
30
- }
31
-
32
18
  export interface SomaPrecompiledPage {
33
19
  path: string;
34
20
  uiConfig: UiConfig;
@@ -37,7 +23,7 @@ export interface SomaPrecompiledPage {
37
23
  timers: ParsedTimer[];
38
24
  styleConfig?: StyleConfig;
39
25
  appConfig?: unknown;
40
- /** 数据对象层声明(*_data.yaml 预编译物化产物,可选件) */
26
+ /** 从统一声明编译出的数据对象资产。 */
41
27
  dataConfig?: SomaDataConfig;
42
28
  }
43
29
 
@@ -58,11 +44,11 @@ export function precompileAll(inputs: {
58
44
  appConfigYaml: string;
59
45
  pages: Array<{
60
46
  path: string;
47
+ html: string;
61
48
  uiYaml: string;
62
- eventYaml: string;
63
- styleYaml?: string;
64
- dataYaml?: string;
65
49
  appConfigYaml?: string;
50
+ externalBlocks?: ReadonlyMap<string, string>;
51
+ includes?: Readonly<Record<string, string>>;
66
52
  }>;
67
53
  }): SomaPrecompiledManifest {
68
54
  const hostAppConfig = parseAppConfigYaml(inputs.appConfigYaml);
@@ -71,15 +57,20 @@ export function precompileAll(inputs: {
71
57
  locale: (yaml.load(inputs.localeYaml) as Record<string, unknown>) || {},
72
58
  appConfig: hostAppConfig,
73
59
  pages: inputs.pages.map(p => {
74
- const eventBundle = compileEventBundle(p.eventYaml);
60
+ const page = compilePageDeclaration({
61
+ html: p.html,
62
+ uiYaml: p.uiYaml,
63
+ ...(p.externalBlocks ? { externalBlocks: p.externalBlocks } : {}),
64
+ ...(p.includes ? { includes: p.includes } : {}),
65
+ });
75
66
  return {
76
67
  path: p.path,
77
- uiConfig: compileUiConfig(p.uiYaml),
78
- eventConfig: eventBundle.events,
79
- triggers: eventBundle.triggers,
80
- timers: eventBundle.timers,
81
- styleConfig: p.styleYaml ? compileStyleConfig(p.styleYaml) : undefined,
82
- dataConfig: p.dataYaml ? compileDataConfig(p.dataYaml) : undefined,
68
+ uiConfig: page.uiConfig,
69
+ eventConfig: page.eventConfig,
70
+ triggers: page.triggers,
71
+ timers: page.timers,
72
+ styleConfig: page.styleConfig,
73
+ dataConfig: page.dataConfig,
83
74
  appConfig: p.appConfigYaml
84
75
  ? mergeSourcePackageAppConfig(parseAppConfigYaml(p.appConfigYaml), hostAppConfig)
85
76
  : undefined,
@@ -0,0 +1,56 @@
1
+ /**
2
+ * 构建期 HTML include 展开。
3
+ *
4
+ * 浏览器运行时会由 SomaContainerAdapter 把 data-soma-include 的片段填入占位元素。
5
+ * 单文件 DSL 在构建期校验 data-soma-id 时也必须看到同一棵 DOM;否则共享 Shell
6
+ * 里的锚点会被误报为不存在。这里仅按调用方已经收集好的 include 名称表查找,绝不
7
+ * 从声明值拼接或读取文件路径。
8
+ */
9
+ export class SomaHtmlIncludeError extends Error {
10
+ constructor(message: string) {
11
+ super(`[soma-core][HTML include] ${message}`);
12
+ this.name = 'SomaHtmlIncludeError';
13
+ }
14
+ }
15
+
16
+ const INCLUDE_ATTRIBUTE = /\sdata-soma-include\s*=\s*(["'])([^"']+)\1/i;
17
+ const OPEN_TAG = /<([A-Za-z][A-Za-z0-9:-]*)(\s[^<>]*?)?>/g;
18
+
19
+ /**
20
+ * 返回供声明编译器做锚点校验的逻辑 HTML。
21
+ *
22
+ * include 容器本身保留,行为与运行时的 element.innerHTML = fragment 一致;目前
23
+ * Shell 的 include 占位节点均为空。嵌套 include 递归展开并检查循环,缺失片段
24
+ * 显式失败,避免运行时出现静默空壳而构建误通过。
25
+ */
26
+ export function expandSomaHtmlIncludes(
27
+ html: string,
28
+ includes: Readonly<Record<string, string>> | undefined,
29
+ ): string {
30
+ if (!includes || Object.keys(includes).length === 0 || !html.includes('data-soma-include')) {
31
+ return html;
32
+ }
33
+
34
+ const expand = (source: string, chain: readonly string[]): string => source.replace(
35
+ OPEN_TAG,
36
+ (full, tagName: string, attributes: string | undefined) => {
37
+ const rawAttributes = attributes ?? '';
38
+ const matched = INCLUDE_ATTRIBUTE.exec(rawAttributes);
39
+ if (!matched) return full;
40
+
41
+ const includeName = matched[2];
42
+ if (!Object.prototype.hasOwnProperty.call(includes, includeName)) {
43
+ throw new SomaHtmlIncludeError(`找不到共享片段 "${includeName}"`);
44
+ }
45
+ if (chain.includes(includeName)) {
46
+ throw new SomaHtmlIncludeError(`检测到循环引用:${[...chain, includeName].join(' -> ')}`);
47
+ }
48
+
49
+ const withoutInclude = rawAttributes.replace(INCLUDE_ATTRIBUTE, '');
50
+ const expandedFragment = expand(includes[includeName], [...chain, includeName]);
51
+ return `<${tagName}${withoutInclude}>${expandedFragment}`;
52
+ },
53
+ );
54
+
55
+ return expand(html, []);
56
+ }
@@ -0,0 +1,26 @@
1
+ // 测试替身与预览器使用的页面级编译入口。生产 Host 走同一个 compilePageDeclaration。
2
+
3
+ import type { SomaPageAssets } from '../../client/engine/soma-bootstrap';
4
+ import { compilePageDeclaration } from './page-declaration-compiler';
5
+
6
+ /** 把 SOMA DSL 源声明编译成客户端挂载所需的运行时资产。 */
7
+ export function compilePageAssets(assets: SomaPageAssets): SomaPageAssets {
8
+ const compiled = compilePageDeclaration({
9
+ html: assets.html ?? '',
10
+ uiYaml: assets.uiYaml ?? '',
11
+ ...(assets.eventYaml ? { eventYaml: assets.eventYaml } : {}),
12
+ ...(assets.styleYaml ? { styleYaml: assets.styleYaml } : {}),
13
+ ...(assets.dataYaml ? { dataYaml: assets.dataYaml } : {}),
14
+ ...(assets.declarationBlocks ? { externalBlocks: assets.declarationBlocks } : {}),
15
+ });
16
+
17
+ return {
18
+ ...assets,
19
+ uiYaml: compiled.runtimeAssets.uiYaml,
20
+ eventYaml: compiled.runtimeAssets.eventYaml,
21
+ styleYaml: '',
22
+ ...(compiled.runtimeAssets.dataYaml
23
+ ? { dataYaml: compiled.runtimeAssets.dataYaml }
24
+ : { dataYaml: undefined }),
25
+ };
26
+ }
@@ -0,0 +1,85 @@
1
+ import yaml from 'js-yaml';
2
+ import { compileSomaV2 } from '../v2/block-compile';
3
+ import { expandSomaHtmlIncludes } from './html-include-expander';
4
+ import type { SomaDataConfig } from '../../shared/types/data-config.types';
5
+ import type { ParsedEvent } from '../../shared/types/event.types';
6
+ import type { PropertyTrigger } from '../../shared/types/trigger.types';
7
+ import type { StyleConfig } from '../../shared/types/style-config.types';
8
+ import type { ParsedTimer } from '../../shared/types/timer.types';
9
+ import type { UiConfig } from '../../shared/types/ui-config.types';
10
+ import {
11
+ compileDataConfig,
12
+ compileEventConfigFromRaw,
13
+ } from './soma-precompile';
14
+
15
+ export interface SomaPageDeclarationInput {
16
+ html: string;
17
+ uiYaml: string;
18
+ eventYaml?: string;
19
+ styleYaml?: string;
20
+ dataYaml?: string;
21
+ externalBlocks?: ReadonlyMap<string, string>;
22
+ /** 与浏览器运行时一致的共享 HTML 片段;仅在构建锚点校验前展开。 */
23
+ includes?: Readonly<Record<string, string>>;
24
+ }
25
+
26
+ export interface SomaRuntimeDeclarationAssets {
27
+ uiYaml: string;
28
+ eventYaml: string;
29
+ styleYaml: string;
30
+ dataYaml?: string;
31
+ }
32
+
33
+ export interface CompiledPageDeclaration {
34
+ uiConfig: UiConfig;
35
+ eventConfig: ParsedEvent[];
36
+ triggers: PropertyTrigger[];
37
+ timers: ParsedTimer[];
38
+ styleConfig?: StyleConfig;
39
+ dataConfig?: SomaDataConfig;
40
+ runtimeAssets: SomaRuntimeDeclarationAssets;
41
+ }
42
+
43
+ const dump = (value: unknown): string =>
44
+ yaml.dump(value, { lineWidth: 120, noRefs: true, quotingType: '"' });
45
+
46
+ /** 编译一整页 SOMA DSL 声明。旧页面形状与页面侧车在唯一入口直接拒绝。 */
47
+ export function compilePageDeclaration(input: SomaPageDeclarationInput): CompiledPageDeclaration {
48
+ const eventYaml = input.eventYaml ?? '';
49
+ const styleYaml = input.styleYaml ?? '';
50
+ const dataYaml = input.dataYaml ?? '';
51
+
52
+ const sidecars = [
53
+ eventYaml.trim() ? 'eventYaml / *_event.yaml' : '',
54
+ styleYaml.trim() ? 'styleYaml / *_style.yaml' : '',
55
+ dataYaml.trim() ? 'dataYaml / *_data.yaml' : '',
56
+ ].filter(Boolean);
57
+ if (sidecars.length > 0) {
58
+ throw new Error(
59
+ `[soma-core][SOMA DSL] 页面侧车已退役:${sidecars.join('、')}。`
60
+ + '事件、样式状态与数据对象必须写进同一份 SOMA DSL 声明。',
61
+ );
62
+ }
63
+
64
+ const compiled = compileSomaV2({
65
+ html: expandSomaHtmlIncludes(input.html, input.includes),
66
+ uiYamlRaw: input.uiYaml,
67
+ ...(input.externalBlocks ? { externalBlocks: input.externalBlocks } : {}),
68
+ });
69
+ const eventBundle = compileEventConfigFromRaw(compiled.event);
70
+ const embeddedDataYaml = Object.keys(compiled.data).length > 0 ? dump({ data: compiled.data }) : '';
71
+
72
+ return {
73
+ uiConfig: compiled.ui,
74
+ eventConfig: eventBundle.events,
75
+ triggers: eventBundle.triggers,
76
+ timers: eventBundle.timers,
77
+ ...(embeddedDataYaml ? { dataConfig: compileDataConfig(embeddedDataYaml) } : {}),
78
+ runtimeAssets: {
79
+ uiYaml: dump(compiled.ui),
80
+ eventYaml: Object.keys(compiled.event).length > 0 ? dump(compiled.event) : '',
81
+ styleYaml: '',
82
+ ...(embeddedDataYaml ? { dataYaml: embeddedDataYaml } : {}),
83
+ },
84
+ };
85
+ }
@@ -2,25 +2,16 @@
2
2
  // Rollup 预编译插件:构建阶段将 YAML 编译为 JS 模块(与 vite-plugin-soma 对齐)
3
3
 
4
4
  import {
5
- precompileUiYaml,
6
- precompileEventYaml,
7
- precompileStyleYaml,
8
- precompileDataYaml,
5
+ precompileUiModule,
9
6
  precompileYaml,
10
7
  } from './soma-precompile';
11
8
 
12
9
  export interface SomaRollupPluginOptions {
13
10
  uiSuffix?: string;
14
- eventSuffix?: string;
15
- styleSuffix?: string;
16
- dataSuffix?: string;
17
11
  }
18
12
 
19
13
  export function somaRollupPlugin(options: SomaRollupPluginOptions = {}) {
20
14
  const uiSuffix = options.uiSuffix ?? '_ui.yaml';
21
- const eventSuffix = options.eventSuffix ?? '_event.yaml';
22
- const styleSuffix = options.styleSuffix ?? '_style.yaml';
23
- const dataSuffix = options.dataSuffix ?? '_data.yaml';
24
15
 
25
16
  return {
26
17
  name: 'rollup-plugin-soma',
@@ -31,16 +22,10 @@ export function somaRollupPlugin(options: SomaRollupPluginOptions = {}) {
31
22
 
32
23
  try {
33
24
  if (id.endsWith(uiSuffix)) {
34
- return { code: `export default ${precompileUiYaml(code)};`, map: { mappings: '' } };
25
+ return { code: `export default ${precompileUiModule(code)};`, map: { mappings: '' } };
35
26
  }
36
- if (id.endsWith(eventSuffix)) {
37
- return { code: `export default ${precompileEventYaml(code)};`, map: { mappings: '' } };
38
- }
39
- if (id.endsWith(styleSuffix)) {
40
- return { code: `export default ${precompileStyleYaml(code)};`, map: { mappings: '' } };
41
- }
42
- if (id.endsWith(dataSuffix)) {
43
- return { code: `export default ${precompileDataYaml(code)};`, map: { mappings: '' } };
27
+ if (/_((event)|(style)|(data))\.ya?ml$/.test(id)) {
28
+ throw new Error('页面侧车已退役;事件、样式状态与数据对象必须并入同一份 SOMA DSL 声明');
44
29
  }
45
30
  return { code: `export default ${precompileYaml(code)};`, map: { mappings: '' } };
46
31
  } catch (err) {
@@ -5,7 +5,6 @@ export interface SomaRouteEntry {
5
5
  path: string;
6
6
  htmlFile: string;
7
7
  uiFile: string;
8
- eventFile: string;
9
8
  }
10
9
 
11
10
  /**
@@ -21,7 +20,6 @@ export function buildRouteTable(htmlFiles: string[]): SomaRouteEntry[] {
21
20
  path: `/${base}`,
22
21
  htmlFile,
23
22
  uiFile: `${base}_ui.yaml`,
24
- eventFile: `${base}_event.yaml`,
25
23
  };
26
24
  });
27
25
  }
@@ -1,14 +1,5 @@
1
- const {
2
- precompileUiYaml,
3
- precompileEventYaml,
4
- precompileStyleYaml,
5
- precompileYaml,
6
- } = require('./soma-precompile');
7
-
8
1
  function somaRspackPlugin(options = {}) {
9
2
  const uiSuffix = options.uiSuffix ?? '_ui.yaml';
10
- const eventSuffix = options.eventSuffix ?? '_event.yaml';
11
- const styleSuffix = options.styleSuffix ?? '_style.yaml';
12
3
 
13
4
  return {
14
5
  name: 'rspack-plugin-soma',
@@ -21,7 +12,7 @@ function somaRspackPlugin(options = {}) {
21
12
  use: [
22
13
  {
23
14
  loader: require.resolve('./soma-rspack-yaml-loader.js'),
24
- options: { uiSuffix, eventSuffix, styleSuffix },
15
+ options: { uiSuffix },
25
16
  },
26
17
  ],
27
18
  },
@@ -30,6 +30,13 @@ function readJson(filePath) {
30
30
  }
31
31
  }
32
32
 
33
+ function writeFileIfChanged(filePath, content) {
34
+ if (fs.existsSync(filePath) && fs.readFileSync(filePath, 'utf8') === content) {
35
+ return;
36
+ }
37
+ fs.writeFileSync(filePath, content);
38
+ }
39
+
33
40
  function packagePathInNodeModules(hostDir, packageName) {
34
41
  return path.join(hostDir, 'node_modules', ...packageName.split('/'));
35
42
  }
@@ -151,6 +158,40 @@ function collectPageEntries(sources) {
151
158
  return [...byHtmlFile.values()].sort((a, b) => a.htmlFile.localeCompare(b.htmlFile));
152
159
  }
153
160
 
161
+ /** 与运行期 collectIncludes 保持同一覆盖顺序:包默认值,Host 同名片段覆盖。 */
162
+ function collectIncludes(sources) {
163
+ const includes = {};
164
+ const orderedSources = [
165
+ ...sources.filter((source) => source.type === 'package'),
166
+ ...sources.filter((source) => source.type === 'host'),
167
+ ];
168
+ for (const source of orderedSources) {
169
+ const sharedDir = path.join(source.assetRoot, 'html', 'shared');
170
+ if (!fs.existsSync(sharedDir)) continue;
171
+ for (const entry of fs.readdirSync(sharedDir, { withFileTypes: true })) {
172
+ if (!entry.isFile() || !entry.name.endsWith('.html')) continue;
173
+ includes[`shared/${entry.name}`] = fs.readFileSync(path.join(sharedDir, entry.name), 'utf8');
174
+ }
175
+ }
176
+ return includes;
177
+ }
178
+
179
+ function assertNoPageSidecars(source, base) {
180
+ const uiDir = path.join(source.assetRoot, 'ui');
181
+ const forbidden = [
182
+ path.join(source.assetRoot, 'event', `${base}_event.yaml`),
183
+ path.join(uiDir, `${base}_style.yaml`),
184
+ path.join(source.assetRoot, 'data', `${base}_data.yaml`),
185
+ path.join(uiDir, `${base}_data.yaml`),
186
+ ].filter((file) => fs.existsSync(file));
187
+ if (forbidden.length > 0) {
188
+ throw new Error(
189
+ '页面侧车已退役;请把事件、样式状态与数据对象并入同一份 SOMA DSL 声明:'
190
+ + forbidden.map((file) => path.relative(source.assetRoot, file)).join(', '),
191
+ );
192
+ }
193
+ }
194
+
154
195
  class SomaManifestWebpackPlugin {
155
196
  constructor(options = {}) {
156
197
  this.hostDir = options.hostDir;
@@ -158,7 +199,7 @@ class SomaManifestWebpackPlugin {
158
199
  this.jiti = createJiti(__filename, {
159
200
  interopDefault: true,
160
201
  alias: {
161
- '@core': path.resolve(__dirname, '..'),
202
+ '@core': path.resolve(__dirname, '../..'),
162
203
  },
163
204
  });
164
205
  }
@@ -173,8 +214,10 @@ class SomaManifestWebpackPlugin {
173
214
 
174
215
  try {
175
216
  const { precompileAll } = this.jiti('./full-manifest-builder.ts');
217
+ const { loadExternalBlocks } = this.jiti('../v2/block-loader.ts');
176
218
  const sources = getSomaAssetSources(hostDir);
177
219
  const pageEntries = collectPageEntries(sources);
220
+ const includes = collectIncludes(sources);
178
221
  const htmlFiles = pageEntries.map((entry) => entry.htmlFile);
179
222
  const readYaml = (filePath) =>
180
223
  fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf8') : '';
@@ -186,15 +229,15 @@ class SomaManifestWebpackPlugin {
186
229
  .map(({ source, htmlFile }) => {
187
230
  const base = htmlFile.replace(/\.html$/, '');
188
231
  const sourceAppConfigPath = resolveAppConfigPath(source.configRoot);
232
+ const uiDir = path.join(source.assetRoot, 'ui');
233
+ assertNoPageSidecars(source, base);
234
+ const externalBlocks = loadExternalBlocks(uiDir, base);
189
235
  return {
190
236
  path: `/${base}`,
191
- uiYaml: readYaml(path.join(source.assetRoot, 'ui', `${base}_ui.yaml`)),
192
- eventYaml: readYaml(path.join(source.assetRoot, 'event', `${base}_event.yaml`)),
193
- styleYaml: readYaml(path.join(source.assetRoot, 'ui', `${base}_style.yaml`)) || undefined,
194
- // 数据对象层(可选件):约定优先 data/,回退 ui/ 同目录
195
- dataYaml: readYaml(path.join(source.assetRoot, 'data', `${base}_data.yaml`))
196
- || readYaml(path.join(source.assetRoot, 'ui', `${base}_data.yaml`))
197
- || undefined,
237
+ html: fs.readFileSync(path.join(source.assetRoot, 'html', htmlFile), 'utf8'),
238
+ uiYaml: readYaml(path.join(uiDir, `${base}_ui.yaml`)),
239
+ ...(externalBlocks.size > 0 ? { externalBlocks } : {}),
240
+ ...(Object.keys(includes).length > 0 ? { includes } : {}),
198
241
  appConfigYaml: sourceAppConfigPath
199
242
  ? readYaml(sourceAppConfigPath)
200
243
  : (hostAppConfigPath ? readYaml(hostAppConfigPath) : ''),
@@ -207,15 +250,13 @@ class SomaManifestWebpackPlugin {
207
250
 
208
251
  const outDir = path.join(hostDir, this.outputDir);
209
252
  if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
210
- fs.writeFileSync(
253
+ writeFileIfChanged(
211
254
  path.join(outDir, 'manifest.json'),
212
255
  JSON.stringify(manifest, null, 2),
213
256
  );
214
257
  } catch (e) {
215
- compiler.hooks.infrastructureLog?.call(
216
- 'SomaManifestWebpackPlugin',
217
- `[soma-manifest] 预编译跳过: ${e.message}`,
218
- );
258
+ callback(e);
259
+ return;
219
260
  }
220
261
 
221
262
  callback();