soma-core 1.3.2 → 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.
- package/README.md +43 -0
- package/app/SomaPageClient.tsx +22 -68
- package/app/[...path]/page.tsx +7 -6
- package/app/api/[...path]/route.ts +3 -3
- package/app/api/soma-registry/[...path]/route.ts +67 -0
- package/app/components/SomaHostPage.tsx +3 -8
- package/app/components/SomaPageClientBoundary.tsx +13 -0
- package/app/layout.tsx +6 -1
- package/app/page.tsx +3 -3
- package/app/soma/manifest/route.ts +1 -0
- package/app/soma/page-object/route.ts +1 -1
- package/bin/soma-lsp.cjs +1 -4
- package/core/build/compiler/full-manifest-builder.ts +19 -23
- 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 -13
- 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 -9
- package/core/build/compiler/soma-precompile.js +3581 -2387
- package/core/build/compiler/soma-precompile.ts +44 -2
- package/core/build/compiler/soma-rspack-yaml-loader.js +5 -8
- package/core/build/compiler/soma-webpack-loader.js +6 -15
- package/core/build/compiler/vite-plugin-soma.ts +9 -21
- package/core/build/component/pack-from-dir.ts +58 -0
- package/core/build/component/package-loader.ts +281 -0
- package/core/build/eject/apply-eject.ts +133 -0
- package/core/build/eject/plan-eject.ts +133 -0
- 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/adaptive/flush-scheduler.ts +96 -0
- package/core/client/adaptive/hysteresis.ts +56 -0
- package/core/client/adaptive/index.ts +23 -0
- package/core/client/adaptive/observer-pool.ts +90 -0
- package/core/client/base/SomaPageRootBase.ts +50 -0
- package/core/client/compiler/adaptive-compile.ts +136 -0
- package/core/client/compiler/manifest-compiler.ts +155 -27
- package/core/client/component/ai-catalog.ts +277 -0
- package/core/client/component/component-definition.types.ts +65 -0
- package/core/client/component/component-expander.ts +119 -0
- package/core/client/component/component-manager.ts +933 -0
- package/core/client/component/component-pack.ts +445 -0
- package/core/client/component/component-registry.ts +71 -0
- package/core/client/component/contract-compat.ts +132 -0
- package/core/client/component/example-v2.ts +132 -0
- package/core/client/component/pack-styles.ts +94 -0
- package/core/client/component/scoped-adapter.ts +85 -0
- package/core/client/component/verify-pack.ts +247 -0
- package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
- package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
- package/core/client/container/SomaContainerAdapter.ts +186 -180
- package/core/client/data/data-object-resolver.ts +417 -0
- package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
- package/core/client/diagnostics/boot-timings.ts +152 -0
- package/core/client/engine/asset-resolver.ts +12 -2
- package/core/client/engine/dom-renderer.ts +249 -24
- package/core/client/engine/emit-event.ts +21 -0
- package/core/client/engine/event-engine.ts +34 -4
- package/core/client/engine/on-change-engine.ts +1 -1
- package/core/client/engine/property-watcher.ts +1 -1
- package/core/client/engine/reactive-proxy.ts +11 -10
- package/core/client/engine/render-engine.ts +39 -4
- package/core/client/engine/runtime-builder.ts +23 -0
- package/core/client/engine/soma-bootstrap.ts +380 -7
- package/core/client/engine/style-binder.ts +47 -1
- package/core/client/observability/error-reporter.ts +173 -0
- package/core/client/parser/adaptive-parser.ts +264 -0
- package/core/client/parser/data-config-parser.ts +396 -0
- package/core/client/parser/pipeline-parser.ts +15 -3
- package/core/client/parser/style-config-parser.ts +65 -2
- package/core/client/parser/ui-config-parser.ts +52 -3
- 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 +249 -0
- package/core/client/quality/SomaQualityRegistry.ts +3 -0
- package/core/client/registry/declaration-renderer.ts +161 -0
- package/core/client/registry/index.ts +28 -0
- package/core/client/registry/page-lifecycle.ts +171 -0
- package/core/client/registry/remote-page.ts +139 -0
- package/core/client/runtime/SomaRuntimeServices.ts +24 -0
- package/core/client/security/attribute-presence.ts +70 -0
- package/core/client/security/html-sanitizer.ts +113 -0
- package/core/client/security/url-guard.ts +0 -0
- 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 +1196 -0
- package/core/client/theme/tone-solver.ts +118 -0
- package/core/client/typography/typography-base.ts +34 -0
- package/core/client/validation/contract-validator.ts +48 -5
- package/core/index.ts +262 -2
- package/core/server/api/IService.ts +18 -6
- package/core/server/api/load-host-service.ts +3 -3
- package/core/server/host/content-security-policy.ts +190 -0
- package/core/server/host/csp-nonce-metadata.ts +75 -0
- package/core/server/host/resolve-host-request-hook.ts +5 -0
- package/core/server/host/server-page-assets.ts +34 -7
- package/core/server/host/soma-page-compile.ts +33 -0
- package/core/server/index.ts +45 -1
- package/core/server/infra/cache/ServerFileCache.ts +1 -1
- package/core/server/mock/node/cli.mjs +2 -2
- package/core/server/registry/api-registry-store.ts +518 -0
- package/core/server/registry/conditional-object-store.ts +29 -0
- package/core/server/registry/declaration-precompiler.ts +99 -0
- package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
- package/core/server/registry/index.ts +142 -0
- package/core/server/registry/page-registry-repository.ts +373 -0
- package/core/server/registry/page-registry-store.ts +932 -0
- package/core/server/registry/registry-error.ts +17 -0
- package/core/server/registry/registry-file-lock.ts +173 -0
- package/core/server/registry/registry-response.ts +71 -0
- package/core/server/registry/registry-subjects.ts +194 -0
- package/core/server/registry/registry-write-pipeline.ts +240 -0
- package/core/server/registry/request-body-limit.ts +99 -0
- package/core/server/registry/services/apis/Get.ts +25 -0
- package/core/server/registry/services/apis/List.ts +41 -0
- package/core/server/registry/services/apis/Register.ts +39 -0
- package/core/server/registry/services/apis/Unregister.ts +24 -0
- package/core/server/registry/services/index.ts +40 -0
- package/core/server/registry/services/pages/Get.ts +96 -0
- package/core/server/registry/services/pages/List.ts +53 -0
- package/core/server/registry/services/pages/Register.ts +49 -0
- package/core/server/registry/services/pages/Rollback.ts +36 -0
- package/core/server/registry/services/pages/Unregister.ts +23 -0
- package/core/server/registry/services/service-utils.ts +21 -0
- package/core/server/registry/validation/compile-gate.ts +54 -0
- package/core/server/registry/validation/component-existence.ts +55 -0
- package/core/server/registry/validation/credential-ban.ts +86 -0
- package/core/server/registry/validation/cross-check.ts +67 -0
- package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
- package/core/shared/types/adaptive.types.ts +109 -0
- package/core/shared/types/component-contract.types.ts +120 -0
- package/core/shared/types/data-config.types.ts +105 -0
- package/core/shared/types/domain.types.ts +6 -0
- package/core/shared/types/engine.types.ts +41 -35
- package/core/shared/types/manifest.types.ts +55 -32
- package/core/shared/types/scope-manifest.types.ts +96 -0
- package/core/shared/types/style-config.types.ts +9 -0
- package/core/shared/types/ui-config.types.ts +118 -98
- package/dist/cli/soma.js +20 -5
- package/dist/core/index.cjs +7811 -1380
- package/dist/types/build/compiler/full-manifest-builder.d.ts +6 -2
- 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 -2
- package/dist/types/build/compiler/route-builder.d.ts +0 -1
- package/dist/types/build/compiler/soma-precompile.d.ts +17 -0
- package/dist/types/build/compiler/vite-plugin-soma.d.ts +2 -7
- package/dist/types/build/component/pack-from-dir.d.ts +17 -0
- package/dist/types/build/component/package-loader.d.ts +40 -0
- package/dist/types/build/eject/apply-eject.d.ts +22 -0
- package/dist/types/build/eject/plan-eject.d.ts +32 -0
- 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/adaptive/flush-scheduler.d.ts +13 -0
- package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
- package/dist/types/client/adaptive/index.d.ts +5 -0
- package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
- package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
- package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
- package/dist/types/client/component/ai-catalog.d.ts +73 -0
- package/dist/types/client/component/component-definition.types.d.ts +54 -0
- package/dist/types/client/component/component-expander.d.ts +25 -0
- package/dist/types/client/component/component-manager.d.ts +160 -0
- package/dist/types/client/component/component-pack.d.ts +110 -0
- package/dist/types/client/component/component-registry.d.ts +12 -0
- 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/component/pack-styles.d.ts +22 -0
- package/dist/types/client/component/scoped-adapter.d.ts +18 -0
- package/dist/types/client/component/verify-pack.d.ts +14 -0
- package/dist/types/client/data/data-object-resolver.d.ts +71 -0
- package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
- package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
- package/dist/types/client/engine/asset-resolver.d.ts +7 -1
- package/dist/types/client/engine/dom-renderer.d.ts +51 -1
- package/dist/types/client/engine/emit-event.d.ts +6 -0
- package/dist/types/client/engine/event-engine.d.ts +6 -0
- package/dist/types/client/engine/render-engine.d.ts +25 -1
- package/dist/types/client/engine/runtime-builder.d.ts +7 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +118 -0
- package/dist/types/client/observability/error-reporter.d.ts +51 -0
- package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
- package/dist/types/client/parser/data-config-parser.d.ts +34 -0
- package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
- package/dist/types/client/parser/style-config-parser.d.ts +5 -3
- package/dist/types/client/parser/ui-config-parser.d.ts +22 -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/pipes/pipe-registry.d.ts +38 -0
- package/dist/types/client/registry/declaration-renderer.d.ts +50 -0
- package/dist/types/client/registry/index.d.ts +10 -0
- package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
- package/dist/types/client/registry/remote-page.d.ts +52 -0
- package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
- package/dist/types/client/security/attribute-presence.d.ts +7 -0
- package/dist/types/client/security/html-sanitizer.d.ts +37 -0
- package/dist/types/client/security/url-guard.d.ts +11 -0
- 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 +77 -0
- package/dist/types/client/theme/tone-solver.d.ts +47 -0
- package/dist/types/client/typography/typography-base.d.ts +15 -0
- package/dist/types/client/validation/contract-validator.d.ts +2 -0
- package/dist/types/index.d.ts +47 -2
- package/dist/types/server/api/IService.d.ts +18 -6
- package/dist/types/server/host/content-security-policy.d.ts +53 -0
- package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
- package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
- package/dist/types/server/host/server-page-assets.d.ts +2 -0
- package/dist/types/server/host/soma-page-compile.d.ts +7 -0
- package/dist/types/server/index.d.ts +6 -1
- package/dist/types/server/registry/api-registry-store.d.ts +141 -0
- package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
- package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
- package/dist/types/server/registry/index.d.ts +29 -0
- package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
- package/dist/types/server/registry/page-registry-store.d.ts +259 -0
- package/dist/types/server/registry/registry-error.d.ts +12 -0
- package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
- package/dist/types/server/registry/registry-response.d.ts +18 -0
- package/dist/types/server/registry/registry-subjects.d.ts +59 -0
- package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
- package/dist/types/server/registry/request-body-limit.d.ts +31 -0
- package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
- package/dist/types/server/registry/services/apis/List.d.ts +10 -0
- package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
- package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
- package/dist/types/server/registry/services/index.d.ts +3 -0
- package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
- package/dist/types/server/registry/services/pages/List.d.ts +13 -0
- package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
- package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
- package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
- package/dist/types/server/registry/services/service-utils.d.ts +10 -0
- package/dist/types/server/registry/validation/compile-gate.d.ts +12 -0
- package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
- package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
- package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
- package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
- package/dist/types/shared/types/adaptive.types.d.ts +88 -0
- package/dist/types/shared/types/component-contract.types.d.ts +78 -0
- package/dist/types/shared/types/data-config.types.d.ts +81 -0
- package/dist/types/shared/types/domain.types.d.ts +6 -0
- package/dist/types/shared/types/engine.types.d.ts +6 -0
- package/dist/types/shared/types/manifest.types.d.ts +23 -0
- package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
- package/dist/types/shared/types/style-config.types.d.ts +8 -0
- package/dist/types/shared/types/ui-config.types.d.ts +20 -0
- package/editor/vscode/package.json +1 -24
- package/global.d.ts +5 -11
- package/next.config.js +85 -28
- package/package.json +33 -11
- package/proxy.ts +62 -0
- package/tailwind.config.ts +0 -1
- package/core/build/lsp/event-yaml-completion.cjs +0 -106
- package/core/client/engine/theme-variables.ts +0 -37
- package/dist/tsconfig.types.tsbuildinfo +0 -1
- package/dist/types/client/engine/theme-variables.d.ts +0 -3
- package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +0 -30
- package/middleware.ts +0 -33
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
// 组件包:让 soma-ui 之外的业务组件库像 soma-ui 一样被使用。
|
|
2
|
+
//
|
|
3
|
+
// 今天一个第三方库其实已经能被 spread 进宿主的 components 表里,能跑——
|
|
4
|
+
// 但有四个**静默**的坑,每一个都不报错:
|
|
5
|
+
// 1. 重名:两个库都叫 Card,后 spread 的赢,页面用的是哪个只能靠顺序猜;
|
|
6
|
+
// 2. AI 不认识:组件目录只从 soma-ui 生成,第三方组件形同不存在;
|
|
7
|
+
// 3. 样式装配:按需注入是 soma-ui 私有的,第三方要么全量注入要么没样式;
|
|
8
|
+
// 4. 质量漂移:裸色值让换肤失效、物理方向属性让 RTL 错位——都不报错。
|
|
9
|
+
//
|
|
10
|
+
// 本模块把这四件事变成显式契约:包交出完整描述,平台负责合成、查重、
|
|
11
|
+
// 喂目录、登记样式,并给一套可自检的闸门(verifyComponentPack)。
|
|
12
|
+
//
|
|
13
|
+
// 一条刻意的默认值:**重名直接抛错**,不做"后者胜出"。静默覆盖的排查成本极高
|
|
14
|
+
// (页面渲染诡异,而所有测试都绿);要覆盖就显式写出来——
|
|
15
|
+
// 写出来的覆盖是设计决定,猜出来的覆盖是事故。
|
|
16
|
+
//
|
|
17
|
+
// 覆盖有两条显式通道,都不是"猜":
|
|
18
|
+
// ① 契约上写 `overrides: <被覆盖的包名>`——意图与组件同处、随组件评审、宿主零维护;
|
|
19
|
+
// ② 宿主 bootstrap 传 allowOverride——宿主的强裁决,优先级最高。
|
|
20
|
+
// 两条通道产出的覆盖都必须过契约兼容闸门(props 超集 + emit 全保留),
|
|
21
|
+
// 否则"优化存量组件、页面零改动"就只是运气。
|
|
22
|
+
//
|
|
23
|
+
// 接入指南见 external-docs/soma-组件包接入指南.md。
|
|
24
|
+
|
|
25
|
+
import type { SomaComponentDefinition } from './component-definition.types';
|
|
26
|
+
import { generateAiCatalog, type SomaAiCatalog } from './ai-catalog';
|
|
27
|
+
import { attachPackStyles } from './pack-styles';
|
|
28
|
+
import { checkOverrideCompat } from './contract-compat';
|
|
29
|
+
|
|
30
|
+
/** 包标识:小写字母/数字/连字符——冲突报错与来源登记都用它 */
|
|
31
|
+
export const PACK_NAME_PATTERN = /^[a-z][a-z0-9-]*$/;
|
|
32
|
+
|
|
33
|
+
export interface SomaComponentPackStyles {
|
|
34
|
+
/** 组件名 → 该组件独有的 CSS(首次实例化时按需注入) */
|
|
35
|
+
components?: Record<string, string>;
|
|
36
|
+
/** 共享分片名 → CSS(被多个组件依赖的资产;不要复制进组件分片) */
|
|
37
|
+
shared?: Record<string, string>;
|
|
38
|
+
/** 组件名 → 依赖的共享分片名列表 */
|
|
39
|
+
deps?: Record<string, string[]>;
|
|
40
|
+
/** 包级基座 CSS(恒定注入;克制使用——它是每个页面首屏都要付的钱) */
|
|
41
|
+
base?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface SomaComponentPack {
|
|
45
|
+
/** 包标识(小写字母/数字/连字符) */
|
|
46
|
+
name: string;
|
|
47
|
+
version: string;
|
|
48
|
+
/** 组件名 → 运行时定义,与 createSomaUiComponents() 同构 */
|
|
49
|
+
components: Record<string, SomaComponentDefinition>;
|
|
50
|
+
/** 按需装配的样式资产(可选) */
|
|
51
|
+
styles?: SomaComponentPackStyles;
|
|
52
|
+
/** 并进组件目录的写作规范补充(可选) */
|
|
53
|
+
catalogConventions?: string;
|
|
54
|
+
meta?: {
|
|
55
|
+
description?: string;
|
|
56
|
+
homepage?: string;
|
|
57
|
+
license?: string;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class ComponentPackError extends Error {
|
|
62
|
+
constructor(message: string) {
|
|
63
|
+
super(`[SomaComponentPack] ${message}`);
|
|
64
|
+
this.name = 'ComponentPackError';
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 一条生效的覆盖:谁替了谁、凭什么 */
|
|
69
|
+
export interface PackOverrideEvent {
|
|
70
|
+
name: string;
|
|
71
|
+
winner: string;
|
|
72
|
+
winnerVersion: string;
|
|
73
|
+
shadowed: Array<{ pack: string; version: string }>;
|
|
74
|
+
/** contract = 契约上写的 overrides;allowOverride = 宿主 bootstrap 的强裁决 */
|
|
75
|
+
source: 'contract' | 'allowOverride';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface ComposePacksOptions {
|
|
79
|
+
/**
|
|
80
|
+
* 显式覆盖:组件名 → 胜出的包名。宿主的强裁决,优先级高于契约声明。
|
|
81
|
+
* 不在这张表、也没有契约声明的重名一律抛错——默认拒绝,
|
|
82
|
+
* 而不是让 spread 顺序决定渲染结果。
|
|
83
|
+
*/
|
|
84
|
+
allowOverride?: Record<string, string>;
|
|
85
|
+
/**
|
|
86
|
+
* 覆盖生效时的通知(缺省打一行 console.info,带双方版本)。
|
|
87
|
+
* 版本必须在日志里:排障时"页面上跑的是哪个 DataTable"全靠这一行。
|
|
88
|
+
* 测试与静默场景传自己的回调。
|
|
89
|
+
*/
|
|
90
|
+
onOverride?: (event: PackOverrideEvent) => void;
|
|
91
|
+
/**
|
|
92
|
+
* 按需样式装配(缺省开)。开着时返回的组件定义已经包好——首次实例化才注入 CSS。
|
|
93
|
+
* 关掉只有一种正当理由:宿主自己接管样式注入。**不要因为"在 Node 里跑"而关它**——
|
|
94
|
+
* 包装只在实例化那一刻碰 document,合成本身是纯数据操作。
|
|
95
|
+
*/
|
|
96
|
+
lazyStyles?: boolean;
|
|
97
|
+
/** CSP nonce,透传给注入的 <style> */
|
|
98
|
+
styleNonce?: string | null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface ComposedComponentPacks {
|
|
102
|
+
/** 合成后的组件表,直接喂 bootstrap 的 components */
|
|
103
|
+
components: Record<string, SomaComponentDefinition>;
|
|
104
|
+
/** 组件名 → 来源包名。排障、目录展示、闸门报错都用它 */
|
|
105
|
+
provenance: Record<string, string>;
|
|
106
|
+
/** 合成后的按需装配样式(键已按包做过冲突检查) */
|
|
107
|
+
styles: Required<Omit<SomaComponentPackStyles, 'base'>> & { base: string[] };
|
|
108
|
+
/** 参与合成的包(顺序保留;目录生成用) */
|
|
109
|
+
packs: SomaComponentPack[];
|
|
110
|
+
/** 被显式覆盖的组件名 → { 胜出包, 被盖掉的包, 覆盖来源 } */
|
|
111
|
+
overrides: Record<string, {
|
|
112
|
+
winner: string;
|
|
113
|
+
shadowed: string[];
|
|
114
|
+
source: 'contract' | 'allowOverride';
|
|
115
|
+
}>;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function assertPack(pack: SomaComponentPack): void {
|
|
119
|
+
if (!pack || typeof pack !== 'object') {
|
|
120
|
+
throw new ComponentPackError('组件包必须是对象');
|
|
121
|
+
}
|
|
122
|
+
if (typeof pack.name !== 'string' || !PACK_NAME_PATTERN.test(pack.name)) {
|
|
123
|
+
throw new ComponentPackError(`包名 "${String(pack?.name)}" 必须匹配 [a-z][a-z0-9-]*`);
|
|
124
|
+
}
|
|
125
|
+
if (typeof pack.version !== 'string' || !pack.version.trim()) {
|
|
126
|
+
throw new ComponentPackError(`包 "${pack.name}" 缺少 version`);
|
|
127
|
+
}
|
|
128
|
+
if (!pack.components || typeof pack.components !== 'object' || Array.isArray(pack.components)) {
|
|
129
|
+
throw new ComponentPackError(`包 "${pack.name}" 的 components 必须是 { 组件名: 定义 } 对象`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 合成多个组件包。
|
|
135
|
+
*
|
|
136
|
+
* 重名默认抛错并点名到两个包;要覆盖就在 allowOverride 里写清楚谁赢。
|
|
137
|
+
* 样式分片同理:两个包提供同名共享分片时也拦下——那通常意味着有人复制了别人的资产。
|
|
138
|
+
*/
|
|
139
|
+
export function composeComponentPacks(
|
|
140
|
+
packs: readonly SomaComponentPack[],
|
|
141
|
+
options: ComposePacksOptions = {},
|
|
142
|
+
): ComposedComponentPacks {
|
|
143
|
+
if (!Array.isArray(packs) || packs.length === 0) {
|
|
144
|
+
throw new ComponentPackError('至少要给一个组件包');
|
|
145
|
+
}
|
|
146
|
+
const seenPackNames = new Set<string>();
|
|
147
|
+
for (const pack of packs) {
|
|
148
|
+
assertPack(pack);
|
|
149
|
+
if (seenPackNames.has(pack.name)) {
|
|
150
|
+
throw new ComponentPackError(`包名重复:${pack.name}(同一包不要合成两次)`);
|
|
151
|
+
}
|
|
152
|
+
seenPackNames.add(pack.name);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const components: Record<string, SomaComponentDefinition> = {};
|
|
156
|
+
const provenance: Record<string, string> = {};
|
|
157
|
+
const overrides: ComposedComponentPacks['overrides'] = {};
|
|
158
|
+
const allowOverride = options.allowOverride ?? {};
|
|
159
|
+
|
|
160
|
+
for (const [name, winner] of Object.entries(allowOverride)) {
|
|
161
|
+
if (!seenPackNames.has(winner)) {
|
|
162
|
+
throw new ComponentPackError(
|
|
163
|
+
`allowOverride 指定 "${name}" 由包 "${winner}" 胜出,但该包不在本次合成里`,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const { table: overrideTable, source: overrideSource } = resolveOverrideTable(packs, allowOverride);
|
|
169
|
+
const versionOf = new Map(packs.map((pack) => [pack.name, pack.version]));
|
|
170
|
+
|
|
171
|
+
// 先收齐每个组件名的全部提供者,再逐名裁决。
|
|
172
|
+
// 刻意不在遍历中就地改写 provenance:三个包以上时,"上一个提供者"会被改写成胜者,
|
|
173
|
+
// 于是胜者自己被记进 shadowed、真正被盖掉的包反而漏掉(两个包时碰巧正确)。
|
|
174
|
+
const providers = new Map<string, Array<{ pack: string; definition: SomaComponentDefinition }>>();
|
|
175
|
+
for (const pack of packs) {
|
|
176
|
+
for (const [name, definition] of Object.entries(pack.components) as Array<[string, SomaComponentDefinition]>) {
|
|
177
|
+
const list = providers.get(name) ?? [];
|
|
178
|
+
list.push({ pack: pack.name, definition });
|
|
179
|
+
providers.set(name, list);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
for (const [name, list] of providers) {
|
|
184
|
+
if (list.length === 1) {
|
|
185
|
+
components[name] = list[0].definition;
|
|
186
|
+
provenance[name] = list[0].pack;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const winnerName = overrideTable[name];
|
|
191
|
+
if (winnerName === undefined) {
|
|
192
|
+
const [first, second] = list;
|
|
193
|
+
throw new ComponentPackError(
|
|
194
|
+
`组件名冲突:"${name}" 同时来自包 ${list.map((item) => `"${item.pack}"`).join(' 与 ')}。`
|
|
195
|
+
+ `\n 合成默认拒绝重名——静默覆盖会让"页面用的是哪个组件"只能靠顺序猜。`
|
|
196
|
+
+ '\n 确实要覆盖,两条写法二选一:'
|
|
197
|
+
+ `\n ① 在覆盖方的契约里声明:overrides: '${first.pack}'(意图与组件同处,推荐)`
|
|
198
|
+
+ `\n ② 在宿主 bootstrap 里裁决:composeComponentPacks(..., { allowOverride: { ${name}: '${second.pack}' } })`,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const winner = list.find((item) => item.pack === winnerName);
|
|
203
|
+
if (!winner) {
|
|
204
|
+
throw new ComponentPackError(
|
|
205
|
+
`"${name}" 的覆盖裁决指向包 "${winnerName}",但该包没有提供这个组件`,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
const shadowed = list.filter((item) => item.pack !== winnerName);
|
|
209
|
+
// 兼容闸门:胜者必须收得下页面对**每一个**被盖掉的组件写的 props / emit
|
|
210
|
+
for (const loser of shadowed) {
|
|
211
|
+
assertOverrideCompat(name, winnerName, loser.pack, winner.definition, loser.definition);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
components[name] = winner.definition;
|
|
215
|
+
provenance[name] = winnerName;
|
|
216
|
+
overrides[name] = {
|
|
217
|
+
winner: winnerName,
|
|
218
|
+
shadowed: shadowed.map((item) => item.pack),
|
|
219
|
+
source: overrideSource[name] ?? 'allowOverride',
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
reportOverrides(overrides, versionOf, options.onOverride);
|
|
224
|
+
|
|
225
|
+
const composed: ComposedComponentPacks = {
|
|
226
|
+
components,
|
|
227
|
+
provenance,
|
|
228
|
+
styles: composeStyles(packs, provenance),
|
|
229
|
+
packs: [...packs],
|
|
230
|
+
overrides,
|
|
231
|
+
};
|
|
232
|
+
if (options.lazyStyles === false) return composed;
|
|
233
|
+
return {
|
|
234
|
+
...composed,
|
|
235
|
+
components: attachPackStyles(composed, { nonce: options.styleNonce ?? null }),
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* 收集覆盖表:契约声明 + 宿主裁决 → 组件名 → 胜出包名。
|
|
241
|
+
*
|
|
242
|
+
* 撞名裁决的唯一事实源。四条规则,每条都在拒绝"猜":
|
|
243
|
+
* 悬空声明(目标包不在合成里 / 目标包没这个组件)→ 抛错,
|
|
244
|
+
* 因为声明失效通常意味着目标改名或拼错,静默比报错危险得多;
|
|
245
|
+
* 双覆盖者互撞(两个包都声明要覆盖同一个组件)→ 抛错要求宿主裁决,
|
|
246
|
+
* 自动选边就回到了"猜出来的覆盖";
|
|
247
|
+
* 宿主 allowOverride 最后覆入,永远最高优先——bootstrap 是宿主的地盘。
|
|
248
|
+
*/
|
|
249
|
+
function resolveOverrideTable(
|
|
250
|
+
packs: readonly SomaComponentPack[],
|
|
251
|
+
allowOverride: Record<string, string>,
|
|
252
|
+
): { table: Record<string, string>; source: Record<string, 'contract' | 'allowOverride'> } {
|
|
253
|
+
const table: Record<string, string> = {};
|
|
254
|
+
const source: Record<string, 'contract' | 'allowOverride'> = {};
|
|
255
|
+
const packByName = new Map(packs.map((pack) => [pack.name, pack]));
|
|
256
|
+
|
|
257
|
+
for (const pack of packs) {
|
|
258
|
+
for (const [name, definition] of Object.entries(pack.components) as Array<[string, SomaComponentDefinition]>) {
|
|
259
|
+
const target = definition?.contract?.overrides;
|
|
260
|
+
if (!target) continue;
|
|
261
|
+
|
|
262
|
+
if (target === pack.name) {
|
|
263
|
+
throw new ComponentPackError(
|
|
264
|
+
`包 "${pack.name}" 的组件 "${name}" 声明 overrides: "${target}" 指向自己——`
|
|
265
|
+
+ '覆盖是跨包的事,同包内重名请直接改名',
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
const targetPack = packByName.get(target);
|
|
269
|
+
if (!targetPack) {
|
|
270
|
+
throw new ComponentPackError(
|
|
271
|
+
`组件 "${name}"(来自包 "${pack.name}")声明 overrides: "${target}",`
|
|
272
|
+
+ `但包 "${target}" 不在本次合成里。`
|
|
273
|
+
+ '\n 声明失效多半意味着包名写错或该包没被引入——静默忽略会让你以为覆盖生效了',
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
if (!(name in targetPack.components)) {
|
|
277
|
+
throw new ComponentPackError(
|
|
278
|
+
`组件 "${name}"(来自包 "${pack.name}")声明 overrides: "${target}",`
|
|
279
|
+
+ `但包 "${target}" 里没有叫 "${name}" 的组件。`
|
|
280
|
+
+ '\n 多半是被覆盖的组件改了名——请对齐组件名,或删掉这条 overrides 声明',
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const existing = table[name];
|
|
285
|
+
if (existing !== undefined && existing !== pack.name) {
|
|
286
|
+
if (allowOverride[name] === undefined) {
|
|
287
|
+
throw new ComponentPackError(
|
|
288
|
+
`组件 "${name}" 有两个覆盖者:包 "${existing}" 与包 "${pack.name}" 都声明要覆盖它。`
|
|
289
|
+
+ '\n 平台不替你选边——请在宿主 bootstrap 里裁决:'
|
|
290
|
+
+ `\n composeComponentPacks(..., { allowOverride: { ${name}: '包名' } })`,
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
table[name] = pack.name;
|
|
296
|
+
source[name] = 'contract';
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
for (const [name, winner] of Object.entries(allowOverride)) {
|
|
301
|
+
table[name] = winner;
|
|
302
|
+
source[name] = 'allowOverride';
|
|
303
|
+
}
|
|
304
|
+
return { table, source };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** 兼容闸门:不兼容就抛错并逐条点名,不让"页面零改动"变成运气 */
|
|
308
|
+
function assertOverrideCompat(
|
|
309
|
+
name: string,
|
|
310
|
+
winnerPack: string,
|
|
311
|
+
loserPack: string,
|
|
312
|
+
winnerDefinition: SomaComponentDefinition | undefined,
|
|
313
|
+
loserDefinition: SomaComponentDefinition | undefined,
|
|
314
|
+
): void {
|
|
315
|
+
const winnerContract = winnerDefinition?.contract;
|
|
316
|
+
const loserContract = loserDefinition?.contract;
|
|
317
|
+
if (!winnerContract || !loserContract) return;
|
|
318
|
+
|
|
319
|
+
const issues = checkOverrideCompat(winnerContract, loserContract);
|
|
320
|
+
if (issues.length === 0) return;
|
|
321
|
+
|
|
322
|
+
throw new ComponentPackError(
|
|
323
|
+
`包 "${winnerPack}" 的 "${name}" 覆盖包 "${loserPack}" 的同名组件,但契约不兼容:`
|
|
324
|
+
+ `\n${issues.map((issue) => ` · ${issue.detail}`).join('\n')}`
|
|
325
|
+
+ '\n 存量页面是对着被覆盖者的契约写的——覆盖者必须收得下它们传的 props、'
|
|
326
|
+
+ '发得出它们订阅的 emit。\n (闸门只保证结构兼容,不保证行为等价:渲染细节仍需你自己的测试兜底)',
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** 覆盖日志:带双方版本,排障"页面上跑的是哪个版本"全靠它 */
|
|
331
|
+
function reportOverrides(
|
|
332
|
+
overrides: ComposedComponentPacks['overrides'],
|
|
333
|
+
versionOf: Map<string, string>,
|
|
334
|
+
onOverride?: (event: PackOverrideEvent) => void,
|
|
335
|
+
): void {
|
|
336
|
+
for (const [name, record] of Object.entries(overrides)) {
|
|
337
|
+
const event: PackOverrideEvent = {
|
|
338
|
+
name,
|
|
339
|
+
winner: record.winner,
|
|
340
|
+
winnerVersion: versionOf.get(record.winner) ?? '?',
|
|
341
|
+
shadowed: record.shadowed.map((pack) => ({ pack, version: versionOf.get(pack) ?? '?' })),
|
|
342
|
+
source: record.source,
|
|
343
|
+
};
|
|
344
|
+
if (onOverride) {
|
|
345
|
+
onOverride(event);
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
const shadowed = event.shadowed.map((item) => `${item.pack}@${item.version}`).join('、');
|
|
349
|
+
const via = event.source === 'contract' ? '契约声明' : 'allowOverride';
|
|
350
|
+
// eslint-disable-next-line no-console
|
|
351
|
+
console.info(
|
|
352
|
+
`[soma-core][组件] ${name}: ${event.winner}@${event.winnerVersion} 覆盖 ${shadowed}(${via})`,
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* 合成按需装配的样式资产。
|
|
359
|
+
*
|
|
360
|
+
* 两条检查:
|
|
361
|
+
* - 组件分片的键必须是**本包真的提供的组件**(写错组件名的下场是样式永远不注入,
|
|
362
|
+
* 而页面看起来"就是有点不对",没人能一眼看出来);
|
|
363
|
+
* - 共享分片重名拦下:两个包起了同名共享资产,多半是有人复制了别人的 CSS。
|
|
364
|
+
*/
|
|
365
|
+
function composeStyles(
|
|
366
|
+
packs: readonly SomaComponentPack[],
|
|
367
|
+
provenance: Record<string, string>,
|
|
368
|
+
): ComposedComponentPacks['styles'] {
|
|
369
|
+
const components: Record<string, string> = {};
|
|
370
|
+
const shared: Record<string, string> = {};
|
|
371
|
+
const deps: Record<string, string[]> = {};
|
|
372
|
+
const base: string[] = [];
|
|
373
|
+
const sharedOwner: Record<string, string> = {};
|
|
374
|
+
|
|
375
|
+
for (const pack of packs) {
|
|
376
|
+
const styles = pack.styles;
|
|
377
|
+
if (!styles) continue;
|
|
378
|
+
|
|
379
|
+
for (const [name, css] of Object.entries(styles.components ?? {})) {
|
|
380
|
+
if (!(name in pack.components)) {
|
|
381
|
+
throw new ComponentPackError(
|
|
382
|
+
`包 "${pack.name}" 为组件 "${name}" 登记了样式,但它没有提供这个组件`
|
|
383
|
+
+ '(组件名写错时样式永远不会注入,而页面只是"看起来有点不对")',
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
// 组件已被别的包覆盖时,它的样式也不该生效
|
|
387
|
+
if (provenance[name] === pack.name) components[name] = css;
|
|
388
|
+
}
|
|
389
|
+
for (const [name, css] of Object.entries(styles.shared ?? {})) {
|
|
390
|
+
const owner = sharedOwner[name];
|
|
391
|
+
if (owner !== undefined && owner !== pack.name) {
|
|
392
|
+
throw new ComponentPackError(
|
|
393
|
+
`共享样式分片 "${name}" 同时来自包 "${owner}" 与 "${pack.name}"——`
|
|
394
|
+
+ '同名共享资产通常意味着有人复制了别人的 CSS,请改名或复用',
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
sharedOwner[name] = pack.name;
|
|
398
|
+
shared[name] = css;
|
|
399
|
+
}
|
|
400
|
+
for (const [name, list] of Object.entries(styles.deps ?? {})) {
|
|
401
|
+
if (provenance[name] === pack.name) deps[name] = [...list];
|
|
402
|
+
}
|
|
403
|
+
if (styles.base?.trim()) base.push(styles.base);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return { components, shared, deps, base };
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* 生成带来源标注的组件目录。
|
|
411
|
+
*
|
|
412
|
+
* 复用 generateAiCatalog(单一事实源:目录条目怎么长、ai 段怎么校验只有一处实现),
|
|
413
|
+
* 在它之上补两件包相关的事:
|
|
414
|
+
* - 每个条目盖 `pack` 戳,让人和排障工具知道这个组件是谁提供的;
|
|
415
|
+
* - 把各包的 catalogConventions 并进规范章节——第三方包对自己的组件有话要说时,
|
|
416
|
+
* 那些话必须跟着组件一起进模型的上下文,否则等于没写。
|
|
417
|
+
*
|
|
418
|
+
* 参数是合成结果而不是散装组件表:目录必须与运行时注册的是同一批组件,
|
|
419
|
+
* 中间隔一次手工拼装就迟早漂移。
|
|
420
|
+
*/
|
|
421
|
+
export function generatePackCatalog(
|
|
422
|
+
composed: ComposedComponentPacks,
|
|
423
|
+
options: { conventions?: string; strict?: boolean } = {},
|
|
424
|
+
): SomaAiCatalog & { packOf: Record<string, string> } {
|
|
425
|
+
const conventions = [
|
|
426
|
+
options.conventions,
|
|
427
|
+
...composed.packs
|
|
428
|
+
.filter((pack) => pack.catalogConventions?.trim())
|
|
429
|
+
.map((pack) => `### 来自组件包 ${pack.name}\n\n${pack.catalogConventions!.trim()}`),
|
|
430
|
+
].filter(Boolean).join('\n\n');
|
|
431
|
+
|
|
432
|
+
const catalog = generateAiCatalog(composed.components, {
|
|
433
|
+
...(conventions ? { conventions } : {}),
|
|
434
|
+
...(options.strict === undefined ? {} : { strict: options.strict }),
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
return {
|
|
438
|
+
...catalog,
|
|
439
|
+
entries: catalog.entries.map((entry) => ({
|
|
440
|
+
...entry,
|
|
441
|
+
pack: composed.provenance[entry.name],
|
|
442
|
+
})) as typeof catalog.entries,
|
|
443
|
+
packOf: { ...composed.provenance },
|
|
444
|
+
};
|
|
445
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// 组件注册表:组件名 → 运行时定义。
|
|
2
|
+
// Host 级单注册域;Host 本地组件覆盖同名包组件(覆盖告警由 LSP/诊断承担)。
|
|
3
|
+
|
|
4
|
+
import type { SomaComponentDefinition } from './component-definition.types';
|
|
5
|
+
import { SOMA_COMPONENT_CTRL_KEY } from './component-definition.types';
|
|
6
|
+
import { findNestedMutableContractDefault } from '../../shared/types/component-contract.types';
|
|
7
|
+
|
|
8
|
+
export class SomaComponentRegistryError extends Error {
|
|
9
|
+
constructor(message: string) {
|
|
10
|
+
super(`[SomaComponentRegistry] ${message}`);
|
|
11
|
+
this.name = 'SomaComponentRegistryError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class SomaComponentRegistry {
|
|
16
|
+
private readonly defs = new Map<string, SomaComponentDefinition>();
|
|
17
|
+
|
|
18
|
+
register(def: SomaComponentDefinition): void {
|
|
19
|
+
const { contract } = def;
|
|
20
|
+
if (!contract?.name) {
|
|
21
|
+
throw new SomaComponentRegistryError('契约缺少 name');
|
|
22
|
+
}
|
|
23
|
+
if (!def.templateHtml) {
|
|
24
|
+
throw new SomaComponentRegistryError(`组件 "${contract.name}" 缺少 templateHtml`);
|
|
25
|
+
}
|
|
26
|
+
if (contract.props && SOMA_COMPONENT_CTRL_KEY in contract.props) {
|
|
27
|
+
throw new SomaComponentRegistryError(
|
|
28
|
+
`组件 "${contract.name}" 的 prop 名 "${SOMA_COMPONENT_CTRL_KEY}" 是保留键`,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
for (const [propName, prop] of Object.entries(contract.props ?? {})) {
|
|
32
|
+
if (!prop.required && prop.default === undefined) {
|
|
33
|
+
throw new SomaComponentRegistryError(
|
|
34
|
+
`组件 "${contract.name}" 的可选 prop "${propName}" 缺少 default(契约规则 C4)`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
const nestedMutablePath = findNestedMutableContractDefault(prop.default);
|
|
38
|
+
if (nestedMutablePath) {
|
|
39
|
+
throw new SomaComponentRegistryError(
|
|
40
|
+
`组件 "${contract.name}" 的 prop "${propName}" default.${nestedMutablePath} 是嵌套可变默认值;默认值只允许一层数组/普通对象(契约规则 C4a)`,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
this.defs.set(contract.name, def);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
registerAll(defs: Record<string, SomaComponentDefinition> | SomaComponentDefinition[]): void {
|
|
48
|
+
const list = Array.isArray(defs) ? defs : Object.values(defs);
|
|
49
|
+
for (const def of list) {
|
|
50
|
+
this.register(def);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get(name: string): SomaComponentDefinition {
|
|
55
|
+
const def = this.defs.get(name);
|
|
56
|
+
if (!def) {
|
|
57
|
+
throw new SomaComponentRegistryError(
|
|
58
|
+
`未注册的组件 "${name}"(已注册:${[...this.defs.keys()].join(', ') || '无'})`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return def;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
has(name: string): boolean {
|
|
65
|
+
return this.defs.has(name);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
names(): string[] {
|
|
69
|
+
return [...this.defs.keys()];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// 覆盖兼容闸门:一个组件要替换另一个包的同名组件时,先证明存量页面不会被打碎。
|
|
2
|
+
//
|
|
3
|
+
// 为什么必须有这道闸门:页面是**对着被覆盖者的契约**写的——它传哪些 props、
|
|
4
|
+
// 订阅哪些 emit,都以官方契约为准。覆盖者少一个 prop、少一个 emit,
|
|
5
|
+
// 页面不会有编译错误,只会在运行时少一块内容或点了没反应。
|
|
6
|
+
// 这正是本体系最忌讳的那类失败:不报错,但坏掉。
|
|
7
|
+
//
|
|
8
|
+
// 闸门保证的是**结构兼容**,不是行为等价:覆盖者仍然可以画得不一样、
|
|
9
|
+
// 交互细节不同。行为由宿主自己的测试与端到端用例兜底,闸门只保证
|
|
10
|
+
// "页面能传的它都收,页面在听的它都发"。
|
|
11
|
+
//
|
|
12
|
+
// 三类检查各有实弹背景:
|
|
13
|
+
// props 缺失/类型变 → 页面照常传值,组件读不到,白屏或空块;
|
|
14
|
+
// 可选变必填 → 存量页面没传那个 prop,覆盖后变成缺必填;
|
|
15
|
+
// default 变化 → 最阴的一种:结构完全兼容,但存量页面没传该 prop 的地方
|
|
16
|
+
// 行为静默改变(分页大小从 20 变 50 这种),任何结构检查都抓不到。
|
|
17
|
+
|
|
18
|
+
import type {
|
|
19
|
+
SomaComponentContract,
|
|
20
|
+
SomaContractProp,
|
|
21
|
+
} from '../../shared/types/component-contract.types';
|
|
22
|
+
|
|
23
|
+
export type ContractCompatIssueKind =
|
|
24
|
+
| 'prop-missing'
|
|
25
|
+
| 'prop-type-changed'
|
|
26
|
+
| 'prop-required-tightened'
|
|
27
|
+
| 'prop-default-diverged'
|
|
28
|
+
| 'emit-missing';
|
|
29
|
+
|
|
30
|
+
export interface ContractCompatIssue {
|
|
31
|
+
kind: ContractCompatIssueKind;
|
|
32
|
+
/** prop 名或 emit 名 */
|
|
33
|
+
member: string;
|
|
34
|
+
/** 点名到具体差异内容——报错信息是接入者读得最认真的文档 */
|
|
35
|
+
detail: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 检查 winner 覆盖 base 的契约兼容性,返回问题清单(空数组 = 兼容)。
|
|
40
|
+
*
|
|
41
|
+
* @param winner 覆盖者契约(业务包的组件)
|
|
42
|
+
* @param base 被覆盖者契约(通常是 soma-ui 的官方组件)
|
|
43
|
+
*/
|
|
44
|
+
export function checkOverrideCompat(
|
|
45
|
+
winner: SomaComponentContract,
|
|
46
|
+
base: SomaComponentContract,
|
|
47
|
+
): ContractCompatIssue[] {
|
|
48
|
+
const issues: ContractCompatIssue[] = [];
|
|
49
|
+
const winnerProps = winner.props ?? {};
|
|
50
|
+
const baseProps = base.props ?? {};
|
|
51
|
+
|
|
52
|
+
for (const [name, baseProp] of Object.entries(baseProps)) {
|
|
53
|
+
const winnerProp = winnerProps[name];
|
|
54
|
+
if (!winnerProp) {
|
|
55
|
+
issues.push({
|
|
56
|
+
kind: 'prop-missing',
|
|
57
|
+
member: name,
|
|
58
|
+
detail: `缺少 prop "${name}"(${describeProp(baseProp)})——存量页面仍会传它,覆盖后这个值无处可去`,
|
|
59
|
+
});
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (winnerProp.type !== baseProp.type) {
|
|
63
|
+
issues.push({
|
|
64
|
+
kind: 'prop-type-changed',
|
|
65
|
+
member: name,
|
|
66
|
+
detail: `prop "${name}" 的 type 从 "${baseProp.type}" 变成 "${winnerProp.type}"——`
|
|
67
|
+
+ '页面传下来的值形状不变,读法却变了',
|
|
68
|
+
});
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (winnerProp.required === true && baseProp.required !== true) {
|
|
72
|
+
issues.push({
|
|
73
|
+
kind: 'prop-required-tightened',
|
|
74
|
+
member: name,
|
|
75
|
+
detail: `prop "${name}" 从可选收紧为必填——存量页面没传它的地方会缺必填`,
|
|
76
|
+
});
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (baseProp.required !== true && !sameDefault(winnerProp.default, baseProp.default)) {
|
|
80
|
+
issues.push({
|
|
81
|
+
kind: 'prop-default-diverged',
|
|
82
|
+
member: name,
|
|
83
|
+
detail: `可选 prop "${name}" 的 default 从 ${formatValue(baseProp.default)} 变成 `
|
|
84
|
+
+ `${formatValue(winnerProp.default)}——结构兼容但行为会静默改变:`
|
|
85
|
+
+ '存量页面没传这个 prop 的地方,渲染结果直接跟着变',
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const baseEmits = Object.keys(base.emits ?? {});
|
|
91
|
+
const winnerEmits = new Set(Object.keys(winner.emits ?? {}));
|
|
92
|
+
for (const name of baseEmits) {
|
|
93
|
+
if (!winnerEmits.has(name)) {
|
|
94
|
+
issues.push({
|
|
95
|
+
kind: 'emit-missing',
|
|
96
|
+
member: name,
|
|
97
|
+
detail: `缺少 emit "${name}"——存量页面的 _event.yaml 仍订阅着它,覆盖后点了没反应`,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return issues;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* default 深比较。
|
|
107
|
+
*
|
|
108
|
+
* 契约规则 C4a 保证默认值最多一层(数组/普通对象里不再嵌套引用型值),
|
|
109
|
+
* 所以 JSON 序列化比较就足够,不必引入深比较工具。
|
|
110
|
+
*/
|
|
111
|
+
function sameDefault(a: unknown, b: unknown): boolean {
|
|
112
|
+
if (a === b) return true;
|
|
113
|
+
if (a === undefined || b === undefined) return false;
|
|
114
|
+
try {
|
|
115
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
116
|
+
} catch {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function describeProp(prop: SomaContractProp): string {
|
|
122
|
+
return prop.required === true ? `${prop.type},必填` : `${prop.type},可选`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function formatValue(value: unknown): string {
|
|
126
|
+
if (value === undefined) return '未设置';
|
|
127
|
+
try {
|
|
128
|
+
return JSON.stringify(value) ?? String(value);
|
|
129
|
+
} catch {
|
|
130
|
+
return String(value);
|
|
131
|
+
}
|
|
132
|
+
}
|