soma-core 1.3.2 → 1.3.3

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 (229) hide show
  1. package/README.md +42 -0
  2. package/app/SomaPageClient.tsx +26 -2
  3. package/app/[...path]/page.tsx +7 -6
  4. package/app/api/[...path]/route.ts +3 -3
  5. package/app/api/soma-registry/[...path]/route.ts +67 -0
  6. package/app/components/SomaHostPage.tsx +3 -8
  7. package/app/components/SomaPageClientBoundary.tsx +13 -0
  8. package/app/layout.tsx +6 -1
  9. package/app/page.tsx +3 -3
  10. package/app/soma/manifest/route.ts +1 -0
  11. package/app/soma/page-object/route.ts +1 -1
  12. package/core/build/compiler/full-manifest-builder.ts +6 -1
  13. package/core/build/compiler/rollup-plugin-soma.ts +6 -0
  14. package/core/build/compiler/soma-manifest-webpack-plugin.js +4 -0
  15. package/core/build/compiler/soma-precompile.js +3507 -2387
  16. package/core/build/compiler/soma-precompile.ts +31 -2
  17. package/core/build/compiler/soma-rspack-yaml-loader.js +3 -0
  18. package/core/build/compiler/soma-webpack-loader.js +7 -1
  19. package/core/build/compiler/vite-plugin-soma.ts +11 -1
  20. package/core/build/component/package-loader.ts +135 -0
  21. package/core/build/eject/apply-eject.ts +133 -0
  22. package/core/build/eject/plan-eject.ts +139 -0
  23. package/core/client/adaptive/flush-scheduler.ts +96 -0
  24. package/core/client/adaptive/hysteresis.ts +56 -0
  25. package/core/client/adaptive/index.ts +23 -0
  26. package/core/client/adaptive/observer-pool.ts +90 -0
  27. package/core/client/base/SomaPageRootBase.ts +50 -0
  28. package/core/client/compiler/adaptive-compile.ts +136 -0
  29. package/core/client/compiler/manifest-compiler.ts +155 -27
  30. package/core/client/component/ai-catalog.ts +262 -0
  31. package/core/client/component/component-definition.types.ts +65 -0
  32. package/core/client/component/component-expander.ts +119 -0
  33. package/core/client/component/component-manager.ts +933 -0
  34. package/core/client/component/component-pack.ts +271 -0
  35. package/core/client/component/component-registry.ts +71 -0
  36. package/core/client/component/pack-styles.ts +94 -0
  37. package/core/client/component/scoped-adapter.ts +85 -0
  38. package/core/client/component/verify-pack.ts +247 -0
  39. package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
  40. package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
  41. package/core/client/container/SomaContainerAdapter.ts +186 -180
  42. package/core/client/data/data-object-resolver.ts +417 -0
  43. package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
  44. package/core/client/diagnostics/boot-timings.ts +152 -0
  45. package/core/client/engine/asset-resolver.ts +12 -2
  46. package/core/client/engine/dom-renderer.ts +235 -16
  47. package/core/client/engine/emit-event.ts +21 -0
  48. package/core/client/engine/event-engine.ts +34 -4
  49. package/core/client/engine/on-change-engine.ts +1 -1
  50. package/core/client/engine/property-watcher.ts +1 -1
  51. package/core/client/engine/reactive-proxy.ts +3 -3
  52. package/core/client/engine/render-engine.ts +39 -4
  53. package/core/client/engine/runtime-builder.ts +23 -0
  54. package/core/client/engine/soma-bootstrap.ts +359 -4
  55. package/core/client/engine/style-binder.ts +47 -1
  56. package/core/client/host-bridge/empty-host/data/README.md +17 -0
  57. package/core/client/observability/error-reporter.ts +173 -0
  58. package/core/client/parser/adaptive-parser.ts +264 -0
  59. package/core/client/parser/data-config-parser.ts +396 -0
  60. package/core/client/parser/pipeline-parser.ts +15 -3
  61. package/core/client/parser/style-config-parser.ts +65 -2
  62. package/core/client/parser/ui-config-parser.ts +52 -3
  63. package/core/client/pipes/pipe-registry.ts +249 -0
  64. package/core/client/quality/SomaQualityRegistry.ts +3 -0
  65. package/core/client/registry/declaration-renderer.ts +159 -0
  66. package/core/client/registry/index.ts +28 -0
  67. package/core/client/registry/page-lifecycle.ts +171 -0
  68. package/core/client/registry/remote-page.ts +139 -0
  69. package/core/client/runtime/SomaRuntimeServices.ts +24 -0
  70. package/core/client/security/attribute-presence.ts +70 -0
  71. package/core/client/security/html-sanitizer.ts +113 -0
  72. package/core/client/security/url-guard.ts +0 -0
  73. package/core/client/theme/theme-engine.ts +936 -0
  74. package/core/client/typography/typography-base.ts +34 -0
  75. package/core/client/validation/contract-validator.ts +48 -5
  76. package/core/index.ts +190 -2
  77. package/core/server/api/IService.ts +18 -6
  78. package/core/server/api/load-host-service.ts +3 -3
  79. package/core/server/host/content-security-policy.ts +190 -0
  80. package/core/server/host/csp-nonce-metadata.ts +75 -0
  81. package/core/server/host/resolve-host-request-hook.ts +5 -0
  82. package/core/server/host/server-page-assets.ts +10 -0
  83. package/core/server/index.ts +30 -1
  84. package/core/server/infra/cache/ServerFileCache.ts +1 -1
  85. package/core/server/mock/node/cli.mjs +2 -2
  86. package/core/server/registry/api-registry-store.ts +518 -0
  87. package/core/server/registry/conditional-object-store.ts +29 -0
  88. package/core/server/registry/declaration-precompiler.ts +129 -0
  89. package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
  90. package/core/server/registry/index.ts +142 -0
  91. package/core/server/registry/page-registry-repository.ts +373 -0
  92. package/core/server/registry/page-registry-store.ts +903 -0
  93. package/core/server/registry/registry-error.ts +17 -0
  94. package/core/server/registry/registry-file-lock.ts +173 -0
  95. package/core/server/registry/registry-response.ts +71 -0
  96. package/core/server/registry/registry-subjects.ts +194 -0
  97. package/core/server/registry/registry-write-pipeline.ts +240 -0
  98. package/core/server/registry/request-body-limit.ts +99 -0
  99. package/core/server/registry/services/apis/Get.ts +25 -0
  100. package/core/server/registry/services/apis/List.ts +41 -0
  101. package/core/server/registry/services/apis/Register.ts +39 -0
  102. package/core/server/registry/services/apis/Unregister.ts +24 -0
  103. package/core/server/registry/services/index.ts +40 -0
  104. package/core/server/registry/services/pages/Get.ts +86 -0
  105. package/core/server/registry/services/pages/List.ts +53 -0
  106. package/core/server/registry/services/pages/Register.ts +49 -0
  107. package/core/server/registry/services/pages/Rollback.ts +36 -0
  108. package/core/server/registry/services/pages/Unregister.ts +23 -0
  109. package/core/server/registry/services/service-utils.ts +21 -0
  110. package/core/server/registry/validation/compile-gate.ts +71 -0
  111. package/core/server/registry/validation/component-existence.ts +55 -0
  112. package/core/server/registry/validation/credential-ban.ts +86 -0
  113. package/core/server/registry/validation/cross-check.ts +77 -0
  114. package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
  115. package/core/shared/types/adaptive.types.ts +109 -0
  116. package/core/shared/types/component-contract.types.ts +110 -0
  117. package/core/shared/types/data-config.types.ts +105 -0
  118. package/core/shared/types/domain.types.ts +6 -0
  119. package/core/shared/types/engine.types.ts +41 -35
  120. package/core/shared/types/manifest.types.ts +55 -32
  121. package/core/shared/types/scope-manifest.types.ts +96 -0
  122. package/core/shared/types/style-config.types.ts +9 -0
  123. package/core/shared/types/ui-config.types.ts +118 -98
  124. package/dist/cli/soma.js +20 -5
  125. package/dist/core/index.cjs +5341 -894
  126. package/dist/types/build/compiler/full-manifest-builder.d.ts +4 -0
  127. package/dist/types/build/compiler/rollup-plugin-soma.d.ts +1 -0
  128. package/dist/types/build/compiler/soma-precompile.d.ts +9 -0
  129. package/dist/types/build/compiler/vite-plugin-soma.d.ts +3 -0
  130. package/dist/types/build/component/package-loader.d.ts +9 -0
  131. package/dist/types/build/eject/apply-eject.d.ts +22 -0
  132. package/dist/types/build/eject/plan-eject.d.ts +32 -0
  133. package/dist/types/client/adaptive/flush-scheduler.d.ts +13 -0
  134. package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
  135. package/dist/types/client/adaptive/index.d.ts +5 -0
  136. package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
  137. package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
  138. package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
  139. package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
  140. package/dist/types/client/component/ai-catalog.d.ts +73 -0
  141. package/dist/types/client/component/component-definition.types.d.ts +54 -0
  142. package/dist/types/client/component/component-expander.d.ts +25 -0
  143. package/dist/types/client/component/component-manager.d.ts +160 -0
  144. package/dist/types/client/component/component-pack.d.ts +90 -0
  145. package/dist/types/client/component/component-registry.d.ts +12 -0
  146. package/dist/types/client/component/pack-styles.d.ts +22 -0
  147. package/dist/types/client/component/scoped-adapter.d.ts +18 -0
  148. package/dist/types/client/component/verify-pack.d.ts +14 -0
  149. package/dist/types/client/data/data-object-resolver.d.ts +71 -0
  150. package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
  151. package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
  152. package/dist/types/client/engine/asset-resolver.d.ts +7 -1
  153. package/dist/types/client/engine/dom-renderer.d.ts +49 -1
  154. package/dist/types/client/engine/emit-event.d.ts +6 -0
  155. package/dist/types/client/engine/event-engine.d.ts +6 -0
  156. package/dist/types/client/engine/render-engine.d.ts +25 -1
  157. package/dist/types/client/engine/runtime-builder.d.ts +7 -0
  158. package/dist/types/client/engine/soma-bootstrap.d.ts +116 -0
  159. package/dist/types/client/observability/error-reporter.d.ts +51 -0
  160. package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
  161. package/dist/types/client/parser/data-config-parser.d.ts +34 -0
  162. package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
  163. package/dist/types/client/parser/style-config-parser.d.ts +5 -3
  164. package/dist/types/client/parser/ui-config-parser.d.ts +22 -1
  165. package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
  166. package/dist/types/client/registry/declaration-renderer.d.ts +51 -0
  167. package/dist/types/client/registry/index.d.ts +10 -0
  168. package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
  169. package/dist/types/client/registry/remote-page.d.ts +52 -0
  170. package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
  171. package/dist/types/client/security/attribute-presence.d.ts +7 -0
  172. package/dist/types/client/security/html-sanitizer.d.ts +37 -0
  173. package/dist/types/client/security/url-guard.d.ts +11 -0
  174. package/dist/types/client/theme/theme-engine.d.ts +49 -0
  175. package/dist/types/client/typography/typography-base.d.ts +15 -0
  176. package/dist/types/client/validation/contract-validator.d.ts +2 -0
  177. package/dist/types/index.d.ts +36 -2
  178. package/dist/types/server/api/IService.d.ts +18 -6
  179. package/dist/types/server/host/content-security-policy.d.ts +53 -0
  180. package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
  181. package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
  182. package/dist/types/server/host/server-page-assets.d.ts +5 -0
  183. package/dist/types/server/index.d.ts +4 -1
  184. package/dist/types/server/registry/api-registry-store.d.ts +141 -0
  185. package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
  186. package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
  187. package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
  188. package/dist/types/server/registry/index.d.ts +29 -0
  189. package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
  190. package/dist/types/server/registry/page-registry-store.d.ts +259 -0
  191. package/dist/types/server/registry/registry-error.d.ts +12 -0
  192. package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
  193. package/dist/types/server/registry/registry-response.d.ts +18 -0
  194. package/dist/types/server/registry/registry-subjects.d.ts +59 -0
  195. package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
  196. package/dist/types/server/registry/request-body-limit.d.ts +31 -0
  197. package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
  198. package/dist/types/server/registry/services/apis/List.d.ts +10 -0
  199. package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
  200. package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
  201. package/dist/types/server/registry/services/index.d.ts +3 -0
  202. package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
  203. package/dist/types/server/registry/services/pages/List.d.ts +13 -0
  204. package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
  205. package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
  206. package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
  207. package/dist/types/server/registry/services/service-utils.d.ts +10 -0
  208. package/dist/types/server/registry/validation/compile-gate.d.ts +10 -0
  209. package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
  210. package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
  211. package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
  212. package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
  213. package/dist/types/shared/types/adaptive.types.d.ts +88 -0
  214. package/dist/types/shared/types/component-contract.types.d.ts +68 -0
  215. package/dist/types/shared/types/data-config.types.d.ts +81 -0
  216. package/dist/types/shared/types/domain.types.d.ts +6 -0
  217. package/dist/types/shared/types/engine.types.d.ts +6 -0
  218. package/dist/types/shared/types/manifest.types.d.ts +23 -0
  219. package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
  220. package/dist/types/shared/types/style-config.types.d.ts +8 -0
  221. package/dist/types/shared/types/ui-config.types.d.ts +20 -0
  222. package/global.d.ts +4 -0
  223. package/next.config.js +76 -1
  224. package/package.json +32 -11
  225. package/proxy.ts +62 -0
  226. package/core/client/engine/theme-variables.ts +0 -37
  227. package/dist/tsconfig.types.tsbuildinfo +0 -1
  228. package/dist/types/client/engine/theme-variables.d.ts +0 -3
  229. package/middleware.ts +0 -33
