@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.
- package/dist/core.cjs +4 -8
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +16 -14
- package/dist/core.js +4 -8
- package/dist/core.js.map +1 -1
- package/dist/global.cjs +17 -2
- package/dist/global.cjs.map +1 -1
- package/dist/global.d.ts +1 -0
- package/dist/global.js +16 -3
- package/dist/global.js.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js.map +1 -1
- package/dist/module/index.cjs +11 -9
- package/dist/module/index.cjs.map +1 -1
- package/dist/module/index.d.ts +3 -2
- package/dist/module/index.js +11 -9
- package/dist/module/index.js.map +1 -1
- package/dist/plugins/snapshot/SnapshotHandler.cjs +48 -5
- package/dist/plugins/snapshot/SnapshotHandler.cjs.map +1 -1
- package/dist/plugins/snapshot/SnapshotHandler.d.ts +23 -0
- package/dist/plugins/snapshot/SnapshotHandler.js +48 -5
- package/dist/plugins/snapshot/SnapshotHandler.js.map +1 -1
- package/dist/plugins/snapshot/index.cjs +31 -21
- package/dist/plugins/snapshot/index.cjs.map +1 -1
- package/dist/plugins/snapshot/index.js +31 -21
- package/dist/plugins/snapshot/index.js.map +1 -1
- package/dist/remote/index.cjs +1 -1
- package/dist/remote/index.cjs.map +1 -1
- package/dist/remote/index.d.ts +1 -0
- package/dist/remote/index.js +1 -1
- package/dist/remote/index.js.map +1 -1
- package/dist/shared/index.cjs +86 -27
- package/dist/shared/index.cjs.map +1 -1
- package/dist/shared/index.d.ts +19 -10
- package/dist/shared/index.js +86 -27
- package/dist/shared/index.js.map +1 -1
- package/dist/type/config.d.ts +9 -1
- package/dist/type/index.d.ts +3 -3
- package/dist/type/plugin.d.ts +4 -3
- package/dist/type/preload.d.ts +7 -1
- package/dist/types.d.ts +3 -3
- package/dist/utils/hooks/pluginSystem.cjs +6 -5
- package/dist/utils/hooks/pluginSystem.cjs.map +1 -1
- package/dist/utils/hooks/pluginSystem.d.ts +2 -5
- package/dist/utils/hooks/pluginSystem.js +6 -5
- package/dist/utils/hooks/pluginSystem.js.map +1 -1
- package/dist/utils/load.cjs +48 -14
- package/dist/utils/load.cjs.map +1 -1
- package/dist/utils/load.js +48 -14
- package/dist/utils/load.js.map +1 -1
- package/dist/utils/plugin.cjs +30 -7
- package/dist/utils/plugin.cjs.map +1 -1
- package/dist/utils/plugin.js +30 -7
- package/dist/utils/plugin.js.map +1 -1
- package/dist/utils/preload.cjs +21 -10
- package/dist/utils/preload.cjs.map +1 -1
- package/dist/utils/preload.d.ts +1 -1
- package/dist/utils/preload.js +21 -10
- package/dist/utils/preload.js.map +1 -1
- package/dist/utils/share.cjs +3 -2
- package/dist/utils/share.cjs.map +1 -1
- package/dist/utils/share.d.ts +3 -2
- package/dist/utils/share.js +3 -2
- package/dist/utils/share.js.map +1 -1
- package/package.json +3 -3
- package/dist/utils/hooks/index.d.ts +0 -5
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["helpersDefault","RemoteModule"],"sources":["../src/index.ts"],"sourcesContent":["import helpersDefault, { type IGlobalUtils, type IShareUtils } from './helpers';\nimport { Module as RemoteModule } from './module';\nimport { UnavailableRemoteModule } from './remote/disabled';\n\ndeclare const FEDERATION_OPTIMIZE_NO_REMOTE: boolean;\n\nconst helpers = helpersDefault;\nconst Module = (\n typeof FEDERATION_OPTIMIZE_NO_REMOTE === 'boolean' &&\n FEDERATION_OPTIMIZE_NO_REMOTE\n ? UnavailableRemoteModule\n : RemoteModule\n) as typeof RemoteModule;\n\nexport { ModuleFederation } from './core';\nexport {\n type Federation,\n CurrentGlobal,\n Global,\n getGlobalFederationConstructor,\n setGlobalFederationInstance,\n setGlobalFederationConstructor,\n resetFederationGlobalInfo,\n addGlobalSnapshot,\n getGlobalSnapshot,\n getInfoWithoutType,\n} from './global';\nexport type {
|
|
1
|
+
{"version":3,"file":"index.js","names":["helpersDefault","RemoteModule"],"sources":["../src/index.ts"],"sourcesContent":["import helpersDefault, { type IGlobalUtils, type IShareUtils } from './helpers';\nimport { Module as RemoteModule } from './module';\nimport { UnavailableRemoteModule } from './remote/disabled';\n\ndeclare const FEDERATION_OPTIMIZE_NO_REMOTE: boolean;\n\nconst helpers = helpersDefault;\nconst Module = (\n typeof FEDERATION_OPTIMIZE_NO_REMOTE === 'boolean' &&\n FEDERATION_OPTIMIZE_NO_REMOTE\n ? UnavailableRemoteModule\n : RemoteModule\n) as typeof RemoteModule;\n\nexport { ModuleFederation } from './core';\nexport {\n type Federation,\n CurrentGlobal,\n Global,\n getGlobalFederationConstructor,\n setGlobalFederationInstance,\n setGlobalFederationConstructor,\n resetFederationGlobalInfo,\n addGlobalSnapshot,\n getGlobalSnapshot,\n getInfoWithoutType,\n} from './global';\nexport type {\n UserOptions,\n ModuleFederationRuntimePlugin,\n RuntimePluginHooks,\n} from './type';\nexport { assert, error } from './utils/logger';\nexport { registerGlobalPlugins } from './global';\nexport {\n getRemoteEntry,\n getRemoteInfo,\n isStaticResourcesEqual,\n matchRemoteWithNameAndExpose,\n safeWrapper,\n} from './utils';\nexport { getRegisteredShare } from '../src/utils/share';\nexport { loadScript, loadScriptNode } from '@module-federation/sdk';\nexport { Module };\nexport * as types from './type';\nexport { helpers };\nexport { satisfy } from '../src/utils/semver';\nexport type { IGlobalUtils, IShareUtils };\n"],"mappings":";;;;;;;;;;;;;;;;AAMA,MAAM,UAAUA;AAChB,MAAM,SACJ,OAAO,kCAAkC,aACzC,gCACI,0BACAC"}
|
package/dist/module/index.cjs
CHANGED
|
@@ -42,24 +42,26 @@ var Module = class {
|
|
|
42
42
|
this.remoteInfo = remoteInfo;
|
|
43
43
|
this.host = host;
|
|
44
44
|
}
|
|
45
|
-
async getEntry(expose) {
|
|
46
|
-
|
|
45
|
+
async getEntry(expose, resourceContext) {
|
|
46
|
+
const effectiveResourceContext = resourceContext || {
|
|
47
|
+
initiator: "loadRemote",
|
|
48
|
+
id: require_manifest.composeRemoteRequestId(this.remoteInfo.name, expose),
|
|
49
|
+
resourceType: "remoteEntry",
|
|
50
|
+
url: this.remoteInfo.entry,
|
|
51
|
+
expose
|
|
52
|
+
};
|
|
47
53
|
const remoteEntryExports = await require_load.getRemoteEntry({
|
|
48
54
|
origin: this.host,
|
|
49
55
|
remoteInfo: this.remoteInfo,
|
|
50
56
|
remoteEntryExports: this.remoteEntryExports,
|
|
51
|
-
resourceContext:
|
|
52
|
-
initiator: "loadRemote",
|
|
53
|
-
id: require_manifest.composeRemoteRequestId(this.remoteInfo.name, expose),
|
|
54
|
-
resourceType: "remoteEntry"
|
|
55
|
-
}
|
|
57
|
+
resourceContext: effectiveResourceContext
|
|
56
58
|
});
|
|
57
59
|
require_logger.assert(remoteEntryExports, `remoteEntryExports is undefined \n ${(0, _module_federation_sdk.safeToString)(this.remoteInfo)}`);
|
|
58
60
|
this.remoteEntryExports = remoteEntryExports;
|
|
59
61
|
return this.remoteEntryExports;
|
|
60
62
|
}
|
|
61
|
-
async init(id, remoteSnapshot, rawInitScope, expose) {
|
|
62
|
-
const remoteEntryExports = await this.getEntry(expose);
|
|
63
|
+
async init(id, remoteSnapshot, rawInitScope, expose, resourceContext) {
|
|
64
|
+
const remoteEntryExports = await this.getEntry(expose, resourceContext);
|
|
63
65
|
if (this.inited) {
|
|
64
66
|
await this.host.loaderHook.lifecycle.afterInitRemote.emit({
|
|
65
67
|
id,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["getRemoteEntry","composeRemoteRequestId","RUNTIME_002","runtimeDescMap","optionsToMFContext","RUNTIME_015","RUNTIME_014","processModuleAlias"],"sources":["../../src/module/index.ts"],"sourcesContent":["import {\n assert,\n error,\n processModuleAlias,\n optionsToMFContext,\n composeRemoteRequestId,\n} from '../utils';\nimport { safeToString, ModuleInfo } from '@module-federation/sdk';\nimport {\n RUNTIME_002,\n RUNTIME_014,\n RUNTIME_015,\n runtimeDescMap,\n} from '@module-federation/error-codes';\nimport { getRemoteEntry } from '../utils/load';\nimport { ModuleFederation } from '../core';\nimport {\n RemoteEntryExports,\n RemoteInfo,\n InitScope,\n ShareScopeMap,\n} from '../type';\n\nexport type ModuleOptions = ConstructorParameters<typeof Module>[0];\nexport type RemoteModuleFactory = () => unknown | Promise<unknown>;\n\nfunction getAvailableExposeNames(\n remoteSnapshot?: ModuleInfo,\n): string | undefined {\n if (\n !remoteSnapshot ||\n !('modules' in remoteSnapshot) ||\n !Array.isArray(remoteSnapshot.modules)\n ) {\n return undefined;\n }\n\n const exposes = remoteSnapshot.modules\n .map((module) => module.moduleName)\n .filter(Boolean);\n\n return exposes.length ? exposes.join(',') : undefined;\n}\n\nexport function createRemoteEntryInitOptions(\n remoteInfo: RemoteInfo,\n hostShareScopeMap: ShareScopeMap,\n rawInitScope?: InitScope,\n): Record<string, any> {\n const localShareScopeMap = hostShareScopeMap;\n\n const shareScopeKeys = Array.isArray(remoteInfo.shareScope)\n ? remoteInfo.shareScope\n : [remoteInfo.shareScope];\n\n if (!shareScopeKeys.length) {\n shareScopeKeys.push('default');\n }\n shareScopeKeys.forEach((shareScopeKey) => {\n if (!localShareScopeMap[shareScopeKey]) {\n localShareScopeMap[shareScopeKey] = {};\n }\n });\n\n const remoteEntryInitOptions = {\n version: remoteInfo.version || '',\n shareScopeKeys: Array.isArray(remoteInfo.shareScope)\n ? shareScopeKeys\n : remoteInfo.shareScope || 'default',\n };\n\n // Help to find host instance\n Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {\n value: localShareScopeMap,\n // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed\n enumerable: false,\n });\n\n // TODO: compate legacy init params, should use shareScopeMap if exist\n const shareScope = localShareScopeMap[shareScopeKeys[0]];\n const initScope: InitScope = rawInitScope ?? [];\n\n return {\n remoteEntryInitOptions,\n shareScope,\n initScope,\n };\n}\n\nclass Module {\n remoteInfo: RemoteInfo;\n inited = false;\n initing = false;\n initPromise?: Promise<void>;\n remoteEntryExports?: RemoteEntryExports;\n lib: RemoteEntryExports | undefined = undefined;\n host: ModuleFederation;\n\n constructor({\n remoteInfo,\n host,\n }: {\n remoteInfo: RemoteInfo;\n host: ModuleFederation;\n }) {\n this.remoteInfo = remoteInfo;\n this.host = host;\n }\n\n async getEntry(expose?: string): Promise<RemoteEntryExports> {\n if (this.remoteEntryExports) {\n return this.remoteEntryExports;\n }\n\n const remoteEntryExports = await getRemoteEntry({\n origin: this.host,\n remoteInfo: this.remoteInfo,\n remoteEntryExports: this.remoteEntryExports,\n resourceContext: {\n initiator: 'loadRemote',\n id: composeRemoteRequestId(this.remoteInfo.name, expose),\n resourceType: 'remoteEntry',\n },\n });\n\n assert(\n remoteEntryExports,\n `remoteEntryExports is undefined \\n ${safeToString(this.remoteInfo)}`,\n );\n\n this.remoteEntryExports = remoteEntryExports;\n return this.remoteEntryExports;\n }\n\n // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\n\n async init(\n id?: string,\n remoteSnapshot?: ModuleInfo,\n rawInitScope?: InitScope,\n expose?: string,\n ) {\n // Get remoteEntry.js\n const remoteEntryExports = await this.getEntry(expose);\n\n if (this.inited) {\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n cached: true,\n origin: this.host,\n });\n return remoteEntryExports;\n }\n\n if (this.initPromise) {\n try {\n await this.initPromise;\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n cached: true,\n origin: this.host,\n });\n } catch (initError) {\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n error: initError,\n cached: true,\n origin: this.host,\n });\n throw initError;\n }\n return remoteEntryExports;\n }\n\n this.initing = true;\n this.initPromise = (async () => {\n await this.host.loaderHook.lifecycle.beforeInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n origin: this.host,\n });\n\n const { remoteEntryInitOptions, shareScope, initScope } =\n createRemoteEntryInitOptions(\n this.remoteInfo,\n this.host.shareScopeMap,\n rawInitScope,\n );\n\n const initContainerOptions =\n await this.host.hooks.lifecycle.beforeInitContainer.emit({\n shareScope,\n // @ts-ignore shareScopeMap will be set by Object.defineProperty\n remoteEntryInitOptions,\n initScope,\n remoteInfo: this.remoteInfo,\n origin: this.host,\n });\n\n if (typeof remoteEntryExports?.init === 'undefined') {\n error(\n RUNTIME_002,\n runtimeDescMap,\n {\n hostName: this.host.name,\n remoteName: this.remoteInfo.name,\n remoteEntryUrl: this.remoteInfo.entry,\n remoteEntryKey: this.remoteInfo.entryGlobalName,\n },\n undefined,\n optionsToMFContext(this.host.options),\n );\n }\n\n try {\n await remoteEntryExports.init(\n initContainerOptions.shareScope,\n initContainerOptions.initScope,\n initContainerOptions.remoteEntryInitOptions,\n );\n } catch (initError) {\n error(\n RUNTIME_015,\n runtimeDescMap,\n {\n hostName: this.host.name,\n remoteName: this.remoteInfo.name,\n remoteEntryUrl: this.remoteInfo.entry,\n remoteEntryKey: this.remoteInfo.entryGlobalName,\n shareScope: this.remoteInfo.shareScope,\n },\n `${initError}`,\n optionsToMFContext(this.host.options),\n );\n }\n\n await this.host.hooks.lifecycle.initContainer.emit({\n ...initContainerOptions,\n id,\n remoteSnapshot,\n remoteEntryExports,\n });\n this.inited = true;\n })();\n\n try {\n await this.initPromise;\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n origin: this.host,\n });\n } catch (initError) {\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n error: initError,\n origin: this.host,\n });\n throw initError;\n } finally {\n this.initing = false;\n this.initPromise = undefined;\n }\n\n return remoteEntryExports;\n }\n\n // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\n async get(\n id: string,\n expose: string,\n options?: { loadFactory?: boolean },\n remoteSnapshot?: ModuleInfo,\n ) {\n const { loadFactory = true } = options || { loadFactory: true };\n\n const remoteEntryExports = await this.init(\n id,\n remoteSnapshot,\n undefined,\n expose,\n );\n this.lib = remoteEntryExports;\n\n await this.host.loaderHook.lifecycle.beforeGetExpose.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n remoteEntryExports,\n origin: this.host,\n });\n\n let moduleFactory: RemoteModuleFactory | undefined;\n try {\n const hookModuleFactory =\n await this.host.loaderHook.lifecycle.getModuleFactory.emit({\n remoteEntryExports,\n expose,\n moduleInfo: this.remoteInfo,\n });\n moduleFactory =\n typeof hookModuleFactory === 'function' ? hookModuleFactory : undefined;\n\n // get exposeGetter\n if (!moduleFactory) {\n moduleFactory = await remoteEntryExports.get(expose);\n }\n\n if (!moduleFactory) {\n error(\n RUNTIME_014,\n runtimeDescMap,\n {\n hostName: this.host.name,\n remoteName: this.remoteInfo.name,\n remoteEntryUrl: this.remoteInfo.entry,\n expose,\n requestId: id,\n availableExposes: getAvailableExposeNames(remoteSnapshot),\n },\n undefined,\n optionsToMFContext(this.host.options),\n );\n }\n\n await this.host.loaderHook.lifecycle.afterGetExpose.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n remoteEntryExports,\n moduleFactory,\n origin: this.host,\n });\n } catch (getExposeError) {\n await this.host.loaderHook.lifecycle.afterGetExpose.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n remoteEntryExports,\n error: getExposeError,\n origin: this.host,\n });\n throw getExposeError;\n }\n\n // keep symbol for module name always one format\n const symbolName = processModuleAlias(this.remoteInfo.name, expose);\n const wrapModuleFactory = this.wraperFactory(moduleFactory, symbolName);\n\n if (!loadFactory) {\n return wrapModuleFactory;\n }\n\n await this.host.loaderHook.lifecycle.beforeExecuteFactory.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n loadFactory,\n origin: this.host,\n });\n\n try {\n const exposeContent = await wrapModuleFactory();\n\n await this.host.loaderHook.lifecycle.afterExecuteFactory.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n loadFactory,\n exposeModule: exposeContent,\n origin: this.host,\n });\n\n return exposeContent;\n } catch (executeFactoryError) {\n await this.host.loaderHook.lifecycle.afterExecuteFactory.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n loadFactory,\n error: executeFactoryError,\n origin: this.host,\n });\n throw executeFactoryError;\n }\n }\n\n private wraperFactory(moduleFactory: RemoteModuleFactory, id: string) {\n function defineModuleId(res: unknown, id: string) {\n if (\n res &&\n typeof res === 'object' &&\n Object.isExtensible(res) &&\n !Object.getOwnPropertyDescriptor(res, Symbol.for('mf_module_id'))\n ) {\n Object.defineProperty(res, Symbol.for('mf_module_id'), {\n value: id,\n enumerable: false,\n });\n }\n }\n\n return () => {\n const res = moduleFactory();\n\n if (res instanceof Promise) {\n return res.then((asyncRes) => {\n // This parameter is used for bridge debugging\n defineModuleId(asyncRes, id);\n return asyncRes;\n });\n }\n\n // This parameter is used for bridge debugging\n defineModuleId(res, id);\n return res;\n };\n }\n}\n\nexport { Module };\n"],"mappings":";;;;;;;;;;AA0BA,SAAS,wBACP,gBACoB;AACpB,KACE,CAAC,kBACD,EAAE,aAAa,mBACf,CAAC,MAAM,QAAQ,eAAe,QAAQ,CAEtC;CAGF,MAAM,UAAU,eAAe,QAC5B,KAAK,WAAW,OAAO,WAAW,CAClC,OAAO,QAAQ;AAElB,QAAO,QAAQ,SAAS,QAAQ,KAAK,IAAI,GAAG;;AAG9C,SAAgB,6BACd,YACA,mBACA,cACqB;CACrB,MAAM,qBAAqB;CAE3B,MAAM,iBAAiB,MAAM,QAAQ,WAAW,WAAW,GACvD,WAAW,aACX,CAAC,WAAW,WAAW;AAE3B,KAAI,CAAC,eAAe,OAClB,gBAAe,KAAK,UAAU;AAEhC,g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EAIR,MAAM,aAAaG,gCAAmB,KAAK,WAAW,MAAM,OAAO;EACnE,MAAM,oBAAoB,KAAK,cAAc,eAAe,WAAW;AAEvE,MAAI,CAAC,YACH,QAAO;AAGT,QAAM,KAAK,KAAK,WAAW,UAAU,qBAAqB,KAAK;GAC7D;GACA;GACA,YAAY,KAAK;GACjB;GACA,QAAQ,KAAK;GACd,CAAC;AAEF,MAAI;GACF,MAAM,gBAAgB,MAAM,mBAAmB;AAE/C,SAAM,KAAK,KAAK,WAAW,UAAU,oBAAoB,KAAK;IAC5D;IACA;IACA,YAAY,KAAK;IACjB;IACA,cAAc;IACd,QAAQ,KAAK;IACd,CAAC;AAEF,UAAO;WACA,qBAAqB;AAC5B,SAAM,KAAK,KAAK,WAAW,UAAU,oBAAoB,KAAK;IAC5D;IACA;IACA,YAAY,KAAK;IACjB;IACA,OAAO;IACP,QAAQ,KAAK;IACd,CAAC;AACF,SAAM;;;CAIV,AAAQ,cAAc,eAAoC,IAAY;EACpE,SAAS,eAAe,KAAc,IAAY;AAChD,OACE,OACA,OAAO,QAAQ,YACf,OAAO,aAAa,IAAI,IACxB,CAAC,OAAO,yBAAyB,KAAK,OAAO,IAAI,eAAe,CAAC,CAEjE,QAAO,eAAe,KAAK,OAAO,IAAI,eAAe,EAAE;IACrD,OAAO;IACP,YAAY;IACb,CAAC;;AAIN,eAAa;GACX,MAAM,MAAM,eAAe;AAE3B,OAAI,eAAe,QACjB,QAAO,IAAI,MAAM,aAAa;AAE5B,mBAAe,UAAU,GAAG;AAC5B,WAAO;KACP;AAIJ,kBAAe,KAAK,GAAG;AACvB,UAAO"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["composeRemoteRequestId","getRemoteEntry","RUNTIME_002","runtimeDescMap","optionsToMFContext","RUNTIME_015","RUNTIME_014","processModuleAlias"],"sources":["../../src/module/index.ts"],"sourcesContent":["import {\n assert,\n error,\n processModuleAlias,\n optionsToMFContext,\n composeRemoteRequestId,\n} from '../utils';\nimport { safeToString, ModuleInfo } from '@module-federation/sdk';\nimport {\n RUNTIME_002,\n RUNTIME_014,\n RUNTIME_015,\n runtimeDescMap,\n} from '@module-federation/error-codes';\nimport { getRemoteEntry } from '../utils/load';\nimport { ModuleFederation } from '../core';\nimport {\n RemoteEntryExports,\n RemoteInfo,\n InitScope,\n ShareScopeMap,\n ResourceLoadContext,\n} from '../type';\n\nexport type ModuleOptions = ConstructorParameters<typeof Module>[0];\nexport type RemoteModuleFactory = () => unknown | Promise<unknown>;\n\nfunction getAvailableExposeNames(\n remoteSnapshot?: ModuleInfo,\n): string | undefined {\n if (\n !remoteSnapshot ||\n !('modules' in remoteSnapshot) ||\n !Array.isArray(remoteSnapshot.modules)\n ) {\n return undefined;\n }\n\n const exposes = remoteSnapshot.modules\n .map((module) => module.moduleName)\n .filter(Boolean);\n\n return exposes.length ? exposes.join(',') : undefined;\n}\n\nexport function createRemoteEntryInitOptions(\n remoteInfo: RemoteInfo,\n hostShareScopeMap: ShareScopeMap,\n rawInitScope?: InitScope,\n): Record<string, any> {\n const localShareScopeMap = hostShareScopeMap;\n\n const shareScopeKeys = Array.isArray(remoteInfo.shareScope)\n ? remoteInfo.shareScope\n : [remoteInfo.shareScope];\n\n if (!shareScopeKeys.length) {\n shareScopeKeys.push('default');\n }\n shareScopeKeys.forEach((shareScopeKey) => {\n if (!localShareScopeMap[shareScopeKey]) {\n localShareScopeMap[shareScopeKey] = {};\n }\n });\n\n const remoteEntryInitOptions = {\n version: remoteInfo.version || '',\n shareScopeKeys: Array.isArray(remoteInfo.shareScope)\n ? shareScopeKeys\n : remoteInfo.shareScope || 'default',\n };\n\n // Help to find host instance\n Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {\n value: localShareScopeMap,\n // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed\n enumerable: false,\n });\n\n // TODO: compate legacy init params, should use shareScopeMap if exist\n const shareScope = localShareScopeMap[shareScopeKeys[0]];\n const initScope: InitScope = rawInitScope ?? [];\n\n return {\n remoteEntryInitOptions,\n shareScope,\n initScope,\n };\n}\n\nclass Module {\n remoteInfo: RemoteInfo;\n inited = false;\n initing = false;\n initPromise?: Promise<void>;\n remoteEntryExports?: RemoteEntryExports;\n lib: RemoteEntryExports | undefined = undefined;\n host: ModuleFederation;\n\n constructor({\n remoteInfo,\n host,\n }: {\n remoteInfo: RemoteInfo;\n host: ModuleFederation;\n }) {\n this.remoteInfo = remoteInfo;\n this.host = host;\n }\n\n async getEntry(\n expose?: string,\n resourceContext?: ResourceLoadContext,\n ): Promise<RemoteEntryExports> {\n const effectiveResourceContext = resourceContext || {\n initiator: 'loadRemote',\n id: composeRemoteRequestId(this.remoteInfo.name, expose),\n resourceType: 'remoteEntry' as const,\n url: this.remoteInfo.entry,\n expose,\n };\n const remoteEntryExports = await getRemoteEntry({\n origin: this.host,\n remoteInfo: this.remoteInfo,\n remoteEntryExports: this.remoteEntryExports,\n resourceContext: effectiveResourceContext,\n });\n\n assert(\n remoteEntryExports,\n `remoteEntryExports is undefined \\n ${safeToString(this.remoteInfo)}`,\n );\n\n this.remoteEntryExports = remoteEntryExports;\n return this.remoteEntryExports;\n }\n\n // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\n\n async init(\n id?: string,\n remoteSnapshot?: ModuleInfo,\n rawInitScope?: InitScope,\n expose?: string,\n resourceContext?: ResourceLoadContext,\n ) {\n // Get remoteEntry.js\n const remoteEntryExports = await this.getEntry(expose, resourceContext);\n\n if (this.inited) {\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n cached: true,\n origin: this.host,\n });\n return remoteEntryExports;\n }\n\n if (this.initPromise) {\n try {\n await this.initPromise;\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n cached: true,\n origin: this.host,\n });\n } catch (initError) {\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n error: initError,\n cached: true,\n origin: this.host,\n });\n throw initError;\n }\n return remoteEntryExports;\n }\n\n this.initing = true;\n this.initPromise = (async () => {\n await this.host.loaderHook.lifecycle.beforeInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n origin: this.host,\n });\n\n const { remoteEntryInitOptions, shareScope, initScope } =\n createRemoteEntryInitOptions(\n this.remoteInfo,\n this.host.shareScopeMap,\n rawInitScope,\n );\n\n const initContainerOptions =\n await this.host.hooks.lifecycle.beforeInitContainer.emit({\n shareScope,\n // @ts-ignore shareScopeMap will be set by Object.defineProperty\n remoteEntryInitOptions,\n initScope,\n remoteInfo: this.remoteInfo,\n origin: this.host,\n });\n\n if (typeof remoteEntryExports?.init === 'undefined') {\n error(\n RUNTIME_002,\n runtimeDescMap,\n {\n hostName: this.host.name,\n remoteName: this.remoteInfo.name,\n remoteEntryUrl: this.remoteInfo.entry,\n remoteEntryKey: this.remoteInfo.entryGlobalName,\n },\n undefined,\n optionsToMFContext(this.host.options),\n );\n }\n\n try {\n await remoteEntryExports.init(\n initContainerOptions.shareScope,\n initContainerOptions.initScope,\n initContainerOptions.remoteEntryInitOptions,\n );\n } catch (initError) {\n error(\n RUNTIME_015,\n runtimeDescMap,\n {\n hostName: this.host.name,\n remoteName: this.remoteInfo.name,\n remoteEntryUrl: this.remoteInfo.entry,\n remoteEntryKey: this.remoteInfo.entryGlobalName,\n shareScope: this.remoteInfo.shareScope,\n },\n `${initError}`,\n optionsToMFContext(this.host.options),\n );\n }\n\n await this.host.hooks.lifecycle.initContainer.emit({\n ...initContainerOptions,\n id,\n remoteSnapshot,\n remoteEntryExports,\n });\n this.inited = true;\n })();\n\n try {\n await this.initPromise;\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n origin: this.host,\n });\n } catch (initError) {\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n error: initError,\n origin: this.host,\n });\n throw initError;\n } finally {\n this.initing = false;\n this.initPromise = undefined;\n }\n\n return remoteEntryExports;\n }\n\n // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\n async get(\n id: string,\n expose: string,\n options?: { loadFactory?: boolean },\n remoteSnapshot?: ModuleInfo,\n ) {\n const { loadFactory = true } = options || { loadFactory: true };\n\n const remoteEntryExports = await this.init(\n id,\n remoteSnapshot,\n undefined,\n expose,\n );\n this.lib = remoteEntryExports;\n\n await this.host.loaderHook.lifecycle.beforeGetExpose.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n remoteEntryExports,\n origin: this.host,\n });\n\n let moduleFactory: RemoteModuleFactory | undefined;\n try {\n const hookModuleFactory =\n await this.host.loaderHook.lifecycle.getModuleFactory.emit({\n remoteEntryExports,\n expose,\n moduleInfo: this.remoteInfo,\n });\n moduleFactory =\n typeof hookModuleFactory === 'function' ? hookModuleFactory : undefined;\n\n // get exposeGetter\n if (!moduleFactory) {\n moduleFactory = await remoteEntryExports.get(expose);\n }\n\n if (!moduleFactory) {\n error(\n RUNTIME_014,\n runtimeDescMap,\n {\n hostName: this.host.name,\n remoteName: this.remoteInfo.name,\n remoteEntryUrl: this.remoteInfo.entry,\n expose,\n requestId: id,\n availableExposes: getAvailableExposeNames(remoteSnapshot),\n },\n undefined,\n optionsToMFContext(this.host.options),\n );\n }\n\n await this.host.loaderHook.lifecycle.afterGetExpose.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n remoteEntryExports,\n moduleFactory,\n origin: this.host,\n });\n } catch (getExposeError) {\n await this.host.loaderHook.lifecycle.afterGetExpose.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n remoteEntryExports,\n error: getExposeError,\n origin: this.host,\n });\n throw getExposeError;\n }\n\n // keep symbol for module name always one format\n const symbolName = processModuleAlias(this.remoteInfo.name, expose);\n const wrapModuleFactory = this.wraperFactory(moduleFactory, symbolName);\n\n if (!loadFactory) {\n return wrapModuleFactory;\n }\n\n await this.host.loaderHook.lifecycle.beforeExecuteFactory.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n loadFactory,\n origin: this.host,\n });\n\n try {\n const exposeContent = await wrapModuleFactory();\n\n await this.host.loaderHook.lifecycle.afterExecuteFactory.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n loadFactory,\n exposeModule: exposeContent,\n origin: this.host,\n });\n\n return exposeContent;\n } catch (executeFactoryError) {\n await this.host.loaderHook.lifecycle.afterExecuteFactory.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n loadFactory,\n error: executeFactoryError,\n origin: this.host,\n });\n throw executeFactoryError;\n }\n }\n\n private wraperFactory(moduleFactory: RemoteModuleFactory, id: string) {\n function defineModuleId(res: unknown, id: string) {\n if (\n res &&\n typeof res === 'object' &&\n Object.isExtensible(res) &&\n !Object.getOwnPropertyDescriptor(res, Symbol.for('mf_module_id'))\n ) {\n Object.defineProperty(res, Symbol.for('mf_module_id'), {\n value: id,\n enumerable: false,\n });\n }\n }\n\n return () => {\n const res = moduleFactory();\n\n if (res instanceof Promise) {\n return res.then((asyncRes) => {\n // This parameter is used for bridge debugging\n defineModuleId(asyncRes, id);\n return asyncRes;\n });\n }\n\n // This parameter is used for bridge debugging\n defineModuleId(res, id);\n return res;\n };\n }\n}\n\nexport { Module };\n"],"mappings":";;;;;;;;;;AA2BA,SAAS,wBACP,gBACoB;AACpB,KACE,CAAC,kBACD,EAAE,aAAa,mBACf,CAAC,MAAM,QAAQ,eAAe,QAAQ,CAEtC;CAGF,MAAM,UAAU,eAAe,QAC5B,KAAK,WAAW,OAAO,WAAW,CAClC,OAAO,QAAQ;AAElB,QAAO,QAAQ,SAAS,QAAQ,KAAK,IAAI,GAAG;;AAG9C,SAAgB,6BACd,YACA,mBACA,cACqB;CACrB,MAAM,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5 +1,6 @@
|
|
|
1
1
|
import { ModuleFederation } from "../core.js";
|
|
2
2
|
import { InitScope, RemoteEntryExports, RemoteInfo } from "../type/config.js";
|
|
3
|
+
import { ResourceLoadContext } from "../type/preload.js";
|
|
3
4
|
import { ModuleInfo } from "@module-federation/sdk";
|
|
4
5
|
|
|
5
6
|
//#region src/module/index.d.ts
|
|
@@ -20,8 +21,8 @@ declare class Module$1 {
|
|
|
20
21
|
remoteInfo: RemoteInfo;
|
|
21
22
|
host: ModuleFederation;
|
|
22
23
|
});
|
|
23
|
-
getEntry(expose?: string): Promise<RemoteEntryExports>;
|
|
24
|
-
init(id?: string, remoteSnapshot?: ModuleInfo, rawInitScope?: InitScope, expose?: string): Promise<RemoteEntryExports>;
|
|
24
|
+
getEntry(expose?: string, resourceContext?: ResourceLoadContext): Promise<RemoteEntryExports>;
|
|
25
|
+
init(id?: string, remoteSnapshot?: ModuleInfo, rawInitScope?: InitScope, expose?: string, resourceContext?: ResourceLoadContext): Promise<RemoteEntryExports>;
|
|
25
26
|
get(id: string, expose: string, options?: {
|
|
26
27
|
loadFactory?: boolean;
|
|
27
28
|
}, remoteSnapshot?: ModuleInfo): Promise<unknown>;
|
package/dist/module/index.js
CHANGED
|
@@ -42,24 +42,26 @@ var Module$1 = class {
|
|
|
42
42
|
this.remoteInfo = remoteInfo;
|
|
43
43
|
this.host = host;
|
|
44
44
|
}
|
|
45
|
-
async getEntry(expose) {
|
|
46
|
-
|
|
45
|
+
async getEntry(expose, resourceContext) {
|
|
46
|
+
const effectiveResourceContext = resourceContext || {
|
|
47
|
+
initiator: "loadRemote",
|
|
48
|
+
id: composeRemoteRequestId(this.remoteInfo.name, expose),
|
|
49
|
+
resourceType: "remoteEntry",
|
|
50
|
+
url: this.remoteInfo.entry,
|
|
51
|
+
expose
|
|
52
|
+
};
|
|
47
53
|
const remoteEntryExports = await getRemoteEntry({
|
|
48
54
|
origin: this.host,
|
|
49
55
|
remoteInfo: this.remoteInfo,
|
|
50
56
|
remoteEntryExports: this.remoteEntryExports,
|
|
51
|
-
resourceContext:
|
|
52
|
-
initiator: "loadRemote",
|
|
53
|
-
id: composeRemoteRequestId(this.remoteInfo.name, expose),
|
|
54
|
-
resourceType: "remoteEntry"
|
|
55
|
-
}
|
|
57
|
+
resourceContext: effectiveResourceContext
|
|
56
58
|
});
|
|
57
59
|
assert(remoteEntryExports, `remoteEntryExports is undefined \n ${safeToString(this.remoteInfo)}`);
|
|
58
60
|
this.remoteEntryExports = remoteEntryExports;
|
|
59
61
|
return this.remoteEntryExports;
|
|
60
62
|
}
|
|
61
|
-
async init(id, remoteSnapshot, rawInitScope, expose) {
|
|
62
|
-
const remoteEntryExports = await this.getEntry(expose);
|
|
63
|
+
async init(id, remoteSnapshot, rawInitScope, expose, resourceContext) {
|
|
64
|
+
const remoteEntryExports = await this.getEntry(expose, resourceContext);
|
|
63
65
|
if (this.inited) {
|
|
64
66
|
await this.host.loaderHook.lifecycle.afterInitRemote.emit({
|
|
65
67
|
id,
|
package/dist/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Module"],"sources":["../../src/module/index.ts"],"sourcesContent":["import {\n assert,\n error,\n processModuleAlias,\n optionsToMFContext,\n composeRemoteRequestId,\n} from '../utils';\nimport { safeToString, ModuleInfo } from '@module-federation/sdk';\nimport {\n RUNTIME_002,\n RUNTIME_014,\n RUNTIME_015,\n runtimeDescMap,\n} from '@module-federation/error-codes';\nimport { getRemoteEntry } from '../utils/load';\nimport { ModuleFederation } from '../core';\nimport {\n RemoteEntryExports,\n RemoteInfo,\n InitScope,\n ShareScopeMap,\n} from '../type';\n\nexport type ModuleOptions = ConstructorParameters<typeof Module>[0];\nexport type RemoteModuleFactory = () => unknown | Promise<unknown>;\n\nfunction getAvailableExposeNames(\n remoteSnapshot?: ModuleInfo,\n): string | undefined {\n if (\n !remoteSnapshot ||\n !('modules' in remoteSnapshot) ||\n !Array.isArray(remoteSnapshot.modules)\n ) {\n return undefined;\n }\n\n const exposes = remoteSnapshot.modules\n .map((module) => module.moduleName)\n .filter(Boolean);\n\n return exposes.length ? exposes.join(',') : undefined;\n}\n\nexport function createRemoteEntryInitOptions(\n remoteInfo: RemoteInfo,\n hostShareScopeMap: ShareScopeMap,\n rawInitScope?: InitScope,\n): Record<string, any> {\n const localShareScopeMap = hostShareScopeMap;\n\n const shareScopeKeys = Array.isArray(remoteInfo.shareScope)\n ? remoteInfo.shareScope\n : [remoteInfo.shareScope];\n\n if (!shareScopeKeys.length) {\n shareScopeKeys.push('default');\n }\n shareScopeKeys.forEach((shareScopeKey) => {\n if (!localShareScopeMap[shareScopeKey]) {\n localShareScopeMap[shareScopeKey] = {};\n }\n });\n\n const remoteEntryInitOptions = {\n version: remoteInfo.version || '',\n shareScopeKeys: Array.isArray(remoteInfo.shareScope)\n ? shareScopeKeys\n : remoteInfo.shareScope || 'default',\n };\n\n // Help to find host instance\n Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {\n value: localShareScopeMap,\n // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed\n enumerable: false,\n });\n\n // TODO: compate legacy init params, should use shareScopeMap if exist\n const shareScope = localShareScopeMap[shareScopeKeys[0]];\n const initScope: InitScope = rawInitScope ?? [];\n\n return {\n remoteEntryInitOptions,\n shareScope,\n initScope,\n };\n}\n\nclass Module {\n remoteInfo: RemoteInfo;\n inited = false;\n initing = false;\n initPromise?: Promise<void>;\n remoteEntryExports?: RemoteEntryExports;\n lib: RemoteEntryExports | undefined = undefined;\n host: ModuleFederation;\n\n constructor({\n remoteInfo,\n host,\n }: {\n remoteInfo: RemoteInfo;\n host: ModuleFederation;\n }) {\n this.remoteInfo = remoteInfo;\n this.host = host;\n }\n\n async getEntry(expose?: string): Promise<RemoteEntryExports> {\n if (this.remoteEntryExports) {\n return this.remoteEntryExports;\n }\n\n const remoteEntryExports = await getRemoteEntry({\n origin: this.host,\n remoteInfo: this.remoteInfo,\n remoteEntryExports: this.remoteEntryExports,\n resourceContext: {\n initiator: 'loadRemote',\n id: composeRemoteRequestId(this.remoteInfo.name, expose),\n resourceType: 'remoteEntry',\n },\n });\n\n assert(\n remoteEntryExports,\n `remoteEntryExports is undefined \\n ${safeToString(this.remoteInfo)}`,\n );\n\n this.remoteEntryExports = remoteEntryExports;\n return this.remoteEntryExports;\n }\n\n // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\n\n async init(\n id?: string,\n remoteSnapshot?: ModuleInfo,\n rawInitScope?: InitScope,\n expose?: string,\n ) {\n // Get remoteEntry.js\n const remoteEntryExports = await this.getEntry(expose);\n\n if (this.inited) {\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n cached: true,\n origin: this.host,\n });\n return remoteEntryExports;\n }\n\n if (this.initPromise) {\n try {\n await this.initPromise;\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n cached: true,\n origin: this.host,\n });\n } catch (initError) {\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n error: initError,\n cached: true,\n origin: this.host,\n });\n throw initError;\n }\n return remoteEntryExports;\n }\n\n this.initing = true;\n this.initPromise = (async () => {\n await this.host.loaderHook.lifecycle.beforeInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n origin: this.host,\n });\n\n const { remoteEntryInitOptions, shareScope, initScope } =\n createRemoteEntryInitOptions(\n this.remoteInfo,\n this.host.shareScopeMap,\n rawInitScope,\n );\n\n const initContainerOptions =\n await this.host.hooks.lifecycle.beforeInitContainer.emit({\n shareScope,\n // @ts-ignore shareScopeMap will be set by Object.defineProperty\n remoteEntryInitOptions,\n initScope,\n remoteInfo: this.remoteInfo,\n origin: this.host,\n });\n\n if (typeof remoteEntryExports?.init === 'undefined') {\n error(\n RUNTIME_002,\n runtimeDescMap,\n {\n hostName: this.host.name,\n remoteName: this.remoteInfo.name,\n remoteEntryUrl: this.remoteInfo.entry,\n remoteEntryKey: this.remoteInfo.entryGlobalName,\n },\n undefined,\n optionsToMFContext(this.host.options),\n );\n }\n\n try {\n await remoteEntryExports.init(\n initContainerOptions.shareScope,\n initContainerOptions.initScope,\n initContainerOptions.remoteEntryInitOptions,\n );\n } catch (initError) {\n error(\n RUNTIME_015,\n runtimeDescMap,\n {\n hostName: this.host.name,\n remoteName: this.remoteInfo.name,\n remoteEntryUrl: this.remoteInfo.entry,\n remoteEntryKey: this.remoteInfo.entryGlobalName,\n shareScope: this.remoteInfo.shareScope,\n },\n `${initError}`,\n optionsToMFContext(this.host.options),\n );\n }\n\n await this.host.hooks.lifecycle.initContainer.emit({\n ...initContainerOptions,\n id,\n remoteSnapshot,\n remoteEntryExports,\n });\n this.inited = true;\n })();\n\n try {\n await this.initPromise;\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n origin: this.host,\n });\n } catch (initError) {\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n error: initError,\n origin: this.host,\n });\n throw initError;\n } finally {\n this.initing = false;\n this.initPromise = undefined;\n }\n\n return remoteEntryExports;\n }\n\n // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\n async get(\n id: string,\n expose: string,\n options?: { loadFactory?: boolean },\n remoteSnapshot?: ModuleInfo,\n ) {\n const { loadFactory = true } = options || { loadFactory: true };\n\n const remoteEntryExports = await this.init(\n id,\n remoteSnapshot,\n undefined,\n expose,\n );\n this.lib = remoteEntryExports;\n\n await this.host.loaderHook.lifecycle.beforeGetExpose.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n remoteEntryExports,\n origin: this.host,\n });\n\n let moduleFactory: RemoteModuleFactory | undefined;\n try {\n const hookModuleFactory =\n await this.host.loaderHook.lifecycle.getModuleFactory.emit({\n remoteEntryExports,\n expose,\n moduleInfo: this.remoteInfo,\n });\n moduleFactory =\n typeof hookModuleFactory === 'function' ? hookModuleFactory : undefined;\n\n // get exposeGetter\n if (!moduleFactory) {\n moduleFactory = await remoteEntryExports.get(expose);\n }\n\n if (!moduleFactory) {\n error(\n RUNTIME_014,\n runtimeDescMap,\n {\n hostName: this.host.name,\n remoteName: this.remoteInfo.name,\n remoteEntryUrl: this.remoteInfo.entry,\n expose,\n requestId: id,\n availableExposes: getAvailableExposeNames(remoteSnapshot),\n },\n undefined,\n optionsToMFContext(this.host.options),\n );\n }\n\n await this.host.loaderHook.lifecycle.afterGetExpose.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n remoteEntryExports,\n moduleFactory,\n origin: this.host,\n });\n } catch (getExposeError) {\n await this.host.loaderHook.lifecycle.afterGetExpose.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n remoteEntryExports,\n error: getExposeError,\n origin: this.host,\n });\n throw getExposeError;\n }\n\n // keep symbol for module name always one format\n const symbolName = processModuleAlias(this.remoteInfo.name, expose);\n const wrapModuleFactory = this.wraperFactory(moduleFactory, symbolName);\n\n if (!loadFactory) {\n return wrapModuleFactory;\n }\n\n await this.host.loaderHook.lifecycle.beforeExecuteFactory.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n loadFactory,\n origin: this.host,\n });\n\n try {\n const exposeContent = await wrapModuleFactory();\n\n await this.host.loaderHook.lifecycle.afterExecuteFactory.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n loadFactory,\n exposeModule: exposeContent,\n origin: this.host,\n });\n\n return exposeContent;\n } catch (executeFactoryError) {\n await this.host.loaderHook.lifecycle.afterExecuteFactory.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n loadFactory,\n error: executeFactoryError,\n origin: this.host,\n });\n throw executeFactoryError;\n }\n }\n\n private wraperFactory(moduleFactory: RemoteModuleFactory, id: string) {\n function defineModuleId(res: unknown, id: string) {\n if (\n res &&\n typeof res === 'object' &&\n Object.isExtensible(res) &&\n !Object.getOwnPropertyDescriptor(res, Symbol.for('mf_module_id'))\n ) {\n Object.defineProperty(res, Symbol.for('mf_module_id'), {\n value: id,\n enumerable: false,\n });\n }\n }\n\n return () => {\n const res = moduleFactory();\n\n if (res instanceof Promise) {\n return res.then((asyncRes) => {\n // This parameter is used for bridge debugging\n defineModuleId(asyncRes, id);\n return asyncRes;\n });\n }\n\n // This parameter is used for bridge debugging\n defineModuleId(res, id);\n return res;\n };\n }\n}\n\nexport { Module };\n"],"mappings":";;;;;;;;;;AA0BA,SAAS,wBACP,gBACoB;AACpB,KACE,CAAC,kBACD,EAAE,aAAa,mBACf,CAAC,MAAM,QAAQ,eAAe,QAAQ,CAEtC;CAGF,MAAM,UAAU,eAAe,QAC5B,KAAK,WAAW,OAAO,WAAW,CAClC,OAAO,QAAQ;AAElB,QAAO,QAAQ,SAAS,QAAQ,KAAK,IAAI,GAAG;;AAG9C,SAAgB,6BACd,YACA,mBACA,cACqB;CACrB,MAAM,qBAAqB;CAE3B,MAAM,iBAAiB,MAAM,QAAQ,WAAW,WAAW,GACvD,WAAW,aACX,CAAC,WAAW,WAAW;AAE3B,KAAI,CAAC,eAAe,OAClB,gBAAe,KAAK,UAAU;AAEhC,gBAAe,SAAS,kBAAkB;AACxC,MAAI,CAAC,mBAAmB,eACtB,oBAAmB,iBAAiB,EAAE;GAExC;CAEF,MAAM,yBAAyB;EAC7B,SAAS,WAAW,WAAW;EAC/B,gBAAgB,MAAM,QAAQ,WAAW,WAAW,GAChD,iBACA,WAAW,cAAc;EAC9B;AAGD,QAAO,eAAe,wBAAwB,iBAAiB;EAC7D,OAAO;EAEP,YAAY;EACb,CAAC;AAMF,QAAO;EACL;EACA,YALiB,mBAAmB,eAAe;EAMnD,WAL2B,gBAAgB,EAAE;EAM9C;;AAGH,IAAMA,WAAN,MAA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AAQ,KAAK;IACd,CAAC;AAEF,UAAO;WACA,qBAAqB;AAC5B,SAAM,KAAK,KAAK,WAAW,UAAU,oBAAoB,KAAK;IAC5D;IACA;IACA,YAAY,KAAK;IACjB;IACA,OAAO;IACP,QAAQ,KAAK;IACd,CAAC;AACF,SAAM;;;CAIV,AAAQ,cAAc,eAAoC,IAAY;EACpE,SAAS,eAAe,KAAc,IAAY;AAChD,OACE,OACA,OAAO,QAAQ,YACf,OAAO,aAAa,IAAI,IACxB,CAAC,OAAO,yBAAyB,KAAK,OAAO,IAAI,eAAe,CAAC,CAEjE,QAAO,eAAe,KAAK,OAAO,IAAI,eAAe,EAAE;IACrD,OAAO;IACP,YAAY;IACb,CAAC;;AAIN,eAAa;GACX,MAAM,MAAM,eAAe;AAE3B,OAAI,eAAe,QACjB,QAAO,IAAI,MAAM,aAAa;AAE5B,mBAAe,UAAU,GAAG;AAC5B,WAAO;KACP;AAIJ,kBAAe,KAAK,GAAG;AACvB,UAAO"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["Module"],"sources":["../../src/module/index.ts"],"sourcesContent":["import {\n assert,\n error,\n processModuleAlias,\n optionsToMFContext,\n composeRemoteRequestId,\n} from '../utils';\nimport { safeToString, ModuleInfo } from '@module-federation/sdk';\nimport {\n RUNTIME_002,\n RUNTIME_014,\n RUNTIME_015,\n runtimeDescMap,\n} from '@module-federation/error-codes';\nimport { getRemoteEntry } from '../utils/load';\nimport { ModuleFederation } from '../core';\nimport {\n RemoteEntryExports,\n RemoteInfo,\n InitScope,\n ShareScopeMap,\n ResourceLoadContext,\n} from '../type';\n\nexport type ModuleOptions = ConstructorParameters<typeof Module>[0];\nexport type RemoteModuleFactory = () => unknown | Promise<unknown>;\n\nfunction getAvailableExposeNames(\n remoteSnapshot?: ModuleInfo,\n): string | undefined {\n if (\n !remoteSnapshot ||\n !('modules' in remoteSnapshot) ||\n !Array.isArray(remoteSnapshot.modules)\n ) {\n return undefined;\n }\n\n const exposes = remoteSnapshot.modules\n .map((module) => module.moduleName)\n .filter(Boolean);\n\n return exposes.length ? exposes.join(',') : undefined;\n}\n\nexport function createRemoteEntryInitOptions(\n remoteInfo: RemoteInfo,\n hostShareScopeMap: ShareScopeMap,\n rawInitScope?: InitScope,\n): Record<string, any> {\n const localShareScopeMap = hostShareScopeMap;\n\n const shareScopeKeys = Array.isArray(remoteInfo.shareScope)\n ? remoteInfo.shareScope\n : [remoteInfo.shareScope];\n\n if (!shareScopeKeys.length) {\n shareScopeKeys.push('default');\n }\n shareScopeKeys.forEach((shareScopeKey) => {\n if (!localShareScopeMap[shareScopeKey]) {\n localShareScopeMap[shareScopeKey] = {};\n }\n });\n\n const remoteEntryInitOptions = {\n version: remoteInfo.version || '',\n shareScopeKeys: Array.isArray(remoteInfo.shareScope)\n ? shareScopeKeys\n : remoteInfo.shareScope || 'default',\n };\n\n // Help to find host instance\n Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {\n value: localShareScopeMap,\n // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed\n enumerable: false,\n });\n\n // TODO: compate legacy init params, should use shareScopeMap if exist\n const shareScope = localShareScopeMap[shareScopeKeys[0]];\n const initScope: InitScope = rawInitScope ?? [];\n\n return {\n remoteEntryInitOptions,\n shareScope,\n initScope,\n };\n}\n\nclass Module {\n remoteInfo: RemoteInfo;\n inited = false;\n initing = false;\n initPromise?: Promise<void>;\n remoteEntryExports?: RemoteEntryExports;\n lib: RemoteEntryExports | undefined = undefined;\n host: ModuleFederation;\n\n constructor({\n remoteInfo,\n host,\n }: {\n remoteInfo: RemoteInfo;\n host: ModuleFederation;\n }) {\n this.remoteInfo = remoteInfo;\n this.host = host;\n }\n\n async getEntry(\n expose?: string,\n resourceContext?: ResourceLoadContext,\n ): Promise<RemoteEntryExports> {\n const effectiveResourceContext = resourceContext || {\n initiator: 'loadRemote',\n id: composeRemoteRequestId(this.remoteInfo.name, expose),\n resourceType: 'remoteEntry' as const,\n url: this.remoteInfo.entry,\n expose,\n };\n const remoteEntryExports = await getRemoteEntry({\n origin: this.host,\n remoteInfo: this.remoteInfo,\n remoteEntryExports: this.remoteEntryExports,\n resourceContext: effectiveResourceContext,\n });\n\n assert(\n remoteEntryExports,\n `remoteEntryExports is undefined \\n ${safeToString(this.remoteInfo)}`,\n );\n\n this.remoteEntryExports = remoteEntryExports;\n return this.remoteEntryExports;\n }\n\n // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\n\n async init(\n id?: string,\n remoteSnapshot?: ModuleInfo,\n rawInitScope?: InitScope,\n expose?: string,\n resourceContext?: ResourceLoadContext,\n ) {\n // Get remoteEntry.js\n const remoteEntryExports = await this.getEntry(expose, resourceContext);\n\n if (this.inited) {\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n cached: true,\n origin: this.host,\n });\n return remoteEntryExports;\n }\n\n if (this.initPromise) {\n try {\n await this.initPromise;\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n cached: true,\n origin: this.host,\n });\n } catch (initError) {\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n error: initError,\n cached: true,\n origin: this.host,\n });\n throw initError;\n }\n return remoteEntryExports;\n }\n\n this.initing = true;\n this.initPromise = (async () => {\n await this.host.loaderHook.lifecycle.beforeInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n origin: this.host,\n });\n\n const { remoteEntryInitOptions, shareScope, initScope } =\n createRemoteEntryInitOptions(\n this.remoteInfo,\n this.host.shareScopeMap,\n rawInitScope,\n );\n\n const initContainerOptions =\n await this.host.hooks.lifecycle.beforeInitContainer.emit({\n shareScope,\n // @ts-ignore shareScopeMap will be set by Object.defineProperty\n remoteEntryInitOptions,\n initScope,\n remoteInfo: this.remoteInfo,\n origin: this.host,\n });\n\n if (typeof remoteEntryExports?.init === 'undefined') {\n error(\n RUNTIME_002,\n runtimeDescMap,\n {\n hostName: this.host.name,\n remoteName: this.remoteInfo.name,\n remoteEntryUrl: this.remoteInfo.entry,\n remoteEntryKey: this.remoteInfo.entryGlobalName,\n },\n undefined,\n optionsToMFContext(this.host.options),\n );\n }\n\n try {\n await remoteEntryExports.init(\n initContainerOptions.shareScope,\n initContainerOptions.initScope,\n initContainerOptions.remoteEntryInitOptions,\n );\n } catch (initError) {\n error(\n RUNTIME_015,\n runtimeDescMap,\n {\n hostName: this.host.name,\n remoteName: this.remoteInfo.name,\n remoteEntryUrl: this.remoteInfo.entry,\n remoteEntryKey: this.remoteInfo.entryGlobalName,\n shareScope: this.remoteInfo.shareScope,\n },\n `${initError}`,\n optionsToMFContext(this.host.options),\n );\n }\n\n await this.host.hooks.lifecycle.initContainer.emit({\n ...initContainerOptions,\n id,\n remoteSnapshot,\n remoteEntryExports,\n });\n this.inited = true;\n })();\n\n try {\n await this.initPromise;\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n origin: this.host,\n });\n } catch (initError) {\n await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n id,\n remoteInfo: this.remoteInfo,\n remoteSnapshot,\n remoteEntryExports,\n error: initError,\n origin: this.host,\n });\n throw initError;\n } finally {\n this.initing = false;\n this.initPromise = undefined;\n }\n\n return remoteEntryExports;\n }\n\n // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\n async get(\n id: string,\n expose: string,\n options?: { loadFactory?: boolean },\n remoteSnapshot?: ModuleInfo,\n ) {\n const { loadFactory = true } = options || { loadFactory: true };\n\n const remoteEntryExports = await this.init(\n id,\n remoteSnapshot,\n undefined,\n expose,\n );\n this.lib = remoteEntryExports;\n\n await this.host.loaderHook.lifecycle.beforeGetExpose.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n remoteEntryExports,\n origin: this.host,\n });\n\n let moduleFactory: RemoteModuleFactory | undefined;\n try {\n const hookModuleFactory =\n await this.host.loaderHook.lifecycle.getModuleFactory.emit({\n remoteEntryExports,\n expose,\n moduleInfo: this.remoteInfo,\n });\n moduleFactory =\n typeof hookModuleFactory === 'function' ? hookModuleFactory : undefined;\n\n // get exposeGetter\n if (!moduleFactory) {\n moduleFactory = await remoteEntryExports.get(expose);\n }\n\n if (!moduleFactory) {\n error(\n RUNTIME_014,\n runtimeDescMap,\n {\n hostName: this.host.name,\n remoteName: this.remoteInfo.name,\n remoteEntryUrl: this.remoteInfo.entry,\n expose,\n requestId: id,\n availableExposes: getAvailableExposeNames(remoteSnapshot),\n },\n undefined,\n optionsToMFContext(this.host.options),\n );\n }\n\n await this.host.loaderHook.lifecycle.afterGetExpose.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n remoteEntryExports,\n moduleFactory,\n origin: this.host,\n });\n } catch (getExposeError) {\n await this.host.loaderHook.lifecycle.afterGetExpose.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n remoteEntryExports,\n error: getExposeError,\n origin: this.host,\n });\n throw getExposeError;\n }\n\n // keep symbol for module name always one format\n const symbolName = processModuleAlias(this.remoteInfo.name, expose);\n const wrapModuleFactory = this.wraperFactory(moduleFactory, symbolName);\n\n if (!loadFactory) {\n return wrapModuleFactory;\n }\n\n await this.host.loaderHook.lifecycle.beforeExecuteFactory.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n loadFactory,\n origin: this.host,\n });\n\n try {\n const exposeContent = await wrapModuleFactory();\n\n await this.host.loaderHook.lifecycle.afterExecuteFactory.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n loadFactory,\n exposeModule: exposeContent,\n origin: this.host,\n });\n\n return exposeContent;\n } catch (executeFactoryError) {\n await this.host.loaderHook.lifecycle.afterExecuteFactory.emit({\n id,\n expose,\n moduleInfo: this.remoteInfo,\n loadFactory,\n error: executeFactoryError,\n origin: this.host,\n });\n throw executeFactoryError;\n }\n }\n\n private wraperFactory(moduleFactory: RemoteModuleFactory, id: string) {\n function defineModuleId(res: unknown, id: string) {\n if (\n res &&\n typeof res === 'object' &&\n Object.isExtensible(res) &&\n !Object.getOwnPropertyDescriptor(res, Symbol.for('mf_module_id'))\n ) {\n Object.defineProperty(res, Symbol.for('mf_module_id'), {\n value: id,\n enumerable: false,\n });\n }\n }\n\n return () => {\n const res = moduleFactory();\n\n if (res instanceof Promise) {\n return res.then((asyncRes) => {\n // This parameter is used for bridge debugging\n defineModuleId(asyncRes, id);\n return asyncRes;\n });\n }\n\n // This parameter is used for bridge debugging\n defineModuleId(res, id);\n return res;\n };\n }\n}\n\nexport { Module };\n"],"mappings":";;;;;;;;;;AA2BA,SAAS,wBACP,gBACoB;AACpB,KACE,CAAC,kBACD,EAAE,aAAa,mBACf,CAAC,MAAM,QAAQ,eAAe,QAAQ,CAEtC;CAGF,MAAM,UAAU,eAAe,QAC5B,KAAK,WAAW,OAAO,WAAW,CAClC,OAAO,QAAQ;AAElB,QAAO,QAAQ,SAAS,QAAQ,KAAK,IAAI,GAAG;;AAG9C,SAAgB,6BACd,YACA,mBACA,cACqB;CACrB,MAAM,qBAAqB;CAE3B,MAAM,iBAAiB,MAAM,QAAQ,WAAW,WAAW,GACvD,WAAW,aACX,CAAC,WAAW,WAAW;AAE3B,KAAI,CAAC,eAAe,OAClB,gBAAe,KAAK,UAAU;AAEhC,gBAAe,SAAS,kBAAkB;AACxC,MAAI,CAAC,mBAAmB,eACtB,oBAAmB,iBAAiB,EAAE;GAExC;CAEF,MAAM,yBAAyB;EAC7B,SAAS,WAAW,WAAW;EAC/B,gBAAgB,MAAM,QAAQ,WAAW,WAAW,GAChD,iBACA,WAAW,cAAc;EAC9B;AAGD,QAAO,eAAe,wBAAwB,iBAAiB;EAC7D,OAAO;EAEP,YAAY;EACb,CAAC;AAMF,QAAO;EACL;EACA,YALiB,mBAAmB,eAAe;EAMnD,WAL2B,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KAAK,KAAK,WAAW,UAAU,oBAAoB,KAAK;IAC5D;IACA;IACA,YAAY,KAAK;IACjB;IACA,cAAc;IACd,QAAQ,KAAK;IACd,CAAC;AAEF,UAAO;WACA,qBAAqB;AAC5B,SAAM,KAAK,KAAK,WAAW,UAAU,oBAAoB,KAAK;IAC5D;IACA;IACA,YAAY,KAAK;IACjB;IACA,OAAO;IACP,QAAQ,KAAK;IACd,CAAC;AACF,SAAM;;;CAIV,AAAQ,cAAc,eAAoC,IAAY;EACpE,SAAS,eAAe,KAAc,IAAY;AAChD,OACE,OACA,OAAO,QAAQ,YACf,OAAO,aAAa,IAAI,IACxB,CAAC,OAAO,yBAAyB,KAAK,OAAO,IAAI,eAAe,CAAC,CAEjE,QAAO,eAAe,KAAK,OAAO,IAAI,eAAe,EAAE;IACrD,OAAO;IACP,YAAY;IACb,CAAC;;AAIN,eAAa;GACX,MAAM,MAAM,eAAe;AAE3B,OAAI,eAAe,QACjB,QAAO,IAAI,MAAM,aAAa;AAE5B,mBAAe,UAAU,GAAG;AAC5B,WAAO;KACP;AAIJ,kBAAe,KAAK,GAAG;AACvB,UAAO"}
|
|
@@ -43,7 +43,9 @@ var SnapshotHandler = class {
|
|
|
43
43
|
beforeLoadRemoteSnapshot: new require_asyncHook.AsyncHook("beforeLoadRemoteSnapshot"),
|
|
44
44
|
loadSnapshot: new require_asyncWaterfallHooks.AsyncWaterfallHook("loadGlobalSnapshot"),
|
|
45
45
|
loadRemoteSnapshot: new require_asyncWaterfallHooks.AsyncWaterfallHook("loadRemoteSnapshot"),
|
|
46
|
-
afterLoadSnapshot: new require_asyncWaterfallHooks.AsyncWaterfallHook("afterLoadSnapshot")
|
|
46
|
+
afterLoadSnapshot: new require_asyncWaterfallHooks.AsyncWaterfallHook("afterLoadSnapshot"),
|
|
47
|
+
beforeLoadManifest: new require_asyncHook.AsyncHook("beforeLoadManifest"),
|
|
48
|
+
afterLoadManifest: new require_asyncHook.AsyncHook("afterLoadManifest")
|
|
47
49
|
});
|
|
48
50
|
this.manifestLoading = require_global.Global.__FEDERATION__.__MANIFEST_LOADING__;
|
|
49
51
|
this.HostInstance = HostInstance;
|
|
@@ -139,7 +141,26 @@ var SnapshotHandler = class {
|
|
|
139
141
|
const getManifest = async () => {
|
|
140
142
|
const remoteInfo = require_load.getRemoteInfo(moduleInfo);
|
|
141
143
|
let manifestJson = this.manifestCache.get(manifestUrl);
|
|
142
|
-
if (manifestJson)
|
|
144
|
+
if (manifestJson) {
|
|
145
|
+
await this.hooks.lifecycle.afterLoadManifest.emit({
|
|
146
|
+
manifestUrl,
|
|
147
|
+
moduleInfo,
|
|
148
|
+
resourceOptions,
|
|
149
|
+
manifestJson,
|
|
150
|
+
cached: true,
|
|
151
|
+
origin: this.HostInstance
|
|
152
|
+
});
|
|
153
|
+
return manifestJson;
|
|
154
|
+
}
|
|
155
|
+
await this.hooks.lifecycle.beforeLoadManifest.emit({
|
|
156
|
+
manifestUrl,
|
|
157
|
+
moduleInfo,
|
|
158
|
+
resourceOptions,
|
|
159
|
+
origin: this.HostInstance
|
|
160
|
+
});
|
|
161
|
+
let response;
|
|
162
|
+
let loadError;
|
|
163
|
+
let recovered = false;
|
|
143
164
|
try {
|
|
144
165
|
let res = await this.loaderHook.lifecycle.fetch.emit(manifestUrl, {}, remoteInfo, resourceOptions ? {
|
|
145
166
|
...resourceOptions,
|
|
@@ -147,8 +168,10 @@ var SnapshotHandler = class {
|
|
|
147
168
|
resourceType: "manifest"
|
|
148
169
|
} : void 0);
|
|
149
170
|
if (!res || !(res instanceof Response)) res = await fetch(manifestUrl, {});
|
|
171
|
+
response = res;
|
|
150
172
|
manifestJson = await res.json();
|
|
151
173
|
} catch (err) {
|
|
174
|
+
loadError = err;
|
|
152
175
|
manifestJson = await this.HostInstance.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({
|
|
153
176
|
id: manifestUrl,
|
|
154
177
|
error: err,
|
|
@@ -159,27 +182,47 @@ var SnapshotHandler = class {
|
|
|
159
182
|
});
|
|
160
183
|
if (!manifestJson) {
|
|
161
184
|
delete this.manifestLoading[manifestUrl];
|
|
185
|
+
await this.hooks.lifecycle.afterLoadManifest.emit({
|
|
186
|
+
manifestUrl,
|
|
187
|
+
moduleInfo,
|
|
188
|
+
resourceOptions,
|
|
189
|
+
response,
|
|
190
|
+
error: err,
|
|
191
|
+
origin: this.HostInstance
|
|
192
|
+
});
|
|
162
193
|
require_logger.error(_module_federation_error_codes.RUNTIME_003, _module_federation_error_codes.runtimeDescMap, {
|
|
163
194
|
manifestUrl,
|
|
164
195
|
moduleName: moduleInfo.name,
|
|
165
196
|
hostName: this.HostInstance.options.name
|
|
166
197
|
}, `${err}`, require_context.optionsToMFContext(this.HostInstance.options));
|
|
167
198
|
}
|
|
199
|
+
recovered = true;
|
|
168
200
|
}
|
|
169
201
|
const missingRequiredFields = [
|
|
170
202
|
!manifestJson.metaData && "metaData",
|
|
171
203
|
!manifestJson.exposes && "exposes",
|
|
172
204
|
!manifestJson.shared && "shared"
|
|
173
205
|
].filter(Boolean);
|
|
174
|
-
|
|
206
|
+
const validationError = missingRequiredFields.length > 0 ? /* @__PURE__ */ new Error(`"${manifestUrl}" is not a valid federation manifest for remote "${moduleInfo.name}". Missing required fields: ${missingRequiredFields.join(", ")}.`) : void 0;
|
|
207
|
+
if (validationError) await this.HostInstance.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({
|
|
175
208
|
id: manifestUrl,
|
|
176
|
-
error:
|
|
209
|
+
error: validationError,
|
|
177
210
|
from: "runtime",
|
|
178
211
|
lifecycle: "afterResolve",
|
|
179
212
|
remote: remoteInfo,
|
|
180
213
|
origin: this.HostInstance
|
|
181
214
|
});
|
|
182
|
-
|
|
215
|
+
await this.hooks.lifecycle.afterLoadManifest.emit({
|
|
216
|
+
manifestUrl,
|
|
217
|
+
moduleInfo,
|
|
218
|
+
resourceOptions,
|
|
219
|
+
manifestJson,
|
|
220
|
+
response,
|
|
221
|
+
error: validationError || loadError,
|
|
222
|
+
recovered: validationError ? void 0 : recovered || void 0,
|
|
223
|
+
origin: this.HostInstance
|
|
224
|
+
});
|
|
225
|
+
if (validationError) require_logger.error(_module_federation_error_codes.RUNTIME_013, _module_federation_error_codes.runtimeDescMap, {
|
|
183
226
|
manifestUrl,
|
|
184
227
|
moduleName: moduleInfo.name,
|
|
185
228
|
hostName: this.HostInstance.options.name,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapshotHandler.cjs","names":["getGlobalSnapshotInfoByModuleInfo","getInfoWithoutType","getGlobalSnapshot","PluginSystem","AsyncHook","AsyncWaterfallHook","Global","isBrowserEnvValue","setGlobalSnapshotInfoByModuleInfo","isRemoteInfoWithEntry","RUNTIME_007","runtimeDescMap","optionsToMFContext","getRemoteInfo","RUNTIME_003","RUNTIME_013"],"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 RUNTIME_013,\n runtimeDescMap,\n} from '@module-federation/error-codes';\nimport { Options, Remote, ResourceLoadInitiator } from '../../type';\nimport {\n isRemoteInfoWithEntry,\n error,\n optionsToMFContext,\n getRemoteInfo,\n} 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';\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 origin: ModuleFederation;\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 initiator = 'loadRemote',\n }: {\n moduleInfo: Remote;\n id?: string;\n initiator?: ResourceLoadInitiator;\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 origin: this.HostInstance,\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.loadManifestSnapshot(\n remoteEntry,\n moduleInfo,\n {},\n {\n initiator,\n id: id || moduleInfo.name,\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.loadManifestSnapshot(\n moduleInfo.entry,\n moduleInfo,\n {},\n {\n initiator,\n id: id || moduleInfo.name,\n },\n );\n // eslint-disable-next-line @typescript-eslint/no-shadow\n const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(\n moduleInfo,\n moduleSnapshot,\n );\n mSnapshot = moduleSnapshot;\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 resourceOptions?: {\n initiator: ResourceLoadInitiator;\n id: string;\n },\n ): Promise<Manifest> {\n const getManifest = async (): Promise<Manifest> => {\n const remoteInfo = getRemoteInfo(moduleInfo);\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(\n manifestUrl,\n {},\n remoteInfo,\n resourceOptions\n ? {\n ...resourceOptions,\n url: manifestUrl,\n resourceType: 'manifest',\n }\n : undefined,\n );\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 remote: remoteInfo,\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 const missingRequiredFields = [\n !manifestJson.metaData && 'metaData',\n !manifestJson.exposes && 'exposes',\n !manifestJson.shared && 'shared',\n ].filter(Boolean);\n if (missingRequiredFields.length > 0) {\n await this.HostInstance.remoteHandler.hooks.lifecycle.errorLoadRemote.emit(\n {\n id: manifestUrl,\n error: new Error(\n `\"${manifestUrl}\" is not a valid federation manifest for remote \"${moduleInfo.name}\". Missing required fields: ${missingRequiredFields.join(', ')}.`,\n ),\n from: 'runtime',\n lifecycle: 'afterResolve',\n remote: remoteInfo,\n origin: this.HostInstance,\n },\n );\n }\n\n if (missingRequiredFields.length > 0) {\n error(\n RUNTIME_013,\n runtimeDescMap,\n {\n manifestUrl,\n moduleName: moduleInfo.name,\n hostName: this.HostInstance.options.name,\n missingFields: missingRequiredFields.join(','),\n },\n undefined,\n optionsToMFContext(this.HostInstance.options),\n );\n }\n this.manifestCache.set(manifestUrl, manifestJson);\n return manifestJson;\n };\n\n return getManifest();\n }\n\n private async loadManifestSnapshot(\n manifestUrl: string,\n moduleInfo: Remote,\n extraOptions: Record<string, any>,\n resourceOptions?: {\n initiator: ResourceLoadInitiator;\n id: string;\n },\n ): Promise<ModuleInfo> {\n const asyncLoadProcess = async () => {\n const manifestJson = await this.getManifestJson(\n manifestUrl,\n moduleInfo,\n extraOptions,\n resourceOptions,\n );\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":";;;;;;;;;;;;;;AAgCA,SAAgB,oBACd,YACA,QAKA;CACA,MAAM,qBAAqBA,iDAAk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T,IAAI,YAAY,mDAAmD,WAAW,KAAK,8BAA8B,sBAAsB,KAAK,KAAK,CAAC,GACnJ;IACD,MAAM;IACN,WAAW;IACX,QAAQ;IACR,QAAQ,KAAK;IACd,CACF;AAGH,OAAI,sBAAsB,SAAS,EACjC,sBACEG,4CACAJ,+CACA;IACE;IACA,YAAY,WAAW;IACvB,UAAU,KAAK,aAAa,QAAQ;IACpC,eAAe,sBAAsB,KAAK,IAAI;IAC/C,EACD,QACAC,mCAAmB,KAAK,aAAa,QAAQ,CAC9C;AAEH,QAAK,cAAc,IAAI,aAAa,aAAa;AACjD,UAAO;;AAGT,SAAO,aAAa;;CAGtB,MAAc,qBACZ,aACA,YACA,cACA,iBAIqB;EACrB,MAAM,mBAAmB,YAAY;GACnC,MAAM,eAAe,MAAM,KAAK,gBAC9B,aACA,YACA,cACA,gBACD;GACD,MAAM,0EAA8C,cAAc,EAChE,SAAS,aACV,CAAC;GAEF,MAAM,EAAE,gBAAgB,sBACtB,MAAM,KAAK,MAAM,UAAU,mBAAmB,KAAK;IACjD,SAAS,KAAK,aAAa;IAC3B;IACA;IACA;IACA;IACA,MAAM;IACP,CAAC;AACJ,UAAO;;AAGT,MAAI,CAAC,KAAK,gBAAgB,aACxB,MAAK,gBAAgB,eAAe,kBAAkB,CAAC,MAAM,QAAQ,IAAI;AAE3E,SAAO,KAAK,gBAAgB"}
|
|
1
|
+
{"version":3,"file":"SnapshotHandler.cjs","names":["getGlobalSnapshotInfoByModuleInfo","getInfoWithoutType","getGlobalSnapshot","PluginSystem","AsyncHook","AsyncWaterfallHook","Global","isBrowserEnvValue","setGlobalSnapshotInfoByModuleInfo","isRemoteInfoWithEntry","RUNTIME_007","runtimeDescMap","optionsToMFContext","getRemoteInfo","RUNTIME_003","RUNTIME_013"],"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 RUNTIME_013,\n runtimeDescMap,\n} from '@module-federation/error-codes';\nimport { Options, Remote, ResourceLoadInitiator } from '../../type';\nimport {\n isRemoteInfoWithEntry,\n error,\n optionsToMFContext,\n getRemoteInfo,\n} 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';\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 origin: ModuleFederation;\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 beforeLoadManifest: new AsyncHook<\n [\n {\n manifestUrl: string;\n moduleInfo: Remote;\n resourceOptions?: {\n initiator: ResourceLoadInitiator;\n id: string;\n };\n origin: ModuleFederation;\n },\n ],\n void\n >('beforeLoadManifest'),\n afterLoadManifest: new AsyncHook<\n [\n {\n manifestUrl: string;\n moduleInfo: Remote;\n resourceOptions?: {\n initiator: ResourceLoadInitiator;\n id: string;\n };\n manifestJson?: Manifest;\n response?: Response;\n error?: unknown;\n cached?: boolean;\n recovered?: boolean;\n origin: ModuleFederation;\n },\n ],\n void\n >('afterLoadManifest'),\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 initiator = 'loadRemote',\n }: {\n moduleInfo: Remote;\n id?: string;\n initiator?: ResourceLoadInitiator;\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 origin: this.HostInstance,\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.loadManifestSnapshot(\n remoteEntry,\n moduleInfo,\n {},\n {\n initiator,\n id: id || moduleInfo.name,\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.loadManifestSnapshot(\n moduleInfo.entry,\n moduleInfo,\n {},\n {\n initiator,\n id: id || moduleInfo.name,\n },\n );\n // eslint-disable-next-line @typescript-eslint/no-shadow\n const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(\n moduleInfo,\n moduleSnapshot,\n );\n mSnapshot = moduleSnapshot;\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 resourceOptions?: {\n initiator: ResourceLoadInitiator;\n id: string;\n },\n ): Promise<Manifest> {\n const getManifest = async (): Promise<Manifest> => {\n const remoteInfo = getRemoteInfo(moduleInfo);\n let manifestJson: Manifest | undefined =\n this.manifestCache.get(manifestUrl);\n if (manifestJson) {\n await this.hooks.lifecycle.afterLoadManifest.emit({\n manifestUrl,\n moduleInfo,\n resourceOptions,\n manifestJson,\n cached: true,\n origin: this.HostInstance,\n });\n return manifestJson;\n }\n\n await this.hooks.lifecycle.beforeLoadManifest.emit({\n manifestUrl,\n moduleInfo,\n resourceOptions,\n origin: this.HostInstance,\n });\n\n let response: Response | undefined;\n let loadError: unknown;\n let recovered = false;\n\n try {\n let res = await this.loaderHook.lifecycle.fetch.emit(\n manifestUrl,\n {},\n remoteInfo,\n resourceOptions\n ? {\n ...resourceOptions,\n url: manifestUrl,\n resourceType: 'manifest',\n }\n : undefined,\n );\n if (!res || !(res instanceof Response)) {\n res = await fetch(manifestUrl, {});\n }\n response = res;\n manifestJson = (await res.json()) as Manifest;\n } catch (err) {\n loadError = 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 remote: remoteInfo,\n origin: this.HostInstance,\n },\n )) as Manifest | undefined;\n\n if (!manifestJson) {\n delete this.manifestLoading[manifestUrl];\n await this.hooks.lifecycle.afterLoadManifest.emit({\n manifestUrl,\n moduleInfo,\n resourceOptions,\n response,\n error: err,\n origin: this.HostInstance,\n });\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 recovered = true;\n }\n\n const missingRequiredFields = [\n !manifestJson.metaData && 'metaData',\n !manifestJson.exposes && 'exposes',\n !manifestJson.shared && 'shared',\n ].filter(Boolean);\n const validationError =\n missingRequiredFields.length > 0\n ? new Error(\n `\"${manifestUrl}\" is not a valid federation manifest for remote \"${moduleInfo.name}\". Missing required fields: ${missingRequiredFields.join(', ')}.`,\n )\n : undefined;\n if (validationError) {\n await this.HostInstance.remoteHandler.hooks.lifecycle.errorLoadRemote.emit(\n {\n id: manifestUrl,\n error: validationError,\n from: 'runtime',\n lifecycle: 'afterResolve',\n remote: remoteInfo,\n origin: this.HostInstance,\n },\n );\n }\n\n await this.hooks.lifecycle.afterLoadManifest.emit({\n manifestUrl,\n moduleInfo,\n resourceOptions,\n manifestJson,\n response,\n error: validationError || loadError,\n recovered: validationError ? undefined : recovered || undefined,\n origin: this.HostInstance,\n });\n\n if (validationError) {\n error(\n RUNTIME_013,\n runtimeDescMap,\n {\n manifestUrl,\n moduleName: moduleInfo.name,\n hostName: this.HostInstance.options.name,\n missingFields: missingRequiredFields.join(','),\n },\n undefined,\n optionsToMFContext(this.HostInstance.options),\n );\n }\n\n this.manifestCache.set(manifestUrl, manifestJson);\n return manifestJson;\n };\n\n return getManifest();\n }\n\n private async loadManifestSnapshot(\n manifestUrl: string,\n moduleInfo: Remote,\n extraOptions: Record<string, any>,\n resourceOptions?: {\n initiator: ResourceLoadInitiator;\n id: string;\n },\n ): Promise<ModuleInfo> {\n const asyncLoadProcess = async () => {\n const manifestJson = await this.getManifestJson(\n manifestUrl,\n moduleInfo,\n extraOptions,\n resourceOptions,\n );\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":";;;;;;;;;;;;;;AAgCA,SAAgB,oBACd,YACA,QAKA;CACA,MAAM,qBAAqBA,iDAAkC;EAC3D,MAAM,OAAO;EACb,SAAS,OAAO,QAAQ;EACzB,CAAC;CAGF,MAAM,mBACJ,sBACA,iBAAiB,sBACjB,mBAAmB,eACnBC,kCAAmB,mBAAmB,aAAa,WAAW,KAAK,CAAC;AAEtE,KAAI,oBAAoB,iBAAiB,eACvC,QAAO;EACL;EACA,gBAAgBC,kCAAmB;EACnC,gBAAgBF,iDAAkC;GAChD,MAAM,WAAW;GACjB,SAAS,iBAAiB;GAC3B,CAAC;EACH;AAGH,QAAO;EACL,oBAAoB;EACpB,gBAAgBE,kCAAmB;EACnC,gBAAgBF,iDAAkC;GAChD,MAAM,WAAW;GACjB,SAAS,aAAa,aAAa,WAAW,UAAU;GACzD,CAAC;EACH;;AAGH,IAAa,kBAAb,MAA6B;CA2E3B,YAAY,cAAgC;6BA1EmB;uCAExB,IAAI,KAAK;eACxC,IAAIG,kCAAa;GACvB,0BAA0B,IAAIC,4BAS5B,2BAA2B;GAC7B,cAAc,IAAIC,+CAMf,qBAAqB;GACxB,oBAAoB,IAAIA,+CAOrB,qBAAqB;GACxB,mBAAmB,IAAIA,+CAMpB,oBAAoB;GACvB,oBAAoB,IAAID,4BAatB,qBAAqB;GACvB,mBAAmB,IAAIA,4BAkBrB,oBAAoB;GACvB,CAAC;yBAGAE,sBAAO,eAAe;AAGtB,OAAK,eAAe;AACpB,OAAK,aAAa,aAAa;;CAIjC,MAAM,uBAAuB,EAC3B,YACA,IACA,YAAY,gBAUJ;EACR,MAAM,EAAE,YAAY,KAAK;AAEzB,QAAM,KAAK,MAAM,UAAU,yBAAyB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cAAc,IAAI,aAAa,aAAa;AACjD,UAAO;;AAGT,SAAO,aAAa;;CAGtB,MAAc,qBACZ,aACA,YACA,cACA,iBAIqB;EACrB,MAAM,mBAAmB,YAAY;GACnC,MAAM,eAAe,MAAM,KAAK,gBAC9B,aACA,YACA,cACA,gBACD;GACD,MAAM,0EAA8C,cAAc,EAChE,SAAS,aACV,CAAC;GAEF,MAAM,EAAE,gBAAgB,sBACtB,MAAM,KAAK,MAAM,UAAU,mBAAmB,KAAK;IACjD,SAAS,KAAK,aAAa;IAC3B;IACA;IACA;IACA;IACA,MAAM;IACP,CAAC;AACJ,UAAO;;AAGT,MAAI,CAAC,KAAK,gBAAgB,aACxB,MAAK,gBAAgB,eAAe,kBAAkB,CAAC,MAAM,QAAQ,IAAI;AAE3E,SAAO,KAAK,gBAAgB"}
|
|
@@ -40,6 +40,29 @@ declare class SnapshotHandler {
|
|
|
40
40
|
moduleInfo: Remote;
|
|
41
41
|
remoteSnapshot: ModuleInfo;
|
|
42
42
|
}>;
|
|
43
|
+
beforeLoadManifest: AsyncHook<[{
|
|
44
|
+
manifestUrl: string;
|
|
45
|
+
moduleInfo: Remote;
|
|
46
|
+
resourceOptions?: {
|
|
47
|
+
initiator: ResourceLoadInitiator;
|
|
48
|
+
id: string;
|
|
49
|
+
};
|
|
50
|
+
origin: ModuleFederation;
|
|
51
|
+
}], void>;
|
|
52
|
+
afterLoadManifest: AsyncHook<[{
|
|
53
|
+
manifestUrl: string;
|
|
54
|
+
moduleInfo: Remote;
|
|
55
|
+
resourceOptions?: {
|
|
56
|
+
initiator: ResourceLoadInitiator;
|
|
57
|
+
id: string;
|
|
58
|
+
};
|
|
59
|
+
manifestJson?: Manifest;
|
|
60
|
+
response?: Response;
|
|
61
|
+
error?: unknown;
|
|
62
|
+
cached?: boolean;
|
|
63
|
+
recovered?: boolean;
|
|
64
|
+
origin: ModuleFederation;
|
|
65
|
+
}], void>;
|
|
43
66
|
}>;
|
|
44
67
|
loaderHook: ModuleFederation['loaderHook'];
|
|
45
68
|
manifestLoading: Record<string, Promise<ModuleInfo>>;
|
|
@@ -43,7 +43,9 @@ var SnapshotHandler = class {
|
|
|
43
43
|
beforeLoadRemoteSnapshot: new AsyncHook("beforeLoadRemoteSnapshot"),
|
|
44
44
|
loadSnapshot: new AsyncWaterfallHook("loadGlobalSnapshot"),
|
|
45
45
|
loadRemoteSnapshot: new AsyncWaterfallHook("loadRemoteSnapshot"),
|
|
46
|
-
afterLoadSnapshot: new AsyncWaterfallHook("afterLoadSnapshot")
|
|
46
|
+
afterLoadSnapshot: new AsyncWaterfallHook("afterLoadSnapshot"),
|
|
47
|
+
beforeLoadManifest: new AsyncHook("beforeLoadManifest"),
|
|
48
|
+
afterLoadManifest: new AsyncHook("afterLoadManifest")
|
|
47
49
|
});
|
|
48
50
|
this.manifestLoading = Global.__FEDERATION__.__MANIFEST_LOADING__;
|
|
49
51
|
this.HostInstance = HostInstance;
|
|
@@ -139,7 +141,26 @@ var SnapshotHandler = class {
|
|
|
139
141
|
const getManifest = async () => {
|
|
140
142
|
const remoteInfo = getRemoteInfo(moduleInfo);
|
|
141
143
|
let manifestJson = this.manifestCache.get(manifestUrl);
|
|
142
|
-
if (manifestJson)
|
|
144
|
+
if (manifestJson) {
|
|
145
|
+
await this.hooks.lifecycle.afterLoadManifest.emit({
|
|
146
|
+
manifestUrl,
|
|
147
|
+
moduleInfo,
|
|
148
|
+
resourceOptions,
|
|
149
|
+
manifestJson,
|
|
150
|
+
cached: true,
|
|
151
|
+
origin: this.HostInstance
|
|
152
|
+
});
|
|
153
|
+
return manifestJson;
|
|
154
|
+
}
|
|
155
|
+
await this.hooks.lifecycle.beforeLoadManifest.emit({
|
|
156
|
+
manifestUrl,
|
|
157
|
+
moduleInfo,
|
|
158
|
+
resourceOptions,
|
|
159
|
+
origin: this.HostInstance
|
|
160
|
+
});
|
|
161
|
+
let response;
|
|
162
|
+
let loadError;
|
|
163
|
+
let recovered = false;
|
|
143
164
|
try {
|
|
144
165
|
let res = await this.loaderHook.lifecycle.fetch.emit(manifestUrl, {}, remoteInfo, resourceOptions ? {
|
|
145
166
|
...resourceOptions,
|
|
@@ -147,8 +168,10 @@ var SnapshotHandler = class {
|
|
|
147
168
|
resourceType: "manifest"
|
|
148
169
|
} : void 0);
|
|
149
170
|
if (!res || !(res instanceof Response)) res = await fetch(manifestUrl, {});
|
|
171
|
+
response = res;
|
|
150
172
|
manifestJson = await res.json();
|
|
151
173
|
} catch (err) {
|
|
174
|
+
loadError = err;
|
|
152
175
|
manifestJson = await this.HostInstance.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({
|
|
153
176
|
id: manifestUrl,
|
|
154
177
|
error: err,
|
|
@@ -159,27 +182,47 @@ var SnapshotHandler = class {
|
|
|
159
182
|
});
|
|
160
183
|
if (!manifestJson) {
|
|
161
184
|
delete this.manifestLoading[manifestUrl];
|
|
185
|
+
await this.hooks.lifecycle.afterLoadManifest.emit({
|
|
186
|
+
manifestUrl,
|
|
187
|
+
moduleInfo,
|
|
188
|
+
resourceOptions,
|
|
189
|
+
response,
|
|
190
|
+
error: err,
|
|
191
|
+
origin: this.HostInstance
|
|
192
|
+
});
|
|
162
193
|
error(RUNTIME_003, runtimeDescMap, {
|
|
163
194
|
manifestUrl,
|
|
164
195
|
moduleName: moduleInfo.name,
|
|
165
196
|
hostName: this.HostInstance.options.name
|
|
166
197
|
}, `${err}`, optionsToMFContext(this.HostInstance.options));
|
|
167
198
|
}
|
|
199
|
+
recovered = true;
|
|
168
200
|
}
|
|
169
201
|
const missingRequiredFields = [
|
|
170
202
|
!manifestJson.metaData && "metaData",
|
|
171
203
|
!manifestJson.exposes && "exposes",
|
|
172
204
|
!manifestJson.shared && "shared"
|
|
173
205
|
].filter(Boolean);
|
|
174
|
-
|
|
206
|
+
const validationError = missingRequiredFields.length > 0 ? /* @__PURE__ */ new Error(`"${manifestUrl}" is not a valid federation manifest for remote "${moduleInfo.name}". Missing required fields: ${missingRequiredFields.join(", ")}.`) : void 0;
|
|
207
|
+
if (validationError) await this.HostInstance.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({
|
|
175
208
|
id: manifestUrl,
|
|
176
|
-
error:
|
|
209
|
+
error: validationError,
|
|
177
210
|
from: "runtime",
|
|
178
211
|
lifecycle: "afterResolve",
|
|
179
212
|
remote: remoteInfo,
|
|
180
213
|
origin: this.HostInstance
|
|
181
214
|
});
|
|
182
|
-
|
|
215
|
+
await this.hooks.lifecycle.afterLoadManifest.emit({
|
|
216
|
+
manifestUrl,
|
|
217
|
+
moduleInfo,
|
|
218
|
+
resourceOptions,
|
|
219
|
+
manifestJson,
|
|
220
|
+
response,
|
|
221
|
+
error: validationError || loadError,
|
|
222
|
+
recovered: validationError ? void 0 : recovered || void 0,
|
|
223
|
+
origin: this.HostInstance
|
|
224
|
+
});
|
|
225
|
+
if (validationError) error(RUNTIME_013, runtimeDescMap, {
|
|
183
226
|
manifestUrl,
|
|
184
227
|
moduleName: moduleInfo.name,
|
|
185
228
|
hostName: this.HostInstance.options.name,
|