@module-federation/runtime-core 2.0.1 → 2.2.0
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/dist/_virtual/_rolldown/runtime.cjs +19 -0
- package/dist/_virtual/_rolldown/runtime.js +18 -0
- package/dist/constant.cjs +9 -0
- package/dist/constant.cjs.map +1 -0
- package/dist/constant.js +7 -0
- package/dist/constant.js.map +1 -0
- package/dist/core.cjs +153 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.ts +129 -0
- package/dist/core.js +153 -0
- package/dist/core.js.map +1 -0
- package/dist/global.cjs +160 -0
- package/dist/global.cjs.map +1 -0
- package/dist/global.d.ts +45 -0
- package/dist/global.js +142 -0
- package/dist/global.js.map +1 -0
- package/dist/helpers.cjs +44 -0
- package/dist/helpers.cjs.map +1 -0
- package/dist/helpers.d.ts +30 -0
- package/dist/helpers.js +44 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.cjs +60 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +29 -1
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/module/index.cjs +129 -0
- package/dist/module/index.cjs.map +1 -0
- package/dist/module/index.d.ts +31 -0
- package/dist/module/index.js +129 -0
- package/dist/module/index.js.map +1 -0
- package/dist/plugins/generate-preload-assets.cjs +182 -0
- package/dist/plugins/generate-preload-assets.cjs.map +1 -0
- package/dist/plugins/generate-preload-assets.js +182 -0
- package/dist/plugins/generate-preload-assets.js.map +1 -0
- package/dist/plugins/snapshot/SnapshotHandler.cjs +190 -0
- package/dist/plugins/snapshot/SnapshotHandler.cjs.map +1 -0
- package/dist/plugins/snapshot/SnapshotHandler.d.ts +66 -0
- package/dist/plugins/snapshot/SnapshotHandler.js +189 -0
- package/dist/plugins/snapshot/SnapshotHandler.js.map +1 -0
- package/dist/plugins/snapshot/index.cjs +63 -0
- package/dist/plugins/snapshot/index.cjs.map +1 -0
- package/dist/plugins/snapshot/index.js +62 -0
- package/dist/plugins/snapshot/index.js.map +1 -0
- package/dist/remote/index.cjs +304 -0
- package/dist/remote/index.cjs.map +1 -0
- package/dist/remote/index.d.ts +119 -0
- package/dist/remote/index.js +304 -0
- package/dist/remote/index.js.map +1 -0
- package/dist/shared/index.cjs +303 -0
- package/dist/shared/index.cjs.map +1 -0
- package/dist/shared/index.d.ts +82 -0
- package/dist/shared/index.js +303 -0
- package/dist/shared/index.js.map +1 -0
- package/dist/type/config.d.ts +130 -0
- package/dist/type/index.cjs +13 -0
- package/dist/type/index.cjs.map +1 -0
- package/dist/type/index.d.ts +11 -0
- package/dist/type/index.js +8 -0
- package/dist/type/index.js.map +1 -0
- package/dist/type/plugin.d.ts +27 -0
- package/dist/type/preload.d.ts +31 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +4 -1
- package/dist/types.js +1 -0
- package/dist/utils/context.cjs +45 -0
- package/dist/utils/context.cjs.map +1 -0
- package/dist/utils/context.d.ts +1 -0
- package/dist/utils/context.js +44 -0
- package/dist/utils/context.js.map +1 -0
- package/dist/utils/env.cjs +10 -0
- package/dist/utils/env.cjs.map +1 -0
- package/dist/utils/env.d.ts +1 -0
- package/dist/utils/env.js +10 -0
- package/dist/utils/env.js.map +1 -0
- package/dist/utils/hooks/asyncHook.cjs +23 -0
- package/dist/utils/hooks/asyncHook.cjs.map +1 -0
- package/dist/utils/hooks/asyncHook.d.ts +10 -0
- package/dist/utils/hooks/asyncHook.js +23 -0
- package/dist/utils/hooks/asyncHook.js.map +1 -0
- package/dist/utils/hooks/asyncWaterfallHooks.cjs +42 -0
- package/dist/utils/hooks/asyncWaterfallHooks.cjs.map +1 -0
- package/dist/utils/hooks/asyncWaterfallHooks.d.ts +12 -0
- package/dist/utils/hooks/asyncWaterfallHooks.js +42 -0
- package/dist/utils/hooks/asyncWaterfallHooks.js.map +1 -0
- package/dist/utils/hooks/index.cjs +5 -0
- package/dist/utils/hooks/index.d.ts +5 -0
- package/dist/utils/hooks/index.js +7 -0
- package/dist/utils/hooks/pluginSystem.cjs +37 -0
- package/dist/utils/hooks/pluginSystem.cjs.map +1 -0
- package/dist/utils/hooks/pluginSystem.d.ts +19 -0
- package/dist/utils/hooks/pluginSystem.js +37 -0
- package/dist/utils/hooks/pluginSystem.js.map +1 -0
- package/dist/utils/hooks/syncHook.cjs +36 -0
- package/dist/utils/hooks/syncHook.cjs.map +1 -0
- package/dist/utils/hooks/syncHook.d.ts +16 -0
- package/dist/utils/hooks/syncHook.js +35 -0
- package/dist/utils/hooks/syncHook.js.map +1 -0
- package/dist/utils/hooks/syncWaterfallHook.cjs +39 -0
- package/dist/utils/hooks/syncWaterfallHook.cjs.map +1 -0
- package/dist/utils/hooks/syncWaterfallHook.d.ts +11 -0
- package/dist/utils/hooks/syncWaterfallHook.js +38 -0
- package/dist/utils/hooks/syncWaterfallHook.js.map +1 -0
- package/dist/utils/index.cjs +8 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +9 -0
- package/dist/utils/load.cjs +179 -0
- package/dist/utils/load.cjs.map +1 -0
- package/dist/utils/load.d.ts +14 -0
- package/dist/utils/load.js +177 -0
- package/dist/utils/load.js.map +1 -0
- package/dist/utils/logger.cjs +34 -0
- package/dist/utils/logger.cjs.map +1 -0
- package/dist/utils/logger.d.ts +10 -0
- package/dist/utils/logger.js +31 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/manifest.cjs +51 -0
- package/dist/utils/manifest.cjs.map +1 -0
- package/dist/utils/manifest.d.ts +10 -0
- package/dist/utils/manifest.js +49 -0
- package/dist/utils/manifest.js.map +1 -0
- package/dist/utils/plugin.cjs +27 -0
- package/dist/utils/plugin.cjs.map +1 -0
- package/dist/utils/plugin.d.ts +1 -0
- package/dist/utils/plugin.js +27 -0
- package/dist/utils/plugin.js.map +1 -0
- package/dist/utils/preload.cjs +146 -0
- package/dist/utils/preload.cjs.map +1 -0
- package/dist/utils/preload.d.ts +8 -0
- package/dist/utils/preload.js +143 -0
- package/dist/utils/preload.js.map +1 -0
- package/dist/utils/semver/compare.cjs +48 -0
- package/dist/utils/semver/compare.cjs.map +1 -0
- package/dist/utils/semver/compare.js +47 -0
- package/dist/utils/semver/compare.js.map +1 -0
- package/dist/utils/semver/constants.cjs +41 -0
- package/dist/utils/semver/constants.cjs.map +1 -0
- package/dist/utils/semver/constants.js +31 -0
- package/dist/utils/semver/constants.js.map +1 -0
- package/dist/utils/semver/index.cjs +68 -0
- package/dist/utils/semver/index.cjs.map +1 -0
- package/dist/utils/semver/index.d.ts +5 -0
- package/dist/utils/semver/index.js +68 -0
- package/dist/utils/semver/index.js.map +1 -0
- package/dist/utils/semver/parser.cjs +104 -0
- package/dist/utils/semver/parser.cjs.map +1 -0
- package/dist/utils/semver/parser.js +96 -0
- package/dist/utils/semver/parser.js.map +1 -0
- package/dist/utils/semver/utils.cjs +28 -0
- package/dist/utils/semver/utils.cjs.map +1 -0
- package/dist/utils/semver/utils.js +24 -0
- package/dist/utils/semver/utils.js.map +1 -0
- package/dist/utils/share.cjs +284 -0
- package/dist/utils/share.cjs.map +1 -0
- package/dist/utils/share.d.ts +23 -0
- package/dist/utils/share.js +278 -0
- package/dist/utils/share.js.map +1 -0
- package/dist/utils/tool.cjs +82 -0
- package/dist/utils/tool.cjs.map +1 -0
- package/dist/utils/tool.d.ts +8 -0
- package/dist/utils/tool.js +71 -0
- package/dist/utils/tool.js.map +1 -0
- package/package.json +16 -9
- package/dist/index.cjs.cjs +0 -3442
- package/dist/index.cjs.cjs.map +0 -1
- package/dist/index.cjs.d.ts +0 -1
- package/dist/index.esm.js +0 -3418
- package/dist/index.esm.js.map +0 -1
- package/dist/src/constant.d.ts +0 -2
- package/dist/src/core.d.ts +0 -119
- package/dist/src/global.d.ts +0 -42
- package/dist/src/helpers.d.ts +0 -38
- package/dist/src/index.d.ts +0 -14
- package/dist/src/module/index.d.ts +0 -25
- package/dist/src/plugins/generate-preload-assets.d.ts +0 -8
- package/dist/src/plugins/snapshot/SnapshotHandler.d.ts +0 -60
- package/dist/src/plugins/snapshot/index.d.ts +0 -5
- package/dist/src/remote/index.d.ts +0 -109
- package/dist/src/shared/index.d.ts +0 -75
- package/dist/src/type/config.d.ts +0 -128
- package/dist/src/type/index.d.ts +0 -3
- package/dist/src/type/plugin.d.ts +0 -35
- package/dist/src/type/preload.d.ts +0 -26
- package/dist/src/types.d.ts +0 -1
- package/dist/src/utils/env.d.ts +0 -3
- package/dist/src/utils/hooks/asyncHook.d.ts +0 -6
- package/dist/src/utils/hooks/asyncWaterfallHooks.d.ts +0 -10
- package/dist/src/utils/hooks/index.d.ts +0 -6
- package/dist/src/utils/hooks/pluginSystem.d.ts +0 -16
- package/dist/src/utils/hooks/syncHook.d.ts +0 -12
- package/dist/src/utils/hooks/syncWaterfallHook.d.ts +0 -9
- package/dist/src/utils/index.d.ts +0 -6
- package/dist/src/utils/load.d.ts +0 -11
- package/dist/src/utils/logger.d.ts +0 -6
- package/dist/src/utils/manifest.d.ts +0 -7
- package/dist/src/utils/plugin.d.ts +0 -3
- package/dist/src/utils/preload.d.ts +0 -6
- package/dist/src/utils/semver/compare.d.ts +0 -9
- package/dist/src/utils/semver/constants.d.ts +0 -10
- package/dist/src/utils/semver/index.d.ts +0 -2
- package/dist/src/utils/semver/parser.d.ts +0 -9
- package/dist/src/utils/semver/utils.d.ts +0 -11
- package/dist/src/utils/share.d.ts +0 -45
- package/dist/src/utils/tool.d.ts +0 -18
- package/dist/types.cjs.cjs +0 -3
- package/dist/types.cjs.cjs.map +0 -1
- package/dist/types.cjs.d.ts +0 -1
- package/dist/types.esm.js +0 -2
- package/dist/types.esm.js.map +0 -1
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { assert, error } from "../../utils/logger.js";
|
|
2
|
+
import { isRemoteInfoWithEntry } from "../../utils/tool.js";
|
|
3
|
+
import { Global, addGlobalSnapshot, getGlobalSnapshot, getGlobalSnapshotInfoByModuleInfo, getInfoWithoutType, setGlobalSnapshotInfoByModuleInfo } from "../../global.js";
|
|
4
|
+
import { optionsToMFContext } from "../../utils/context.js";
|
|
5
|
+
import "../../utils/index.js";
|
|
6
|
+
import { AsyncHook } from "../../utils/hooks/asyncHook.js";
|
|
7
|
+
import { AsyncWaterfallHook } from "../../utils/hooks/asyncWaterfallHooks.js";
|
|
8
|
+
import { PluginSystem } from "../../utils/hooks/pluginSystem.js";
|
|
9
|
+
import "../../utils/hooks/index.js";
|
|
10
|
+
import { generateSnapshotFromManifest, isBrowserEnvValue, isManifestProvider } from "@module-federation/sdk";
|
|
11
|
+
import { RUNTIME_003, RUNTIME_007, runtimeDescMap } from "@module-federation/error-codes";
|
|
12
|
+
|
|
13
|
+
//#region src/plugins/snapshot/SnapshotHandler.ts
|
|
14
|
+
function getGlobalRemoteInfo(moduleInfo, origin) {
|
|
15
|
+
const hostGlobalSnapshot = getGlobalSnapshotInfoByModuleInfo({
|
|
16
|
+
name: origin.name,
|
|
17
|
+
version: origin.options.version
|
|
18
|
+
});
|
|
19
|
+
const globalRemoteInfo = hostGlobalSnapshot && "remotesInfo" in hostGlobalSnapshot && hostGlobalSnapshot.remotesInfo && getInfoWithoutType(hostGlobalSnapshot.remotesInfo, moduleInfo.name).value;
|
|
20
|
+
if (globalRemoteInfo && globalRemoteInfo.matchedVersion) return {
|
|
21
|
+
hostGlobalSnapshot,
|
|
22
|
+
globalSnapshot: getGlobalSnapshot(),
|
|
23
|
+
remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
|
|
24
|
+
name: moduleInfo.name,
|
|
25
|
+
version: globalRemoteInfo.matchedVersion
|
|
26
|
+
})
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
hostGlobalSnapshot: void 0,
|
|
30
|
+
globalSnapshot: getGlobalSnapshot(),
|
|
31
|
+
remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
|
|
32
|
+
name: moduleInfo.name,
|
|
33
|
+
version: "version" in moduleInfo ? moduleInfo.version : void 0
|
|
34
|
+
})
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
var SnapshotHandler = class {
|
|
38
|
+
constructor(HostInstance) {
|
|
39
|
+
this.loadingHostSnapshot = null;
|
|
40
|
+
this.manifestCache = /* @__PURE__ */ new Map();
|
|
41
|
+
this.hooks = new PluginSystem({
|
|
42
|
+
beforeLoadRemoteSnapshot: new AsyncHook("beforeLoadRemoteSnapshot"),
|
|
43
|
+
loadSnapshot: new AsyncWaterfallHook("loadGlobalSnapshot"),
|
|
44
|
+
loadRemoteSnapshot: new AsyncWaterfallHook("loadRemoteSnapshot"),
|
|
45
|
+
afterLoadSnapshot: new AsyncWaterfallHook("afterLoadSnapshot")
|
|
46
|
+
});
|
|
47
|
+
this.manifestLoading = Global.__FEDERATION__.__MANIFEST_LOADING__;
|
|
48
|
+
this.HostInstance = HostInstance;
|
|
49
|
+
this.loaderHook = HostInstance.loaderHook;
|
|
50
|
+
}
|
|
51
|
+
async loadRemoteSnapshotInfo({ moduleInfo, id, expose }) {
|
|
52
|
+
const { options } = this.HostInstance;
|
|
53
|
+
await this.hooks.lifecycle.beforeLoadRemoteSnapshot.emit({
|
|
54
|
+
options,
|
|
55
|
+
moduleInfo
|
|
56
|
+
});
|
|
57
|
+
let hostSnapshot = getGlobalSnapshotInfoByModuleInfo({
|
|
58
|
+
name: this.HostInstance.options.name,
|
|
59
|
+
version: this.HostInstance.options.version
|
|
60
|
+
});
|
|
61
|
+
if (!hostSnapshot) {
|
|
62
|
+
hostSnapshot = {
|
|
63
|
+
version: this.HostInstance.options.version || "",
|
|
64
|
+
remoteEntry: "",
|
|
65
|
+
remotesInfo: {}
|
|
66
|
+
};
|
|
67
|
+
addGlobalSnapshot({ [this.HostInstance.options.name]: hostSnapshot });
|
|
68
|
+
}
|
|
69
|
+
if (hostSnapshot && "remotesInfo" in hostSnapshot && !getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name).value) {
|
|
70
|
+
if ("version" in moduleInfo || "entry" in moduleInfo) hostSnapshot.remotesInfo = {
|
|
71
|
+
...hostSnapshot?.remotesInfo,
|
|
72
|
+
[moduleInfo.name]: { matchedVersion: "version" in moduleInfo ? moduleInfo.version : moduleInfo.entry }
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const { hostGlobalSnapshot, remoteSnapshot, globalSnapshot } = this.getGlobalRemoteInfo(moduleInfo);
|
|
76
|
+
const { remoteSnapshot: globalRemoteSnapshot, globalSnapshot: globalSnapshotRes } = await this.hooks.lifecycle.loadSnapshot.emit({
|
|
77
|
+
options,
|
|
78
|
+
moduleInfo,
|
|
79
|
+
hostGlobalSnapshot,
|
|
80
|
+
remoteSnapshot,
|
|
81
|
+
globalSnapshot
|
|
82
|
+
});
|
|
83
|
+
let mSnapshot;
|
|
84
|
+
let gSnapshot;
|
|
85
|
+
if (globalRemoteSnapshot) if (isManifestProvider(globalRemoteSnapshot)) {
|
|
86
|
+
const remoteEntry = isBrowserEnvValue ? globalRemoteSnapshot.remoteEntry : globalRemoteSnapshot.ssrRemoteEntry || globalRemoteSnapshot.remoteEntry || "";
|
|
87
|
+
const moduleSnapshot = await this.getManifestJson(remoteEntry, moduleInfo, {});
|
|
88
|
+
const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo({
|
|
89
|
+
...moduleInfo,
|
|
90
|
+
entry: remoteEntry
|
|
91
|
+
}, moduleSnapshot);
|
|
92
|
+
mSnapshot = moduleSnapshot;
|
|
93
|
+
gSnapshot = globalSnapshotRes;
|
|
94
|
+
} else {
|
|
95
|
+
const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
|
|
96
|
+
options: this.HostInstance.options,
|
|
97
|
+
moduleInfo,
|
|
98
|
+
remoteSnapshot: globalRemoteSnapshot,
|
|
99
|
+
from: "global"
|
|
100
|
+
});
|
|
101
|
+
mSnapshot = remoteSnapshotRes;
|
|
102
|
+
gSnapshot = globalSnapshotRes;
|
|
103
|
+
}
|
|
104
|
+
else if (isRemoteInfoWithEntry(moduleInfo)) {
|
|
105
|
+
const moduleSnapshot = await this.getManifestJson(moduleInfo.entry, moduleInfo, {});
|
|
106
|
+
const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(moduleInfo, moduleSnapshot);
|
|
107
|
+
const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
|
|
108
|
+
options: this.HostInstance.options,
|
|
109
|
+
moduleInfo,
|
|
110
|
+
remoteSnapshot: moduleSnapshot,
|
|
111
|
+
from: "global"
|
|
112
|
+
});
|
|
113
|
+
mSnapshot = remoteSnapshotRes;
|
|
114
|
+
gSnapshot = globalSnapshotRes;
|
|
115
|
+
} else error(RUNTIME_007, runtimeDescMap, {
|
|
116
|
+
remoteName: moduleInfo.name,
|
|
117
|
+
remoteVersion: moduleInfo.version,
|
|
118
|
+
hostName: this.HostInstance.options.name,
|
|
119
|
+
globalSnapshot: JSON.stringify(globalSnapshotRes)
|
|
120
|
+
}, void 0, optionsToMFContext(this.HostInstance.options));
|
|
121
|
+
await this.hooks.lifecycle.afterLoadSnapshot.emit({
|
|
122
|
+
id,
|
|
123
|
+
host: this.HostInstance,
|
|
124
|
+
options,
|
|
125
|
+
moduleInfo,
|
|
126
|
+
remoteSnapshot: mSnapshot
|
|
127
|
+
});
|
|
128
|
+
return {
|
|
129
|
+
remoteSnapshot: mSnapshot,
|
|
130
|
+
globalSnapshot: gSnapshot
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
getGlobalRemoteInfo(moduleInfo) {
|
|
134
|
+
return getGlobalRemoteInfo(moduleInfo, this.HostInstance);
|
|
135
|
+
}
|
|
136
|
+
async getManifestJson(manifestUrl, moduleInfo, extraOptions) {
|
|
137
|
+
const getManifest = async () => {
|
|
138
|
+
let manifestJson = this.manifestCache.get(manifestUrl);
|
|
139
|
+
if (manifestJson) return manifestJson;
|
|
140
|
+
try {
|
|
141
|
+
let res = await this.loaderHook.lifecycle.fetch.emit(manifestUrl, {});
|
|
142
|
+
if (!res || !(res instanceof Response)) res = await fetch(manifestUrl, {});
|
|
143
|
+
manifestJson = await res.json();
|
|
144
|
+
} catch (err) {
|
|
145
|
+
manifestJson = await this.HostInstance.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({
|
|
146
|
+
id: manifestUrl,
|
|
147
|
+
error: err,
|
|
148
|
+
from: "runtime",
|
|
149
|
+
lifecycle: "afterResolve",
|
|
150
|
+
origin: this.HostInstance
|
|
151
|
+
});
|
|
152
|
+
if (!manifestJson) {
|
|
153
|
+
delete this.manifestLoading[manifestUrl];
|
|
154
|
+
error(RUNTIME_003, runtimeDescMap, {
|
|
155
|
+
manifestUrl,
|
|
156
|
+
moduleName: moduleInfo.name,
|
|
157
|
+
hostName: this.HostInstance.options.name
|
|
158
|
+
}, `${err}`, optionsToMFContext(this.HostInstance.options));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
assert(manifestJson.metaData && manifestJson.exposes && manifestJson.shared, `"${manifestUrl}" is not a valid federation manifest for remote "${moduleInfo.name}". Missing required fields: ${[
|
|
162
|
+
!manifestJson.metaData && "metaData",
|
|
163
|
+
!manifestJson.exposes && "exposes",
|
|
164
|
+
!manifestJson.shared && "shared"
|
|
165
|
+
].filter(Boolean).join(", ")}.`);
|
|
166
|
+
this.manifestCache.set(manifestUrl, manifestJson);
|
|
167
|
+
return manifestJson;
|
|
168
|
+
};
|
|
169
|
+
const asyncLoadProcess = async () => {
|
|
170
|
+
const manifestJson = await getManifest();
|
|
171
|
+
const remoteSnapshot = generateSnapshotFromManifest(manifestJson, { version: manifestUrl });
|
|
172
|
+
const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
|
|
173
|
+
options: this.HostInstance.options,
|
|
174
|
+
moduleInfo,
|
|
175
|
+
manifestJson,
|
|
176
|
+
remoteSnapshot,
|
|
177
|
+
manifestUrl,
|
|
178
|
+
from: "manifest"
|
|
179
|
+
});
|
|
180
|
+
return remoteSnapshotRes;
|
|
181
|
+
};
|
|
182
|
+
if (!this.manifestLoading[manifestUrl]) this.manifestLoading[manifestUrl] = asyncLoadProcess().then((res) => res);
|
|
183
|
+
return this.manifestLoading[manifestUrl];
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
//#endregion
|
|
188
|
+
export { SnapshotHandler, getGlobalRemoteInfo };
|
|
189
|
+
//# sourceMappingURL=SnapshotHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SnapshotHandler.js","names":[],"sources":["../../../src/plugins/snapshot/SnapshotHandler.ts"],"sourcesContent":["import {\n GlobalModuleInfo,\n Manifest,\n ModuleInfo,\n generateSnapshotFromManifest,\n isManifestProvider,\n isBrowserEnvValue,\n} from '@module-federation/sdk';\nimport {\n RUNTIME_003,\n RUNTIME_007,\n runtimeDescMap,\n} from '@module-federation/error-codes';\nimport { Options, Remote } from '../../type';\nimport { isRemoteInfoWithEntry, error, optionsToMFContext } from '../../utils';\nimport {\n getGlobalSnapshot,\n setGlobalSnapshotInfoByModuleInfo,\n Global,\n addGlobalSnapshot,\n getGlobalSnapshotInfoByModuleInfo,\n getInfoWithoutType,\n} from '../../global';\nimport { PluginSystem, AsyncHook, AsyncWaterfallHook } from '../../utils/hooks';\nimport { ModuleFederation } from '../../core';\nimport { assert } from '../../utils/logger';\n\nexport function getGlobalRemoteInfo(\n moduleInfo: Remote,\n origin: ModuleFederation,\n): {\n hostGlobalSnapshot: ModuleInfo | undefined;\n globalSnapshot: ReturnType<typeof getGlobalSnapshot>;\n remoteSnapshot: GlobalModuleInfo[string] | undefined;\n} {\n const hostGlobalSnapshot = getGlobalSnapshotInfoByModuleInfo({\n name: origin.name,\n version: origin.options.version,\n });\n\n // get remote detail info from global\n const globalRemoteInfo =\n hostGlobalSnapshot &&\n 'remotesInfo' in hostGlobalSnapshot &&\n hostGlobalSnapshot.remotesInfo &&\n getInfoWithoutType(hostGlobalSnapshot.remotesInfo, moduleInfo.name).value;\n\n if (globalRemoteInfo && globalRemoteInfo.matchedVersion) {\n return {\n hostGlobalSnapshot,\n globalSnapshot: getGlobalSnapshot(),\n remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({\n name: moduleInfo.name,\n version: globalRemoteInfo.matchedVersion,\n }),\n };\n }\n\n return {\n hostGlobalSnapshot: undefined,\n globalSnapshot: getGlobalSnapshot(),\n remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({\n name: moduleInfo.name,\n version: 'version' in moduleInfo ? moduleInfo.version : undefined,\n }),\n };\n}\n\nexport class SnapshotHandler {\n loadingHostSnapshot: Promise<GlobalModuleInfo | void> | null = null;\n HostInstance: ModuleFederation;\n manifestCache: Map<string, Manifest> = new Map();\n hooks = new PluginSystem({\n beforeLoadRemoteSnapshot: new AsyncHook<\n [\n {\n options: Options;\n moduleInfo: Remote;\n },\n ],\n void\n >('beforeLoadRemoteSnapshot'),\n loadSnapshot: new AsyncWaterfallHook<{\n options: Options;\n moduleInfo: Remote;\n hostGlobalSnapshot: GlobalModuleInfo[string] | undefined;\n globalSnapshot: ReturnType<typeof getGlobalSnapshot>;\n remoteSnapshot?: GlobalModuleInfo[string] | undefined;\n }>('loadGlobalSnapshot'),\n loadRemoteSnapshot: new AsyncWaterfallHook<{\n options: Options;\n moduleInfo: Remote;\n manifestJson?: Manifest;\n manifestUrl?: string;\n remoteSnapshot: ModuleInfo;\n from: 'global' | 'manifest';\n }>('loadRemoteSnapshot'),\n afterLoadSnapshot: new AsyncWaterfallHook<{\n id?: string;\n host: ModuleFederation;\n options: Options;\n moduleInfo: Remote;\n remoteSnapshot: ModuleInfo;\n }>('afterLoadSnapshot'),\n });\n loaderHook: ModuleFederation['loaderHook'];\n manifestLoading: Record<string, Promise<ModuleInfo>> =\n Global.__FEDERATION__.__MANIFEST_LOADING__;\n\n constructor(HostInstance: ModuleFederation) {\n this.HostInstance = HostInstance;\n this.loaderHook = HostInstance.loaderHook;\n }\n\n // eslint-disable-next-line max-lines-per-function\n async loadRemoteSnapshotInfo({\n moduleInfo,\n id,\n expose,\n }: {\n moduleInfo: Remote;\n id?: string;\n expose?: string;\n }):\n | Promise<{\n remoteSnapshot: ModuleInfo;\n globalSnapshot: GlobalModuleInfo;\n }>\n | never {\n const { options } = this.HostInstance;\n\n await this.hooks.lifecycle.beforeLoadRemoteSnapshot.emit({\n options,\n moduleInfo,\n });\n\n let hostSnapshot = getGlobalSnapshotInfoByModuleInfo({\n name: this.HostInstance.options.name,\n version: this.HostInstance.options.version,\n });\n\n if (!hostSnapshot) {\n hostSnapshot = {\n version: this.HostInstance.options.version || '',\n remoteEntry: '',\n remotesInfo: {},\n };\n addGlobalSnapshot({\n [this.HostInstance.options.name]: hostSnapshot,\n });\n }\n\n // In dynamic loadRemote scenarios, incomplete remotesInfo delivery may occur. In such cases, the remotesInfo in the host needs to be completed in the snapshot at runtime.\n // This ensures the snapshot's integrity and helps the chrome plugin correctly identify all producer modules, ensuring that proxyable producer modules will not be missing.\n if (\n hostSnapshot &&\n 'remotesInfo' in hostSnapshot &&\n !getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name).value\n ) {\n if ('version' in moduleInfo || 'entry' in moduleInfo) {\n hostSnapshot.remotesInfo = {\n ...hostSnapshot?.remotesInfo,\n [moduleInfo.name]: {\n matchedVersion:\n 'version' in moduleInfo ? moduleInfo.version : moduleInfo.entry,\n },\n };\n }\n }\n\n const { hostGlobalSnapshot, remoteSnapshot, globalSnapshot } =\n this.getGlobalRemoteInfo(moduleInfo);\n const {\n remoteSnapshot: globalRemoteSnapshot,\n globalSnapshot: globalSnapshotRes,\n } = await this.hooks.lifecycle.loadSnapshot.emit({\n options,\n moduleInfo,\n hostGlobalSnapshot,\n remoteSnapshot,\n globalSnapshot,\n });\n\n let mSnapshot;\n let gSnapshot;\n // global snapshot includes manifest or module info includes manifest\n if (globalRemoteSnapshot) {\n if (isManifestProvider(globalRemoteSnapshot)) {\n const remoteEntry = isBrowserEnvValue\n ? globalRemoteSnapshot.remoteEntry\n : globalRemoteSnapshot.ssrRemoteEntry ||\n globalRemoteSnapshot.remoteEntry ||\n '';\n const moduleSnapshot = await this.getManifestJson(\n remoteEntry,\n moduleInfo,\n {},\n );\n // eslint-disable-next-line @typescript-eslint/no-shadow\n const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(\n {\n ...moduleInfo,\n // The global remote may be overridden\n // Therefore, set the snapshot key to the global address of the actual request\n entry: remoteEntry,\n },\n moduleSnapshot,\n );\n mSnapshot = moduleSnapshot;\n gSnapshot = globalSnapshotRes;\n } else {\n const { remoteSnapshot: remoteSnapshotRes } =\n await this.hooks.lifecycle.loadRemoteSnapshot.emit({\n options: this.HostInstance.options,\n moduleInfo,\n remoteSnapshot: globalRemoteSnapshot,\n from: 'global',\n });\n mSnapshot = remoteSnapshotRes;\n gSnapshot = globalSnapshotRes;\n }\n } else {\n if (isRemoteInfoWithEntry(moduleInfo)) {\n // get from manifest.json and merge remote info from remote server\n const moduleSnapshot = await this.getManifestJson(\n moduleInfo.entry,\n moduleInfo,\n {},\n );\n // eslint-disable-next-line @typescript-eslint/no-shadow\n const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(\n moduleInfo,\n moduleSnapshot,\n );\n const { remoteSnapshot: remoteSnapshotRes } =\n await this.hooks.lifecycle.loadRemoteSnapshot.emit({\n options: this.HostInstance.options,\n moduleInfo,\n remoteSnapshot: moduleSnapshot,\n from: 'global',\n });\n\n mSnapshot = remoteSnapshotRes;\n gSnapshot = globalSnapshotRes;\n } else {\n error(\n RUNTIME_007,\n runtimeDescMap,\n {\n remoteName: moduleInfo.name,\n remoteVersion: moduleInfo.version,\n hostName: this.HostInstance.options.name,\n globalSnapshot: JSON.stringify(globalSnapshotRes),\n },\n undefined,\n optionsToMFContext(this.HostInstance.options),\n );\n }\n }\n\n await this.hooks.lifecycle.afterLoadSnapshot.emit({\n id,\n host: this.HostInstance,\n options,\n moduleInfo,\n remoteSnapshot: mSnapshot,\n });\n\n return {\n remoteSnapshot: mSnapshot,\n globalSnapshot: gSnapshot,\n };\n }\n\n getGlobalRemoteInfo(moduleInfo: Remote): {\n hostGlobalSnapshot: ModuleInfo | undefined;\n globalSnapshot: ReturnType<typeof getGlobalSnapshot>;\n remoteSnapshot: GlobalModuleInfo[string] | undefined;\n } {\n return getGlobalRemoteInfo(moduleInfo, this.HostInstance);\n }\n\n private async getManifestJson(\n manifestUrl: string,\n moduleInfo: Remote,\n extraOptions: Record<string, any>,\n ): Promise<ModuleInfo> {\n const getManifest = async (): Promise<Manifest> => {\n let manifestJson: Manifest | undefined =\n this.manifestCache.get(manifestUrl);\n if (manifestJson) {\n return manifestJson;\n }\n try {\n let res = await this.loaderHook.lifecycle.fetch.emit(manifestUrl, {});\n if (!res || !(res instanceof Response)) {\n res = await fetch(manifestUrl, {});\n }\n manifestJson = (await res.json()) as Manifest;\n } catch (err) {\n manifestJson =\n (await this.HostInstance.remoteHandler.hooks.lifecycle.errorLoadRemote.emit(\n {\n id: manifestUrl,\n error: err,\n from: 'runtime',\n lifecycle: 'afterResolve',\n origin: this.HostInstance,\n },\n )) as Manifest | undefined;\n\n if (!manifestJson) {\n delete this.manifestLoading[manifestUrl];\n error(\n RUNTIME_003,\n runtimeDescMap,\n {\n manifestUrl,\n moduleName: moduleInfo.name,\n hostName: this.HostInstance.options.name,\n },\n `${err}`,\n optionsToMFContext(this.HostInstance.options),\n );\n }\n }\n\n assert(\n manifestJson.metaData && manifestJson.exposes && manifestJson.shared,\n `\"${manifestUrl}\" is not a valid federation manifest for remote \"${moduleInfo.name}\". Missing required fields: ${[!manifestJson.metaData && 'metaData', !manifestJson.exposes && 'exposes', !manifestJson.shared && 'shared'].filter(Boolean).join(', ')}.`,\n );\n this.manifestCache.set(manifestUrl, manifestJson);\n return manifestJson;\n };\n\n const asyncLoadProcess = async () => {\n const manifestJson = await getManifest();\n const remoteSnapshot = generateSnapshotFromManifest(manifestJson, {\n version: manifestUrl,\n });\n\n const { remoteSnapshot: remoteSnapshotRes } =\n await this.hooks.lifecycle.loadRemoteSnapshot.emit({\n options: this.HostInstance.options,\n moduleInfo,\n manifestJson,\n remoteSnapshot,\n manifestUrl,\n from: 'manifest',\n });\n return remoteSnapshotRes;\n };\n\n if (!this.manifestLoading[manifestUrl]) {\n this.manifestLoading[manifestUrl] = asyncLoadProcess().then((res) => res);\n }\n return this.manifestLoading[manifestUrl];\n }\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const require_logger = require('../../utils/logger.cjs');
|
|
2
|
+
const require_tool = require('../../utils/tool.cjs');
|
|
3
|
+
require('../../utils/index.cjs');
|
|
4
|
+
const require_preload = require('../../utils/preload.cjs');
|
|
5
|
+
let _module_federation_sdk = require("@module-federation/sdk");
|
|
6
|
+
let _module_federation_error_codes = require("@module-federation/error-codes");
|
|
7
|
+
|
|
8
|
+
//#region src/plugins/snapshot/index.ts
|
|
9
|
+
function assignRemoteInfo(remoteInfo, remoteSnapshot) {
|
|
10
|
+
const remoteEntryInfo = require_tool.getRemoteEntryInfoFromSnapshot(remoteSnapshot);
|
|
11
|
+
if (!remoteEntryInfo.url) require_logger.error(_module_federation_error_codes.RUNTIME_011, _module_federation_error_codes.runtimeDescMap, { remoteName: remoteInfo.name });
|
|
12
|
+
let entryUrl = (0, _module_federation_sdk.getResourceUrl)(remoteSnapshot, remoteEntryInfo.url);
|
|
13
|
+
if (!_module_federation_sdk.isBrowserEnvValue && !entryUrl.startsWith("http")) entryUrl = `https:${entryUrl}`;
|
|
14
|
+
remoteInfo.type = remoteEntryInfo.type;
|
|
15
|
+
remoteInfo.entryGlobalName = remoteEntryInfo.globalName;
|
|
16
|
+
remoteInfo.entry = entryUrl;
|
|
17
|
+
remoteInfo.version = remoteSnapshot.version;
|
|
18
|
+
remoteInfo.buildVersion = remoteSnapshot.buildVersion;
|
|
19
|
+
}
|
|
20
|
+
function snapshotPlugin() {
|
|
21
|
+
return {
|
|
22
|
+
name: "snapshot-plugin",
|
|
23
|
+
async afterResolve(args) {
|
|
24
|
+
const { remote, pkgNameOrAlias, expose, origin, remoteInfo, id } = args;
|
|
25
|
+
if (!require_tool.isRemoteInfoWithEntry(remote) || !require_tool.isPureRemoteEntry(remote)) {
|
|
26
|
+
const { remoteSnapshot, globalSnapshot } = await origin.snapshotHandler.loadRemoteSnapshotInfo({
|
|
27
|
+
moduleInfo: remote,
|
|
28
|
+
id
|
|
29
|
+
});
|
|
30
|
+
assignRemoteInfo(remoteInfo, remoteSnapshot);
|
|
31
|
+
const preloadOptions = {
|
|
32
|
+
remote,
|
|
33
|
+
preloadConfig: {
|
|
34
|
+
nameOrAlias: pkgNameOrAlias,
|
|
35
|
+
exposes: [expose],
|
|
36
|
+
resourceCategory: "sync",
|
|
37
|
+
share: false,
|
|
38
|
+
depsRemote: false
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const assets = await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit({
|
|
42
|
+
origin,
|
|
43
|
+
preloadOptions,
|
|
44
|
+
remoteInfo,
|
|
45
|
+
remote,
|
|
46
|
+
remoteSnapshot,
|
|
47
|
+
globalSnapshot
|
|
48
|
+
});
|
|
49
|
+
if (assets) require_preload.preloadAssets(remoteInfo, origin, assets, false);
|
|
50
|
+
return {
|
|
51
|
+
...args,
|
|
52
|
+
remoteSnapshot
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return args;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
exports.assignRemoteInfo = assignRemoteInfo;
|
|
62
|
+
exports.snapshotPlugin = snapshotPlugin;
|
|
63
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["getRemoteEntryInfoFromSnapshot","RUNTIME_011","runtimeDescMap","isBrowserEnvValue","isRemoteInfoWithEntry","isPureRemoteEntry"],"sources":["../../../src/plugins/snapshot/index.ts"],"sourcesContent":["import {\n ModuleInfo,\n getResourceUrl,\n isBrowserEnvValue,\n} from '@module-federation/sdk';\nimport { ModuleFederationRuntimePlugin } from '../../type/plugin';\nimport { RUNTIME_011, runtimeDescMap } from '@module-federation/error-codes';\nimport {\n error,\n isPureRemoteEntry,\n isRemoteInfoWithEntry,\n getRemoteEntryInfoFromSnapshot,\n} from '../../utils';\nimport { PreloadOptions, RemoteInfo } from '../../type';\nimport { preloadAssets } from '../../utils/preload';\n\nexport function assignRemoteInfo(\n remoteInfo: RemoteInfo,\n remoteSnapshot: ModuleInfo,\n): void {\n const remoteEntryInfo = getRemoteEntryInfoFromSnapshot(remoteSnapshot);\n if (!remoteEntryInfo.url) {\n error(RUNTIME_011, runtimeDescMap, { remoteName: remoteInfo.name });\n }\n\n let entryUrl = getResourceUrl(remoteSnapshot, remoteEntryInfo.url);\n\n if (!isBrowserEnvValue && !entryUrl.startsWith('http')) {\n entryUrl = `https:${entryUrl}`;\n }\n\n remoteInfo.type = remoteEntryInfo.type;\n remoteInfo.entryGlobalName = remoteEntryInfo.globalName;\n remoteInfo.entry = entryUrl;\n remoteInfo.version = remoteSnapshot.version;\n remoteInfo.buildVersion = remoteSnapshot.buildVersion;\n}\n\nexport function snapshotPlugin(): ModuleFederationRuntimePlugin {\n return {\n name: 'snapshot-plugin',\n async afterResolve(args) {\n const { remote, pkgNameOrAlias, expose, origin, remoteInfo, id } = args;\n\n if (!isRemoteInfoWithEntry(remote) || !isPureRemoteEntry(remote)) {\n const { remoteSnapshot, globalSnapshot } =\n await origin.snapshotHandler.loadRemoteSnapshotInfo({\n moduleInfo: remote,\n id,\n });\n\n assignRemoteInfo(remoteInfo, remoteSnapshot);\n // preloading assets\n const preloadOptions: PreloadOptions[0] = {\n remote,\n preloadConfig: {\n nameOrAlias: pkgNameOrAlias,\n exposes: [expose],\n resourceCategory: 'sync',\n share: false,\n depsRemote: false,\n },\n };\n\n const assets =\n await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit(\n {\n origin,\n preloadOptions,\n remoteInfo,\n remote,\n remoteSnapshot,\n globalSnapshot,\n },\n );\n\n if (assets) {\n preloadAssets(remoteInfo, origin, assets, false);\n }\n\n return {\n ...args,\n remoteSnapshot,\n };\n }\n\n return args;\n },\n };\n}\n"],"mappings":";;;;;;;;AAgBA,SAAgB,iBACd,YACA,gBACM;CACN,MAAM,kBAAkBA,4CAA+B,eAAe;AACtE,KAAI,CAAC,gBAAgB,IACnB,sBAAMC,4CAAaC,+CAAgB,EAAE,YAAY,WAAW,MAAM,CAAC;CAGrE,IAAI,sDAA0B,gBAAgB,gBAAgB,IAAI;AAElE,KAAI,CAACC,4CAAqB,CAAC,SAAS,WAAW,OAAO,CACpD,YAAW,SAAS;AAGtB,YAAW,OAAO,gBAAgB;AAClC,YAAW,kBAAkB,gBAAgB;AAC7C,YAAW,QAAQ;AACnB,YAAW,UAAU,eAAe;AACpC,YAAW,eAAe,eAAe;;AAG3C,SAAgB,iBAAgD;AAC9D,QAAO;EACL,MAAM;EACN,MAAM,aAAa,MAAM;GACvB,MAAM,EAAE,QAAQ,gBAAgB,QAAQ,QAAQ,YAAY,OAAO;AAEnE,OAAI,CAACC,mCAAsB,OAAO,IAAI,CAACC,+BAAkB,OAAO,EAAE;IAChE,MAAM,EAAE,gBAAgB,mBACtB,MAAM,OAAO,gBAAgB,uBAAuB;KAClD,YAAY;KACZ;KACD,CAAC;AAEJ,qBAAiB,YAAY,eAAe;IAE5C,MAAM,iBAAoC;KACxC;KACA,eAAe;MACb,aAAa;MACb,SAAS,CAAC,OAAO;MACjB,kBAAkB;MAClB,OAAO;MACP,YAAY;MACb;KACF;IAED,MAAM,SACJ,MAAM,OAAO,cAAc,MAAM,UAAU,sBAAsB,KAC/D;KACE;KACA;KACA;KACA;KACA;KACA;KACD,CACF;AAEH,QAAI,OACF,+BAAc,YAAY,QAAQ,QAAQ,MAAM;AAGlD,WAAO;KACL,GAAG;KACH;KACD;;AAGH,UAAO;;EAEV"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { error } from "../../utils/logger.js";
|
|
2
|
+
import { getRemoteEntryInfoFromSnapshot, isPureRemoteEntry, isRemoteInfoWithEntry } from "../../utils/tool.js";
|
|
3
|
+
import "../../utils/index.js";
|
|
4
|
+
import { preloadAssets } from "../../utils/preload.js";
|
|
5
|
+
import { getResourceUrl, isBrowserEnvValue } from "@module-federation/sdk";
|
|
6
|
+
import { RUNTIME_011, runtimeDescMap } from "@module-federation/error-codes";
|
|
7
|
+
|
|
8
|
+
//#region src/plugins/snapshot/index.ts
|
|
9
|
+
function assignRemoteInfo(remoteInfo, remoteSnapshot) {
|
|
10
|
+
const remoteEntryInfo = getRemoteEntryInfoFromSnapshot(remoteSnapshot);
|
|
11
|
+
if (!remoteEntryInfo.url) error(RUNTIME_011, runtimeDescMap, { remoteName: remoteInfo.name });
|
|
12
|
+
let entryUrl = getResourceUrl(remoteSnapshot, remoteEntryInfo.url);
|
|
13
|
+
if (!isBrowserEnvValue && !entryUrl.startsWith("http")) entryUrl = `https:${entryUrl}`;
|
|
14
|
+
remoteInfo.type = remoteEntryInfo.type;
|
|
15
|
+
remoteInfo.entryGlobalName = remoteEntryInfo.globalName;
|
|
16
|
+
remoteInfo.entry = entryUrl;
|
|
17
|
+
remoteInfo.version = remoteSnapshot.version;
|
|
18
|
+
remoteInfo.buildVersion = remoteSnapshot.buildVersion;
|
|
19
|
+
}
|
|
20
|
+
function snapshotPlugin() {
|
|
21
|
+
return {
|
|
22
|
+
name: "snapshot-plugin",
|
|
23
|
+
async afterResolve(args) {
|
|
24
|
+
const { remote, pkgNameOrAlias, expose, origin, remoteInfo, id } = args;
|
|
25
|
+
if (!isRemoteInfoWithEntry(remote) || !isPureRemoteEntry(remote)) {
|
|
26
|
+
const { remoteSnapshot, globalSnapshot } = await origin.snapshotHandler.loadRemoteSnapshotInfo({
|
|
27
|
+
moduleInfo: remote,
|
|
28
|
+
id
|
|
29
|
+
});
|
|
30
|
+
assignRemoteInfo(remoteInfo, remoteSnapshot);
|
|
31
|
+
const preloadOptions = {
|
|
32
|
+
remote,
|
|
33
|
+
preloadConfig: {
|
|
34
|
+
nameOrAlias: pkgNameOrAlias,
|
|
35
|
+
exposes: [expose],
|
|
36
|
+
resourceCategory: "sync",
|
|
37
|
+
share: false,
|
|
38
|
+
depsRemote: false
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const assets = await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit({
|
|
42
|
+
origin,
|
|
43
|
+
preloadOptions,
|
|
44
|
+
remoteInfo,
|
|
45
|
+
remote,
|
|
46
|
+
remoteSnapshot,
|
|
47
|
+
globalSnapshot
|
|
48
|
+
});
|
|
49
|
+
if (assets) preloadAssets(remoteInfo, origin, assets, false);
|
|
50
|
+
return {
|
|
51
|
+
...args,
|
|
52
|
+
remoteSnapshot
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return args;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
export { assignRemoteInfo, snapshotPlugin };
|
|
62
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/plugins/snapshot/index.ts"],"sourcesContent":["import {\n ModuleInfo,\n getResourceUrl,\n isBrowserEnvValue,\n} from '@module-federation/sdk';\nimport { ModuleFederationRuntimePlugin } from '../../type/plugin';\nimport { RUNTIME_011, runtimeDescMap } from '@module-federation/error-codes';\nimport {\n error,\n isPureRemoteEntry,\n isRemoteInfoWithEntry,\n getRemoteEntryInfoFromSnapshot,\n} from '../../utils';\nimport { PreloadOptions, RemoteInfo } from '../../type';\nimport { preloadAssets } from '../../utils/preload';\n\nexport function assignRemoteInfo(\n remoteInfo: RemoteInfo,\n remoteSnapshot: ModuleInfo,\n): void {\n const remoteEntryInfo = getRemoteEntryInfoFromSnapshot(remoteSnapshot);\n if (!remoteEntryInfo.url) {\n error(RUNTIME_011, runtimeDescMap, { remoteName: remoteInfo.name });\n }\n\n let entryUrl = getResourceUrl(remoteSnapshot, remoteEntryInfo.url);\n\n if (!isBrowserEnvValue && !entryUrl.startsWith('http')) {\n entryUrl = `https:${entryUrl}`;\n }\n\n remoteInfo.type = remoteEntryInfo.type;\n remoteInfo.entryGlobalName = remoteEntryInfo.globalName;\n remoteInfo.entry = entryUrl;\n remoteInfo.version = remoteSnapshot.version;\n remoteInfo.buildVersion = remoteSnapshot.buildVersion;\n}\n\nexport function snapshotPlugin(): ModuleFederationRuntimePlugin {\n return {\n name: 'snapshot-plugin',\n async afterResolve(args) {\n const { remote, pkgNameOrAlias, expose, origin, remoteInfo, id } = args;\n\n if (!isRemoteInfoWithEntry(remote) || !isPureRemoteEntry(remote)) {\n const { remoteSnapshot, globalSnapshot } =\n await origin.snapshotHandler.loadRemoteSnapshotInfo({\n moduleInfo: remote,\n id,\n });\n\n assignRemoteInfo(remoteInfo, remoteSnapshot);\n // preloading assets\n const preloadOptions: PreloadOptions[0] = {\n remote,\n preloadConfig: {\n nameOrAlias: pkgNameOrAlias,\n exposes: [expose],\n resourceCategory: 'sync',\n share: false,\n depsRemote: false,\n },\n };\n\n const assets =\n await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit(\n {\n origin,\n preloadOptions,\n remoteInfo,\n remote,\n remoteSnapshot,\n globalSnapshot,\n },\n );\n\n if (assets) {\n preloadAssets(remoteInfo, origin, assets, false);\n }\n\n return {\n ...args,\n remoteSnapshot,\n };\n }\n\n return args;\n },\n };\n}\n"],"mappings":";;;;;;;;AAgBA,SAAgB,iBACd,YACA,gBACM;CACN,MAAM,kBAAkB,+BAA+B,eAAe;AACtE,KAAI,CAAC,gBAAgB,IACnB,OAAM,aAAa,gBAAgB,EAAE,YAAY,WAAW,MAAM,CAAC;CAGrE,IAAI,WAAW,eAAe,gBAAgB,gBAAgB,IAAI;AAElE,KAAI,CAAC,qBAAqB,CAAC,SAAS,WAAW,OAAO,CACpD,YAAW,SAAS;AAGtB,YAAW,OAAO,gBAAgB;AAClC,YAAW,kBAAkB,gBAAgB;AAC7C,YAAW,QAAQ;AACnB,YAAW,UAAU,eAAe;AACpC,YAAW,eAAe,eAAe;;AAG3C,SAAgB,iBAAgD;AAC9D,QAAO;EACL,MAAM;EACN,MAAM,aAAa,MAAM;GACvB,MAAM,EAAE,QAAQ,gBAAgB,QAAQ,QAAQ,YAAY,OAAO;AAEnE,OAAI,CAAC,sBAAsB,OAAO,IAAI,CAAC,kBAAkB,OAAO,EAAE;IAChE,MAAM,EAAE,gBAAgB,mBACtB,MAAM,OAAO,gBAAgB,uBAAuB;KAClD,YAAY;KACZ;KACD,CAAC;AAEJ,qBAAiB,YAAY,eAAe;IAE5C,MAAM,iBAAoC;KACxC;KACA,eAAe;MACb,aAAa;MACb,SAAS,CAAC,OAAO;MACjB,kBAAkB;MAClB,OAAO;MACP,YAAY;MACb;KACF;IAED,MAAM,SACJ,MAAM,OAAO,cAAc,MAAM,UAAU,sBAAsB,KAC/D;KACE;KACA;KACA;KACA;KACA;KACA;KACD,CACF;AAEH,QAAI,OACF,eAAc,YAAY,QAAQ,QAAQ,MAAM;AAGlD,WAAO;KACL,GAAG;KACH;KACD;;AAGH,UAAO;;EAEV"}
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
const require_logger = require('../utils/logger.cjs');
|
|
2
|
+
const require_global = require('../global.cjs');
|
|
3
|
+
const require_constant = require('../constant.cjs');
|
|
4
|
+
const require_share = require('../utils/share.cjs');
|
|
5
|
+
const require_manifest = require('../utils/manifest.cjs');
|
|
6
|
+
const require_load = require('../utils/load.cjs');
|
|
7
|
+
const require_context = require('../utils/context.cjs');
|
|
8
|
+
require('../utils/index.cjs');
|
|
9
|
+
const require_preload = require('../utils/preload.cjs');
|
|
10
|
+
const require_index$1 = require('../module/index.cjs');
|
|
11
|
+
const require_syncHook = require('../utils/hooks/syncHook.cjs');
|
|
12
|
+
const require_asyncHook = require('../utils/hooks/asyncHook.cjs');
|
|
13
|
+
const require_syncWaterfallHook = require('../utils/hooks/syncWaterfallHook.cjs');
|
|
14
|
+
const require_asyncWaterfallHooks = require('../utils/hooks/asyncWaterfallHooks.cjs');
|
|
15
|
+
const require_pluginSystem = require('../utils/hooks/pluginSystem.cjs');
|
|
16
|
+
require('../utils/hooks/index.cjs');
|
|
17
|
+
const require_SnapshotHandler = require('../plugins/snapshot/SnapshotHandler.cjs');
|
|
18
|
+
let _module_federation_sdk = require("@module-federation/sdk");
|
|
19
|
+
let _module_federation_error_codes = require("@module-federation/error-codes");
|
|
20
|
+
|
|
21
|
+
//#region src/remote/index.ts
|
|
22
|
+
var RemoteHandler = class {
|
|
23
|
+
constructor(host) {
|
|
24
|
+
this.hooks = new require_pluginSystem.PluginSystem({
|
|
25
|
+
beforeRegisterRemote: new require_syncWaterfallHook.SyncWaterfallHook("beforeRegisterRemote"),
|
|
26
|
+
registerRemote: new require_syncWaterfallHook.SyncWaterfallHook("registerRemote"),
|
|
27
|
+
beforeRequest: new require_asyncWaterfallHooks.AsyncWaterfallHook("beforeRequest"),
|
|
28
|
+
onLoad: new require_asyncHook.AsyncHook("onLoad"),
|
|
29
|
+
handlePreloadModule: new require_syncHook.SyncHook("handlePreloadModule"),
|
|
30
|
+
errorLoadRemote: new require_asyncHook.AsyncHook("errorLoadRemote"),
|
|
31
|
+
beforePreloadRemote: new require_asyncHook.AsyncHook("beforePreloadRemote"),
|
|
32
|
+
generatePreloadAssets: new require_asyncHook.AsyncHook("generatePreloadAssets"),
|
|
33
|
+
afterPreloadRemote: new require_asyncHook.AsyncHook(),
|
|
34
|
+
loadEntry: new require_asyncHook.AsyncHook()
|
|
35
|
+
});
|
|
36
|
+
this.host = host;
|
|
37
|
+
this.idToRemoteMap = {};
|
|
38
|
+
}
|
|
39
|
+
formatAndRegisterRemote(globalOptions, userOptions) {
|
|
40
|
+
return (userOptions.remotes || []).reduce((res, remote) => {
|
|
41
|
+
this.registerRemote(remote, res, { force: false });
|
|
42
|
+
return res;
|
|
43
|
+
}, globalOptions.remotes);
|
|
44
|
+
}
|
|
45
|
+
setIdToRemoteMap(id, remoteMatchInfo) {
|
|
46
|
+
const { remote, expose } = remoteMatchInfo;
|
|
47
|
+
const { name, alias } = remote;
|
|
48
|
+
this.idToRemoteMap[id] = {
|
|
49
|
+
name: remote.name,
|
|
50
|
+
expose
|
|
51
|
+
};
|
|
52
|
+
if (alias && id.startsWith(name)) {
|
|
53
|
+
const idWithAlias = id.replace(name, alias);
|
|
54
|
+
this.idToRemoteMap[idWithAlias] = {
|
|
55
|
+
name: remote.name,
|
|
56
|
+
expose
|
|
57
|
+
};
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (alias && id.startsWith(alias)) {
|
|
61
|
+
const idWithName = id.replace(alias, name);
|
|
62
|
+
this.idToRemoteMap[idWithName] = {
|
|
63
|
+
name: remote.name,
|
|
64
|
+
expose
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async loadRemote(id, options) {
|
|
69
|
+
const { host } = this;
|
|
70
|
+
try {
|
|
71
|
+
const { loadFactory = true } = options || { loadFactory: true };
|
|
72
|
+
const { module, moduleOptions, remoteMatchInfo } = await this.getRemoteModuleAndOptions({ id });
|
|
73
|
+
const { pkgNameOrAlias, remote, expose, id: idRes, remoteSnapshot } = remoteMatchInfo;
|
|
74
|
+
const moduleOrFactory = await module.get(idRes, expose, options, remoteSnapshot);
|
|
75
|
+
const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
|
|
76
|
+
id: idRes,
|
|
77
|
+
pkgNameOrAlias,
|
|
78
|
+
expose,
|
|
79
|
+
exposeModule: loadFactory ? moduleOrFactory : void 0,
|
|
80
|
+
exposeModuleFactory: loadFactory ? void 0 : moduleOrFactory,
|
|
81
|
+
remote,
|
|
82
|
+
options: moduleOptions,
|
|
83
|
+
moduleInstance: module,
|
|
84
|
+
origin: host
|
|
85
|
+
});
|
|
86
|
+
this.setIdToRemoteMap(id, remoteMatchInfo);
|
|
87
|
+
if (typeof moduleWrapper === "function") return moduleWrapper;
|
|
88
|
+
return moduleOrFactory;
|
|
89
|
+
} catch (error) {
|
|
90
|
+
const { from = "runtime" } = options || { from: "runtime" };
|
|
91
|
+
const failOver = await this.hooks.lifecycle.errorLoadRemote.emit({
|
|
92
|
+
id,
|
|
93
|
+
error,
|
|
94
|
+
from,
|
|
95
|
+
lifecycle: "onLoad",
|
|
96
|
+
origin: host
|
|
97
|
+
});
|
|
98
|
+
if (!failOver) throw error;
|
|
99
|
+
return failOver;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async preloadRemote(preloadOptions) {
|
|
103
|
+
const { host } = this;
|
|
104
|
+
await this.hooks.lifecycle.beforePreloadRemote.emit({
|
|
105
|
+
preloadOps: preloadOptions,
|
|
106
|
+
options: host.options,
|
|
107
|
+
origin: host
|
|
108
|
+
});
|
|
109
|
+
const preloadOps = require_preload.formatPreloadArgs(host.options.remotes, preloadOptions);
|
|
110
|
+
await Promise.all(preloadOps.map(async (ops) => {
|
|
111
|
+
const { remote } = ops;
|
|
112
|
+
const remoteInfo = require_load.getRemoteInfo(remote);
|
|
113
|
+
const { globalSnapshot, remoteSnapshot } = await host.snapshotHandler.loadRemoteSnapshotInfo({ moduleInfo: remote });
|
|
114
|
+
const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({
|
|
115
|
+
origin: host,
|
|
116
|
+
preloadOptions: ops,
|
|
117
|
+
remote,
|
|
118
|
+
remoteInfo,
|
|
119
|
+
globalSnapshot,
|
|
120
|
+
remoteSnapshot
|
|
121
|
+
});
|
|
122
|
+
if (!assets) return;
|
|
123
|
+
require_preload.preloadAssets(remoteInfo, host, assets);
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
registerRemotes(remotes, options) {
|
|
127
|
+
const { host } = this;
|
|
128
|
+
remotes.forEach((remote) => {
|
|
129
|
+
this.registerRemote(remote, host.options.remotes, { force: options?.force });
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
async getRemoteModuleAndOptions(options) {
|
|
133
|
+
const { host } = this;
|
|
134
|
+
const { id } = options;
|
|
135
|
+
let loadRemoteArgs;
|
|
136
|
+
try {
|
|
137
|
+
loadRemoteArgs = await this.hooks.lifecycle.beforeRequest.emit({
|
|
138
|
+
id,
|
|
139
|
+
options: host.options,
|
|
140
|
+
origin: host
|
|
141
|
+
});
|
|
142
|
+
} catch (error) {
|
|
143
|
+
loadRemoteArgs = await this.hooks.lifecycle.errorLoadRemote.emit({
|
|
144
|
+
id,
|
|
145
|
+
options: host.options,
|
|
146
|
+
origin: host,
|
|
147
|
+
from: "runtime",
|
|
148
|
+
error,
|
|
149
|
+
lifecycle: "beforeRequest"
|
|
150
|
+
});
|
|
151
|
+
if (!loadRemoteArgs) throw error;
|
|
152
|
+
}
|
|
153
|
+
const { id: idRes } = loadRemoteArgs;
|
|
154
|
+
const remoteSplitInfo = require_manifest.matchRemoteWithNameAndExpose(host.options.remotes, idRes);
|
|
155
|
+
if (!remoteSplitInfo) require_logger.error(_module_federation_error_codes.RUNTIME_004, _module_federation_error_codes.runtimeDescMap, {
|
|
156
|
+
hostName: host.options.name,
|
|
157
|
+
requestId: idRes
|
|
158
|
+
}, void 0, require_context.optionsToMFContext(host.options));
|
|
159
|
+
const { remote: rawRemote } = remoteSplitInfo;
|
|
160
|
+
const remoteInfo = require_load.getRemoteInfo(rawRemote);
|
|
161
|
+
const matchInfo = await host.sharedHandler.hooks.lifecycle.afterResolve.emit({
|
|
162
|
+
id: idRes,
|
|
163
|
+
...remoteSplitInfo,
|
|
164
|
+
options: host.options,
|
|
165
|
+
origin: host,
|
|
166
|
+
remoteInfo
|
|
167
|
+
});
|
|
168
|
+
const { remote, expose } = matchInfo;
|
|
169
|
+
require_logger.assert(remote && expose, `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`);
|
|
170
|
+
let module = host.moduleCache.get(remote.name);
|
|
171
|
+
const moduleOptions = {
|
|
172
|
+
host,
|
|
173
|
+
remoteInfo
|
|
174
|
+
};
|
|
175
|
+
if (!module) {
|
|
176
|
+
module = new require_index$1.Module(moduleOptions);
|
|
177
|
+
host.moduleCache.set(remote.name, module);
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
module,
|
|
181
|
+
moduleOptions,
|
|
182
|
+
remoteMatchInfo: matchInfo
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
registerRemote(remote, targetRemotes, options) {
|
|
186
|
+
const { host } = this;
|
|
187
|
+
const normalizeRemote = () => {
|
|
188
|
+
if (remote.alias) {
|
|
189
|
+
const findEqual = targetRemotes.find((item) => remote.alias && (item.name.startsWith(remote.alias) || item.alias?.startsWith(remote.alias)));
|
|
190
|
+
require_logger.assert(!findEqual, `The alias ${remote.alias} of remote ${remote.name} is not allowed to be the prefix of ${findEqual && findEqual.name} name or alias`);
|
|
191
|
+
}
|
|
192
|
+
if ("entry" in remote) {
|
|
193
|
+
if (_module_federation_sdk.isBrowserEnvValue && typeof window !== "undefined" && !remote.entry.startsWith("http")) remote.entry = new URL(remote.entry, window.location.origin).href;
|
|
194
|
+
}
|
|
195
|
+
if (!remote.shareScope) remote.shareScope = require_constant.DEFAULT_SCOPE;
|
|
196
|
+
if (!remote.type) remote.type = require_constant.DEFAULT_REMOTE_TYPE;
|
|
197
|
+
};
|
|
198
|
+
this.hooks.lifecycle.beforeRegisterRemote.emit({
|
|
199
|
+
remote,
|
|
200
|
+
origin: host
|
|
201
|
+
});
|
|
202
|
+
const registeredRemote = targetRemotes.find((item) => item.name === remote.name);
|
|
203
|
+
if (!registeredRemote) {
|
|
204
|
+
normalizeRemote();
|
|
205
|
+
targetRemotes.push(remote);
|
|
206
|
+
this.hooks.lifecycle.registerRemote.emit({
|
|
207
|
+
remote,
|
|
208
|
+
origin: host
|
|
209
|
+
});
|
|
210
|
+
} else {
|
|
211
|
+
const messages = [`The remote "${remote.name}" is already registered.`, "Please note that overriding it may cause unexpected errors."];
|
|
212
|
+
if (options?.force) {
|
|
213
|
+
this.removeRemote(registeredRemote);
|
|
214
|
+
normalizeRemote();
|
|
215
|
+
targetRemotes.push(remote);
|
|
216
|
+
this.hooks.lifecycle.registerRemote.emit({
|
|
217
|
+
remote,
|
|
218
|
+
origin: host
|
|
219
|
+
});
|
|
220
|
+
(0, _module_federation_sdk.warn)(messages.join(" "));
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
removeRemote(remote) {
|
|
225
|
+
try {
|
|
226
|
+
const { host } = this;
|
|
227
|
+
const { name } = remote;
|
|
228
|
+
const remoteIndex = host.options.remotes.findIndex((item) => item.name === name);
|
|
229
|
+
if (remoteIndex !== -1) host.options.remotes.splice(remoteIndex, 1);
|
|
230
|
+
const loadedModule = host.moduleCache.get(remote.name);
|
|
231
|
+
if (loadedModule) {
|
|
232
|
+
const remoteInfo = loadedModule.remoteInfo;
|
|
233
|
+
const key = remoteInfo.entryGlobalName;
|
|
234
|
+
if (require_global.CurrentGlobal[key]) if (Object.getOwnPropertyDescriptor(require_global.CurrentGlobal, key)?.configurable) delete require_global.CurrentGlobal[key];
|
|
235
|
+
else require_global.CurrentGlobal[key] = void 0;
|
|
236
|
+
const remoteEntryUniqueKey = require_load.getRemoteEntryUniqueKey(loadedModule.remoteInfo);
|
|
237
|
+
if (require_global.globalLoading[remoteEntryUniqueKey]) delete require_global.globalLoading[remoteEntryUniqueKey];
|
|
238
|
+
host.snapshotHandler.manifestCache.delete(remoteInfo.entry);
|
|
239
|
+
let remoteInsId = remoteInfo.buildVersion ? (0, _module_federation_sdk.composeKeyWithSeparator)(remoteInfo.name, remoteInfo.buildVersion) : remoteInfo.name;
|
|
240
|
+
const remoteInsIndex = require_global.CurrentGlobal.__FEDERATION__.__INSTANCES__.findIndex((ins) => {
|
|
241
|
+
if (remoteInfo.buildVersion) return ins.options.id === remoteInsId;
|
|
242
|
+
else return ins.name === remoteInsId;
|
|
243
|
+
});
|
|
244
|
+
if (remoteInsIndex !== -1) {
|
|
245
|
+
const remoteIns = require_global.CurrentGlobal.__FEDERATION__.__INSTANCES__[remoteInsIndex];
|
|
246
|
+
remoteInsId = remoteIns.options.id || remoteInsId;
|
|
247
|
+
const globalShareScopeMap = require_share.getGlobalShareScope();
|
|
248
|
+
let isAllSharedNotUsed = true;
|
|
249
|
+
const needDeleteKeys = [];
|
|
250
|
+
Object.keys(globalShareScopeMap).forEach((instId) => {
|
|
251
|
+
const shareScopeMap = globalShareScopeMap[instId];
|
|
252
|
+
shareScopeMap && Object.keys(shareScopeMap).forEach((shareScope) => {
|
|
253
|
+
const shareScopeVal = shareScopeMap[shareScope];
|
|
254
|
+
shareScopeVal && Object.keys(shareScopeVal).forEach((shareName) => {
|
|
255
|
+
const sharedPkgs = shareScopeVal[shareName];
|
|
256
|
+
sharedPkgs && Object.keys(sharedPkgs).forEach((shareVersion) => {
|
|
257
|
+
const shared = sharedPkgs[shareVersion];
|
|
258
|
+
if (shared && typeof shared === "object" && shared.from === remoteInfo.name) if (shared.loaded || shared.loading) {
|
|
259
|
+
shared.useIn = shared.useIn.filter((usedHostName) => usedHostName !== remoteInfo.name);
|
|
260
|
+
if (shared.useIn.length) isAllSharedNotUsed = false;
|
|
261
|
+
else needDeleteKeys.push([
|
|
262
|
+
instId,
|
|
263
|
+
shareScope,
|
|
264
|
+
shareName,
|
|
265
|
+
shareVersion
|
|
266
|
+
]);
|
|
267
|
+
} else needDeleteKeys.push([
|
|
268
|
+
instId,
|
|
269
|
+
shareScope,
|
|
270
|
+
shareName,
|
|
271
|
+
shareVersion
|
|
272
|
+
]);
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
if (isAllSharedNotUsed) {
|
|
278
|
+
remoteIns.shareScopeMap = {};
|
|
279
|
+
delete globalShareScopeMap[remoteInsId];
|
|
280
|
+
}
|
|
281
|
+
needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion]) => {
|
|
282
|
+
delete globalShareScopeMap[insId]?.[shareScope]?.[shareName]?.[shareVersion];
|
|
283
|
+
});
|
|
284
|
+
require_global.CurrentGlobal.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
|
|
285
|
+
}
|
|
286
|
+
const { hostGlobalSnapshot } = require_SnapshotHandler.getGlobalRemoteInfo(remote, host);
|
|
287
|
+
if (hostGlobalSnapshot) {
|
|
288
|
+
const remoteKey = hostGlobalSnapshot && "remotesInfo" in hostGlobalSnapshot && hostGlobalSnapshot.remotesInfo && require_global.getInfoWithoutType(hostGlobalSnapshot.remotesInfo, remote.name).key;
|
|
289
|
+
if (remoteKey) {
|
|
290
|
+
delete hostGlobalSnapshot.remotesInfo[remoteKey];
|
|
291
|
+
if (Boolean(require_global.Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey])) delete require_global.Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey];
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
host.moduleCache.delete(remote.name);
|
|
295
|
+
}
|
|
296
|
+
} catch (err) {
|
|
297
|
+
require_logger.logger.error(`removeRemote failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
//#endregion
|
|
303
|
+
exports.RemoteHandler = RemoteHandler;
|
|
304
|
+
//# sourceMappingURL=index.cjs.map
|