@@ -2,8 +2,11 @@
2
2
  // 职责:从解析后的资产 + DOM 容器组装完整的运行时引擎组
3
3
 
4
4
  import { SomaManifestCompiler } from '../compiler/manifest-compiler';
5
+ import { applyCompiledAdaptive } from '../compiler/adaptive-compile';
6
+ import type { SomaAdaptiveVocabulary } from '../../shared/types/adaptive.types';
5
7
  import type { IContainerAdapter } from '../container/IContainerAdapter';
6
8
  import type { SomaDiagnostics } from '../diagnostics/SomaDiagnostics';
9
+ import { markBootPhase } from '../diagnostics/boot-timings';
7
10
  import type { SomaMiddlewareChain } from '../middleware/SomaMiddlewareChain';
8
11
  import type { SomaScheduler } from '../scheduler/SomaScheduler';
9
12
  import type { ParsedEvent } from '../../shared/types/event.types';
@@ -37,6 +40,10 @@ export interface RuntimeBuildInput {
37
40
  diagnostics: SomaDiagnostics;
38
41
  renderMiddleware?: SomaMiddlewareChain<SomaRenderMiddlewareContext>;
39
42
  eventMiddleware?: SomaMiddlewareChain<SomaEventMiddlewareContext>;
43
+ /** 格式化管道注册表(缺省内置管道集) */
44
+ pipeRegistry?: import('../pipes/pipe-registry').SomaPipeRegistry;
45
+ /** 阶梯词汇表(F-09),经宿主 bootstrap 注入;无 adaptive 声明的页面用不到 */
46
+ adaptiveVocabulary?: SomaAdaptiveVocabulary;
40
47
  }
