@module-federation/runtime-core 2.8.2 → 2.9.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.
Files changed (67) hide show
  1. package/dist/core.cjs +4 -8
  2. package/dist/core.cjs.map +1 -1
  3. package/dist/core.d.ts +16 -14
  4. package/dist/core.js +4 -8
  5. package/dist/core.js.map +1 -1
  6. package/dist/global.cjs +17 -2
  7. package/dist/global.cjs.map +1 -1
  8. package/dist/global.d.ts +1 -0
  9. package/dist/global.js +16 -3
  10. package/dist/global.js.map +1 -1
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.ts +2 -2
  13. package/dist/index.js.map +1 -1
  14. package/dist/module/index.cjs +11 -9
  15. package/dist/module/index.cjs.map +1 -1
  16. package/dist/module/index.d.ts +3 -2
  17. package/dist/module/index.js +11 -9
  18. package/dist/module/index.js.map +1 -1
  19. package/dist/plugins/snapshot/SnapshotHandler.cjs +48 -5
  20. package/dist/plugins/snapshot/SnapshotHandler.cjs.map +1 -1
  21. package/dist/plugins/snapshot/SnapshotHandler.d.ts +23 -0
  22. package/dist/plugins/snapshot/SnapshotHandler.js +48 -5
  23. package/dist/plugins/snapshot/SnapshotHandler.js.map +1 -1
  24. package/dist/plugins/snapshot/index.cjs +31 -21
  25. package/dist/plugins/snapshot/index.cjs.map +1 -1
  26. package/dist/plugins/snapshot/index.js +31 -21
  27. package/dist/plugins/snapshot/index.js.map +1 -1
  28. package/dist/remote/index.cjs +1 -1
  29. package/dist/remote/index.cjs.map +1 -1
  30. package/dist/remote/index.d.ts +1 -0
  31. package/dist/remote/index.js +1 -1
  32. package/dist/remote/index.js.map +1 -1
  33. package/dist/shared/index.cjs +86 -27
  34. package/dist/shared/index.cjs.map +1 -1
  35. package/dist/shared/index.d.ts +19 -10
  36. package/dist/shared/index.js +86 -27
  37. package/dist/shared/index.js.map +1 -1
  38. package/dist/type/config.d.ts +9 -1
  39. package/dist/type/index.d.ts +3 -3
  40. package/dist/type/plugin.d.ts +4 -3
  41. package/dist/type/preload.d.ts +7 -1
  42. package/dist/types.d.ts +3 -3
  43. package/dist/utils/hooks/pluginSystem.cjs +6 -5
  44. package/dist/utils/hooks/pluginSystem.cjs.map +1 -1
  45. package/dist/utils/hooks/pluginSystem.d.ts +2 -5
  46. package/dist/utils/hooks/pluginSystem.js +6 -5
  47. package/dist/utils/hooks/pluginSystem.js.map +1 -1
  48. package/dist/utils/load.cjs +48 -14
  49. package/dist/utils/load.cjs.map +1 -1
  50. package/dist/utils/load.js +48 -14
  51. package/dist/utils/load.js.map +1 -1
  52. package/dist/utils/plugin.cjs +30 -7
  53. package/dist/utils/plugin.cjs.map +1 -1
  54. package/dist/utils/plugin.js +30 -7
  55. package/dist/utils/plugin.js.map +1 -1
  56. package/dist/utils/preload.cjs +21 -10
  57. package/dist/utils/preload.cjs.map +1 -1
  58. package/dist/utils/preload.d.ts +1 -1
  59. package/dist/utils/preload.js +21 -10
  60. package/dist/utils/preload.js.map +1 -1
  61. package/dist/utils/share.cjs +3 -2
  62. package/dist/utils/share.cjs.map +1 -1
  63. package/dist/utils/share.d.ts +3 -2
  64. package/dist/utils/share.js +3 -2
  65. package/dist/utils/share.js.map +1 -1
  66. package/package.json +3 -3
  67. package/dist/utils/hooks/index.d.ts +0 -5
@@ -9,14 +9,13 @@ var PluginSystem = class {
9
9
  this.lifecycle = lifecycle;
10
10
  this.lifecycleKeys = Object.keys(lifecycle);
11
11
  }
