@sparqjs/frontend-core 0.2.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 +39 -0
- package/dist/frontend-module-loader.d.ts +91 -0
- package/dist/frontend-module-loader.d.ts.map +1 -0
- package/dist/frontend-module-loader.js +527 -0
- package/dist/frontend-module-loader.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/kernel.d.ts +111 -0
- package/dist/kernel.d.ts.map +1 -0
- package/dist/kernel.js +229 -0
- package/dist/kernel.js.map +1 -0
- package/dist/plugin-runtime.d.ts +64 -0
- package/dist/plugin-runtime.d.ts.map +1 -0
- package/dist/plugin-runtime.js +144 -0
- package/dist/plugin-runtime.js.map +1 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Sparq Frontend Core
|
|
2
|
+
|
|
3
|
+
`@sparqjs/frontend-core` 是前端基础包,统一承载前端贡献注册和可信进程内插件生命周期。它不包含 Vue、Schema、Admin 或设计器领域类型,可被 Admin、插件和其他宿主独立安装消费。
|
|
4
|
+
|
|
5
|
+
## 稳定边界
|
|
6
|
+
|
|
7
|
+
- 领域模块通过 `defineContributionPoint<T>()` 声明强类型贡献点。
|
|
8
|
+
- 应用或领域实例通过 `createFrontendKernel()` 创建隔离注册环境。
|
|
9
|
+
- 插件使用 `kernel.install()` 批量安装贡献,并通过返回的 handle 幂等卸载。
|
|
10
|
+
- 同 key 贡献按 `builtin < plugin < host < local` 解析,同层再比较 `priority`,完全相同时后安装项生效。
|
|
11
|
+
- registry 提供当前解析结果、重复贡献诊断和变更订阅。
|
|
12
|
+
|
|
13
|
+
registry 订阅者只负责观察解析结果,异常不会阻断已经完成的贡献安装或卸载。宿主可以通过 `createFrontendKernel({ onSubscriberError })` 记录这些异常;错误回调自身的失败也会被隔离,避免安装完成却无法返回卸载 handle。
|
|
14
|
+
|
|
15
|
+
kernel 不负责加载插件 manifest、检查权限、解析远程入口或决定领域匹配规则。这些职责由 Admin、renderer、designer 等调用方承担。
|
|
16
|
+
|
|
17
|
+
## 插件生命周期
|
|
18
|
+
|
|
19
|
+
- `createFrontendPluginRuntime()` 管理宿主已经加载并信任的插件模块。
|
|
20
|
+
- 调用方通过 `installRendererContribution` 注入具体 renderer 贡献的安装方式,Core 不反向依赖 renderer。
|
|
21
|
+
- 激活失败时按相反顺序回滚已经安装的贡献,停用和 handle 释放保持幂等。
|
|
22
|
+
- Designer 扩展以实例快照形式保留,由具体设计器实例自行解析。
|
|
23
|
+
- iframe、manifest、权限和远程脚本加载继续属于 `@sparqjs/plugin-sdk` 与宿主边界。
|
|
24
|
+
|
|
25
|
+
## 使用方式
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { createFrontendKernel, defineContributionPoint, defineContributionSet } from '@sparqjs/frontend-core'
|
|
29
|
+
|
|
30
|
+
const previewPoint = defineContributionPoint<PreviewDefinition>('resource.previews')
|
|
31
|
+
const kernel = createFrontendKernel()
|
|
32
|
+
const handle = kernel.install({
|
|
33
|
+
owner: 'plugin.pdf',
|
|
34
|
+
contributions: [defineContributionSet(previewPoint, [{ key: 'pdf', value: pdfPreview }])],
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const preview = kernel.registry(previewPoint).get('pdf')
|
|
38
|
+
handle.dispose()
|
|
39
|
+
```
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { DesignerStepContribution, FrontendModuleActivation, FrontendModuleFactory, FrontendModuleHostV1, AdminExtensionHost, AdminExtensionDisposable, FrontendModuleIdentity, FrontendModuleRendererContribution, FrontendRuntimeV1, PluginScopedClient, ResolvedFrontendModule, FrontendSurfaceMount } from '@sparqjs/plugin-sdk/runtime';
|
|
2
|
+
import type { Component } from 'vue';
|
|
3
|
+
import type { ContributionHandle } from './kernel';
|
|
4
|
+
export type FrontendModuleLoadPhase = 'style' | 'import' | 'export' | 'activation' | 'contribution' | 'global';
|
|
5
|
+
export interface FrontendModuleDescriptor extends ResolvedFrontendModule {
|
|
6
|
+
pluginCode: string;
|
|
7
|
+
pluginVersion: string;
|
|
8
|
+
runtimeProfile: 'admin-v1';
|
|
9
|
+
}
|
|
10
|
+
export interface LoadedFrontendModule {
|
|
11
|
+
descriptor: FrontendModuleDescriptor;
|
|
12
|
+
activation: FrontendModuleActivation;
|
|
13
|
+
rendererHandles: readonly ContributionHandle[];
|
|
14
|
+
scope?: FrontendModuleScope;
|
|
15
|
+
surfaces: ReadonlyMap<string, FrontendSurfaceMount>;
|
|
16
|
+
}
|
|
17
|
+
/** 已完成资源获取和 Root 工厂校验,但尚未创建 Scope 或执行激活。 */
|
|
18
|
+
export interface PreparedFrontendModule {
|
|
19
|
+
descriptor: FrontendModuleDescriptor;
|
|
20
|
+
factory: FrontendModuleFactory;
|
|
21
|
+
styleSheets: readonly string[];
|
|
22
|
+
}
|
|
23
|
+
export interface FrontendModuleScope {
|
|
24
|
+
admin?: AdminExtensionHost;
|
|
25
|
+
ui?: FrontendModuleHostV1['ui'];
|
|
26
|
+
onDispose(disposer: () => void | Promise<void>): AdminExtensionDisposable;
|
|
27
|
+
dispose(): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
export interface CreateFrontendModuleLoaderOptions {
|
|
30
|
+
vue: typeof import('vue');
|
|
31
|
+
runtime?: FrontendRuntimeV1;
|
|
32
|
+
ui: Readonly<{
|
|
33
|
+
render: Component;
|
|
34
|
+
provider?: Component;
|
|
35
|
+
overlay?: HTMLElement;
|
|
36
|
+
registerStyle?(key: string, css: string): AdminExtensionDisposable;
|
|
37
|
+
}>;
|
|
38
|
+
createScope?(identity: Readonly<FrontendModuleIdentity>): FrontendModuleScope;
|
|
39
|
+
createClient(pluginCode: string): PluginScopedClient;
|
|
40
|
+
/** 安装当前宿主支持的 renderer 贡献;缺少安装器时不能激活 renderer 模块。 */
|
|
41
|
+
installRendererContribution?(contribution: FrontendModuleRendererContribution, descriptor: FrontendModuleDescriptor): ContributionHandle;
|
|
42
|
+
importModule?(url: string): Promise<unknown>;
|
|
43
|
+
loadStyle?(url: string): Promise<string>;
|
|
44
|
+
/** 接收 root 生命周期和局部贡献错误;诊断回调失败不会影响 Loader。 */
|
|
45
|
+
onDiagnostic?(diagnostic: FrontendModuleDiagnostic): void;
|
|
46
|
+
}
|
|
47
|
+
export interface FrontendModuleDiagnostic {
|
|
48
|
+
pluginCode: string;
|
|
49
|
+
pluginVersion: string;
|
|
50
|
+
moduleId: string;
|
|
51
|
+
phase: FrontendModuleLoadPhase;
|
|
52
|
+
contribution?: string;
|
|
53
|
+
error: unknown;
|
|
54
|
+
}
|
|
55
|
+
export interface FrontendModuleLoader {
|
|
56
|
+
prepare(descriptor: FrontendModuleDescriptor): Promise<PreparedFrontendModule>;
|
|
57
|
+
load(descriptor: FrontendModuleDescriptor): Promise<LoadedFrontendModule>;
|
|
58
|
+
designerStep(descriptor: FrontendModuleDescriptor, contribution: string): Promise<DesignerStepContribution>;
|
|
59
|
+
surface(descriptor: FrontendModuleDescriptor, name: string): Promise<FrontendSurfaceMount>;
|
|
60
|
+
unload(descriptor: FrontendModuleDescriptor): Promise<void>;
|
|
61
|
+
invalidatePrepared(descriptor: FrontendModuleDescriptor): void;
|
|
62
|
+
dispose(): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
export interface FrontendRootDescriptor {
|
|
65
|
+
pluginCode: string;
|
|
66
|
+
pluginVersion: string;
|
|
67
|
+
id: 'root';
|
|
68
|
+
hostApi: 'v1';
|
|
69
|
+
runtimeProfile: 'admin-v1';
|
|
70
|
+
url: string;
|
|
71
|
+
styles?: readonly string[];
|
|
72
|
+
source: 'development' | 'production';
|
|
73
|
+
version?: string;
|
|
74
|
+
provides: {
|
|
75
|
+
schemaComponents: readonly string[];
|
|
76
|
+
richTextEditors: readonly string[];
|
|
77
|
+
captchaRenderers: readonly string[];
|
|
78
|
+
designerContributions: readonly string[];
|
|
79
|
+
designerSteps: readonly string[];
|
|
80
|
+
adminExtensions: readonly string[];
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
export declare class FrontendModuleLoadError extends Error {
|
|
84
|
+
readonly descriptor: FrontendModuleDescriptor;
|
|
85
|
+
readonly phase: FrontendModuleLoadPhase;
|
|
86
|
+
readonly contribution: string | undefined;
|
|
87
|
+
readonly cleanupError?: unknown | undefined;
|
|
88
|
+
constructor(descriptor: FrontendModuleDescriptor, phase: FrontendModuleLoadPhase, contribution: string | undefined, cause: unknown, cleanupError?: unknown | undefined);
|
|
89
|
+
}
|
|
90
|
+
export declare function createFrontendModuleLoader(options: CreateFrontendModuleLoaderOptions): FrontendModuleLoader;
|
|
91
|
+
//# sourceMappingURL=frontend-module-loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frontend-module-loader.d.ts","sourceRoot":"","sources":["../src/frontend-module-loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,wBAAwB,EACxB,wBAAwB,EACxB,qBAAqB,EACrB,oBAAoB,EACpB,kBAAkB,EAClB,wBAAwB,EACxB,sBAAsB,EACtB,kCAAkC,EAElC,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,oBAAoB,EACvB,MAAM,6BAA6B,CAAA;AACpC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,KAAK,CAAA;AACpC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAElD,MAAM,MAAM,uBAAuB,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,GAAG,YAAY,GAAG,cAAc,GAAG,QAAQ,CAAA;AAE9G,MAAM,WAAW,wBAAyB,SAAQ,sBAAsB;IACpE,UAAU,EAAE,MAAM,CAAA;IAClB,aAAa,EAAE,MAAM,CAAA;IACrB,cAAc,EAAE,UAAU,CAAA;CAC7B;AAED,MAAM,WAAW,oBAAoB;IACjC,UAAU,EAAE,wBAAwB,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IACX,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IAC1B,MAAM,EAAE,aAAa,GAAG,YAAY,CAAA;IACpC,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE;QACN,gBAAgB,EAAE,SAAS,MAAM,EAAE,CAAA;QACnC,eAAe,EAAE,SAAS,MAAM,EAAE,CAAA;QAClC,gBAAgB,EAAE,SAAS,MAAM,EAAE,CAAA;QACnC,qBAAqB,EAAE,SAAS,MAAM,EAAE,CAAA;QACxC,aAAa,EAAE,SAAS,MAAM,EAAE,CAAA;QAChC,eAAe,EAAE,SAAS,MAAM,EAAE,CAAA;KACrC,CAAA;CACJ;AAED,qBAAa,uBAAwB,SAAQ,KAAK;IAE1C,QAAQ,CAAC,UAAU,EAAE,wBAAwB;IAC7C,QAAQ,CAAC,KAAK,EAAE,uBAAuB;IACvC,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS;IAEzC,QAAQ,CAAC,YAAY,CAAC,EAAE,OAAO;gBAJtB,UAAU,EAAE,wBAAwB,EACpC,KAAK,EAAE,uBAAuB,EAC9B,YAAY,EAAE,MAAM,GAAG,SAAS,EACzC,KAAK,EAAE,OAAO,EACL,YAAY,CAAC,EAAE,OAAO,YAAA;CAYtC;AAED,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,iCAAiC,GAAG,oBAAoB,CAsU3G"}
|
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
export class FrontendModuleLoadError extends Error {
|
|
2
|
+
descriptor;
|
|
3
|
+
phase;
|
|
4
|
+
contribution;
|
|
5
|
+
cleanupError;
|
|
6
|
+
constructor(descriptor, phase, contribution, cause, cleanupError) {
|
|
7
|
+
const target = `${descriptor.pluginCode}/${descriptor.id}`;
|
|
8
|
+
const contributionDetail = contribution ? ` contribution ${contribution}` : '';
|
|
9
|
+
const detail = cause instanceof Error ? cause.message : String(cause);
|
|
10
|
+
const cleanupDetail = cleanupError === undefined
|
|
11
|
+
? ''
|
|
12
|
+
: `;失败作用域释放异常: ${cleanupError instanceof Error ? cleanupError.message : String(cleanupError)}`;
|
|
13
|
+
super(`Frontend Module ${target}${contributionDetail} ${phase} 失败: ${detail}${cleanupDetail}`, { cause });
|
|
14
|
+
this.descriptor = descriptor;
|
|
15
|
+
this.phase = phase;
|
|
16
|
+
this.contribution = contribution;
|
|
17
|
+
this.cleanupError = cleanupError;
|
|
18
|
+
this.name = 'FrontendModuleLoadError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function createFrontendModuleLoader(options) {
|
|
22
|
+
const preparations = new Map();
|
|
23
|
+
const imports = new Map();
|
|
24
|
+
const activated = [];
|
|
25
|
+
const importModule = options.importModule ?? importFrontendModule;
|
|
26
|
+
const loadStyle = options.loadStyle ?? fetchFrontendStyle;
|
|
27
|
+
const runtime = options.runtime ?? createDefaultRuntime(options.vue);
|
|
28
|
+
function validateDescriptor(descriptor) {
|
|
29
|
+
if (descriptor.hostApi !== 'v1') {
|
|
30
|
+
throw new FrontendModuleLoadError(descriptor, 'export', undefined, new Error(`不支持 Host API ${descriptor.hostApi}`));
|
|
31
|
+
}
|
|
32
|
+
if (descriptor.runtimeProfile !== 'admin-v1') {
|
|
33
|
+
throw new FrontendModuleLoadError(descriptor, 'export', undefined, new Error(`不支持 Runtime Profile ${descriptor.runtimeProfile}`));
|
|
34
|
+
}
|
|
35
|
+
if (runtime.profile !== descriptor.runtimeProfile || runtime.modules.vue !== options.vue) {
|
|
36
|
+
throw new FrontendModuleLoadError(descriptor, 'export', undefined, new Error('Frontend root 的共享 Runtime 与宿主 Vue 实例不匹配'));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function prepare(descriptor) {
|
|
40
|
+
validateDescriptor(descriptor);
|
|
41
|
+
const key = moduleCacheKey(descriptor);
|
|
42
|
+
const existing = preparations.get(key);
|
|
43
|
+
if (existing)
|
|
44
|
+
return existing;
|
|
45
|
+
const preparing = prepareModule(descriptor);
|
|
46
|
+
preparations.set(key, preparing);
|
|
47
|
+
try {
|
|
48
|
+
return await preparing;
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
preparations.delete(key);
|
|
52
|
+
reportDiagnostic(descriptor, error instanceof FrontendModuleLoadError ? error.phase : 'import', error instanceof FrontendModuleLoadError ? error.contribution : undefined, error);
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async function prepareModule(descriptor) {
|
|
57
|
+
let styleSheets;
|
|
58
|
+
try {
|
|
59
|
+
styleSheets = await Promise.all((descriptor.styles ?? []).map((url) => loadStyle(url)));
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
throw new FrontendModuleLoadError(descriptor, 'style', undefined, error);
|
|
63
|
+
}
|
|
64
|
+
let namespace;
|
|
65
|
+
const importGlobals = snapshotGlobalState();
|
|
66
|
+
try {
|
|
67
|
+
namespace = await importModule(descriptor.url);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
throw new FrontendModuleLoadError(descriptor, 'import', undefined, error);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
reportGlobalPollution(descriptor, 'import', importGlobals);
|
|
74
|
+
}
|
|
75
|
+
const factory = readFactory(namespace);
|
|
76
|
+
if (!factory) {
|
|
77
|
+
throw new FrontendModuleLoadError(descriptor, 'export', undefined, new Error('默认导出必须是模块工厂'));
|
|
78
|
+
}
|
|
79
|
+
return { descriptor, factory, styleSheets };
|
|
80
|
+
}
|
|
81
|
+
async function load(descriptor) {
|
|
82
|
+
const key = moduleCacheKey(descriptor);
|
|
83
|
+
const existing = imports.get(key);
|
|
84
|
+
if (existing)
|
|
85
|
+
return existing;
|
|
86
|
+
const loading = activateModule(descriptor);
|
|
87
|
+
imports.set(key, loading);
|
|
88
|
+
try {
|
|
89
|
+
return await loading;
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
imports.delete(key);
|
|
93
|
+
if (!(error instanceof FrontendModuleLoadError) ||
|
|
94
|
+
error.phase === 'activation' ||
|
|
95
|
+
error.phase === 'contribution') {
|
|
96
|
+
reportDiagnostic(descriptor, error instanceof FrontendModuleLoadError ? error.phase : 'activation', error instanceof FrontendModuleLoadError ? error.contribution : undefined, error);
|
|
97
|
+
}
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async function activateModule(descriptor) {
|
|
102
|
+
const { factory, styleSheets } = await prepare(descriptor);
|
|
103
|
+
const identity = Object.freeze({
|
|
104
|
+
pluginCode: descriptor.pluginCode,
|
|
105
|
+
pluginVersion: descriptor.pluginVersion,
|
|
106
|
+
moduleId: descriptor.id,
|
|
107
|
+
});
|
|
108
|
+
const scope = options.createScope?.(identity) ?? createDefaultFrontendModuleScope();
|
|
109
|
+
const registrationHandles = [];
|
|
110
|
+
const designerSteps = {};
|
|
111
|
+
const surfaces = new Map();
|
|
112
|
+
const ui = scope.ui ?? createFallbackUI(options);
|
|
113
|
+
try {
|
|
114
|
+
for (const [index, css] of styleSheets.entries()) {
|
|
115
|
+
if (!css.trim())
|
|
116
|
+
throw new Error(`Frontend 样式 ${descriptor.styles?.[index]} 内容为空`);
|
|
117
|
+
registrationHandles.push(ui.registerStyle(`sparq-artifact:${index}`, css));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
const cleanupError = await releaseFailedActivation(scope, undefined, registrationHandles);
|
|
122
|
+
throw new FrontendModuleLoadError(descriptor, 'style', undefined, error, cleanupError);
|
|
123
|
+
}
|
|
124
|
+
const host = Object.freeze({
|
|
125
|
+
version: 'v1',
|
|
126
|
+
identity,
|
|
127
|
+
client: options.createClient(descriptor.pluginCode),
|
|
128
|
+
vue: options.vue,
|
|
129
|
+
runtime,
|
|
130
|
+
admin: scope.admin,
|
|
131
|
+
onDispose(disposer) {
|
|
132
|
+
return scope.onDispose(disposer);
|
|
133
|
+
},
|
|
134
|
+
ui,
|
|
135
|
+
schema: Object.freeze({
|
|
136
|
+
registerComponent(key, component) {
|
|
137
|
+
return installDirectRendererContribution({ schemaComponents: { [key]: component } }, key);
|
|
138
|
+
},
|
|
139
|
+
registerRichTextEditor(key, editor) {
|
|
140
|
+
return installDirectRendererContribution({ richTextEditors: { [key]: editor } }, key);
|
|
141
|
+
},
|
|
142
|
+
}),
|
|
143
|
+
captcha: Object.freeze({
|
|
144
|
+
registerRenderer(key, renderer) {
|
|
145
|
+
return installDirectRendererContribution({ captchaRenderers: { [key]: renderer } }, key);
|
|
146
|
+
},
|
|
147
|
+
}),
|
|
148
|
+
designer: Object.freeze({
|
|
149
|
+
registerStep(key, contribution) {
|
|
150
|
+
if (!key || designerSteps[key]) {
|
|
151
|
+
const error = new Error(`Designer Step ${key} 重复注册`);
|
|
152
|
+
reportDiagnostic(descriptor, 'contribution', key, error);
|
|
153
|
+
return { dispose() { } };
|
|
154
|
+
}
|
|
155
|
+
designerSteps[key] = contribution;
|
|
156
|
+
const disposable = {
|
|
157
|
+
dispose() {
|
|
158
|
+
delete designerSteps[key];
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
registrationHandles.push(disposable);
|
|
162
|
+
return disposable;
|
|
163
|
+
},
|
|
164
|
+
}),
|
|
165
|
+
surfaces: Object.freeze({
|
|
166
|
+
register(name, mount) {
|
|
167
|
+
if (!name.trim() || typeof mount !== 'function' || surfaces.has(name)) {
|
|
168
|
+
const error = new Error(`Frontend Surface ${name} 重复或无效注册`);
|
|
169
|
+
reportDiagnostic(descriptor, 'contribution', name, error);
|
|
170
|
+
return { dispose() { } };
|
|
171
|
+
}
|
|
172
|
+
surfaces.set(name, mount);
|
|
173
|
+
const disposable = {
|
|
174
|
+
dispose() {
|
|
175
|
+
if (surfaces.get(name) === mount)
|
|
176
|
+
surfaces.delete(name);
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
registrationHandles.push(disposable);
|
|
180
|
+
return disposable;
|
|
181
|
+
},
|
|
182
|
+
}),
|
|
183
|
+
});
|
|
184
|
+
function installDirectRendererContribution(contribution, key) {
|
|
185
|
+
let handle;
|
|
186
|
+
try {
|
|
187
|
+
handle = options.installRendererContribution?.(contribution, descriptor) ?? missingRendererInstaller();
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
reportDiagnostic(descriptor, 'contribution', key, error);
|
|
191
|
+
return { dispose() { } };
|
|
192
|
+
}
|
|
193
|
+
const disposable = toDisposable(handle);
|
|
194
|
+
registrationHandles.push(disposable);
|
|
195
|
+
return disposable;
|
|
196
|
+
}
|
|
197
|
+
let activation = {};
|
|
198
|
+
const activationGlobals = snapshotGlobalState();
|
|
199
|
+
try {
|
|
200
|
+
const result = await factory(host);
|
|
201
|
+
if (result)
|
|
202
|
+
activation = result;
|
|
203
|
+
if (Object.keys(designerSteps).length > 0) {
|
|
204
|
+
activation = { ...activation, designerSteps: { ...(activation.designerSteps ?? {}), ...designerSteps } };
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
const cleanupError = await releaseFailedActivation(scope, undefined, registrationHandles);
|
|
209
|
+
throw new FrontendModuleLoadError(descriptor, 'activation', undefined, error, cleanupError);
|
|
210
|
+
}
|
|
211
|
+
finally {
|
|
212
|
+
reportGlobalPollution(descriptor, 'activation', activationGlobals);
|
|
213
|
+
}
|
|
214
|
+
const rendererHandles = registrationHandles.map((handle) => ({ dispose: handle.dispose }));
|
|
215
|
+
try {
|
|
216
|
+
validateActivation(descriptor, activation, new Set(Object.keys(designerSteps)));
|
|
217
|
+
for (const contribution of activation.rendererContributions ?? []) {
|
|
218
|
+
if (!options.installRendererContribution) {
|
|
219
|
+
throw new Error('宿主未提供 renderer contribution 安装器');
|
|
220
|
+
}
|
|
221
|
+
rendererHandles.push(options.installRendererContribution(contribution, descriptor));
|
|
222
|
+
}
|
|
223
|
+
const loaded = { descriptor, activation, rendererHandles, scope, surfaces };
|
|
224
|
+
activated.push(loaded);
|
|
225
|
+
return loaded;
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
const cleanupError = await releaseFailedActivation(scope, activation, rendererHandles);
|
|
229
|
+
throw new FrontendModuleLoadError(descriptor, 'contribution', undefined, error, cleanupError);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function reportDiagnostic(descriptor, phase, contribution, error) {
|
|
233
|
+
try {
|
|
234
|
+
options.onDiagnostic?.({
|
|
235
|
+
pluginCode: descriptor.pluginCode,
|
|
236
|
+
pluginVersion: descriptor.pluginVersion,
|
|
237
|
+
moduleId: descriptor.id,
|
|
238
|
+
phase,
|
|
239
|
+
contribution,
|
|
240
|
+
error,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
// 诊断出口不能反向破坏插件生命周期。
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function reportGlobalPollution(descriptor, stage, before) {
|
|
248
|
+
const pollution = diffGlobalState(before);
|
|
249
|
+
if (pollution.length === 0)
|
|
250
|
+
return;
|
|
251
|
+
reportDiagnostic(descriptor, 'global', stage, new Error(`检测到全局污染: ${pollution.join('、')}`));
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
prepare,
|
|
255
|
+
load,
|
|
256
|
+
async designerStep(descriptor, contribution) {
|
|
257
|
+
const loaded = await load(descriptor);
|
|
258
|
+
const step = loaded.activation.designerSteps?.[contribution];
|
|
259
|
+
if (!step) {
|
|
260
|
+
throw new FrontendModuleLoadError(descriptor, 'contribution', contribution, new Error('模块没有导出 Manifest 声明的 Designer Step'));
|
|
261
|
+
}
|
|
262
|
+
return step;
|
|
263
|
+
},
|
|
264
|
+
async surface(descriptor, name) {
|
|
265
|
+
const loaded = await load(descriptor);
|
|
266
|
+
const mount = loaded.surfaces.get(name);
|
|
267
|
+
if (!mount) {
|
|
268
|
+
throw new FrontendModuleLoadError(descriptor, 'contribution', name, new Error('模块没有注册请求的 Frontend Surface'));
|
|
269
|
+
}
|
|
270
|
+
return mount;
|
|
271
|
+
},
|
|
272
|
+
async unload(descriptor) {
|
|
273
|
+
const key = moduleCacheKey(descriptor);
|
|
274
|
+
const loading = imports.get(key);
|
|
275
|
+
if (!loading)
|
|
276
|
+
return;
|
|
277
|
+
let loaded;
|
|
278
|
+
try {
|
|
279
|
+
loaded = await loading;
|
|
280
|
+
}
|
|
281
|
+
catch {
|
|
282
|
+
imports.delete(key);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
imports.delete(key);
|
|
286
|
+
const index = activated.indexOf(loaded);
|
|
287
|
+
if (index >= 0)
|
|
288
|
+
activated.splice(index, 1);
|
|
289
|
+
const errors = await releaseLoadedFrontendModule(loaded);
|
|
290
|
+
preparations.delete(key);
|
|
291
|
+
if (errors.length)
|
|
292
|
+
throw new AggregateError(errors, `Frontend Module ${descriptor.pluginCode}/${descriptor.id} 释放失败`);
|
|
293
|
+
},
|
|
294
|
+
invalidatePrepared(descriptor) {
|
|
295
|
+
const key = moduleCacheKey(descriptor);
|
|
296
|
+
if (!imports.has(key))
|
|
297
|
+
preparations.delete(key);
|
|
298
|
+
},
|
|
299
|
+
async dispose() {
|
|
300
|
+
const errors = [];
|
|
301
|
+
for (const loaded of [...activated].reverse()) {
|
|
302
|
+
errors.push(...(await releaseLoadedFrontendModule(loaded)));
|
|
303
|
+
}
|
|
304
|
+
activated.length = 0;
|
|
305
|
+
imports.clear();
|
|
306
|
+
preparations.clear();
|
|
307
|
+
if (errors.length)
|
|
308
|
+
throw new AggregateError(errors, 'Frontend Module Loader 释放失败');
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
async function releaseLoadedFrontendModule(loaded) {
|
|
313
|
+
const errors = [];
|
|
314
|
+
try {
|
|
315
|
+
await loaded.activation.deactivate?.();
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
errors.push(error);
|
|
319
|
+
}
|
|
320
|
+
for (const handle of [...loaded.rendererHandles].reverse()) {
|
|
321
|
+
try {
|
|
322
|
+
handle.dispose();
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
errors.push(error);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
try {
|
|
329
|
+
await loaded.scope?.dispose();
|
|
330
|
+
}
|
|
331
|
+
catch (error) {
|
|
332
|
+
errors.push(error);
|
|
333
|
+
}
|
|
334
|
+
return errors;
|
|
335
|
+
}
|
|
336
|
+
function snapshotGlobalState() {
|
|
337
|
+
return {
|
|
338
|
+
properties: new Set(Reflect.ownKeys(globalThis)),
|
|
339
|
+
headNodes: new Set(typeof document === 'undefined' ? [] : (document.head?.childNodes ?? [])),
|
|
340
|
+
bodyNodes: new Set(typeof document === 'undefined' ? [] : (document.body?.childNodes ?? [])),
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
function diffGlobalState(before) {
|
|
344
|
+
const pollution = [];
|
|
345
|
+
for (const key of Reflect.ownKeys(globalThis)) {
|
|
346
|
+
if (!before.properties.has(key))
|
|
347
|
+
pollution.push(`globalThis.${String(key)}`);
|
|
348
|
+
}
|
|
349
|
+
if (typeof document === 'undefined')
|
|
350
|
+
return pollution;
|
|
351
|
+
for (const node of document.head?.childNodes ?? []) {
|
|
352
|
+
if (!before.headNodes.has(node))
|
|
353
|
+
pollution.push(describeGlobalNode('head', node));
|
|
354
|
+
}
|
|
355
|
+
for (const node of document.body?.childNodes ?? []) {
|
|
356
|
+
if (!before.bodyNodes.has(node))
|
|
357
|
+
pollution.push(describeGlobalNode('body', node));
|
|
358
|
+
}
|
|
359
|
+
return pollution;
|
|
360
|
+
}
|
|
361
|
+
function describeGlobalNode(parent, node) {
|
|
362
|
+
if (!(node instanceof Element))
|
|
363
|
+
return `document.${parent} child`;
|
|
364
|
+
const identity = node.id ? `#${node.id}` : node.tagName.toLowerCase();
|
|
365
|
+
return `document.${parent} > ${identity}`;
|
|
366
|
+
}
|
|
367
|
+
function toDisposable(handle) {
|
|
368
|
+
return { dispose: handle.dispose };
|
|
369
|
+
}
|
|
370
|
+
function missingRendererInstaller() {
|
|
371
|
+
throw new Error('宿主未提供 renderer contribution 安装器');
|
|
372
|
+
}
|
|
373
|
+
function createDefaultFrontendModuleScope() {
|
|
374
|
+
const callbacks = new Set();
|
|
375
|
+
let disposed = false;
|
|
376
|
+
return {
|
|
377
|
+
onDispose(disposer) {
|
|
378
|
+
if (disposed) {
|
|
379
|
+
void Promise.resolve()
|
|
380
|
+
.then(disposer)
|
|
381
|
+
.catch((error) => {
|
|
382
|
+
console.error('Frontend Module 释放回调失败', error);
|
|
383
|
+
});
|
|
384
|
+
return { dispose() { } };
|
|
385
|
+
}
|
|
386
|
+
let disposing;
|
|
387
|
+
const entry = {
|
|
388
|
+
dispose() {
|
|
389
|
+
if (disposing)
|
|
390
|
+
return disposing;
|
|
391
|
+
callbacks.delete(entry);
|
|
392
|
+
disposing = Promise.resolve().then(disposer);
|
|
393
|
+
return disposing;
|
|
394
|
+
},
|
|
395
|
+
};
|
|
396
|
+
callbacks.add(entry);
|
|
397
|
+
return {
|
|
398
|
+
dispose() {
|
|
399
|
+
void entry.dispose().catch((error) => {
|
|
400
|
+
console.error('Frontend Module 释放回调失败', error);
|
|
401
|
+
});
|
|
402
|
+
},
|
|
403
|
+
};
|
|
404
|
+
},
|
|
405
|
+
async dispose() {
|
|
406
|
+
if (disposed)
|
|
407
|
+
return;
|
|
408
|
+
disposed = true;
|
|
409
|
+
const errors = [];
|
|
410
|
+
for (const callback of [...callbacks].reverse()) {
|
|
411
|
+
try {
|
|
412
|
+
await callback.dispose();
|
|
413
|
+
}
|
|
414
|
+
catch (error) {
|
|
415
|
+
errors.push(error);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
callbacks.clear();
|
|
419
|
+
if (errors.length)
|
|
420
|
+
throw new AggregateError(errors, 'Frontend Module 释放回调失败');
|
|
421
|
+
},
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
function createFallbackUI(options) {
|
|
425
|
+
const provider = options.ui.provider ??
|
|
426
|
+
options.vue.defineComponent({
|
|
427
|
+
name: 'FrontendHostProvider',
|
|
428
|
+
setup: (_, context) => context.slots.default,
|
|
429
|
+
});
|
|
430
|
+
return Object.freeze({
|
|
431
|
+
render: options.ui.render,
|
|
432
|
+
provider,
|
|
433
|
+
overlay: options.ui.overlay,
|
|
434
|
+
registerStyle: options.ui.registerStyle ?? (() => ({ dispose() { } })),
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
async function releaseFailedActivation(scope, activation, rendererHandles = []) {
|
|
438
|
+
const errors = [];
|
|
439
|
+
try {
|
|
440
|
+
await activation?.deactivate?.();
|
|
441
|
+
}
|
|
442
|
+
catch (error) {
|
|
443
|
+
errors.push(error);
|
|
444
|
+
}
|
|
445
|
+
for (const handle of [...rendererHandles].reverse()) {
|
|
446
|
+
try {
|
|
447
|
+
handle.dispose();
|
|
448
|
+
}
|
|
449
|
+
catch (error) {
|
|
450
|
+
errors.push(error);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
try {
|
|
454
|
+
await scope?.dispose();
|
|
455
|
+
}
|
|
456
|
+
catch (error) {
|
|
457
|
+
errors.push(error);
|
|
458
|
+
}
|
|
459
|
+
if (errors.length === 0)
|
|
460
|
+
return undefined;
|
|
461
|
+
return errors.length === 1 ? errors[0] : new AggregateError(errors, '失败 Frontend Module 释放异常');
|
|
462
|
+
}
|
|
463
|
+
function readFactory(namespace) {
|
|
464
|
+
if (typeof namespace !== 'object' || namespace === null)
|
|
465
|
+
return undefined;
|
|
466
|
+
const factory = namespace.default;
|
|
467
|
+
return typeof factory === 'function' ? factory : undefined;
|
|
468
|
+
}
|
|
469
|
+
function createDefaultRuntime(vue) {
|
|
470
|
+
return Object.freeze({
|
|
471
|
+
profile: 'admin-v1',
|
|
472
|
+
versions: Object.freeze({ vue: 'host' }),
|
|
473
|
+
modules: Object.freeze({ vue }),
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
function validateActivation(descriptor, activation, registeredDesignerSteps = new Set()) {
|
|
477
|
+
if (typeof activation !== 'object' || activation === null) {
|
|
478
|
+
throw new Error('模块工厂必须返回 activation 对象');
|
|
479
|
+
}
|
|
480
|
+
const declaredRichTextEditors = new Set(descriptor.provides.richTextEditors);
|
|
481
|
+
const declaredCaptchaRenderers = new Set(descriptor.provides.captchaRenderers);
|
|
482
|
+
const declaredSchemaComponents = new Set(descriptor.provides.schemaComponents);
|
|
483
|
+
for (const contribution of activation.rendererContributions ?? []) {
|
|
484
|
+
for (const key of Object.keys(contribution.schemaComponents ?? {})) {
|
|
485
|
+
if (!declaredSchemaComponents.has(key)) {
|
|
486
|
+
throw new Error(`Schema component ${key} 未在 Manifest provides 中声明`);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
for (const key of Object.keys(contribution.richTextEditors ?? {})) {
|
|
490
|
+
if (key === 'default') {
|
|
491
|
+
throw new Error('Rich text editor default 由宿主内置实现保留,插件不能声明');
|
|
492
|
+
}
|
|
493
|
+
if (!declaredRichTextEditors.has(key)) {
|
|
494
|
+
throw new Error(`Rich text editor ${key} 未在 Manifest provides 中声明`);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
for (const key of Object.keys(contribution.captchaRenderers ?? {})) {
|
|
498
|
+
if (key.startsWith('host.captcha.')) {
|
|
499
|
+
throw new Error(`Captcha Renderer ${key} 由宿主保留,插件不能声明`);
|
|
500
|
+
}
|
|
501
|
+
if (!declaredCaptchaRenderers.has(key)) {
|
|
502
|
+
throw new Error(`Captcha Renderer ${key} 未在 Manifest provides 中声明`);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
const declared = new Set(descriptor.provides.designerSteps);
|
|
507
|
+
for (const [key, contribution] of Object.entries(activation.designerSteps ?? {})) {
|
|
508
|
+
if (!registeredDesignerSteps.has(key) && !declared.has(key)) {
|
|
509
|
+
throw new Error(`Designer Step ${key} 未在 Manifest provides 中声明`);
|
|
510
|
+
}
|
|
511
|
+
if (typeof contribution?.create !== 'function')
|
|
512
|
+
throw new Error(`Designer Step ${key} 缺少 create()`);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function moduleCacheKey(descriptor) {
|
|
516
|
+
return [descriptor.pluginCode, descriptor.id, descriptor.version ?? descriptor.url, descriptor.hostApi].join('\n');
|
|
517
|
+
}
|
|
518
|
+
function importFrontendModule(url) {
|
|
519
|
+
return import(/* @vite-ignore */ url);
|
|
520
|
+
}
|
|
521
|
+
async function fetchFrontendStyle(url) {
|
|
522
|
+
const response = await fetch(url);
|
|
523
|
+
if (!response.ok)
|
|
524
|
+
throw new Error(`加载 Frontend 样式 ${url} 失败: HTTP ${response.status}`);
|
|
525
|
+
return response.text();
|
|
526
|
+
}
|
|
527
|
+
//# sourceMappingURL=frontend-module-loader.js.map
|