41
48
 
42
49
  export interface SomaRuntime {
@@ -48,6 +55,8 @@ export interface SomaRuntime {
48
55
  triggerEngine: SomaTriggerEngine;
49
56
  timerEngine: SomaTimerEngine;
50
57
  resolver: SomaScopeResolver;
58
+ /** 页面运行时清单(组件管理器标记 emit 路由用) */
59
+ manifest: import('../../shared/types/manifest.types').SomaRuntimeManifest;
51
60
  }
52
61
 
53
62
  /**
@@ -75,8 +84,17 @@ export class SomaRuntimeBuilder {
75
84
  uiConfig: input.uiConfig,
76
85
  eventConfig: input.eventConfig,
77
86
  resolver,
87
+ pipes: input.pipeRegistry,
88
+ adaptiveVocabulary: input.adaptiveVocabulary,
78
89
  });
79
90
 
91
+ // 阶梯落地:写宿主根画像属性 + 挂断点型作用域类。
92
+ // 放在 render 之前,是为了让首帧就带着正确的作用域类——
93
+ // 渲染后再补类会先闪一帧默认档(首帧协议要求可降解而不是可见跳变)。
94
+ if (manifest.adaptive) {
95
+ applyCompiledAdaptive(manifest.adaptive, input.container, input.adapter);
96
+ }
97
+
80
98
  const propertyWatcher = new SomaPropertyWatcher();
81
99
  propertyWatcher.setRoot(input.proxiedInstance);
82
100
 
@@ -90,9 +108,13 @@ export class SomaRuntimeBuilder {
90
108
  diagnostics: input.diagnostics,
91
109
  middleware: input.renderMiddleware,
92
110
  propertyWatcher,
111
+ pipeRegistry: input.pipeRegistry,
93
112
  });
94
113
 
114
+ // 首屏三段打点:编译段止于此(作用域解析 + 契约校验 + manifest 编译),渲染段自此开始
115
+ markBootPhase('compiled');
95
116
  renderEngine.render();
117
+ markBootPhase('rendered');
96
118
 
97
119
  const eventEngine = new SomaEventEngine({
98
120
  manifest,
@@ -153,6 +175,7 @@ export class SomaRuntimeBuilder {
153
175
  triggerEngine,
154
176
  timerEngine,
155
177
  resolver,
178
+ manifest,
156
179
  };
157
180
  }
158
181
 
@@ -6,6 +6,7 @@ import type { IContainerAdapter } from '../container/IContainerAdapter';
6
6
  import { SomaDiagnostics, type SomaDiagnosticsSnapshot } from '../diagnostics/SomaDiagnostics';
7
7
  import { SomaTelemetry } from '../diagnostics/SomaTelemetry';
8
8
  import { setActiveTelemetry, recordMetric } from '../diagnostics/telemetry-context';
9
+ import { markBootPhase, maybeStartBootTimings } from '../diagnostics/boot-timings';
9
10
  import { SomaMiddlewareChain } from '../middleware/SomaMiddlewareChain';
10
11
  import { SomaScheduler } from '../scheduler/SomaScheduler';
11
12
  import type { ParsedEvent } from '../../shared/types/event.types';
@@ -13,6 +14,7 @@ import type { PropertyTrigger } from '../../shared/types/trigger.types';
13
14
  import type { ParsedTimer } from '../../shared/types/timer.types';
14
15
  import type { StyleConfig } from '../../shared/types/style-config.types';
15
16
  import type { UiConfig } from '../../shared/types/ui-config.types';
17
+ import type { SomaAdaptiveVocabulary } from '../../shared/types/adaptive.types';
16
18
  import type { SomaPageRoot } from '../../shared/types/domain.types';
17
19
  import { SomaSnapshotStore } from '../runtime/SomaSnapshotStore';
18
20
  import { SomaAssetResolver } from './asset-resolver';
@@ -29,8 +31,22 @@ import type { SomaOnChangeEngine } from './on-change-engine';
29
31
  import type { SomaTriggerEngine } from './trigger-engine';
30
32
  import type { SomaTimerEngine } from './timer-engine';
31
33
  import { createDefaultRuntimeServices } from '../runtime/SomaRuntimeServices';
32
- import { getBrowserUrlPath } from '../infra/router/router-path';
34
+ import { getBrowserUrlPath, readQueryParam } from '../infra/router/router-path';
35
+ import { parseDataYaml } from '../parser/data-config-parser';
36
+ import { SomaDataObjectResolver } from '../data/data-object-resolver';
37
+ import type { SomaDataConfig, SomaDataDicts } from '../../shared/types/data-config.types';
33
38
  import type { WatchCallback, WatcherOptions } from '../../shared/types/watcher.types';
39
+ import type { SomaComponentDefinition } from '../component/component-definition.types';
40
+ import { SomaComponentRegistry } from '../component/component-registry';
41
+ import { SomaComponentManager } from '../component/component-manager';
42
+ import { SomaPipeRegistry, type SomaPipeFn } from '../pipes/pipe-registry';
43
+ import { applyTheme, type SomaThemeInput } from '../theme/theme-engine';
44
+ import {
45
+ installSomaErrorReporter,
46
+ type SomaErrorEvent,
47
+ type SomaErrorReporterHandle,
48
+ type SomaPageIdentity,
49
+ } from '../observability/error-reporter';
34
50
 
35
51
  // ─── 类型定义 ──────────────────────────────────────────
36
52
 
@@ -45,6 +61,13 @@ export interface SomaPageAssets {
45
61
  styleYaml?: string;
46
62
  localeYaml?: string;
47
63
  appConfigYaml?: string;
64
+ /**
65
+ * 数据对象层声明(*_data.yaml,五件套可选件)。
66
+ * 提供后 bootstrap 在首渲前解析声明、异步取数并注入 page.data,
67
+ * 完成后触发一次全量渲染;解析/取数失败不阻塞页面(data 保持空对象 + console.error)。
68
+ * 不提供时加载路径零变化。
69
+ */
70
+ dataYaml?: string;
48
71
 
49
72
  // ── 预编译模式(构建阶段已解析,运行时零开销) ──
50
73
  uiConfig?: UiConfig;
@@ -54,9 +77,39 @@ export interface SomaPageAssets {
54
77
  styleConfig?: StyleConfig;
55
78
  locale?: Record<string, unknown>;
56
79
  appConfig?: unknown;
80
+ /** 数据对象层声明的预编译形态(构建阶段已解析校验,优先于 dataYaml) */
81
+ dataConfig?: SomaDataConfig;
57
82
 
58
83
  /** Full router path including query string (e.g. /items/detail?id=1) */
59
84
  routerPath?: string;
85
+
86
+ /**
87
+ * 组件运行时定义(组件体系模块 7,可选)。
88
+ * 键为契约名;页面 HTML 中 data-soma-component 引用的组件必须在此提供。
89
+ * 不提供时组件能力整体旁路,页面走既有路径。
90
+ */
91
+ components?: Record<string, SomaComponentDefinition>;
92
+ }
93
+
94
+ /**
95
+ * 错误采集接线声明(可观测性,默认关闭)。
96
+ *
97
+ * 为什么放在 bootstrap 而不是让 Host 自己 install:采集器本身只会记"出了什么错",
98
+ * 记不住"这是哪个页面的哪个版本"——而页面身份只有挂载时点知道。挂在生命周期
99
+ * 编排器上,身份随挂载自动盖戳、随卸载自动摘监听,Host 不需要维护任何状态。
100
+ */
101
+ export interface SomaBootstrapErrorReporting {
102
+ /** 页面身份:错误归因到「页面名@版本」的唯一依据(声明态页面必须带 version) */
103
+ page: SomaPageIdentity;
104
+ /**
105
+ * 上报通道。缺省走 console.error 单行 JSON——宿主一行开启就先有数据,
106
+ * 等接了真通道(sendBeacon / 第三方 SDK)再替换,不必为"先看看"写胶水。
107
+ */
108
+ report?: (event: SomaErrorEvent) => void;
109
+ /** 同签名错误去重窗口(毫秒),透传采集器 */
110
+ dedupeWindowMs?: number;
111
+ /** 单页上报条数上限,透传采集器 */
112
+ maxEvents?: number;
60
113
  }
61
114
 
62
115
  export interface SomaBootstrapOptions {
@@ -65,6 +118,25 @@ export interface SomaBootstrapOptions {
65
118
  diagnostics?: SomaDiagnostics;
66
119
  telemetry?: SomaTelemetry;
67
120
  assetResolver?: SomaAssetResolver;
121
+ /**
122
+ * Host 级组件注册(组件体系模块 7)。
123
+ * 经 `@soma-host/bootstrap/soma-bootstrap.ts` 的 configureSomaBootstrap 注入,
124
+ * 对该 Host 的所有页面生效;页面资产 components 同名时覆盖 Host 级定义。
125
+ */
126
+ components?: Record<string, SomaComponentDefinition>;
127
+ /**
128
+ * Host 自定义格式化管道(模块 7 U4):{ 管道名: 纯函数 },与内置管道合并。
129
+ * 内置:money / tpl / date / percent / plural。
130
+ */
131
+ pipes?: Record<string, SomaPipeFn>;
132
+ /**
133
+ * 阶梯词汇注册位(F-09 / 契约 C5),与组件注册同点:
134
+ * `adaptiveVocabulary: createSomaUiAdaptiveVocabulary()`。
135
+ *
136
+ * core **不内置任何级语义**——语法归语言、词汇归组件。不注册而页面声明了
137
+ * `adaptive.ladder` 时按"词汇未知"快速失败,而不是静默忽略那些级。
138
+ */
139
+ adaptiveVocabulary?: SomaAdaptiveVocabulary;
68
140
  /** Render-engine middleware chain (opt-in) */
69
141
  renderMiddleware?: SomaMiddlewareChain<SomaRenderMiddlewareContext>;
70
142
  /**
@@ -72,6 +144,20 @@ export interface SomaBootstrapOptions {
72
144
  * Use `createPermissionMiddleware` when events declare `permission:code` in pipeline.
73
145
  */
74
146
  eventMiddleware?: SomaMiddlewareChain<SomaEventMiddlewareContext>;
147
+ /**
148
+ * 错误采集接线(默认关闭:不给就一个全局监听都不装)。
149
+ * 库不该擅自劫持 window 的错误钩子,是否采集由 Host 显式决定。
150
+ */
151
+ errorReporting?: SomaBootstrapErrorReporting;
152
+ // 主题不在此:无 theme / themeEngine。声明走 appConfig.theme,函数走 applyTheme(引擎内建)。
153
+ }
154
+
155
+ /**
156
+ * 缺省上报通道:单行 JSON 进 console.error。
157
+ * 单行 JSON 而不是人读叙述——服务端日志采集器 / jq 能直接聚合(日志规范 §5.2)。
158
+ */
159
+ function reportToConsole(event: SomaErrorEvent): void {
160
+ console.error(`[soma-core][可观测] ${JSON.stringify(event)}`);
75
161
  }
76
162
 
77
163
  export interface ParsedRuntimeAssets {
@@ -107,17 +193,38 @@ export class SomaBootstrap {
107
193
  private runtimeResolver: SomaScopeResolver | null = null;
108
194
  private proxy: SomaReactiveProxy | null = null;
109
195
  private domainInstance: SomaPageRoot | null = null;
196
+ private componentManager: SomaComponentManager | null = null;
197
+ private hostComponents: Record<string, SomaComponentDefinition> = {};
198
+ private pipeRegistry: SomaPipeRegistry;
199
+ /** 阶梯词汇表:Host 级注册,对该 Host 全部页面生效;未注册时恒为 undefined */
200
+ private readonly adaptiveVocabulary?: SomaAdaptiveVocabulary;
201
+ /** 数据对象层:当前页面的声明(无 *_data.yaml 时为 null,整条路径旁路) */
202
+ private dataConfig: SomaDataConfig | null = null;
203
+ /** 数据对象层:最近一次取数任务(whenDataReady 暴露给测试/调试) */
204
+ private dataReady: Promise<void> | null = null;
205
+ /** 数据对象层:字段字典产物(soma-ui 双态映射消费) */
206
+ private dataDicts: SomaDataDicts = {};
110
207
  private mounted = false;
111
208
  private diagnostics: SomaDiagnostics;
112
209
  private readonly telemetry: SomaTelemetry;
113
210
  private container: HTMLElement | null = null;
114
211
  private assets: SomaPageAssets | null = null;
212
+ /** 错误采集配置(null = 未开启,整条路径旁路) */
213
+ private readonly errorReporting: SomaBootstrapErrorReporting | null;
214
+ /** 错误采集句柄:mount 时装、cleanup 时摘,生命周期与页面严格同寿 */
215
+ private errorReporter: SomaErrorReporterHandle | null = null;
115
216
  constructor(options: SomaBootstrapOptions = {}) {
116
217
  this.adapter = options.adapter ?? new SomaContainerAdapter();
117
218
  this.scheduler = options.scheduler ?? new SomaScheduler();
118
219
  this.diagnostics = options.diagnostics ?? new SomaDiagnostics();
119
220
  this.telemetry = options.telemetry ?? new SomaTelemetry();
120
- this.assetResolver = options.assetResolver ?? new SomaAssetResolver();
221
+ this.adaptiveVocabulary = options.adaptiveVocabulary;
222
+ this.assetResolver = options.assetResolver
223
+ ?? new SomaAssetResolver(undefined, { adaptiveVocabulary: this.adaptiveVocabulary });
224
+ this.hostComponents = options.components ?? {};
225
+ this.pipeRegistry = new SomaPipeRegistry(options.pipes);
226
+ // 复制一份:身份会随 setErrorReportingPage 变,不该改到调用方手里的对象
227
+ this.errorReporting = options.errorReporting ? { ...options.errorReporting } : null;
121
228
 
122
229
  this.renderMiddleware = options.renderMiddleware ?? new SomaMiddlewareChain();
123
230
 
@@ -126,7 +233,13 @@ export class SomaBootstrap {
126
233
 
127
234
  async mount(container: HTMLElement, assets: SomaPageAssets): Promise<void> {
128
235
  const mountStarted = globalThis.performance?.now?.() ?? Date.now();
236
+ // 首屏三段打点:走到这里资产已经在手(宿主若在拉取前 startBootTimings,段一即覆盖网络耗时;
237
+ // 宿主直接带资产调 mount 时段一趋近 0,属实测事实而非误差)。默认未开启时这两行是两次布尔判断。
238
+ maybeStartBootTimings();
239
+ markBootPhase('assetsReady');
129
240
  setActiveTelemetry(this.telemetry);
241
+ // 采集器先于一切装上:挂载途中炸掉的错误同样要能归因到页面
242
+ this.setupErrorReporting();
130
243
  try {
131
244
  // 仅当未注入时才创建新的 diagnostics
132
245
  if (!this.mounted) {
@@ -141,9 +254,19 @@ export class SomaBootstrap {
141
254
  // 2. 挂载 HTML
142
255
  this.adapter.mount(container, assets.html, assets.includes);
143
256
 
257
+ // 2.5 组件展开(页面引擎构建前:模板注入、锚点作用域化、插槽投影)
258
+ this.setupComponentManager(container, assets, parsed.uiConfig);
259
+
260
+ // 2.6 主题派生与注入(app.config theme: { brand } 一行换装,缺省旁路)
261
+ this.applyHostTheme(parsed.appConfig);
262
+
144
263
  // 3. 创建领域实例并注入配置
145
264
  this.domainInstance = this.createDomainInstance(assets.DomainClass, parsed, assets.routerPath);
146
265
 
266
+ // 3.5 数据对象层(五件套可选件):首渲前解析声明并注入 reloadData 钩子;
267
+ // 声明非法只告警不阻断挂载(data 保持空对象)
268
+ this.prepareDataLayer(this.domainInstance, assets);
269
+
147
270
  // 4. 包装响应式代理
148
271
  this.proxy = new SomaReactiveProxy(undefined, this.diagnostics);
149
272
  this.domainInstance = this.proxy.wrap(this.domainInstance);
@@ -151,6 +274,10 @@ export class SomaBootstrap {
151
274
  // 5. 构建运行时(委托给 RuntimeBuilder)
152
275
  await this.buildRuntime(parsed);
153
276
 
277
+ // 5.5 数据对象层:异步取数(不阻塞首屏),完成后注入 page.data 并全量渲染
278
+ if (this.dataConfig) {
279
+ void this.loadDataObjects();
280
+ }
154
281
  if (typeof this.domainInstance.onMount === 'function') {
155
282
  await this.domainInstance.onMount();
156
283
  }
@@ -158,7 +285,9 @@ export class SomaBootstrap {
158
285
  this.mounted = true;
159
286
  recordMetric('bootstrap.mount', (globalThis.performance?.now?.() ?? Date.now()) - mountStarted);
160
287
  } catch (error) {
161
- console.error('[SomaBootstrap] Mount failed:', error);
288
+ console.error('[soma-core][引擎] 页面挂载失败:', error);
289
+ // 必须在 cleanup 之前上报:cleanup 会摘掉采集器
290
+ this.captureEngineError(error);
162
291
  this.cleanup();
163
292
  throw error;
164
293
  }
@@ -196,7 +325,19 @@ export class SomaBootstrap {
196
325
  }
197
326
 
198
327
  const parsed = this.assetResolver.resolve(merged);
328
+
329
+ // 旧页面的组件实例必须在 DOM 被换掉**之前**销毁。
330
+ // adapter.mount 是 `container.innerHTML = html`:跑完之后旧宿主已是游离节点,
331
+ // 此时才触发的 ctrl.onUnmount 里,归还焦点、解锁滚动、按 host 定位的
332
+ // portal/observer 清理全部静默落空(游离节点上 focus() 是 no-op,不报错);
333
+ // 更坏的是 `document.querySelector` 这类反查会**命中新页面的同名节点**,
334
+ // 清理动作落到刚挂上来的新 DOM 上——不报错,还误伤。
335
+ // setupComponentManager 里那次 destroy() 是防御性兜底,删这一行会把上述后果全部放回来。
336
+ this.componentManager?.destroy();
337
+ this.componentManager = null;
338
+
199
339
  this.adapter.mount(this.container, merged.html, merged.includes);
340
+ this.setupComponentManager(this.container, merged, parsed.uiConfig);
200
341
 
201
342
  if (overrides.DomainClass) {
202
343
  this.proxy?.dispose();
@@ -218,6 +359,12 @@ export class SomaBootstrap {
218
359
  await this.buildRuntime(parsed);
219
360
  this.startDeclarativeTimers();
220
361
 
362
+ // 数据对象层:hotSwap 后按合并资产重建声明并重取(无声明时零路径变化)
363
+ this.prepareDataLayer(this.domainInstance, merged);
364
+ if (this.dataConfig) {
365
+ void this.loadDataObjects();
366
+ }
367
+
221
368
  if (snapshotId !== null) {
222
369
  try {
223
370
  this.restoreSnapshot(snapshotId);
@@ -267,7 +414,8 @@ export class SomaBootstrap {
267
414
  this.domainInstance.onUnmount();
268
415
  }
269
416
  } catch (error) {
270
- console.error('[SomaBootstrap] onUnmount error:', error);
417
+ console.error('[soma-core][引擎] onUnmount 回调异常:', error);
418
+ this.captureEngineError(error);
271
419
  }
272
420
 
273
421
  this.cleanup();
@@ -324,6 +472,16 @@ export class SomaBootstrap {
324
472
  return this.renderEngine?.getRegisteredSomaIds() ?? [];
325
473
  }
326
474
 
475
+ /** 数据对象层字段字典产物(对象名 → 字段名 → 字典条目;无声明时为空对象) */
476
+ getDataDicts(): SomaDataDicts {
477
+ return this.dataDicts;
478
+ }
479
+
480
+ /** 等待当前数据对象取数任务完成(测试 / 调试;无声明时立即 resolve) */
481
+ whenDataReady(): Promise<void> {
482
+ return this.dataReady ?? Promise.resolve();
483
+ }
484
+
327
485
  /** 获取渲染引擎(测试 / 调试) */
328
486
  getRenderEngine(): SomaRenderEngine | null {
329
487
  return this.renderEngine;
@@ -337,8 +495,48 @@ export class SomaBootstrap {
337
495
  return this.renderEngine.watch(path, callback, options);
338
496
  }
339
497
 
498
+ /**
499
+ * 更新错误采集的页面身份。
500
+ * 声明态页面"发布新版本 → 原地重渲"时调用,后续错误立刻落在新版本上——
501
+ * 这一个调用就是"哪个版本引入的错误"这个问题的全部答案。
502
+ * 未开启采集时是安全的空操作。
503
+ */
504
+ setErrorReportingPage(page: SomaPageIdentity): void {
505
+ if (this.errorReporting) {
506
+ this.errorReporting.page = page;
507
+ }
508
+ this.errorReporter?.setPage(page);
509
+ }
510
+
340
511
  // ─── 私有方法 ──────────────────────────────────────────
341
512
 
513
+ /** 装(或复用)错误采集器;未配置时整条路径旁路,一个全局监听都不装 */
514
+ private setupErrorReporting(): void {
515
+ if (!this.errorReporting) {
516
+ return;
517
+ }
518
+ if (this.errorReporter) {
519
+ // 同一 bootstrap 重复 mount / hotSwap:只换身份,不重复挂全局监听
520
+ this.errorReporter.setPage(this.errorReporting.page);
521
+ return;
522
+ }
523
+ this.errorReporter = installSomaErrorReporter({
524
+ page: this.errorReporting.page,
525
+ report: this.errorReporting.report ?? reportToConsole,
526
+ dedupeWindowMs: this.errorReporting.dedupeWindowMs,
527
+ maxEvents: this.errorReporting.maxEvents,
528
+ });
529
+ }
530
+
531
+ /**
532
+ * 引擎自身捕获的异常进同一采集通道。
533
+ * 全局 error 监听抓不到这些——它们已经被引擎 catch 住了(页面不崩,但确实错了),
534
+ * 不显式补一条,可观测数据就会漏掉最该看的那类问题。
535
+ */
536
+ private captureEngineError(error: unknown): void {
537
+ this.errorReporter?.capture(error, 'runtime');
538
+ }
539
+
342
540
  private createDomainInstance(
343
541
  DomainClass: new () => SomaPageRoot,
344
542
  parsed: ParsedRuntimeAssets,
@@ -370,6 +568,133 @@ export class SomaBootstrap {
370
568
  return raw;
371
569
  }
372
570
 
571
+ // ─── 数据对象层(*_data.yaml,可选件) ─────────────────────
572
+
573
+ /**
574
+ * 首渲前解析数据对象声明并向 Domain 实例注入 reloadData 钩子。
575
+ * 无声明(dataConfig/dataYaml 均缺省)时整条路径旁路,加载零变化;
576
+ * 声明非法只 console.error,页面继续以空 data 挂载(不阻塞)。
577
+ */
578
+ private prepareDataLayer(instance: SomaPageRoot, assets: SomaPageAssets): void {
579
+ this.dataConfig = null;
580
+ this.dataReady = null;
581
+ this.dataDicts = {};
582
+
583
+ const hasDeclaration = assets.dataConfig !== undefined
584
+ || (typeof assets.dataYaml === 'string' && assets.dataYaml.trim() !== '');
585
+ if (!hasDeclaration) return;
586
+
587
+ try {
588
+ const config = assets.dataConfig ?? parseDataYaml(assets.dataYaml!);
589
+ if (Object.keys(config.objects).length === 0) return;
590
+ this.dataConfig = config;
591
+ } catch (error) {
592
+ console.error('[soma-core][数据] 数据对象声明解析失败(页面继续以空 data 挂载):', error);
593
+ this.captureEngineError(error);
594
+ return;
595
+ }
596
+
597
+ instance._reloadData = () => this.loadDataObjects();
598
+ }
599
+
600
+ /**
601
+ * 执行数据对象取数:解析完成后注入 page.data、缓存字段字典并触发一次全量渲染。
602
+ * 任何失败均不阻塞页面:data 置空对象 + console.error。
603
+ */
604
+ private loadDataObjects(): Promise<void> {
605
+ const instance = this.domainInstance;
606
+ const config = this.dataConfig;
607
+ const services = instance?._services;
608
+ if (!instance || !config || !services) {
609
+ return Promise.resolve();
610
+ }
611
+
612
+ // api 声明允许写全路径(如 /api/order/detail):与 http 服务 baseURL 重叠时去重,
613
+ // 避免拼出 /api/api/... 的双前缀
614
+ const baseURL = String(services.config.get('apiBaseUrl') ?? '/api');
615
+ const http = {
616
+ get: <T,>(url: string, requestConfig?: { params?: Record<string, string> }) =>
617
+ services.http.get<T>(
618
+ baseURL && baseURL !== '/' && url.startsWith(`${baseURL}/`) ? url.slice(baseURL.length) : url,
619
+ requestConfig,
620
+ ),
621
+ };
622
+
623
+ const task = Promise.resolve()
624
+ .then(() => {
625
+ const resolver = new SomaDataObjectResolver(config, {
626
+ http,
627
+ query: (key) => readQueryParam(key, services.router.getCurrentPath()),
628
+ page: () => this.domainInstance,
629
+ pipes: this.pipeRegistry,
630
+ locale: () => instance._locale as Record<string, unknown> | undefined,
631
+ });
632
+ return resolver.resolve();
633
+ })
634
+ .then((result) => {
635
+ if (this.domainInstance !== instance) return; // 页面已卸载/热换,丢弃过期结果
636
+ this.dataDicts = result.dicts;
637
+ this.domainInstance.data = result.objects;
638
+ this.renderEngine?.render();
639
+ })
640
+ .catch((error) => {
641
+ console.error('[soma-core][数据] 数据对象解析失败(data 保持空对象):', error);
642
+ this.captureEngineError(error);
643
+ if (this.domainInstance === instance) {
644
+ this.domainInstance.data = {};
645
+ this.renderEngine?.render();
646
+ }
647
+ });
648
+
649
+ this.dataReady = task;
650
+ return task;
651
+ }
652
+
653
+ /**
654
+ * 主题派生注入:app.config 声明 theme.brand 时全库换装;配置非法只告警不阻断挂载。
655
+ * 主题是引擎内建路径,不是 bootstrap 可注册能力。
656
+ */
657
+ private applyHostTheme(appConfig: unknown): void {
658
+ const theme = (appConfig as { theme?: SomaThemeInput } | undefined)?.theme;
659
+ if (!theme?.brand) {
660
+ return;
661
+ }
662
+ try {
663
+ applyTheme(theme);
664
+ } catch (error) {
665
+ console.error('[soma-core][主题] 主题派生失败(保持默认令牌):', error);
666
+ this.captureEngineError(error);
667
+ }
668
+ }
669
+
670
+ /** 组件管理器装配:无 components 资产时保持 null,组件能力整体旁路 */
671
+ private setupComponentManager(
672
+ container: HTMLElement,
673
+ assets: SomaPageAssets,
674
+ uiConfig: UiConfig,
675
+ ): void {
676
+ // 兜底销毁:真正的销毁时机由调用方负责(hotSwap 必须在 adapter.mount 之前销毁,
677
+ // 否则 onUnmount 拿到的是游离宿主)。这里保留是为了任何新调用方都不会漏销毁。
678
+ this.componentManager?.destroy();
679
+ this.componentManager = null;
680
+
681
+ // Host 级注册(构造选项)+ 页面级资产,页面同名覆盖
682
+ const defs = { ...this.hostComponents, ...(assets.components ?? {}) };
683
+ if (Object.keys(defs).length === 0) {
684
+ return;
685
+ }
686
+
687
+ const registry = new SomaComponentRegistry();
688
+ registry.registerAll(defs);
689
+ this.componentManager = new SomaComponentManager({
690
+ registry,
691
+ scheduler: this.scheduler,
692
+ diagnostics: this.diagnostics,
693
+ pipeRegistry: this.pipeRegistry,
694
+ });
695
+ this.componentManager.prepare(container, uiConfig);
696
+ }
697
+
373
698
  private async buildRuntime(parsed: ParsedRuntimeAssets): Promise<void> {
374
699
  if (!this.container || !this.domainInstance) return;
375
700
 
@@ -394,6 +719,8 @@ export class SomaBootstrap {
394
719
  diagnostics: this.diagnostics,
395
720
  renderMiddleware: this.renderMiddleware,
396
721
  eventMiddleware: this.eventMiddleware,
722
+ pipeRegistry: this.pipeRegistry,
723
+ adaptiveVocabulary: this.adaptiveVocabulary,
397
724
  });
398
725
 
399
726
  this.parsedTimers = parsed.timers;
@@ -406,6 +733,26 @@ export class SomaBootstrap {
406
733
  this.triggerEngine = runtime.triggerEngine;
407
734
  this.timerEngine = runtime.timerEngine;
408
735
 
736
+ // 组件实例挂载(页面引擎就绪后:emit 路由标记、props watch、行内钩子)
737
+ this.componentManager?.activate({
738
+ container: this.container,
739
+ renderEngine: runtime.renderEngine,
740
+ eventEngine: runtime.eventEngine,
741
+ resolver: runtime.resolver,
742
+ manifest: runtime.manifest,
743
+ services: this.domainInstance._services as unknown as Record<string, unknown> | undefined,
744
+ });
745
+
746
+ // 界面中途提交接线:Domain 在长任务里调 `this.publishUi()` 时,
747
+ // 把此刻积累的 patch 立刻上屏而不结束事件事务(见 SomaRuntimeServices.ui)
748
+ if (this.domainInstance?._services) {
749
+ this.domainInstance._services.ui = {
750
+ publish: async () => {
751
+ await runtime.renderEngine.publishInTransaction();
752
+ },
753
+ };
754
+ }
755
+
409
756
  this.proxy?.setOnChange((path, value, oldValue) => {
410
757
  this.renderEngine?.schedulePatch(path, value);
411
758
  this.styleBinder?.onPropertyChange(path, value);
@@ -447,6 +794,8 @@ export class SomaBootstrap {
447
794
  private cleanup(): void {
448
795
  this.scheduler.cancelAll('unmount');
449
796
  this.stopDeclarativeTimers();
797
+ this.componentManager?.destroy();
798
+ this.componentManager = null;
450
799
  this.proxy?.dispose();
451
800
  this.eventEngine?.destroy();
452
801
  this.renderEngine?.destroy();
@@ -458,6 +807,12 @@ export class SomaBootstrap {
458
807
  this.routerUnsubscribe?.();
459
808
  this.routerUnsubscribe = null;
460
809
 
810
+ this.errorReporter?.dispose();
811
+ this.errorReporter = null;
812
+
813
+ this.dataConfig = null;
814
+ this.dataReady = null;
815
+ this.dataDicts = {};
461
816
  this.eventEngine = null;
462
817
  this.renderEngine = null;
463
818
  this.styleBinder = null;
@@ -9,7 +9,53 @@ import type {
9
9
  StyleTransition,
10
10
  ThemeConfig,
11
11
  } from '../../shared/types/style-config.types';
12
- import { applyThemeVariables } from './theme-variables';
12
+
13
+ // ─── 内置主题令牌 ─────────────────────────────────────────────────────────────
14
+
15
+ /**
16
+ * `_style.yaml` 内置 dark / light 主题落到的 CSS 变量。
17
+ *
18
+ * 这里的变量名必须是**主题引擎词汇表里的名字**:此前用的
19
+ * `--soma-bg` / `--soma-fg` / `--soma-primary` 四个名字与引擎词汇不通,
20
+ * 全仓无人消费,等于换了主题什么也没发生(而 `--soma-border` 更糟——
21
+ * 它与引擎同名,主题一切就把引擎派生的边框色顶掉)。
22
+ * 对外语法不变,改的只是内部落到的令牌名。
23
+ */
24
+ const BUILTIN_THEME_VARS: Record<string, Record<string, string>> = {
25
+ dark: {
26
+ '--soma-bg-page': '#111827',
27
+ '--soma-text-primary': '#f9fafb',
28
+ '--soma-c-primary': '#60a5fa',
29
+ '--soma-border': '#374151',
30
+ },
31
+ light: {
32
+ '--soma-bg-page': '#ffffff',
33
+ '--soma-text-primary': '#111827',
34
+ '--soma-c-primary': '#3b82f6',
35
+ '--soma-border': '#e5e7eb',
36
+ },
37
+ };
38
+
39
+ /** _style.yaml 主题切换时注入 CSS 变量(样式类仍由 Tailwind 提供) */
40
+ function applyThemeVariables(
41
+ themeName: string | null,
42
+ themeConfig?: ThemeConfig,
43
+ root?: HTMLElement,
44
+ ): void {
45
+ const el = root ?? (typeof document !== 'undefined' ? document.documentElement : null);
46
+ if (!el) return;
47
+
48
+ const vars = !themeName
49
+ ? BUILTIN_THEME_VARS.light
50
+ : {
51
+ ...(BUILTIN_THEME_VARS[themeName] ?? {}),
52
+ ...(themeConfig?.vars ?? {}),
53
+ };
54
+
55
+ for (const [key, value] of Object.entries(vars)) {
56
+ el.style.setProperty(key, value);
57
+ }
58
+ }
13
59
 
14
60
  // ─── 内部类型 ─────────────────────────────────────────────────────────────────
15
61
 
@@ -0,0 +1,17 @@
1
+ # 空数据目录(框架内置回退)
2
+
3
+ 宿主没有 `data/` 目录时,`@soma-host/data` 别名指向这里。
4
+
5
+ 数据对象层(`*_data.yaml`)是可选能力。`SomaPageClient` 把四件套与 data 一次性并发
6
+ `import()`,webpack 需要为 `@soma-host/data/${base}_data.yaml` 建立 context;目录不存在
7
+ 时它不会安静跳过,而是每次页面加载都报 `Module not found: Can't resolve '@soma-host/data'`。
8
+ 运行时有 `.catch(() => null)` 兜底,页面照常渲染——于是这条错误既不影响功能,又永远刷在
9
+ 控制台里,把真正该看见的错误淹掉。
10
+
11
+ 这个目录让 context 成立且为空:`import()` 找不到具体文件 → 走 catch → `dataConfig` 为
12
+ `undefined`,与"该宿主没有数据对象层"的语义一致。
13
+
14
+ 目录名必须是 `data`:`SomaPageClient` 的 `webpackInclude` 正则是 `/\/data\/.*_data\.yaml$/`,
15
+ 匹配的是解析后的完整路径。
16
+
17
+ 本文件仅用于让 git 跟踪该目录(`.md` 不匹配上述正则,不会被打进 context)。