12
- applyPlugin(plugin, instance) {
12
+ applyPlugin(plugin) {
13
13
  require_logger.assert(require_tool.isPlainObject(plugin), "Plugin configuration is invalid.");
14
14
  const pluginName = plugin.name;
15
15
  require_logger.assert(pluginName, "A name must be provided by the plugin.");
16
16
  if (!this.registerPlugins[pluginName]) {
17
17
  this.registerPlugins[pluginName] = plugin;
18
- plugin.apply?.(instance);
19
- Object.keys(this.lifecycle).forEach((key) => {
18
+ this.lifecycleKeys.forEach((key) => {
20
19
  const pluginLife = plugin[key];
21
20
  if (pluginLife) this.lifecycle[key].on(pluginLife);
22
21
  });
@@ -26,9 +25,11 @@ var PluginSystem = class {
26
25
  require_logger.assert(pluginName, "A name is required.");
27
26
  const plugin = this.registerPlugins[pluginName];
28
27
  require_logger.assert(plugin, `The plugin "${pluginName}" is not registered.`);
29
- Object.keys(plugin).forEach((key) => {
30
- if (key !== "name") this.lifecycle[key].remove(plugin[key]);
28
+ this.lifecycleKeys.forEach((key) => {
29
+ const pluginLife = plugin[key];
30
+ if (pluginLife) this.lifecycle[key].remove(pluginLife);
31
31
  });
32
+ delete this.registerPlugins[pluginName];
32
33
  }
33
34
  };
34
35
 
@@ -1 +1 @@
1
- {"version":3,"file":"pluginSystem.cjs","names":["isPlainObject"],"sources":["../../../src/utils/hooks/pluginSystem.ts"],"sourcesContent":["import type { ModuleFederation } from '../../core';\nimport { assert, isPlainObject } from '../../utils';\n\nexport type Plugin<T extends Record<string, any>> = {\n [k in keyof T]?: Parameters<T[k]['on']>[0];\n} & {\n name: string;\n version?: string;\n apply?: (instance: ModuleFederation) => void;\n};\n\nexport class PluginSystem<T extends Record<string, any>> {\n lifecycle: T;\n lifecycleKeys: Array<keyof T>;\n registerPlugins: Record<string, Plugin<T>> = {};\n\n constructor(lifecycle: T) {\n this.lifecycle = lifecycle;\n this.lifecycleKeys = Object.keys(lifecycle);\n }\n\n applyPlugin(plugin: Plugin<T>, instance: ModuleFederation): void {\n assert(isPlainObject(plugin), 'Plugin configuration is invalid.');\n // The plugin's name is mandatory and must be unique\n const pluginName = plugin.name;\n assert(pluginName, 'A name must be provided by the plugin.');\n\n if (!this.registerPlugins[pluginName]) {\n this.registerPlugins[pluginName] = plugin;\n plugin.apply?.(instance);\n\n Object.keys(this.lifecycle).forEach((key) => {\n const pluginLife = plugin[key as string];\n if (pluginLife) {\n this.lifecycle[key].on(pluginLife);\n }\n });\n }\n }\n\n removePlugin(pluginName: string): void {\n assert(pluginName, 'A name is required.');\n const plugin = this.registerPlugins[pluginName];\n assert(plugin, `The plugin \"${pluginName}\" is not registered.`);\n\n Object.keys(plugin).forEach((key) => {\n if (key !== 'name') {\n this.lifecycle[key].remove(plugin[key as string]);\n }\n });\n }\n}\n"],"mappings":";;;;;AAWA,IAAa,eAAb,MAAyD;CAKvD,YAAY,WAAc;yBAFmB,EAAE;AAG7C,OAAK,YAAY;AACjB,OAAK,gBAAgB,OAAO,KAAK,UAAU;;CAG7C,YAAY,QAAmB,UAAkC;AAC/D,wBAAOA,2BAAc,OAAO,EAAE,mCAAmC;EAEjE,MAAM,aAAa,OAAO;AAC1B,wBAAO,YAAY,yCAAyC;AAE5D,MAAI,CAAC,KAAK,gBAAgB,aAAa;AACrC,QAAK,gBAAgB,cAAc;AACnC,UAAO,QAAQ,SAAS;AAExB,UAAO,KAAK,KAAK,UAAU,CAAC,SAAS,QAAQ;IAC3C,MAAM,aAAa,OAAO;AAC1B,QAAI,WACF,MAAK,UAAU,KAAK,GAAG,WAAW;KAEpC;;;CAIN,aAAa,YAA0B;AACrC,wBAAO,YAAY,sBAAsB;EACzC,MAAM,SAAS,KAAK,gBAAgB;AACpC,wBAAO,QAAQ,eAAe,WAAW,sBAAsB;AAE/D,SAAO,KAAK,OAAO,CAAC,SAAS,QAAQ;AACnC,OAAI,QAAQ,OACV,MAAK,UAAU,KAAK,OAAO,OAAO,KAAe;IAEnD"}
1
+ {"version":3,"file":"pluginSystem.cjs","names":["isPlainObject"],"sources":["../../../src/utils/hooks/pluginSystem.ts"],"sourcesContent":["import { assert, isPlainObject } from '../../utils';\n\nexport type Plugin<T extends Record<string, any>> = {\n [k in keyof T]?: Parameters<T[k]['on']>[0];\n} & {\n name: string;\n version?: string;\n};\n\nexport class PluginSystem<T extends Record<string, any>> {\n lifecycle: T;\n lifecycleKeys: Array<keyof T>;\n registerPlugins: Record<string, Plugin<T>> = {};\n\n constructor(lifecycle: T) {\n this.lifecycle = lifecycle;\n this.lifecycleKeys = Object.keys(lifecycle);\n }\n\n applyPlugin(plugin: Plugin<T>): void {\n assert(isPlainObject(plugin), 'Plugin configuration is invalid.');\n // The plugin's name is mandatory and must be unique\n const pluginName = plugin.name;\n assert(pluginName, 'A name must be provided by the plugin.');\n\n if (!this.registerPlugins[pluginName]) {\n this.registerPlugins[pluginName] = plugin;\n\n this.lifecycleKeys.forEach((key) => {\n const pluginLife = plugin[key];\n if (pluginLife) {\n this.lifecycle[key].on(pluginLife);\n }\n });\n }\n }\n\n removePlugin(pluginName: string): void {\n assert(pluginName, 'A name is required.');\n const plugin = this.registerPlugins[pluginName];\n assert(plugin, `The plugin \"${pluginName}\" is not registered.`);\n\n this.lifecycleKeys.forEach((key) => {\n const pluginLife = plugin[key];\n if (pluginLife) {\n this.lifecycle[key].remove(pluginLife);\n }\n });\n delete this.registerPlugins[pluginName];\n }\n}\n"],"mappings":";;;;;AASA,IAAa,eAAb,MAAyD;CAKvD,YAAY,WAAc;yBAFmB,EAAE;AAG7C,OAAK,YAAY;AACjB,OAAK,gBAAgB,OAAO,KAAK,UAAU;;CAG7C,YAAY,QAAyB;AACnC,wBAAOA,2BAAc,OAAO,EAAE,mCAAmC;EAEjE,MAAM,aAAa,OAAO;AAC1B,wBAAO,YAAY,yCAAyC;AAE5D,MAAI,CAAC,KAAK,gBAAgB,aAAa;AACrC,QAAK,gBAAgB,cAAc;AAEnC,QAAK,cAAc,SAAS,QAAQ;IAClC,MAAM,aAAa,OAAO;AAC1B,QAAI,WACF,MAAK,UAAU,KAAK,GAAG,WAAW;KAEpC;;;CAIN,aAAa,YAA0B;AACrC,wBAAO,YAAY,sBAAsB;EACzC,MAAM,SAAS,KAAK,gBAAgB;AACpC,wBAAO,QAAQ,eAAe,WAAW,sBAAsB;AAE/D,OAAK,cAAc,SAAS,QAAQ;GAClC,MAAM,aAAa,OAAO;AAC1B,OAAI,WACF,MAAK,UAAU,KAAK,OAAO,WAAW;IAExC;AACF,SAAO,KAAK,gBAAgB"}
@@ -1,19 +1,16 @@
1
- import { ModuleFederation } from "../../core.js";
2
-
3
1
  //#region src/utils/hooks/pluginSystem.d.ts
4
2
  type Plugin<T extends Record<string, any>> = { [k in keyof T]?: Parameters<T[k]['on']>[0] } & {
5
3
  name: string;
6
4
  version?: string;
7
- apply?: (instance: ModuleFederation) => void;
8
5
  };
9
6
  declare class PluginSystem<T extends Record<string, any>> {
10
7
  lifecycle: T;
11
8
  lifecycleKeys: Array<keyof T>;
12
9
  registerPlugins: Record<string, Plugin<T>>;
13
10
  constructor(lifecycle: T);
14
- applyPlugin(plugin: Plugin<T>, instance: ModuleFederation): void;
11
+ applyPlugin(plugin: Plugin<T>): void;
15
12
  removePlugin(pluginName: string): void;
16
13
  }
17
14
  //#endregion
18
- export { Plugin, PluginSystem };
15
+ export { PluginSystem };
19
16
  //# sourceMappingURL=pluginSystem.d.ts.map
@@ -9,14 +9,13 @@ var PluginSystem = class {
9
9
  this.lifecycle = lifecycle;
10
10
  this.lifecycleKeys = Object.keys(lifecycle);
11
11
  }
12
- applyPlugin(plugin, instance) {
12
+ applyPlugin(plugin) {
13
13
  assert(isPlainObject(plugin), "Plugin configuration is invalid.");
14
14
  const pluginName = plugin.name;
15
15
  assert(pluginName, "A name must be provided by the plugin.");
16
16
  if (!this.registerPlugins[pluginName]) {
17
17
  this.registerPlugins[pluginName] = plugin;
18
- plugin.apply?.(instance);
19
- Object.keys(this.lifecycle).forEach((key) => {
18
+ this.lifecycleKeys.forEach((key) => {
20
19
  const pluginLife = plugin[key];
21
20
  if (pluginLife) this.lifecycle[key].on(pluginLife);
22
21
  });
@@ -26,9 +25,11 @@ var PluginSystem = class {
26
25
  assert(pluginName, "A name is required.");
27
26
  const plugin = this.registerPlugins[pluginName];
28
27
  assert(plugin, `The plugin "${pluginName}" is not registered.`);
29
- Object.keys(plugin).forEach((key) => {
30
- if (key !== "name") this.lifecycle[key].remove(plugin[key]);
28
+ this.lifecycleKeys.forEach((key) => {
29
+ const pluginLife = plugin[key];
30
+ if (pluginLife) this.lifecycle[key].remove(pluginLife);
31
31
  });
32
+ delete this.registerPlugins[pluginName];
32
33
  }
33
34
  };
34
35
 
@@ -1 +1 @@
1
- {"version":3,"file":"pluginSystem.js","names":[],"sources":["../../../src/utils/hooks/pluginSystem.ts"],"sourcesContent":["import type { ModuleFederation } from '../../core';\nimport { assert, isPlainObject } from '../../utils';\n\nexport type Plugin<T extends Record<string, any>> = {\n [k in keyof T]?: Parameters<T[k]['on']>[0];\n} & {\n name: string;\n version?: string;\n apply?: (instance: ModuleFederation) => void;\n};\n\nexport class PluginSystem<T extends Record<string, any>> {\n lifecycle: T;\n lifecycleKeys: Array<keyof T>;\n registerPlugins: Record<string, Plugin<T>> = {};\n\n constructor(lifecycle: T) {\n this.lifecycle = lifecycle;\n this.lifecycleKeys = Object.keys(lifecycle);\n }\n\n applyPlugin(plugin: Plugin<T>, instance: ModuleFederation): void {\n assert(isPlainObject(plugin), 'Plugin configuration is invalid.');\n // The plugin's name is mandatory and must be unique\n const pluginName = plugin.name;\n assert(pluginName, 'A name must be provided by the plugin.');\n\n if (!this.registerPlugins[pluginName]) {\n this.registerPlugins[pluginName] = plugin;\n plugin.apply?.(instance);\n\n Object.keys(this.lifecycle).forEach((key) => {\n const pluginLife = plugin[key as string];\n if (pluginLife) {\n this.lifecycle[key].on(pluginLife);\n }\n });\n }\n }\n\n removePlugin(pluginName: string): void {\n assert(pluginName, 'A name is required.');\n const plugin = this.registerPlugins[pluginName];\n assert(plugin, `The plugin \"${pluginName}\" is not registered.`);\n\n Object.keys(plugin).forEach((key) => {\n if (key !== 'name') {\n this.lifecycle[key].remove(plugin[key as string]);\n }\n });\n }\n}\n"],"mappings":";;;;;AAWA,IAAa,eAAb,MAAyD;CAKvD,YAAY,WAAc;yBAFmB,EAAE;AAG7C,OAAK,YAAY;AACjB,OAAK,gBAAgB,OAAO,KAAK,UAAU;;CAG7C,YAAY,QAAmB,UAAkC;AAC/D,SAAO,cAAc,OAAO,EAAE,mCAAmC;EAEjE,MAAM,aAAa,OAAO;AAC1B,SAAO,YAAY,yCAAyC;AAE5D,MAAI,CAAC,KAAK,gBAAgB,aAAa;AACrC,QAAK,gBAAgB,cAAc;AACnC,UAAO,QAAQ,SAAS;AAExB,UAAO,KAAK,KAAK,UAAU,CAAC,SAAS,QAAQ;IAC3C,MAAM,aAAa,OAAO;AAC1B,QAAI,WACF,MAAK,UAAU,KAAK,GAAG,WAAW;KAEpC;;;CAIN,aAAa,YAA0B;AACrC,SAAO,YAAY,sBAAsB;EACzC,MAAM,SAAS,KAAK,gBAAgB;AACpC,SAAO,QAAQ,eAAe,WAAW,sBAAsB;AAE/D,SAAO,KAAK,OAAO,CAAC,SAAS,QAAQ;AACnC,OAAI,QAAQ,OACV,MAAK,UAAU,KAAK,OAAO,OAAO,KAAe;IAEnD"}
1
+ {"version":3,"file":"pluginSystem.js","names":[],"sources":["../../../src/utils/hooks/pluginSystem.ts"],"sourcesContent":["import { assert, isPlainObject } from '../../utils';\n\nexport type Plugin<T extends Record<string, any>> = {\n [k in keyof T]?: Parameters<T[k]['on']>[0];\n} & {\n name: string;\n version?: string;\n};\n\nexport class PluginSystem<T extends Record<string, any>> {\n lifecycle: T;\n lifecycleKeys: Array<keyof T>;\n registerPlugins: Record<string, Plugin<T>> = {};\n\n constructor(lifecycle: T) {\n this.lifecycle = lifecycle;\n this.lifecycleKeys = Object.keys(lifecycle);\n }\n\n applyPlugin(plugin: Plugin<T>): void {\n assert(isPlainObject(plugin), 'Plugin configuration is invalid.');\n // The plugin's name is mandatory and must be unique\n const pluginName = plugin.name;\n assert(pluginName, 'A name must be provided by the plugin.');\n\n if (!this.registerPlugins[pluginName]) {\n this.registerPlugins[pluginName] = plugin;\n\n this.lifecycleKeys.forEach((key) => {\n const pluginLife = plugin[key];\n if (pluginLife) {\n this.lifecycle[key].on(pluginLife);\n }\n });\n }\n }\n\n removePlugin(pluginName: string): void {\n assert(pluginName, 'A name is required.');\n const plugin = this.registerPlugins[pluginName];\n assert(plugin, `The plugin \"${pluginName}\" is not registered.`);\n\n this.lifecycleKeys.forEach((key) => {\n const pluginLife = plugin[key];\n if (pluginLife) {\n this.lifecycle[key].remove(pluginLife);\n }\n });\n delete this.registerPlugins[pluginName];\n }\n}\n"],"mappings":";;;;;AASA,IAAa,eAAb,MAAyD;CAKvD,YAAY,WAAc;yBAFmB,EAAE;AAG7C,OAAK,YAAY;AACjB,OAAK,gBAAgB,OAAO,KAAK,UAAU;;CAG7C,YAAY,QAAyB;AACnC,SAAO,cAAc,OAAO,EAAE,mCAAmC;EAEjE,MAAM,aAAa,OAAO;AAC1B,SAAO,YAAY,yCAAyC;AAE5D,MAAI,CAAC,KAAK,gBAAgB,aAAa;AACrC,QAAK,gBAAgB,cAAc;AAEnC,QAAK,cAAc,SAAS,QAAQ;IAClC,MAAM,aAAa,OAAO;AAC1B,QAAI,WACF,MAAK,UAAU,KAAK,GAAG,WAAW;KAEpC;;;CAIN,aAAa,YAA0B;AACrC,SAAO,YAAY,sBAAsB;EACzC,MAAM,SAAS,KAAK,gBAAgB;AACpC,SAAO,QAAQ,eAAe,WAAW,sBAAsB;AAE/D,OAAK,cAAc,SAAS,QAAQ;GAClC,MAAM,aAAa,OAAO;AAC1B,OAAI,WACF,MAAK,UAAU,KAAK,OAAO,WAAW;IAExC;AACF,SAAO,KAAK,gBAAgB"}
@@ -6,6 +6,7 @@ let _module_federation_error_codes = require("@module-federation/error-codes");
6
6
 
7
7
  //#region src/utils/load.ts
8
8
  const importCallback = ".then(callbacks[0]).catch(callbacks[1])";
9
+ const remoteEntryLoadingOrigins = /* @__PURE__ */ new WeakMap();
9
10
  const esmRemoteEntryLoadErrorMessages = [
10
11
  "Failed to fetch dynamically imported module",
11
12
  "Importing a module script failed",
@@ -157,7 +158,16 @@ function getRemoteEntryUniqueKey(remoteInfo) {
157
158
  async function getRemoteEntry(params) {
158
159
  const { origin, remoteEntryExports, remoteInfo, getEntryUrl, resourceContext, _inErrorHandling = false } = params;
159
160
  const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);
160
- if (remoteEntryExports) return remoteEntryExports;
161
+ if (remoteEntryExports) {
162
+ await origin.loaderHook.lifecycle.afterLoadEntry.emit({
163
+ origin,
164
+ remoteInfo,
165
+ remoteEntryExports,
166
+ resourceContext,
167
+ cached: true
168
+ });
169
+ return remoteEntryExports;
170
+ }
161
171
  if (!require_global.globalLoading[uniqueKey]) {
162
172
  const loadEntryHook = origin.remoteHandler.hooks.lifecycle.loadEntry;
163
173
  const loaderHook = origin.loaderHook;
@@ -165,7 +175,8 @@ async function getRemoteEntry(params) {
165
175
  origin,
166
176
  loaderHook,
167
177
  remoteInfo,
168
- remoteEntryExports
178
+ remoteEntryExports,
179
+ resourceContext
169
180
  }).then((res) => {
170
181
  if (res) return res;
171
182
  return (typeof ENV_TARGET !== "undefined" ? ENV_TARGET === "web" : _module_federation_sdk.isBrowserEnvValue) ? loadEntryDom({
@@ -183,20 +194,20 @@ async function getRemoteEntry(params) {
183
194
  await origin.loaderHook.lifecycle.afterLoadEntry.emit({
184
195
  origin,
185
196
  remoteInfo,
186
- remoteEntryExports: res
197
+ remoteEntryExports: res,
198
+ resourceContext
187
199
  });
188
200
  return res;
189
- }).catch(async (err) => {
190
- const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);
191
- const isScriptExecutionError = err instanceof Error && err.message.includes("ScriptExecutionError");
192
- if (err instanceof Error && err.message.includes(_module_federation_error_codes.RUNTIME_008) && !isScriptExecutionError && !_inErrorHandling) {
201
+ }).catch(async (loadError) => {
202
+ const isScriptExecutionError = loadError instanceof Error && loadError.message.includes("ScriptExecutionError");
203
+ if (loadError instanceof Error && loadError.message.includes(_module_federation_error_codes.RUNTIME_008) && !isScriptExecutionError && !_inErrorHandling) {
193
204
  const wrappedGetRemoteEntry = (params) => {
194
205
  return getRemoteEntry({
195
206
  ...params,
196
207
  _inErrorHandling: true
197
208
  });
198
209
  };
199
- const RemoteEntryExports = await origin.loaderHook.lifecycle.loadEntryError.emit({
210
+ const recoveredRemoteEntryExports = await origin.loaderHook.lifecycle.loadEntryError.emit({
200
211
  getRemoteEntry: wrappedGetRemoteEntry,
201
212
  origin,
202
213
  remoteInfo,
@@ -204,25 +215,48 @@ async function getRemoteEntry(params) {
204
215
  globalLoading: require_global.globalLoading,
205
216
  uniqueKey
206
217
  });
207
- if (RemoteEntryExports) {
218
+ if (recoveredRemoteEntryExports) {
208
219
  await origin.loaderHook.lifecycle.afterLoadEntry.emit({
209
220
  origin,
210
221
  remoteInfo,
211
- remoteEntryExports: RemoteEntryExports,
222
+ remoteEntryExports: recoveredRemoteEntryExports,
223
+ resourceContext,
224
+ error: loadError,
212
225
  recovered: true
213
226
  });
214
- return RemoteEntryExports;
227
+ return recoveredRemoteEntryExports;
215
228
  }
216
229
  }
217
230
  await origin.loaderHook.lifecycle.afterLoadEntry.emit({
218
231
  origin,
219
232
  remoteInfo,
220
- error: err
233
+ resourceContext,
234
+ error: loadError
221
235
  });
222
- throw err;
236
+ throw loadError;
237
+ });
238
+ remoteEntryLoadingOrigins.set(require_global.globalLoading[uniqueKey], origin);
239
+ }
240
+ const remoteEntryLoading = require_global.globalLoading[uniqueKey];
241
+ if (remoteEntryLoadingOrigins.get(remoteEntryLoading) !== origin) try {
242
+ const result = await remoteEntryLoading;
243
+ await origin.loaderHook.lifecycle.afterLoadEntry.emit({
244
+ origin,
245
+ remoteInfo,
246
+ remoteEntryExports: result,
247
+ resourceContext
248
+ });
249
+ return result;
250
+ } catch (loadError) {
251
+ await origin.loaderHook.lifecycle.afterLoadEntry.emit({
252
+ origin,
253
+ remoteInfo,
254
+ resourceContext,
255
+ error: loadError
223
256
  });
257
+ throw loadError;
224
258
  }
225
- return require_global.globalLoading[uniqueKey];
259
+ return remoteEntryLoading;
226
260
  }
227
261
  function getRemoteInfo(remote) {
228
262
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"load.cjs","names":["RUNTIME_008","runtimeDescMap","getRemoteEntryExports","RUNTIME_001","globalLoading","isBrowserEnvValue","DEFAULT_REMOTE_TYPE","DEFAULT_SCOPE"],"sources":["../../src/utils/load.ts"],"sourcesContent":["import {\n loadScript,\n loadScriptNode,\n composeKeyWithSeparator,\n isBrowserEnvValue,\n} from '@module-federation/sdk';\nimport { DEFAULT_REMOTE_TYPE, DEFAULT_SCOPE } from '../constant';\nimport { ModuleFederation } from '../core';\nimport { globalLoading, getRemoteEntryExports } from '../global';\nimport {\n Remote,\n RemoteEntryExports,\n RemoteInfo,\n ResourceLoadContext,\n} from '../type';\nimport { assert, error } from './logger';\nimport {\n RUNTIME_001,\n RUNTIME_008,\n runtimeDescMap,\n} from '@module-federation/error-codes';\n\n// Declare the ENV_TARGET constant that will be defined by DefinePlugin\ndeclare const ENV_TARGET: 'web' | 'node';\nconst importCallback = '.then(callbacks[0]).catch(callbacks[1])';\n\nconst esmRemoteEntryLoadErrorMessages = [\n 'Failed to fetch dynamically imported module',\n 'Importing a module script failed',\n 'error loading dynamically imported module',\n];\n\nfunction isEsmRemoteEntryLoadError(err: unknown): boolean {\n if (!(err instanceof TypeError)) {\n return false;\n }\n\n return esmRemoteEntryLoadErrorMessages.some((loadErrorMessage) =>\n err.message.includes(loadErrorMessage),\n );\n}\n\nexport function isEsmRemoteType(type: RemoteInfo['type']): boolean {\n return type === 'esm' || type === 'module';\n}\n\nasync function loadEsmEntry({\n entry,\n remoteEntryExports,\n name,\n getEntryUrl,\n}: {\n entry: string;\n remoteEntryExports: RemoteEntryExports | undefined;\n name: string;\n getEntryUrl?: (url: string) => string;\n}): Promise<RemoteEntryExports> {\n return new Promise<RemoteEntryExports>((resolve, reject) => {\n const rejectEntry = (loadError: unknown) => {\n if (isEsmRemoteEntryLoadError(loadError)) {\n const originalMsg =\n loadError instanceof Error ? loadError.message : String(loadError);\n try {\n error(\n RUNTIME_008,\n runtimeDescMap,\n {\n remoteName: name,\n resourceUrl: url,\n },\n originalMsg,\n );\n } catch (runtimeError) {\n reject(runtimeError);\n return;\n }\n }\n\n reject(loadError);\n };\n\n const url = getEntryUrl ? getEntryUrl(entry) : entry;\n try {\n if (!remoteEntryExports) {\n if (typeof FEDERATION_ALLOW_NEW_FUNCTION !== 'undefined') {\n new Function('callbacks', `import(\"${url}\")${importCallback}`)([\n resolve,\n rejectEntry,\n ]);\n } else {\n import(/* webpackIgnore: true */ /* @vite-ignore */ url)\n .then(resolve)\n .catch(rejectEntry);\n }\n } else {\n resolve(remoteEntryExports);\n }\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n error(`Failed to load ESM entry from \"${url}\". ${msg}`);\n }\n });\n}\n\nasync function loadSystemJsEntry({\n entry,\n remoteEntryExports,\n}: {\n entry: string;\n remoteEntryExports: RemoteEntryExports | undefined;\n}): Promise<RemoteEntryExports> {\n return new Promise<RemoteEntryExports>((resolve, reject) => {\n try {\n if (!remoteEntryExports) {\n //@ts-ignore\n if (typeof __system_context__ === 'undefined') {\n //@ts-ignore\n System.import(entry).then(resolve).catch(reject);\n } else {\n new Function(\n 'callbacks',\n `System.import(\"${entry}\")${importCallback}`,\n )([resolve, reject]);\n }\n } else {\n resolve(remoteEntryExports);\n }\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n error(`Failed to load SystemJS entry from \"${entry}\". ${msg}`);\n }\n });\n}\n\nfunction handleRemoteEntryLoaded(\n name: string,\n globalName: string,\n entry: string,\n): RemoteEntryExports {\n const { remoteEntryKey, entryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (!entryExports) {\n error(RUNTIME_001, runtimeDescMap, {\n remoteName: name,\n remoteEntryUrl: entry,\n remoteEntryKey,\n });\n }\n\n return entryExports;\n}\n\nasync function loadEntryScript({\n name,\n globalName,\n entry,\n remoteInfo,\n loaderHook,\n getEntryUrl,\n resourceContext,\n}: {\n name: string;\n globalName: string;\n entry: string;\n remoteInfo: RemoteInfo;\n loaderHook: ModuleFederation['loaderHook'];\n getEntryUrl?: (url: string) => string;\n resourceContext?: ResourceLoadContext;\n}): Promise<RemoteEntryExports> {\n const { entryExports: remoteEntryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n // if getEntryUrl is passed, use the getEntryUrl to get the entry url\n const url = getEntryUrl ? getEntryUrl(entry) : entry;\n return loadScript(url, {\n attrs: {},\n createScriptHook: (url, attrs) => {\n const res = loaderHook.lifecycle.createScript.emit({\n url,\n attrs,\n remoteInfo,\n resourceContext: resourceContext\n ? {\n ...resourceContext,\n url,\n }\n : undefined,\n });\n\n if (!res) return;\n\n if (res instanceof HTMLScriptElement) {\n return res;\n }\n\n if ('script' in res || 'timeout' in res) {\n return res;\n }\n\n return;\n },\n }).then(\n () => {\n // loadScript resolved: script was fetched, executed without throwing, and\n // did not trigger a ScriptExecutionError listener. Now verify the global was registered.\n return handleRemoteEntryLoaded(name, globalName, entry);\n },\n (loadError: unknown) => {\n // loadScript rejected — one of three causes, all with descriptive messages:\n // ScriptNetworkError — URL unreachable, 404, CORS, etc.\n // ScriptExecutionError — script fetched OK but IIFE threw during execution\n // timeout — script took too long to load\n // Errors thrown inside handleRemoteEntryLoaded above are NOT caught here.\n const originalMsg =\n loadError instanceof Error ? loadError.message : String(loadError);\n error(\n RUNTIME_008,\n runtimeDescMap,\n {\n remoteName: name,\n resourceUrl: url,\n },\n originalMsg,\n );\n },\n );\n}\nasync function loadEntryDom({\n remoteInfo,\n remoteEntryExports,\n loaderHook,\n getEntryUrl,\n resourceContext,\n}: {\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports;\n loaderHook: ModuleFederation['loaderHook'];\n getEntryUrl?: (url: string) => string;\n resourceContext?: ResourceLoadContext;\n}) {\n const { entry, entryGlobalName: globalName, name, type } = remoteInfo;\n if (isEsmRemoteType(type)) {\n return loadEsmEntry({ entry, remoteEntryExports, name, getEntryUrl });\n }\n\n if (type === 'system') {\n return loadSystemJsEntry({ entry, remoteEntryExports });\n }\n\n return loadEntryScript({\n entry,\n globalName,\n name,\n remoteInfo,\n loaderHook,\n getEntryUrl,\n resourceContext,\n });\n}\n\nasync function loadEntryNode({\n remoteInfo,\n loaderHook,\n resourceContext,\n}: {\n remoteInfo: RemoteInfo;\n loaderHook: ModuleFederation['loaderHook'];\n resourceContext?: ResourceLoadContext;\n}) {\n const { entry, entryGlobalName: globalName, name, type } = remoteInfo;\n const { entryExports: remoteEntryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n return loadScriptNode(entry, {\n attrs: { name, globalName, type },\n loaderHook: {\n createScriptHook: (url: string, attrs: Record<string, any> = {}) => {\n const res = loaderHook.lifecycle.createScript.emit({\n url,\n attrs,\n remoteInfo,\n resourceContext: resourceContext\n ? {\n ...resourceContext,\n url,\n }\n : undefined,\n });\n\n if (!res) return;\n\n if ('url' in res) {\n return res;\n }\n\n return;\n },\n },\n })\n .then(() => {\n return handleRemoteEntryLoaded(name, globalName, entry);\n })\n .catch((e) => {\n const msg = e instanceof Error ? e.message : String(e);\n error(\n `Failed to load Node.js entry for remote \"${name}\" from \"${entry}\". ${msg}`,\n );\n });\n}\n\nexport function getRemoteEntryUniqueKey(remoteInfo: RemoteInfo): string {\n const { entry, name } = remoteInfo;\n return composeKeyWithSeparator(name, entry);\n}\n\nexport async function getRemoteEntry(params: {\n origin: ModuleFederation;\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports | undefined;\n getEntryUrl?: (url: string) => string;\n _inErrorHandling?: boolean; // Add flag to prevent recursion\n resourceContext?: ResourceLoadContext;\n}): Promise<RemoteEntryExports | false | void> {\n const {\n origin,\n remoteEntryExports,\n remoteInfo,\n getEntryUrl,\n resourceContext,\n _inErrorHandling = false,\n } = params;\n const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n if (!globalLoading[uniqueKey]) {\n const loadEntryHook = origin.remoteHandler.hooks.lifecycle.loadEntry;\n const loaderHook = origin.loaderHook;\n\n globalLoading[uniqueKey] = loadEntryHook\n .emit({\n origin,\n loaderHook,\n remoteInfo,\n remoteEntryExports,\n })\n .then((res) => {\n if (res) {\n return res;\n }\n // Use ENV_TARGET if defined, otherwise fallback to isBrowserEnvValue\n const isWebEnvironment =\n typeof ENV_TARGET !== 'undefined'\n ? ENV_TARGET === 'web'\n : isBrowserEnvValue;\n\n return isWebEnvironment\n ? loadEntryDom({\n remoteInfo,\n remoteEntryExports,\n loaderHook,\n getEntryUrl,\n resourceContext,\n })\n : loadEntryNode({ remoteInfo, loaderHook, resourceContext });\n })\n .then(async (res) => {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports: res,\n });\n return res;\n })\n .catch(async (err) => {\n const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);\n // ScriptExecutionError means the script downloaded fine but its IIFE\n // threw at runtime — retrying would reproduce the same error, so exclude it.\n const isScriptExecutionError =\n err instanceof Error && err.message.includes('ScriptExecutionError');\n const isScriptLoadError =\n err instanceof Error &&\n err.message.includes(RUNTIME_008) &&\n !isScriptExecutionError;\n\n if (isScriptLoadError && !_inErrorHandling) {\n const wrappedGetRemoteEntry = (\n params: Parameters<typeof getRemoteEntry>[0],\n ) => {\n return getRemoteEntry({ ...params, _inErrorHandling: true });\n };\n\n const RemoteEntryExports =\n await origin.loaderHook.lifecycle.loadEntryError.emit({\n getRemoteEntry: wrappedGetRemoteEntry,\n origin,\n remoteInfo: remoteInfo,\n remoteEntryExports,\n globalLoading,\n uniqueKey,\n });\n\n if (RemoteEntryExports) {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports: RemoteEntryExports,\n recovered: true,\n });\n return RemoteEntryExports;\n }\n }\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n error: err,\n });\n throw err;\n });\n }\n\n return globalLoading[uniqueKey];\n}\n\nexport function getRemoteInfo(remote: Remote): RemoteInfo {\n return {\n ...remote,\n entry: 'entry' in remote ? remote.entry : '',\n type: remote.type || DEFAULT_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AAO;AAQT,WAJE,OAAO,eAAe,cAClB,eAAe,QACfC,4CAGF,aAAa;IACX;IACA;IACA;IACA;IACA;IACD,CAAC,GACF,cAAc;IAAE;IAAY;IAAY;IAAiB,CAAC;IAC9D,CACD,KAAK,OAAO,QAAQ;AACnB,SAAM,OAAO,WAAW,UAAU,eAAe,KAAK;IACpD;IACA;IACA,oBAAoB;IACrB,CAAC;AACF,UAAO;IACP,CACD,MAAM,OAAO,QAAQ;GACpB,MAAM,YAAY,wBAAwB,WAAW;GAGrD,MAAM,yBACJ,eAAe,SAAS,IAAI,QAAQ,SAAS,uBAAuB;AAMtE,OAJE,eAAe,SACf,IAAI,QAAQ,SAASL,2CAAY,IACjC,CAAC,0BAEsB,CAAC,kBAAkB;IAC1C,MAAM,yBACJ,WACG;AACH,YAAO,eAAe;MAAE,GAAG;MAAQ,kBAAkB;MAAM,CAAC;;IAG9D,MAAM,qBACJ,MAAM,OAAO,WAAW,UAAU,eAAe,KAAK;KACpD,gBAAgB;KAChB;KACY;KACZ;KACA;KACA;KACD,CAAC;AAEJ,QAAI,oBAAoB;AACtB,WAAM,OAAO,WAAW,UAAU,eAAe,KAAK;MACpD;MACA;MACA,oBAAoB;MACpB,WAAW;MACZ,CAAC;AACF,YAAO;;;AAGX,SAAM,OAAO,WAAW,UAAU,eAAe,KAAK;IACpD;IACA;IACA,OAAO;IACR,CAAC;AACF,SAAM;IACN;;AAGN,QAAOI,6BAAc;;AAGvB,SAAgB,cAAc,QAA4B;AACxD,QAAO;EACL,GAAG;EACH,OAAO,WAAW,SAAS,OAAO,QAAQ;EAC1C,MAAM,OAAO,QAAQE;EACrB,iBAAiB,OAAO,mBAAmB,OAAO;EAClD,YAAY,OAAO,cAAcC;EAClC"}
1
+ {"version":3,"file":"load.cjs","names":["RUNTIME_008","runtimeDescMap","getRemoteEntryExports","RUNTIME_001","globalLoading","isBrowserEnvValue","DEFAULT_REMOTE_TYPE","DEFAULT_SCOPE"],"sources":["../../src/utils/load.ts"],"sourcesContent":["import {\n loadScript,\n loadScriptNode,\n composeKeyWithSeparator,\n isBrowserEnvValue,\n} from '@module-federation/sdk';\nimport { DEFAULT_REMOTE_TYPE, DEFAULT_SCOPE } from '../constant';\nimport { ModuleFederation } from '../core';\nimport { globalLoading, getRemoteEntryExports } from '../global';\nimport {\n Remote,\n RemoteEntryExports,\n RemoteInfo,\n ResourceLoadContext,\n} from '../type';\nimport { assert, error } from './logger';\nimport {\n RUNTIME_001,\n RUNTIME_008,\n runtimeDescMap,\n} from '@module-federation/error-codes';\n\n// Declare the ENV_TARGET constant that will be defined by DefinePlugin\ndeclare const ENV_TARGET: 'web' | 'node';\nconst importCallback = '.then(callbacks[0]).catch(callbacks[1])';\nconst remoteEntryLoadingOrigins = new WeakMap<\n Promise<RemoteEntryExports | void>,\n ModuleFederation\n>();\n\nconst esmRemoteEntryLoadErrorMessages = [\n 'Failed to fetch dynamically imported module',\n 'Importing a module script failed',\n 'error loading dynamically imported module',\n];\n\nfunction isEsmRemoteEntryLoadError(err: unknown): boolean {\n if (!(err instanceof TypeError)) {\n return false;\n }\n\n return esmRemoteEntryLoadErrorMessages.some((loadErrorMessage) =>\n err.message.includes(loadErrorMessage),\n );\n}\n\nexport function isEsmRemoteType(type: RemoteInfo['type']): boolean {\n return type === 'esm' || type === 'module';\n}\n\nasync function loadEsmEntry({\n entry,\n remoteEntryExports,\n name,\n getEntryUrl,\n}: {\n entry: string;\n remoteEntryExports: RemoteEntryExports | undefined;\n name: string;\n getEntryUrl?: (url: string) => string;\n}): Promise<RemoteEntryExports> {\n return new Promise<RemoteEntryExports>((resolve, reject) => {\n const rejectEntry = (loadError: unknown) => {\n if (isEsmRemoteEntryLoadError(loadError)) {\n const originalMsg =\n loadError instanceof Error ? loadError.message : String(loadError);\n try {\n error(\n RUNTIME_008,\n runtimeDescMap,\n {\n remoteName: name,\n resourceUrl: url,\n },\n originalMsg,\n );\n } catch (runtimeError) {\n reject(runtimeError);\n return;\n }\n }\n\n reject(loadError);\n };\n\n const url = getEntryUrl ? getEntryUrl(entry) : entry;\n try {\n if (!remoteEntryExports) {\n if (typeof FEDERATION_ALLOW_NEW_FUNCTION !== 'undefined') {\n new Function('callbacks', `import(\"${url}\")${importCallback}`)([\n resolve,\n rejectEntry,\n ]);\n } else {\n import(/* webpackIgnore: true */ /* @vite-ignore */ url)\n .then(resolve)\n .catch(rejectEntry);\n }\n } else {\n resolve(remoteEntryExports);\n }\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n error(`Failed to load ESM entry from \"${url}\". ${msg}`);\n }\n });\n}\n\nasync function loadSystemJsEntry({\n entry,\n remoteEntryExports,\n}: {\n entry: string;\n remoteEntryExports: RemoteEntryExports | undefined;\n}): Promise<RemoteEntryExports> {\n return new Promise<RemoteEntryExports>((resolve, reject) => {\n try {\n if (!remoteEntryExports) {\n //@ts-ignore\n if (typeof __system_context__ === 'undefined') {\n //@ts-ignore\n System.import(entry).then(resolve).catch(reject);\n } else {\n new Function(\n 'callbacks',\n `System.import(\"${entry}\")${importCallback}`,\n )([resolve, reject]);\n }\n } else {\n resolve(remoteEntryExports);\n }\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n error(`Failed to load SystemJS entry from \"${entry}\". ${msg}`);\n }\n });\n}\n\nfunction handleRemoteEntryLoaded(\n name: string,\n globalName: string,\n entry: string,\n): RemoteEntryExports {\n const { remoteEntryKey, entryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (!entryExports) {\n error(RUNTIME_001, runtimeDescMap, {\n remoteName: name,\n remoteEntryUrl: entry,\n remoteEntryKey,\n });\n }\n\n return entryExports;\n}\n\nasync function loadEntryScript({\n name,\n globalName,\n entry,\n remoteInfo,\n loaderHook,\n getEntryUrl,\n resourceContext,\n}: {\n name: string;\n globalName: string;\n entry: string;\n remoteInfo: RemoteInfo;\n loaderHook: ModuleFederation['loaderHook'];\n getEntryUrl?: (url: string) => string;\n resourceContext?: ResourceLoadContext;\n}): Promise<RemoteEntryExports> {\n const { entryExports: remoteEntryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n // if getEntryUrl is passed, use the getEntryUrl to get the entry url\n const url = getEntryUrl ? getEntryUrl(entry) : entry;\n return loadScript(url, {\n attrs: {},\n createScriptHook: (url, attrs) => {\n const res = loaderHook.lifecycle.createScript.emit({\n url,\n attrs,\n remoteInfo,\n resourceContext: resourceContext\n ? {\n ...resourceContext,\n url,\n }\n : undefined,\n });\n\n if (!res) return;\n\n if (res instanceof HTMLScriptElement) {\n return res;\n }\n\n if ('script' in res || 'timeout' in res) {\n return res;\n }\n\n return;\n },\n }).then(\n () => {\n // loadScript resolved: script was fetched, executed without throwing, and\n // did not trigger a ScriptExecutionError listener. Now verify the global was registered.\n return handleRemoteEntryLoaded(name, globalName, entry);\n },\n (loadError: unknown) => {\n // loadScript rejected — one of three causes, all with descriptive messages:\n // ScriptNetworkError — URL unreachable, 404, CORS, etc.\n // ScriptExecutionError — script fetched OK but IIFE threw during execution\n // timeout — script took too long to load\n // Errors thrown inside handleRemoteEntryLoaded above are NOT caught here.\n const originalMsg =\n loadError instanceof Error ? loadError.message : String(loadError);\n error(\n RUNTIME_008,\n runtimeDescMap,\n {\n remoteName: name,\n resourceUrl: url,\n },\n originalMsg,\n );\n },\n );\n}\nasync function loadEntryDom({\n remoteInfo,\n remoteEntryExports,\n loaderHook,\n getEntryUrl,\n resourceContext,\n}: {\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports;\n loaderHook: ModuleFederation['loaderHook'];\n getEntryUrl?: (url: string) => string;\n resourceContext?: ResourceLoadContext;\n}) {\n const { entry, entryGlobalName: globalName, name, type } = remoteInfo;\n if (isEsmRemoteType(type)) {\n return loadEsmEntry({ entry, remoteEntryExports, name, getEntryUrl });\n }\n\n if (type === 'system') {\n return loadSystemJsEntry({ entry, remoteEntryExports });\n }\n\n return loadEntryScript({\n entry,\n globalName,\n name,\n remoteInfo,\n loaderHook,\n getEntryUrl,\n resourceContext,\n });\n}\n\nasync function loadEntryNode({\n remoteInfo,\n loaderHook,\n resourceContext,\n}: {\n remoteInfo: RemoteInfo;\n loaderHook: ModuleFederation['loaderHook'];\n resourceContext?: ResourceLoadContext;\n}) {\n const { entry, entryGlobalName: globalName, name, type } = remoteInfo;\n const { entryExports: remoteEntryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n return loadScriptNode(entry, {\n attrs: { name, globalName, type },\n loaderHook: {\n createScriptHook: (url: string, attrs: Record<string, any> = {}) => {\n const res = loaderHook.lifecycle.createScript.emit({\n url,\n attrs,\n remoteInfo,\n resourceContext: resourceContext\n ? {\n ...resourceContext,\n url,\n }\n : undefined,\n });\n\n if (!res) return;\n\n if ('url' in res) {\n return res;\n }\n\n return;\n },\n },\n })\n .then(() => {\n return handleRemoteEntryLoaded(name, globalName, entry);\n })\n .catch((e) => {\n const msg = e instanceof Error ? e.message : String(e);\n error(\n `Failed to load Node.js entry for remote \"${name}\" from \"${entry}\". ${msg}`,\n );\n });\n}\n\nexport function getRemoteEntryUniqueKey(remoteInfo: RemoteInfo): string {\n const { entry, name } = remoteInfo;\n return composeKeyWithSeparator(name, entry);\n}\n\nexport async function getRemoteEntry(params: {\n origin: ModuleFederation;\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports | undefined;\n getEntryUrl?: (url: string) => string;\n _inErrorHandling?: boolean; // Add flag to prevent recursion\n resourceContext?: ResourceLoadContext;\n}): Promise<RemoteEntryExports | false | void> {\n const {\n origin,\n remoteEntryExports,\n remoteInfo,\n getEntryUrl,\n resourceContext,\n _inErrorHandling = false,\n } = params;\n const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);\n\n if (remoteEntryExports) {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports,\n resourceContext,\n cached: true,\n });\n return remoteEntryExports;\n }\n\n if (!globalLoading[uniqueKey]) {\n const loadEntryHook = origin.remoteHandler.hooks.lifecycle.loadEntry;\n const loaderHook = origin.loaderHook;\n globalLoading[uniqueKey] = loadEntryHook\n .emit({\n origin,\n loaderHook,\n remoteInfo,\n remoteEntryExports,\n resourceContext,\n })\n .then((res) => {\n if (res) {\n return res;\n }\n const isWebEnvironment =\n typeof ENV_TARGET !== 'undefined'\n ? ENV_TARGET === 'web'\n : isBrowserEnvValue;\n\n return isWebEnvironment\n ? loadEntryDom({\n remoteInfo,\n remoteEntryExports,\n loaderHook,\n getEntryUrl,\n resourceContext,\n })\n : loadEntryNode({ remoteInfo, loaderHook, resourceContext });\n })\n .then(async (res) => {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports: res,\n resourceContext,\n });\n return res;\n })\n .catch(async (loadError) => {\n const isScriptExecutionError =\n loadError instanceof Error &&\n loadError.message.includes('ScriptExecutionError');\n const isScriptLoadError =\n loadError instanceof Error &&\n loadError.message.includes(RUNTIME_008) &&\n !isScriptExecutionError;\n\n if (isScriptLoadError && !_inErrorHandling) {\n const wrappedGetRemoteEntry = (\n params: Parameters<typeof getRemoteEntry>[0],\n ) => {\n return getRemoteEntry({ ...params, _inErrorHandling: true });\n };\n\n const recoveredRemoteEntryExports =\n await origin.loaderHook.lifecycle.loadEntryError.emit({\n getRemoteEntry: wrappedGetRemoteEntry,\n origin,\n remoteInfo,\n remoteEntryExports,\n globalLoading,\n uniqueKey,\n });\n\n if (recoveredRemoteEntryExports) {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports: recoveredRemoteEntryExports,\n resourceContext,\n error: loadError,\n recovered: true,\n });\n return recoveredRemoteEntryExports;\n }\n }\n\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n resourceContext,\n error: loadError,\n });\n throw loadError;\n });\n remoteEntryLoadingOrigins.set(globalLoading[uniqueKey], origin);\n }\n\n const remoteEntryLoading = globalLoading[uniqueKey];\n if (remoteEntryLoadingOrigins.get(remoteEntryLoading) !== origin) {\n try {\n const result = await remoteEntryLoading;\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports: result,\n resourceContext,\n });\n return result;\n } catch (loadError) {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n resourceContext,\n error: loadError,\n });\n throw loadError;\n }\n }\n\n return remoteEntryLoading;\n}\n\nexport function getRemoteInfo(remote: Remote): RemoteInfo {\n return {\n ...remote,\n entry: 'entry' in remote ? remote.entry : '',\n type: remote.type || DEFAULT_REMOTE_TYPE,\n entryGlobalName: remote.entryGlobalName || remote.name,\n shareScope: remote.shareScope || DEFAULT_SCOPE,\n };\n}\n"],"mappings":";;;;;;;AAwBA,MAAM,iBAAiB;AACvB,MAAM,4CAA4B,IAAI,SAGnC;AAEH,MAAM,kCAAkC;CACtC;CACA;CACA;CACD;AAED,SAAS,0BAA0B,KAAuB;AACxD,KAAI,EAAE,eAAe,WACnB,QAAO;AAGT,QAAO,gCAAgC,MAAM,qBAC3C,IAAI,QAAQ,SAAS,iBAAiB,CACvC;;AAGH,SAAgB,gBAAgB,MAAmC;AACjE,QAAO,SAAS,SAAS,SAAS;;AAGpC,eAAe,aAAa,EAC1B,OACA,oBACA,MACA,eAM8B;AAC9B,QAAO,IAAI,SAA6B,SAAS,WAAW;EAC1D,MAAM,eAAe,cAAuB;AAC1C,OAAI,0BAA0B,UAAU,EAAE;IACxC,MAAM,cACJ,qBAAqB,QAAQ,UAAU,UAAU,OAAO,UAAU;AACpE,QAAI;AACF,0BACEA,4CACAC,+CACA;MACE,YAAY;MACZ,aAAa;MACd,EACD,YACD;aACM,cAAc;AACrB,YAAO,aAAa;AACpB;;;AAIJ,UAAO,UAAU;;EAGnB,MAAM,MAAM,cAAc,YAAY,MAAM,GAAG;AAC/C,MAAI;AACF,OAAI,CAAC,mBACH,KAA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,eAAe,KAAK;MACpD;MACA;MACA,oBAAoB;MACpB;MACA,OAAO;MACP,WAAW;MACZ,CAAC;AACF,YAAO;;;AAIX,SAAM,OAAO,WAAW,UAAU,eAAe,KAAK;IACpD;IACA;IACA;IACA,OAAO;IACR,CAAC;AACF,SAAM;IACN;AACJ,4BAA0B,IAAII,6BAAc,YAAY,OAAO;;CAGjE,MAAM,qBAAqBA,6BAAc;AACzC,KAAI,0BAA0B,IAAI,mBAAmB,KAAK,OACxD,KAAI;EACF,MAAM,SAAS,MAAM;AACrB,QAAM,OAAO,WAAW,UAAU,eAAe,KAAK;GACpD;GACA;GACA,oBAAoB;GACpB;GACD,CAAC;AACF,SAAO;UACA,WAAW;AAClB,QAAM,OAAO,WAAW,UAAU,eAAe,KAAK;GACpD;GACA;GACA;GACA,OAAO;GACR,CAAC;AACF,QAAM;;AAIV,QAAO;;AAGT,SAAgB,cAAc,QAA4B;AACxD,QAAO;EACL,GAAG;EACH,OAAO,WAAW,SAAS,OAAO,QAAQ;EAC1C,MAAM,OAAO,QAAQE;EACrB,iBAAiB,OAAO,mBAAmB,OAAO;EAClD,YAAY,OAAO,cAAcC;EAClC"}
@@ -6,6 +6,7 @@ import { RUNTIME_001, RUNTIME_008, runtimeDescMap } from "@module-federation/err
6
6
 
7
7
  //#region src/utils/load.ts
8
8
  const importCallback = ".then(callbacks[0]).catch(callbacks[1])";
9
+ const remoteEntryLoadingOrigins = /* @__PURE__ */ new WeakMap();
9
10
  const esmRemoteEntryLoadErrorMessages = [
10
11
  "Failed to fetch dynamically imported module",
11
12
  "Importing a module script failed",
@@ -157,7 +158,16 @@ function getRemoteEntryUniqueKey(remoteInfo) {
157
158
  async function getRemoteEntry(params) {
158
159
  const { origin, remoteEntryExports, remoteInfo, getEntryUrl, resourceContext, _inErrorHandling = false } = params;
159
160
  const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);
160
- if (remoteEntryExports) return remoteEntryExports;
161
+ if (remoteEntryExports) {
162
+ await origin.loaderHook.lifecycle.afterLoadEntry.emit({
163
+ origin,
164
+ remoteInfo,
165
+ remoteEntryExports,
166
+ resourceContext,
167
+ cached: true
168
+ });
169
+ return remoteEntryExports;
170
+ }
161
171
  if (!globalLoading[uniqueKey]) {
162
172
  const loadEntryHook = origin.remoteHandler.hooks.lifecycle.loadEntry;
163
173
  const loaderHook = origin.loaderHook;
@@ -165,7 +175,8 @@ async function getRemoteEntry(params) {
165
175
  origin,
166
176
  loaderHook,
167
177
  remoteInfo,
168
- remoteEntryExports
178
+ remoteEntryExports,
179
+ resourceContext
169
180
  }).then((res) => {
170
181
  if (res) return res;
171
182
  return (typeof ENV_TARGET !== "undefined" ? ENV_TARGET === "web" : isBrowserEnvValue) ? loadEntryDom({
@@ -183,20 +194,20 @@ async function getRemoteEntry(params) {
183
194
  await origin.loaderHook.lifecycle.afterLoadEntry.emit({
184
195
  origin,
185
196
  remoteInfo,
186
- remoteEntryExports: res
197
+ remoteEntryExports: res,
198
+ resourceContext
187
199
  });
188
200
  return res;
189
- }).catch(async (err) => {
190
- const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);
191
- const isScriptExecutionError = err instanceof Error && err.message.includes("ScriptExecutionError");
192
- if (err instanceof Error && err.message.includes(RUNTIME_008) && !isScriptExecutionError && !_inErrorHandling) {
201
+ }).catch(async (loadError) => {
202
+ const isScriptExecutionError = loadError instanceof Error && loadError.message.includes("ScriptExecutionError");
203
+ if (loadError instanceof Error && loadError.message.includes(RUNTIME_008) && !isScriptExecutionError && !_inErrorHandling) {
193
204
  const wrappedGetRemoteEntry = (params) => {
194
205
  return getRemoteEntry({
195
206
  ...params,
196
207
  _inErrorHandling: true
197
208
  });
198
209
  };
199
- const RemoteEntryExports = await origin.loaderHook.lifecycle.loadEntryError.emit({
210
+ const recoveredRemoteEntryExports = await origin.loaderHook.lifecycle.loadEntryError.emit({
200
211
  getRemoteEntry: wrappedGetRemoteEntry,
201
212
  origin,
202
213
  remoteInfo,
@@ -204,25 +215,48 @@ async function getRemoteEntry(params) {
204
215
  globalLoading,
205
216
  uniqueKey
206
217
  });
207
- if (RemoteEntryExports) {
218
+ if (recoveredRemoteEntryExports) {
208
219
  await origin.loaderHook.lifecycle.afterLoadEntry.emit({
209
220
  origin,
210
221
  remoteInfo,
211
- remoteEntryExports: RemoteEntryExports,
222
+ remoteEntryExports: recoveredRemoteEntryExports,
223
+ resourceContext,
224
+ error: loadError,
212
225
  recovered: true
213
226
  });
214
- return RemoteEntryExports;
227
+ return recoveredRemoteEntryExports;
215
228
  }
216
229
  }
217
230
  await origin.loaderHook.lifecycle.afterLoadEntry.emit({
218
231
  origin,
219
232
  remoteInfo,
220
- error: err
233
+ resourceContext,
234
+ error: loadError
221
235
  });
222
- throw err;
236
+ throw loadError;
237
+ });
238
+ remoteEntryLoadingOrigins.set(globalLoading[uniqueKey], origin);
239
+ }
240
+ const remoteEntryLoading = globalLoading[uniqueKey];
241
+ if (remoteEntryLoadingOrigins.get(remoteEntryLoading) !== origin) try {
242
+ const result = await remoteEntryLoading;
243
+ await origin.loaderHook.lifecycle.afterLoadEntry.emit({
244
+ origin,
245
+ remoteInfo,
246
+ remoteEntryExports: result,
247
+ resourceContext
248
+ });
249
+ return result;
250
+ } catch (loadError) {
251
+ await origin.loaderHook.lifecycle.afterLoadEntry.emit({
252
+ origin,
253
+ remoteInfo,
254
+ resourceContext,
255
+ error: loadError
223
256
  });
257
+ throw loadError;
224
258
  }
225
- return globalLoading[uniqueKey];
259
+ return remoteEntryLoading;
226
260
  }
227
261
  function getRemoteInfo(remote) {
228
262
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"load.js","names":[],"sources":["../../src/utils/load.ts"],"sourcesContent":["import {\n loadScript,\n loadScriptNode,\n composeKeyWithSeparator,\n isBrowserEnvValue,\n} from '@module-federation/sdk';\nimport { DEFAULT_REMOTE_TYPE, DEFAULT_SCOPE } from '../constant';\nimport { ModuleFederation } from '../core';\nimport { globalLoading, getRemoteEntryExports } from '../global';\nimport {\n Remote,\n RemoteEntryExports,\n RemoteInfo,\n ResourceLoadContext,\n} from '../type';\nimport { assert, error } from './logger';\nimport {\n RUNTIME_001,\n RUNTIME_008,\n runtimeDescMap,\n} from '@module-federation/error-codes';\n\n// Declare the ENV_TARGET constant that will be defined by DefinePlugin\ndeclare const ENV_TARGET: 'web' | 'node';\nconst importCallback = '.then(callbacks[0]).catch(callbacks[1])';\n\nconst esmRemoteEntryLoadErrorMessages = [\n 'Failed to fetch dynamically imported module',\n 'Importing a module script failed',\n 'error loading dynamically imported module',\n];\n\nfunction isEsmRemoteEntryLoadError(err: unknown): boolean {\n if (!(err instanceof TypeError)) {\n return false;\n }\n\n return esmRemoteEntryLoadErrorMessages.some((loadErrorMessage) =>\n err.message.includes(loadErrorMessage),\n );\n}\n\nexport function isEsmRemoteType(type: RemoteInfo['type']): boolean {\n return type === 'esm' || type === 'module';\n}\n\nasync function loadEsmEntry({\n entry,\n remoteEntryExports,\n name,\n getEntryUrl,\n}: {\n entry: string;\n remoteEntryExports: RemoteEntryExports | undefined;\n name: string;\n getEntryUrl?: (url: string) => string;\n}): Promise<RemoteEntryExports> {\n return new Promise<RemoteEntryExports>((resolve, reject) => {\n const rejectEntry = (loadError: unknown) => {\n if (isEsmRemoteEntryLoadError(loadError)) {\n const originalMsg =\n loadError instanceof Error ? loadError.message : String(loadError);\n try {\n error(\n RUNTIME_008,\n runtimeDescMap,\n {\n remoteName: name,\n resourceUrl: url,\n },\n originalMsg,\n );\n } catch (runtimeError) {\n reject(runtimeError);\n return;\n }\n }\n\n reject(loadError);\n };\n\n const url = getEntryUrl ? getEntryUrl(entry) : entry;\n try {\n if (!remoteEntryExports) {\n if (typeof FEDERATION_ALLOW_NEW_FUNCTION !== 'undefined') {\n new Function('callbacks', `import(\"${url}\")${importCallback}`)([\n resolve,\n rejectEntry,\n ]);\n } else {\n import(/* webpackIgnore: true */ /* @vite-ignore */ url)\n .then(resolve)\n .catch(rejectEntry);\n }\n } else {\n resolve(remoteEntryExports);\n }\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n error(`Failed to load ESM entry from \"${url}\". ${msg}`);\n }\n });\n}\n\nasync function loadSystemJsEntry({\n entry,\n remoteEntryExports,\n}: {\n entry: string;\n remoteEntryExports: RemoteEntryExports | undefined;\n}): Promise<RemoteEntryExports> {\n return new Promise<RemoteEntryExports>((resolve, reject) => {\n try {\n if (!remoteEntryExports) {\n //@ts-ignore\n if (typeof __system_context__ === 'undefined') {\n //@ts-ignore\n System.import(entry).then(resolve).catch(reject);\n } else {\n new Function(\n 'callbacks',\n `System.import(\"${entry}\")${importCallback}`,\n )([resolve, reject]);\n }\n } else {\n resolve(remoteEntryExports);\n }\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n error(`Failed to load SystemJS entry from \"${entry}\". ${msg}`);\n }\n });\n}\n\nfunction handleRemoteEntryLoaded(\n name: string,\n globalName: string,\n entry: string,\n): RemoteEntryExports {\n const { remoteEntryKey, entryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (!entryExports) {\n error(RUNTIME_001, runtimeDescMap, {\n remoteName: name,\n remoteEntryUrl: entry,\n remoteEntryKey,\n });\n }\n\n return entryExports;\n}\n\nasync function loadEntryScript({\n name,\n globalName,\n entry,\n remoteInfo,\n loaderHook,\n getEntryUrl,\n resourceContext,\n}: {\n name: string;\n globalName: string;\n entry: string;\n remoteInfo: RemoteInfo;\n loaderHook: ModuleFederation['loaderHook'];\n getEntryUrl?: (url: string) => string;\n resourceContext?: ResourceLoadContext;\n}): Promise<RemoteEntryExports> {\n const { entryExports: remoteEntryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n // if getEntryUrl is passed, use the getEntryUrl to get the entry url\n const url = getEntryUrl ? getEntryUrl(entry) : entry;\n return loadScript(url, {\n attrs: {},\n createScriptHook: (url, attrs) => {\n const res = loaderHook.lifecycle.createScript.emit({\n url,\n attrs,\n remoteInfo,\n resourceContext: resourceContext\n ? {\n ...resourceContext,\n url,\n }\n : undefined,\n });\n\n if (!res) return;\n\n if (res instanceof HTMLScriptElement) {\n return res;\n }\n\n if ('script' in res || 'timeout' in res) {\n return res;\n }\n\n return;\n },\n }).then(\n () => {\n // loadScript resolved: script was fetched, executed without throwing, and\n // did not trigger a ScriptExecutionError listener. Now verify the global was registered.\n return handleRemoteEntryLoaded(name, globalName, entry);\n },\n (loadError: unknown) => {\n // loadScript rejected — one of three causes, all with descriptive messages:\n // ScriptNetworkError — URL unreachable, 404, CORS, etc.\n // ScriptExecutionError — script fetched OK but IIFE threw during execution\n // timeout — script took too long to load\n // Errors thrown inside handleRemoteEntryLoaded above are NOT caught here.\n const originalMsg =\n loadError instanceof Error ? loadError.message : String(loadError);\n error(\n RUNTIME_008,\n runtimeDescMap,\n {\n remoteName: name,\n resourceUrl: url,\n },\n originalMsg,\n );\n },\n );\n}\nasync function loadEntryDom({\n remoteInfo,\n remoteEntryExports,\n loaderHook,\n getEntryUrl,\n resourceContext,\n}: {\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports;\n loaderHook: ModuleFederation['loaderHook'];\n getEntryUrl?: (url: string) => string;\n resourceContext?: ResourceLoadContext;\n}) {\n const { entry, entryGlobalName: globalName, name, type } = remoteInfo;\n if (isEsmRemoteType(type)) {\n return loadEsmEntry({ entry, remoteEntryExports, name, getEntryUrl });\n }\n\n if (type === 'system') {\n return loadSystemJsEntry({ entry, remoteEntryExports });\n }\n\n return loadEntryScript({\n entry,\n globalName,\n name,\n remoteInfo,\n loaderHook,\n getEntryUrl,\n resourceContext,\n });\n}\n\nasync function loadEntryNode({\n remoteInfo,\n loaderHook,\n resourceContext,\n}: {\n remoteInfo: RemoteInfo;\n loaderHook: ModuleFederation['loaderHook'];\n resourceContext?: ResourceLoadContext;\n}) {\n const { entry, entryGlobalName: globalName, name, type } = remoteInfo;\n const { entryExports: remoteEntryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n return loadScriptNode(entry, {\n attrs: { name, globalName, type },\n loaderHook: {\n createScriptHook: (url: string, attrs: Record<string, any> = {}) => {\n const res = loaderHook.lifecycle.createScript.emit({\n url,\n attrs,\n remoteInfo,\n resourceContext: resourceContext\n ? {\n ...resourceContext,\n url,\n }\n : undefined,\n });\n\n if (!res) return;\n\n if ('url' in res) {\n return res;\n }\n\n return;\n },\n },\n })\n .then(() => {\n return handleRemoteEntryLoaded(name, globalName, entry);\n })\n .catch((e) => {\n const msg = e instanceof Error ? e.message : String(e);\n error(\n `Failed to load Node.js entry for remote \"${name}\" from \"${entry}\". ${msg}`,\n );\n });\n}\n\nexport function getRemoteEntryUniqueKey(remoteInfo: RemoteInfo): string {\n const { entry, name } = remoteInfo;\n return composeKeyWithSeparator(name, entry);\n}\n\nexport async function getRemoteEntry(params: {\n origin: ModuleFederation;\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports | undefined;\n getEntryUrl?: (url: string) => string;\n _inErrorHandling?: boolean; // Add flag to prevent recursion\n resourceContext?: ResourceLoadContext;\n}): Promise<RemoteEntryExports | false | void> {\n const {\n origin,\n remoteEntryExports,\n remoteInfo,\n getEntryUrl,\n resourceContext,\n _inErrorHandling = false,\n } = params;\n const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n if (!globalLoading[uniqueKey]) {\n const loadEntryHook = origin.remoteHandler.hooks.lifecycle.loadEntry;\n const loaderHook = origin.loaderHook;\n\n globalLoading[uniqueKey] = loadEntryHook\n .emit({\n origin,\n loaderHook,\n remoteInfo,\n remoteEntryExports,\n })\n .then((res) => {\n if (res) {\n return res;\n }\n // Use ENV_TARGET if defined, otherwise fallback to isBrowserEnvValue\n const isWebEnvironment =\n typeof ENV_TARGET !== 'undefined'\n ? ENV_TARGET === 'web'\n : isBrowserEnvValue;\n\n return isWebEnvironment\n ? loadEntryDom({\n remoteInfo,\n remoteEntryExports,\n loaderHook,\n getEntryUrl,\n resourceContext,\n })\n : loadEntryNode({ remoteInfo, loaderHook, resourceContext });\n })\n .then(async (res) => {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports: res,\n });\n return res;\n })\n .catch(async (err) => {\n const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);\n // ScriptExecutionError means the script downloaded fine but its IIFE\n // threw at runtime — retrying would reproduce the same error, so exclude it.\n const isScriptExecutionError =\n err instanceof Error && err.message.includes('ScriptExecutionError');\n const isScriptLoadError =\n err instanceof Error &&\n err.message.includes(RUNTIME_008) &&\n !isScriptExecutionError;\n\n if (isScriptLoadError && !_inErrorHandling) {\n const wrappedGetRemoteEntry = (\n params: Parameters<typeof getRemoteEntry>[0],\n ) => {\n return getRemoteEntry({ ...params, _inErrorHandling: true });\n };\n\n const RemoteEntryExports =\n await origin.loaderHook.lifecycle.loadEntryError.emit({\n getRemoteEntry: wrappedGetRemoteEntry,\n origin,\n remoteInfo: remoteInfo,\n remoteEntryExports,\n globalLoading,\n uniqueKey,\n });\n\n if (RemoteEntryExports) {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports: RemoteEntryExports,\n recovered: true,\n });\n return RemoteEntryExports;\n }\n }\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n error: err,\n });\n throw err;\n });\n }\n\n return globalLoading[uniqueKey];\n}\n\nexport function getRemoteInfo(remote: Remote): RemoteInfo {\n return {\n ...remote,\n entry: 'entry' in remote ? remote.entry : '',\n type: remote.type || DEFAULT_REMOTE_TYPE,\n entryGlobalName: remote.entryGlobalName || remote.name,\n shareScope: remote.shareScope || DEFAULT_SCOPE,\n };\n}\n"],"mappings":";;;;;;;AAwBA,MAAM,iBAAiB;AAEvB,MAAM,kCAAkC;CACtC;CACA;CACA;CACD;AAED,SAAS,0BAA0B,KAAuB;AACxD,KAAI,EAAE,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A,oBAAoB;IACrB,CAAC;AACF,UAAO;IACP,CACD,MAAM,OAAO,QAAQ;GACpB,MAAM,YAAY,wBAAwB,WAAW;GAGrD,MAAM,yBACJ,eAAe,SAAS,IAAI,QAAQ,SAAS,uBAAuB;AAMtE,OAJE,eAAe,SACf,IAAI,QAAQ,SAAS,YAAY,IACjC,CAAC,0BAEsB,CAAC,kBAAkB;IAC1C,MAAM,yBACJ,WACG;AACH,YAAO,eAAe;MAAE,GAAG;MAAQ,kBAAkB;MAAM,CAAC;;IAG9D,MAAM,qBACJ,MAAM,OAAO,WAAW,UAAU,eAAe,KAAK;KACpD,gBAAgB;KAChB;KACY;KACZ;KACA;KACA;KACD,CAAC;AAEJ,QAAI,oBAAoB;AACtB,WAAM,OAAO,WAAW,UAAU,eAAe,KAAK;MACpD;MACA;MACA,oBAAoB;MACpB,WAAW;MACZ,CAAC;AACF,YAAO;;;AAGX,SAAM,OAAO,WAAW,UAAU,eAAe,KAAK;IACpD;IACA;IACA,OAAO;IACR,CAAC;AACF,SAAM;IACN;;AAGN,QAAO,cAAc;;AAGvB,SAAgB,cAAc,QAA4B;AACxD,QAAO;EACL,GAAG;EACH,OAAO,WAAW,SAAS,OAAO,QAAQ;EAC1C,MAAM,OAAO,QAAQ;EACrB,iBAAiB,OAAO,mBAAmB,OAAO;EAClD,YAAY,OAAO,cAAc;EAClC"}
1
+ {"version":3,"file":"load.js","names":[],"sources":["../../src/utils/load.ts"],"sourcesContent":["import {\n loadScript,\n loadScriptNode,\n composeKeyWithSeparator,\n isBrowserEnvValue,\n} from '@module-federation/sdk';\nimport { DEFAULT_REMOTE_TYPE, DEFAULT_SCOPE } from '../constant';\nimport { ModuleFederation } from '../core';\nimport { globalLoading, getRemoteEntryExports } from '../global';\nimport {\n Remote,\n RemoteEntryExports,\n RemoteInfo,\n ResourceLoadContext,\n} from '../type';\nimport { assert, error } from './logger';\nimport {\n RUNTIME_001,\n RUNTIME_008,\n runtimeDescMap,\n} from '@module-federation/error-codes';\n\n// Declare the ENV_TARGET constant that will be defined by DefinePlugin\ndeclare const ENV_TARGET: 'web' | 'node';\nconst importCallback = '.then(callbacks[0]).catch(callbacks[1])';\nconst remoteEntryLoadingOrigins = new WeakMap<\n Promise<RemoteEntryExports | void>,\n ModuleFederation\n>();\n\nconst esmRemoteEntryLoadErrorMessages = [\n 'Failed to fetch dynamically imported module',\n 'Importing a module script failed',\n 'error loading dynamically imported module',\n];\n\nfunction isEsmRemoteEntryLoadError(err: unknown): boolean {\n if (!(err instanceof TypeError)) {\n return false;\n }\n\n return esmRemoteEntryLoadErrorMessages.some((loadErrorMessage) =>\n err.message.includes(loadErrorMessage),\n );\n}\n\nexport function isEsmRemoteType(type: RemoteInfo['type']): boolean {\n return type === 'esm' || type === 'module';\n}\n\nasync function loadEsmEntry({\n entry,\n remoteEntryExports,\n name,\n getEntryUrl,\n}: {\n entry: string;\n remoteEntryExports: RemoteEntryExports | undefined;\n name: string;\n getEntryUrl?: (url: string) => string;\n}): Promise<RemoteEntryExports> {\n return new Promise<RemoteEntryExports>((resolve, reject) => {\n const rejectEntry = (loadError: unknown) => {\n if (isEsmRemoteEntryLoadError(loadError)) {\n const originalMsg =\n loadError instanceof Error ? loadError.message : String(loadError);\n try {\n error(\n RUNTIME_008,\n runtimeDescMap,\n {\n remoteName: name,\n resourceUrl: url,\n },\n originalMsg,\n );\n } catch (runtimeError) {\n reject(runtimeError);\n return;\n }\n }\n\n reject(loadError);\n };\n\n const url = getEntryUrl ? getEntryUrl(entry) : entry;\n try {\n if (!remoteEntryExports) {\n if (typeof FEDERATION_ALLOW_NEW_FUNCTION !== 'undefined') {\n new Function('callbacks', `import(\"${url}\")${importCallback}`)([\n resolve,\n rejectEntry,\n ]);\n } else {\n import(/* webpackIgnore: true */ /* @vite-ignore */ url)\n .then(resolve)\n .catch(rejectEntry);\n }\n } else {\n resolve(remoteEntryExports);\n }\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n error(`Failed to load ESM entry from \"${url}\". ${msg}`);\n }\n });\n}\n\nasync function loadSystemJsEntry({\n entry,\n remoteEntryExports,\n}: {\n entry: string;\n remoteEntryExports: RemoteEntryExports | undefined;\n}): Promise<RemoteEntryExports> {\n return new Promise<RemoteEntryExports>((resolve, reject) => {\n try {\n if (!remoteEntryExports) {\n //@ts-ignore\n if (typeof __system_context__ === 'undefined') {\n //@ts-ignore\n System.import(entry).then(resolve).catch(reject);\n } else {\n new Function(\n 'callbacks',\n `System.import(\"${entry}\")${importCallback}`,\n )([resolve, reject]);\n }\n } else {\n resolve(remoteEntryExports);\n }\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n error(`Failed to load SystemJS entry from \"${entry}\". ${msg}`);\n }\n });\n}\n\nfunction handleRemoteEntryLoaded(\n name: string,\n globalName: string,\n entry: string,\n): RemoteEntryExports {\n const { remoteEntryKey, entryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (!entryExports) {\n error(RUNTIME_001, runtimeDescMap, {\n remoteName: name,\n remoteEntryUrl: entry,\n remoteEntryKey,\n });\n }\n\n return entryExports;\n}\n\nasync function loadEntryScript({\n name,\n globalName,\n entry,\n remoteInfo,\n loaderHook,\n getEntryUrl,\n resourceContext,\n}: {\n name: string;\n globalName: string;\n entry: string;\n remoteInfo: RemoteInfo;\n loaderHook: ModuleFederation['loaderHook'];\n getEntryUrl?: (url: string) => string;\n resourceContext?: ResourceLoadContext;\n}): Promise<RemoteEntryExports> {\n const { entryExports: remoteEntryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n // if getEntryUrl is passed, use the getEntryUrl to get the entry url\n const url = getEntryUrl ? getEntryUrl(entry) : entry;\n return loadScript(url, {\n attrs: {},\n createScriptHook: (url, attrs) => {\n const res = loaderHook.lifecycle.createScript.emit({\n url,\n attrs,\n remoteInfo,\n resourceContext: resourceContext\n ? {\n ...resourceContext,\n url,\n }\n : undefined,\n });\n\n if (!res) return;\n\n if (res instanceof HTMLScriptElement) {\n return res;\n }\n\n if ('script' in res || 'timeout' in res) {\n return res;\n }\n\n return;\n },\n }).then(\n () => {\n // loadScript resolved: script was fetched, executed without throwing, and\n // did not trigger a ScriptExecutionError listener. Now verify the global was registered.\n return handleRemoteEntryLoaded(name, globalName, entry);\n },\n (loadError: unknown) => {\n // loadScript rejected — one of three causes, all with descriptive messages:\n // ScriptNetworkError — URL unreachable, 404, CORS, etc.\n // ScriptExecutionError — script fetched OK but IIFE threw during execution\n // timeout — script took too long to load\n // Errors thrown inside handleRemoteEntryLoaded above are NOT caught here.\n const originalMsg =\n loadError instanceof Error ? loadError.message : String(loadError);\n error(\n RUNTIME_008,\n runtimeDescMap,\n {\n remoteName: name,\n resourceUrl: url,\n },\n originalMsg,\n );\n },\n );\n}\nasync function loadEntryDom({\n remoteInfo,\n remoteEntryExports,\n loaderHook,\n getEntryUrl,\n resourceContext,\n}: {\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports;\n loaderHook: ModuleFederation['loaderHook'];\n getEntryUrl?: (url: string) => string;\n resourceContext?: ResourceLoadContext;\n}) {\n const { entry, entryGlobalName: globalName, name, type } = remoteInfo;\n if (isEsmRemoteType(type)) {\n return loadEsmEntry({ entry, remoteEntryExports, name, getEntryUrl });\n }\n\n if (type === 'system') {\n return loadSystemJsEntry({ entry, remoteEntryExports });\n }\n\n return loadEntryScript({\n entry,\n globalName,\n name,\n remoteInfo,\n loaderHook,\n getEntryUrl,\n resourceContext,\n });\n}\n\nasync function loadEntryNode({\n remoteInfo,\n loaderHook,\n resourceContext,\n}: {\n remoteInfo: RemoteInfo;\n loaderHook: ModuleFederation['loaderHook'];\n resourceContext?: ResourceLoadContext;\n}) {\n const { entry, entryGlobalName: globalName, name, type } = remoteInfo;\n const { entryExports: remoteEntryExports } = getRemoteEntryExports(\n name,\n globalName,\n );\n\n if (remoteEntryExports) {\n return remoteEntryExports;\n }\n\n return loadScriptNode(entry, {\n attrs: { name, globalName, type },\n loaderHook: {\n createScriptHook: (url: string, attrs: Record<string, any> = {}) => {\n const res = loaderHook.lifecycle.createScript.emit({\n url,\n attrs,\n remoteInfo,\n resourceContext: resourceContext\n ? {\n ...resourceContext,\n url,\n }\n : undefined,\n });\n\n if (!res) return;\n\n if ('url' in res) {\n return res;\n }\n\n return;\n },\n },\n })\n .then(() => {\n return handleRemoteEntryLoaded(name, globalName, entry);\n })\n .catch((e) => {\n const msg = e instanceof Error ? e.message : String(e);\n error(\n `Failed to load Node.js entry for remote \"${name}\" from \"${entry}\". ${msg}`,\n );\n });\n}\n\nexport function getRemoteEntryUniqueKey(remoteInfo: RemoteInfo): string {\n const { entry, name } = remoteInfo;\n return composeKeyWithSeparator(name, entry);\n}\n\nexport async function getRemoteEntry(params: {\n origin: ModuleFederation;\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports | undefined;\n getEntryUrl?: (url: string) => string;\n _inErrorHandling?: boolean; // Add flag to prevent recursion\n resourceContext?: ResourceLoadContext;\n}): Promise<RemoteEntryExports | false | void> {\n const {\n origin,\n remoteEntryExports,\n remoteInfo,\n getEntryUrl,\n resourceContext,\n _inErrorHandling = false,\n } = params;\n const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);\n\n if (remoteEntryExports) {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports,\n resourceContext,\n cached: true,\n });\n return remoteEntryExports;\n }\n\n if (!globalLoading[uniqueKey]) {\n const loadEntryHook = origin.remoteHandler.hooks.lifecycle.loadEntry;\n const loaderHook = origin.loaderHook;\n globalLoading[uniqueKey] = loadEntryHook\n .emit({\n origin,\n loaderHook,\n remoteInfo,\n remoteEntryExports,\n resourceContext,\n })\n .then((res) => {\n if (res) {\n return res;\n }\n const isWebEnvironment =\n typeof ENV_TARGET !== 'undefined'\n ? ENV_TARGET === 'web'\n : isBrowserEnvValue;\n\n return isWebEnvironment\n ? loadEntryDom({\n remoteInfo,\n remoteEntryExports,\n loaderHook,\n getEntryUrl,\n resourceContext,\n })\n : loadEntryNode({ remoteInfo, loaderHook, resourceContext });\n })\n .then(async (res) => {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports: res,\n resourceContext,\n });\n return res;\n })\n .catch(async (loadError) => {\n const isScriptExecutionError =\n loadError instanceof Error &&\n loadError.message.includes('ScriptExecutionError');\n const isScriptLoadError =\n loadError instanceof Error &&\n loadError.message.includes(RUNTIME_008) &&\n !isScriptExecutionError;\n\n if (isScriptLoadError && !_inErrorHandling) {\n const wrappedGetRemoteEntry = (\n params: Parameters<typeof getRemoteEntry>[0],\n ) => {\n return getRemoteEntry({ ...params, _inErrorHandling: true });\n };\n\n const recoveredRemoteEntryExports =\n await origin.loaderHook.lifecycle.loadEntryError.emit({\n getRemoteEntry: wrappedGetRemoteEntry,\n origin,\n remoteInfo,\n remoteEntryExports,\n globalLoading,\n uniqueKey,\n });\n\n if (recoveredRemoteEntryExports) {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports: recoveredRemoteEntryExports,\n resourceContext,\n error: loadError,\n recovered: true,\n });\n return recoveredRemoteEntryExports;\n }\n }\n\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n resourceContext,\n error: loadError,\n });\n throw loadError;\n });\n remoteEntryLoadingOrigins.set(globalLoading[uniqueKey], origin);\n }\n\n const remoteEntryLoading = globalLoading[uniqueKey];\n if (remoteEntryLoadingOrigins.get(remoteEntryLoading) !== origin) {\n try {\n const result = await remoteEntryLoading;\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n remoteEntryExports: result,\n resourceContext,\n });\n return result;\n } catch (loadError) {\n await origin.loaderHook.lifecycle.afterLoadEntry.emit({\n origin,\n remoteInfo,\n resourceContext,\n error: loadError,\n });\n throw loadError;\n }\n }\n\n return remoteEntryLoading;\n}\n\nexport function getRemoteInfo(remote: Remote): RemoteInfo {\n return {\n ...remote,\n entry: 'entry' in remote ? remote.entry : '',\n type: remote.type || DEFAULT_REMOTE_TYPE,\n entryGlobalName: remote.entryGlobalName || remote.name,\n shareScope: remote.shareScope || DEFAULT_SCOPE,\n };\n}\n"],"mappings":";;;;;;;AAwBA,MAAM,iBAAiB;AACvB,MAAM,4CAA4B,IAAI,SAGnC;AAEH,MAAM,kCAAkC;CACtC;CACA;CACA;CACD;AAED,SAAS,0BAA0B,KAAuB;AACxD,KAAI,EAAE,eAAe,WACnB,QAAO;AAGT,QAAO,gCAAgC,MAAM,qBAC3C,IAAI,QAAQ,SAAS,iBAAiB,CACvC;;AAGH,SAAgB,gBAAgB,MAAmC;AACjE,QAAO,SAAS,SAAS,SAAS;;AAGpC,eAAe,aAAa,EAC1B,OACA,oBACA,MACA,eAM8B;AAC9B,QAAO,IAAI,SAA6B,SAAS,WAAW;EAC1D,MAAM,eAAe,cAAuB;AAC1C,OAAI,0BAA0B,UAAU,EAAE;IACxC,MAAM,cACJ,qBAAqB,QAAQ,UAAU,UAAU,OAAO,UAAU;AACpE,QAAI;AACF,WACE,aACA,gBACA;MACE,YAAY;MACZ,aAAa;MACd,EACD,YACD;aACM,cAAc;AACrB,YAAO,aAAa;AACpB;;;AAIJ,UAAO,UAAU;;EAGnB,MAAM,MAAM,cAAc,YAAY,MAAM,GAAG;AAC/C,MAAI;AACF,OAAI,CAAC,mBACH,KAAI,OAAO,kCAAkC,YAC3C,KAAI,SAAS,aAAa,WAAW,IAAI,IAAI,iBAAiB,CAAC,CAC7D,SACA,YACD,CAAC;OAEF;;;IAAoD;EACjD,KAAK,QAAQ,CACb,MAAM,YAAY;OAGvB,SAAQ,mBAAmB;WAEtB,GAAG;AAEV,SAAM,kCAAkC,IAAI,KADhC,aAAa,QAAQ,EAAE,UAAU,OAAO,EAAE,GACC;;GAEzD;;AAGJ,eAAe,kBAAkB,EAC/B,OACA,sBAI8B;AAC9B,QAAO,IAAI,SAA6B,S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WAAW,UAAU,eAAe,KAAK;GACpD;GACA;GACA,oBAAoB;GACpB;GACD,CAAC;AACF,SAAO;UACA,WAAW;AAClB,QAAM,OAAO,WAAW,UAAU,eAAe,KAAK;GACpD;GACA;GACA;GACA,OAAO;GACR,CAAC;AACF,QAAM;;AAIV,QAAO;;AAGT,SAAgB,cAAc,QAA4B;AACxD,QAAO;EACL,GAAG;EACH,OAAO,WAAW,SAAS,OAAO,QAAQ;EAC1C,MAAM,OAAO,QAAQ;EACrB,iBAAiB,OAAO,mBAAmB,OAAO;EAClD,YAAY,OAAO,cAAc;EAClC"}
@@ -1,8 +1,31 @@
1
+ const require_logger = require('./logger.cjs');
2
+ const require_tool = require('./tool.cjs');
1
3
  const require_global = require('../global.cjs');
2
4
 
3
5
  //#region src/utils/plugin.ts
6
+ function getPluginsToRegister(plugins) {
7
+ const pluginsByName = /* @__PURE__ */ new Map();
8
+ [...plugins || [], ...require_global.getGlobalHostPlugins()].forEach((plugin) => {
9
+ if (!plugin) return;
10
+ require_logger.assert(require_tool.isPlainObject(plugin), "Plugin configuration is invalid.");
11
+ require_logger.assert(plugin.name, "A name must be provided by the plugin.");
12
+ if (!pluginsByName.has(plugin.name)) pluginsByName.set(plugin.name, plugin);
13
+ });
14
+ return Array.from(pluginsByName.values());
15
+ }
16
+ function getInstancePlugin(plugin, instanceHooks) {
17
+ if (instanceHooks === void 0) return plugin;
18
+ return {
19
+ ...instanceHooks,
20
+ name: plugin.name,
21
+ version: plugin.version
22
+ };
23
+ }
4
24
  function registerPlugins(plugins, instance) {
5
- const globalPlugins = require_global.getGlobalHostPlugins();
25
+ const registeredPlugins = /* @__PURE__ */ new Map();
26
+ instance.options.plugins.forEach((plugin) => {
27
+ if (plugin) registeredPlugins.set(plugin.name, plugin);
28
+ });
6
29
  const hookInstances = [
7
30
  instance.hooks,
8
31
  instance.remoteHandler.hooks,
@@ -11,15 +34,15 @@ function registerPlugins(plugins, instance) {
11
34
  instance.loaderHook,
12
35
  instance.bridgeHook
13
36
  ];
14
- if (globalPlugins.length > 0) globalPlugins.forEach((plugin) => {
15
- if (plugins?.find((item) => item.name !== plugin.name)) plugins.push(plugin);
16
- });
17
- if (plugins && plugins.length > 0) plugins.forEach((plugin) => {
37
+ getPluginsToRegister(plugins).forEach((plugin) => {
38
+ registeredPlugins.set(plugin.name, plugin);
39
+ if (instance.hooks.registerPlugins[plugin.name]) return;
40
+ const instancePlugin = getInstancePlugin(plugin, plugin.apply?.(instance));
18
41
  hookInstances.forEach((hookInstance) => {
19
- hookInstance.applyPlugin(plugin, instance);
42
+ hookInstance.applyPlugin(instancePlugin);
20
43
  });
21
44
  });
22
- return plugins;
45
+ return Array.from(registeredPlugins.values());
23
46
  }
24
47
 
25
48
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.cjs","names":["getGlobalHostPlugins"],"sources":["../../src/utils/plugin.ts"],"sourcesContent":["import { ModuleFederation } from '../core';\nimport { UserOptions } from '../type';\nimport { getGlobalHostPlugins } from '../global';\n\nexport function registerPlugins(\n plugins: UserOptions['plugins'],\n instance: ModuleFederation,\n) {\n const globalPlugins = getGlobalHostPlugins();\n const hookInstances = [\n instance.hooks,\n instance.remoteHandler.hooks,\n instance.sharedHandler.hooks,\n instance.snapshotHandler.hooks,\n instance.loaderHook,\n instance.bridgeHook,\n ];\n // Incorporate global plugins\n if (globalPlugins.length > 0) {\n globalPlugins.forEach((plugin) => {\n if (plugins?.find((item) => item.name !== plugin.name)) {\n plugins.push(plugin);\n }\n });\n }\n\n if (plugins && plugins.length > 0) {\n plugins.forEach((plugin) => {\n hookInstances.forEach((hookInstance) => {\n hookInstance.applyPlugin(plugin, instance);\n });\n });\n }\n return plugins;\n}\n"],"mappings":";;;AAIA,SAAgB,gBACd,SACA,UACA;CACA,MAAM,gBAAgBA,qCAAsB;CAC5C,MAAM,gBAAgB;EACpB,SAAS;EACT,SAAS,cAAc;EACvB,SAAS,cAAc;EACvB,SAAS,gBAAgB;EACzB,SAAS;EACT,SAAS;EACV;AAED,KAAI,cAAc,SAAS,EACzB,eAAc,SAAS,WAAW;AAChC,MAAI,SAAS,MAAM,SAAS,KAAK,SAAS,OAAO,KAAK,CACpD,SAAQ,KAAK,OAAO;GAEtB;AAGJ,KAAI,WAAW,QAAQ,SAAS,EAC9B,SAAQ,SAAS,WAAW;AAC1B,gBAAc,SAAS,iBAAiB;AACtC,gBAAa,YAAY,QAAQ,SAAS;IAC1C;GACF;AAEJ,QAAO"}
1
+ {"version":3,"file":"plugin.cjs","names":["getGlobalHostPlugins","isPlainObject"],"sources":["../../src/utils/plugin.ts"],"sourcesContent":["import { ModuleFederation } from '../core';\nimport {\n ModuleFederationRuntimePlugin,\n RuntimePluginHooks,\n UserOptions,\n} from '../type';\nimport { getGlobalHostPlugins } from '../global';\nimport { assert } from './logger';\nimport { isPlainObject } from './tool';\n\nfunction getPluginsToRegister(\n plugins: UserOptions['plugins'],\n): Array<ModuleFederationRuntimePlugin> {\n const pluginsByName = new Map<string, ModuleFederationRuntimePlugin>();\n\n [...(plugins || []), ...getGlobalHostPlugins()].forEach((plugin) => {\n if (!plugin) {\n return;\n }\n\n assert(isPlainObject(plugin), 'Plugin configuration is invalid.');\n assert(plugin.name, 'A name must be provided by the plugin.');\n\n if (!pluginsByName.has(plugin.name)) {\n pluginsByName.set(plugin.name, plugin);\n }\n });\n\n return Array.from(pluginsByName.values());\n}\n\nfunction getInstancePlugin(\n plugin: ModuleFederationRuntimePlugin,\n instanceHooks: void | RuntimePluginHooks,\n): ModuleFederationRuntimePlugin {\n if (instanceHooks === undefined) {\n return plugin;\n }\n\n return {\n ...instanceHooks,\n name: plugin.name,\n version: plugin.version,\n };\n}\n\nexport function registerPlugins(\n plugins: UserOptions['plugins'],\n instance: ModuleFederation,\n) {\n const registeredPlugins = new Map<string, ModuleFederationRuntimePlugin>();\n instance.options.plugins.forEach((plugin) => {\n if (plugin) {\n registeredPlugins.set(plugin.name, plugin);\n }\n });\n const hookInstances = [\n instance.hooks,\n instance.remoteHandler.hooks,\n instance.sharedHandler.hooks,\n instance.snapshotHandler.hooks,\n instance.loaderHook,\n instance.bridgeHook,\n ];\n\n getPluginsToRegister(plugins).forEach((plugin) => {\n registeredPlugins.set(plugin.name, plugin);\n\n if (instance.hooks.registerPlugins[plugin.name]) {\n return;\n }\n\n const instancePlugin = getInstancePlugin(plugin, plugin.apply?.(instance));\n hookInstances.forEach((hookInstance) => {\n hookInstance.applyPlugin(instancePlugin);\n });\n });\n\n return Array.from(registeredPlugins.values());\n}\n"],"mappings":";;;;;AAUA,SAAS,qBACP,SACsC;CACtC,MAAM,gCAAgB,IAAI,KAA4C;AAEtE,EAAC,GAAI,WAAW,EAAE,EAAG,GAAGA,qCAAsB,CAAC,CAAC,SAAS,WAAW;AAClE,MAAI,CAAC,OACH;AAGF,wBAAOC,2BAAc,OAAO,EAAE,mCAAmC;AACjE,wBAAO,OAAO,MAAM,yCAAyC;AAE7D,MAAI,CAAC,cAAc,IAAI,OAAO,KAAK,CACjC,eAAc,IAAI,OAAO,MAAM,OAAO;GAExC;AAEF,QAAO,MAAM,KAAK,cAAc,QAAQ,CAAC;;AAG3C,SAAS,kBACP,QACA,eAC+B;AAC/B,KAAI,kBAAkB,OACpB,QAAO;AAGT,QAAO;EACL,GAAG;EACH,MAAM,OAAO;EACb,SAAS,OAAO;EACjB;;AAGH,SAAgB,gBACd,SACA,UACA;CACA,MAAM,oCAAoB,IAAI,KAA4C;AAC1E,UAAS,QAAQ,QAAQ,SAAS,WAAW;AAC3C,MAAI,OACF,mBAAkB,IAAI,OAAO,MAAM,OAAO;GAE5C;CACF,MAAM,gBAAgB;EACpB,SAAS;EACT,SAAS,cAAc;EACvB,SAAS,cAAc;EACvB,SAAS,gBAAgB;EACzB,SAAS;EACT,SAAS;EACV;AAED,sBAAqB,QAAQ,CAAC,SAAS,WAAW;AAChD,oBAAkB,IAAI,OAAO,MAAM,OAAO;AAE1C,MAAI,SAAS,MAAM,gBAAgB,OAAO,MACxC;EAGF,MAAM,iBAAiB,kBAAkB,QAAQ,OAAO,QAAQ,SAAS,CAAC;AAC1E,gBAAc,SAAS,iBAAiB;AACtC,gBAAa,YAAY,eAAe;IACxC;GACF;AAEF,QAAO,MAAM,KAAK,kBAAkB,QAAQ,CAAC"}