@module-federation/runtime-core 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.cjs +2 -2
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +0 -3
- package/dist/core.js +3 -3
- package/dist/core.js.map +1 -1
- package/dist/global.cjs +2 -2
- package/dist/global.cjs.map +1 -1
- package/dist/global.d.ts +0 -1
- package/dist/global.js +3 -3
- package/dist/global.js.map +1 -1
- package/dist/helpers.d.ts +0 -2
- package/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/module/index.cjs +3 -2
- package/dist/module/index.cjs.map +1 -1
- package/dist/module/index.d.ts +0 -1
- package/dist/module/index.js +4 -3
- package/dist/module/index.js.map +1 -1
- package/dist/plugins/generate-preload-assets.cjs +2 -2
- package/dist/plugins/generate-preload-assets.cjs.map +1 -1
- package/dist/plugins/generate-preload-assets.js +3 -3
- package/dist/plugins/generate-preload-assets.js.map +1 -1
- package/dist/plugins/snapshot/SnapshotHandler.cjs +14 -8
- package/dist/plugins/snapshot/SnapshotHandler.cjs.map +1 -1
- package/dist/plugins/snapshot/SnapshotHandler.d.ts +0 -2
- package/dist/plugins/snapshot/SnapshotHandler.js +16 -10
- package/dist/plugins/snapshot/SnapshotHandler.js.map +1 -1
- package/dist/plugins/snapshot/index.cjs +3 -2
- package/dist/plugins/snapshot/index.cjs.map +1 -1
- package/dist/plugins/snapshot/index.js +4 -3
- package/dist/plugins/snapshot/index.js.map +1 -1
- package/dist/remote/index.cjs +5 -4
- package/dist/remote/index.cjs.map +1 -1
- package/dist/remote/index.d.ts +0 -2
- package/dist/remote/index.js +8 -7
- package/dist/remote/index.js.map +1 -1
- package/dist/shared/index.cjs +9 -10
- package/dist/shared/index.cjs.map +1 -1
- package/dist/shared/index.d.ts +0 -3
- package/dist/shared/index.js +11 -12
- package/dist/shared/index.js.map +1 -1
- package/dist/types.d.ts +0 -1
- package/dist/utils/context.cjs +45 -0
- package/dist/utils/context.cjs.map +1 -0
- package/dist/utils/context.d.ts +1 -0
- package/dist/utils/context.js +44 -0
- package/dist/utils/context.js.map +1 -0
- package/dist/utils/env.cjs.map +1 -1
- package/dist/utils/env.d.ts +1 -1
- package/dist/utils/env.js.map +1 -1
- package/dist/utils/hooks/pluginSystem.d.ts +1 -1
- package/dist/utils/index.cjs +1 -0
- package/dist/utils/index.d.ts +1 -3
- package/dist/utils/index.js +1 -0
- package/dist/utils/load.cjs +15 -13
- package/dist/utils/load.cjs.map +1 -1
- package/dist/utils/load.d.ts +0 -2
- package/dist/utils/load.js +18 -16
- package/dist/utils/load.js.map +1 -1
- package/dist/utils/logger.cjs +9 -3
- package/dist/utils/logger.cjs.map +1 -1
- package/dist/utils/logger.d.ts +5 -2
- package/dist/utils/logger.js +9 -3
- package/dist/utils/logger.js.map +1 -1
- package/dist/utils/manifest.d.ts +0 -2
- package/dist/utils/plugin.d.ts +1 -2
- package/dist/utils/preload.d.ts +0 -2
- package/dist/utils/share.cjs +2 -2
- package/dist/utils/share.cjs.map +1 -1
- package/dist/utils/share.d.ts +0 -3
- package/dist/utils/share.js +2 -2
- package/dist/utils/share.js.map +1 -1
- package/dist/utils/tool.cjs +1 -1
- package/dist/utils/tool.cjs.map +1 -1
- package/dist/utils/tool.d.ts +0 -1
- package/dist/utils/tool.js +2 -2
- package/dist/utils/tool.js.map +1 -1
- package/package.json +10 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../src/plugins/snapshot/index.ts"],"sourcesContent":["import {\n ModuleInfo,\n getResourceUrl,\n
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/plugins/snapshot/index.ts"],"sourcesContent":["import {\n ModuleInfo,\n getResourceUrl,\n isBrowserEnvValue,\n} from '@module-federation/sdk';\nimport { ModuleFederationRuntimePlugin } from '../../type/plugin';\nimport { RUNTIME_011, runtimeDescMap } from '@module-federation/error-codes';\nimport {\n error,\n isPureRemoteEntry,\n isRemoteInfoWithEntry,\n getRemoteEntryInfoFromSnapshot,\n} from '../../utils';\nimport { PreloadOptions, RemoteInfo } from '../../type';\nimport { preloadAssets } from '../../utils/preload';\n\nexport function assignRemoteInfo(\n remoteInfo: RemoteInfo,\n remoteSnapshot: ModuleInfo,\n): void {\n const remoteEntryInfo = getRemoteEntryInfoFromSnapshot(remoteSnapshot);\n if (!remoteEntryInfo.url) {\n error(RUNTIME_011, runtimeDescMap, { remoteName: remoteInfo.name });\n }\n\n let entryUrl = getResourceUrl(remoteSnapshot, remoteEntryInfo.url);\n\n if (!isBrowserEnvValue && !entryUrl.startsWith('http')) {\n entryUrl = `https:${entryUrl}`;\n }\n\n remoteInfo.type = remoteEntryInfo.type;\n remoteInfo.entryGlobalName = remoteEntryInfo.globalName;\n remoteInfo.entry = entryUrl;\n remoteInfo.version = remoteSnapshot.version;\n remoteInfo.buildVersion = remoteSnapshot.buildVersion;\n}\n\nexport function snapshotPlugin(): ModuleFederationRuntimePlugin {\n return {\n name: 'snapshot-plugin',\n async afterResolve(args) {\n const { remote, pkgNameOrAlias, expose, origin, remoteInfo, id } = args;\n\n if (!isRemoteInfoWithEntry(remote) || !isPureRemoteEntry(remote)) {\n const { remoteSnapshot, globalSnapshot } =\n await origin.snapshotHandler.loadRemoteSnapshotInfo({\n moduleInfo: remote,\n id,\n });\n\n assignRemoteInfo(remoteInfo, remoteSnapshot);\n // preloading assets\n const preloadOptions: PreloadOptions[0] = {\n remote,\n preloadConfig: {\n nameOrAlias: pkgNameOrAlias,\n exposes: [expose],\n resourceCategory: 'sync',\n share: false,\n depsRemote: false,\n },\n };\n\n const assets =\n await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit(\n {\n origin,\n preloadOptions,\n remoteInfo,\n remote,\n remoteSnapshot,\n globalSnapshot,\n },\n );\n\n if (assets) {\n preloadAssets(remoteInfo, origin, assets, false);\n }\n\n return {\n ...args,\n remoteSnapshot,\n };\n }\n\n return args;\n },\n };\n}\n"],"mappings":";;;;;;;;AAgBA,SAAgB,iBACd,YACA,gBACM;CACN,MAAM,kBAAkB,+BAA+B,eAAe;AACtE,KAAI,CAAC,gBAAgB,IACnB,OAAM,aAAa,gBAAgB,EAAE,YAAY,WAAW,MAAM,CAAC;CAGrE,IAAI,WAAW,eAAe,gBAAgB,gBAAgB,IAAI;AAElE,KAAI,CAAC,qBAAqB,CAAC,SAAS,WAAW,OAAO,CACpD,YAAW,SAAS;AAGtB,YAAW,OAAO,gBAAgB;AAClC,YAAW,kBAAkB,gBAAgB;AAC7C,YAAW,QAAQ;AACnB,YAAW,UAAU,eAAe;AACpC,YAAW,eAAe,eAAe;;AAG3C,SAAgB,iBAAgD;AAC9D,QAAO;EACL,MAAM;EACN,MAAM,aAAa,MAAM;GACvB,MAAM,EAAE,QAAQ,gBAAgB,QAAQ,QAAQ,YAAY,OAAO;AAEnE,OAAI,CAAC,sBAAsB,OAAO,IAAI,CAAC,kBAAkB,OAAO,EAAE;IAChE,MAAM,EAAE,gBAAgB,mBACtB,MAAM,OAAO,gBAAgB,uBAAuB;KAClD,YAAY;KACZ;KACD,CAAC;AAEJ,qBAAiB,YAAY,eAAe;IAE5C,MAAM,iBAAoC;KACxC;KACA,eAAe;MACb,aAAa;MACb,SAAS,CAAC,OAAO;MACjB,kBAAkB;MAClB,OAAO;MACP,YAAY;MACb;KACF;IAED,MAAM,SACJ,MAAM,OAAO,cAAc,MAAM,UAAU,sBAAsB,KAC/D;KACE;KACA;KACA;KACA;KACA;KACA;KACD,CACF;AAEH,QAAI,OACF,eAAc,YAAY,QAAQ,QAAQ,MAAM;AAGlD,WAAO;KACL,GAAG;KACH;KACD;;AAGH,UAAO;;EAEV"}
|
package/dist/remote/index.cjs
CHANGED
|
@@ -4,6 +4,7 @@ const require_constant = require('../constant.cjs');
|
|
|
4
4
|
const require_share = require('../utils/share.cjs');
|
|
5
5
|
const require_manifest = require('../utils/manifest.cjs');
|
|
6
6
|
const require_load = require('../utils/load.cjs');
|
|
7
|
+
const require_context = require('../utils/context.cjs');
|
|
7
8
|
require('../utils/index.cjs');
|
|
8
9
|
const require_preload = require('../utils/preload.cjs');
|
|
9
10
|
const require_index$1 = require('../module/index.cjs');
|
|
@@ -151,10 +152,10 @@ var RemoteHandler = class {
|
|
|
151
152
|
}
|
|
152
153
|
const { id: idRes } = loadRemoteArgs;
|
|
153
154
|
const remoteSplitInfo = require_manifest.matchRemoteWithNameAndExpose(host.options.remotes, idRes);
|
|
154
|
-
|
|
155
|
+
if (!remoteSplitInfo) require_logger.error(_module_federation_error_codes.RUNTIME_004, _module_federation_error_codes.runtimeDescMap, {
|
|
155
156
|
hostName: host.options.name,
|
|
156
157
|
requestId: idRes
|
|
157
|
-
}));
|
|
158
|
+
}, void 0, require_context.optionsToMFContext(host.options));
|
|
158
159
|
const { remote: rawRemote } = remoteSplitInfo;
|
|
159
160
|
const remoteInfo = require_load.getRemoteInfo(rawRemote);
|
|
160
161
|
const matchInfo = await host.sharedHandler.hooks.lifecycle.afterResolve.emit({
|
|
@@ -189,7 +190,7 @@ var RemoteHandler = class {
|
|
|
189
190
|
require_logger.assert(!findEqual, `The alias ${remote.alias} of remote ${remote.name} is not allowed to be the prefix of ${findEqual && findEqual.name} name or alias`);
|
|
190
191
|
}
|
|
191
192
|
if ("entry" in remote) {
|
|
192
|
-
if (
|
|
193
|
+
if (_module_federation_sdk.isBrowserEnvValue && typeof window !== "undefined" && !remote.entry.startsWith("http")) remote.entry = new URL(remote.entry, window.location.origin).href;
|
|
193
194
|
}
|
|
194
195
|
if (!remote.shareScope) remote.shareScope = require_constant.DEFAULT_SCOPE;
|
|
195
196
|
if (!remote.type) remote.type = require_constant.DEFAULT_REMOTE_TYPE;
|
|
@@ -293,7 +294,7 @@ var RemoteHandler = class {
|
|
|
293
294
|
host.moduleCache.delete(remote.name);
|
|
294
295
|
}
|
|
295
296
|
} catch (err) {
|
|
296
|
-
require_logger.logger.
|
|
297
|
+
require_logger.logger.error(`removeRemote failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
297
298
|
}
|
|
298
299
|
}
|
|
299
300
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["PluginSystem","SyncWaterfallHook","AsyncWaterfallHook","AsyncHook","SyncHook","formatPreloadArgs","getRemoteInfo","matchRemoteWithNameAndExpose","RUNTIME_004","runtimeDescMap","Module","DEFAULT_SCOPE","DEFAULT_REMOTE_TYPE","CurrentGlobal","getRemoteEntryUniqueKey","globalLoading","getGlobalShareScope","getGlobalRemoteInfo","getInfoWithoutType","Global"],"sources":["../../src/remote/index.ts"],"sourcesContent":["import {\n isBrowserEnv,\n warn,\n composeKeyWithSeparator,\n ModuleInfo,\n GlobalModuleInfo,\n} from '@module-federation/sdk';\nimport {\n getShortErrorMsg,\n RUNTIME_004,\n runtimeDescMap,\n} from '@module-federation/error-codes';\nimport {\n Global,\n getInfoWithoutType,\n globalLoading,\n CurrentGlobal,\n} from '../global';\nimport {\n Options,\n UserOptions,\n PreloadAssets,\n PreloadOptions,\n PreloadRemoteArgs,\n Remote,\n RemoteInfo,\n RemoteEntryExports,\n CallFrom,\n} from '../type';\nimport { ModuleFederation } from '../core';\nimport {\n PluginSystem,\n AsyncHook,\n AsyncWaterfallHook,\n SyncHook,\n SyncWaterfallHook,\n} from '../utils/hooks';\nimport {\n assert,\n getRemoteInfo,\n getRemoteEntryUniqueKey,\n matchRemoteWithNameAndExpose,\n logger,\n} from '../utils';\nimport { DEFAULT_REMOTE_TYPE, DEFAULT_SCOPE } from '../constant';\nimport { Module, ModuleOptions } from '../module';\nimport { formatPreloadArgs, preloadAssets } from '../utils/preload';\nimport { getGlobalShareScope } from '../utils/share';\nimport { getGlobalRemoteInfo } from '../plugins/snapshot/SnapshotHandler';\n\nexport interface LoadRemoteMatch {\n id: string;\n pkgNameOrAlias: string;\n expose: string;\n remote: Remote;\n options: Options;\n origin: ModuleFederation;\n remoteInfo: RemoteInfo;\n remoteSnapshot?: ModuleInfo;\n}\n\nexport class RemoteHandler {\n host: ModuleFederation;\n idToRemoteMap: Record<string, { name: string; expose: string }>;\n\n hooks = new PluginSystem({\n beforeRegisterRemote: new SyncWaterfallHook<{\n remote: Remote;\n origin: ModuleFederation;\n }>('beforeRegisterRemote'),\n registerRemote: new SyncWaterfallHook<{\n remote: Remote;\n origin: ModuleFederation;\n }>('registerRemote'),\n beforeRequest: new AsyncWaterfallHook<{\n id: string;\n options: Options;\n origin: ModuleFederation;\n }>('beforeRequest'),\n onLoad: new AsyncHook<\n [\n {\n id: string;\n expose: string;\n pkgNameOrAlias: string;\n remote: Remote;\n options: ModuleOptions;\n origin: ModuleFederation;\n exposeModule: any;\n exposeModuleFactory: any;\n moduleInstance: Module;\n },\n ],\n void\n >('onLoad'),\n handlePreloadModule: new SyncHook<\n [\n {\n id: string;\n name: string;\n remote: Remote;\n remoteSnapshot: ModuleInfo;\n preloadConfig: PreloadRemoteArgs;\n origin: ModuleFederation;\n },\n ],\n void\n >('handlePreloadModule'),\n errorLoadRemote: new AsyncHook<\n [\n {\n id: string;\n error: unknown;\n options?: any;\n from: CallFrom;\n lifecycle:\n | 'beforeRequest'\n | 'beforeLoadShare'\n | 'afterResolve'\n | 'onLoad';\n origin: ModuleFederation;\n },\n ],\n void | unknown\n >('errorLoadRemote'),\n beforePreloadRemote: new AsyncHook<\n [\n {\n preloadOps: Array<PreloadRemoteArgs>;\n options: Options;\n origin: ModuleFederation;\n },\n ]\n >('beforePreloadRemote'),\n generatePreloadAssets: new AsyncHook<\n [\n {\n origin: ModuleFederation;\n preloadOptions: PreloadOptions[number];\n remote: Remote;\n remoteInfo: RemoteInfo;\n remoteSnapshot: ModuleInfo;\n globalSnapshot: GlobalModuleInfo;\n },\n ],\n Promise<PreloadAssets>\n >('generatePreloadAssets'),\n // not used yet\n afterPreloadRemote: new AsyncHook<{\n preloadOps: Array<PreloadRemoteArgs>;\n options: Options;\n origin: ModuleFederation;\n }>(),\n // TODO: Move to loaderHook\n loadEntry: new AsyncHook<\n [\n {\n loaderHook: ModuleFederation['loaderHook'];\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports;\n },\n ],\n Promise<RemoteEntryExports>\n >(),\n });\n\n constructor(host: ModuleFederation) {\n this.host = host;\n this.idToRemoteMap = {};\n }\n\n formatAndRegisterRemote(globalOptions: Options, userOptions: UserOptions) {\n const userRemotes = userOptions.remotes || [];\n\n return userRemotes.reduce((res, remote) => {\n this.registerRemote(remote, res, { force: false });\n return res;\n }, globalOptions.remotes);\n }\n\n setIdToRemoteMap(id: string, remoteMatchInfo: LoadRemoteMatch) {\n const { remote, expose } = remoteMatchInfo;\n const { name, alias } = remote;\n this.idToRemoteMap[id] = { name: remote.name, expose };\n if (alias && id.startsWith(name)) {\n const idWithAlias = id.replace(name, alias);\n this.idToRemoteMap[idWithAlias] = { name: remote.name, expose };\n return;\n }\n\n if (alias && id.startsWith(alias)) {\n const idWithName = id.replace(alias, name);\n this.idToRemoteMap[idWithName] = { name: remote.name, expose };\n }\n }\n\n // eslint-disable-next-line max-lines-per-function\n // eslint-disable-next-line @typescript-eslint/member-ordering\n async loadRemote<T>(\n id: string,\n options?: { loadFactory?: boolean; from: CallFrom },\n ): Promise<T | null> {\n const { host } = this;\n try {\n const { loadFactory = true } = options || {\n loadFactory: true,\n };\n // 1. Validate the parameters of the retrieved module. There are two module request methods: pkgName + expose and alias + expose.\n // 2. Request the snapshot information of the current host and globally store the obtained snapshot information. The retrieved module information is partially offline and partially online. The online module information will retrieve the modules used online.\n // 3. Retrieve the detailed information of the current module from global (remoteEntry address, expose resource address)\n // 4. After retrieving remoteEntry, call the init of the module, and then retrieve the exported content of the module through get\n // id: pkgName(@federation/app1) + expose(button) = @federation/app1/button\n // id: alias(app1) + expose(button) = app1/button\n // id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort\n const { module, moduleOptions, remoteMatchInfo } =\n await this.getRemoteModuleAndOptions({\n id,\n });\n const {\n pkgNameOrAlias,\n remote,\n expose,\n id: idRes,\n remoteSnapshot,\n } = remoteMatchInfo;\n\n const moduleOrFactory = (await module.get(\n idRes,\n expose,\n options,\n remoteSnapshot,\n )) as T;\n\n const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({\n id: idRes,\n pkgNameOrAlias,\n expose,\n exposeModule: loadFactory ? moduleOrFactory : undefined,\n exposeModuleFactory: loadFactory ? undefined : moduleOrFactory,\n remote,\n options: moduleOptions,\n moduleInstance: module,\n origin: host,\n });\n\n this.setIdToRemoteMap(id, remoteMatchInfo);\n if (typeof moduleWrapper === 'function') {\n return moduleWrapper as T;\n }\n\n return moduleOrFactory;\n } catch (error) {\n const { from = 'runtime' } = options || { from: 'runtime' };\n\n const failOver = await this.hooks.lifecycle.errorLoadRemote.emit({\n id,\n error,\n from,\n lifecycle: 'onLoad',\n origin: host,\n });\n\n if (!failOver) {\n throw error;\n }\n\n return failOver as T;\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/member-ordering\n async preloadRemote(preloadOptions: Array<PreloadRemoteArgs>): Promise<void> {\n const { host } = this;\n\n await this.hooks.lifecycle.beforePreloadRemote.emit({\n preloadOps: preloadOptions,\n options: host.options,\n origin: host,\n });\n\n const preloadOps: PreloadOptions = formatPreloadArgs(\n host.options.remotes,\n preloadOptions,\n );\n\n await Promise.all(\n preloadOps.map(async (ops) => {\n const { remote } = ops;\n const remoteInfo = getRemoteInfo(remote);\n const { globalSnapshot, remoteSnapshot } =\n await host.snapshotHandler.loadRemoteSnapshotInfo({\n moduleInfo: remote,\n });\n\n const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({\n origin: host,\n preloadOptions: ops,\n remote,\n remoteInfo,\n globalSnapshot,\n remoteSnapshot,\n });\n if (!assets) {\n return;\n }\n preloadAssets(remoteInfo, host, assets);\n }),\n );\n }\n\n registerRemotes(remotes: Remote[], options?: { force?: boolean }): void {\n const { host } = this;\n remotes.forEach((remote) => {\n this.registerRemote(remote, host.options.remotes, {\n force: options?.force,\n });\n });\n }\n\n async getRemoteModuleAndOptions(options: { id: string }): Promise<{\n module: Module;\n moduleOptions: ModuleOptions;\n remoteMatchInfo: LoadRemoteMatch;\n }> {\n const { host } = this;\n const { id } = options;\n let loadRemoteArgs;\n\n try {\n loadRemoteArgs = await this.hooks.lifecycle.beforeRequest.emit({\n id,\n options: host.options,\n origin: host,\n });\n } catch (error) {\n loadRemoteArgs = (await this.hooks.lifecycle.errorLoadRemote.emit({\n id,\n options: host.options,\n origin: host,\n from: 'runtime',\n error,\n lifecycle: 'beforeRequest',\n })) as {\n id: string;\n options: Options;\n origin: ModuleFederation;\n };\n\n if (!loadRemoteArgs) {\n throw error;\n }\n }\n\n const { id: idRes } = loadRemoteArgs;\n\n const remoteSplitInfo = matchRemoteWithNameAndExpose(\n host.options.remotes,\n idRes,\n );\n assert(\n remoteSplitInfo,\n getShortErrorMsg(RUNTIME_004, runtimeDescMap, {\n hostName: host.options.name,\n requestId: idRes,\n }),\n );\n\n const { remote: rawRemote } = remoteSplitInfo;\n const remoteInfo = getRemoteInfo(rawRemote);\n const matchInfo =\n await host.sharedHandler.hooks.lifecycle.afterResolve.emit({\n id: idRes,\n ...remoteSplitInfo,\n options: host.options,\n origin: host,\n remoteInfo,\n });\n\n const { remote, expose } = matchInfo;\n assert(\n remote && expose,\n `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`,\n );\n let module: Module | undefined = host.moduleCache.get(remote.name);\n\n const moduleOptions: ModuleOptions = {\n host: host,\n remoteInfo,\n };\n\n if (!module) {\n module = new Module(moduleOptions);\n host.moduleCache.set(remote.name, module);\n }\n return {\n module,\n moduleOptions,\n remoteMatchInfo: matchInfo,\n };\n }\n\n registerRemote(\n remote: Remote,\n targetRemotes: Remote[],\n options?: { force?: boolean },\n ): void {\n const { host } = this;\n const normalizeRemote = () => {\n if (remote.alias) {\n // Validate if alias equals the prefix of remote.name and remote.alias, if so, throw an error\n // As multi-level path references cannot guarantee unique names, alias being a prefix of remote.name is not supported\n const findEqual = targetRemotes.find(\n (item) =>\n remote.alias &&\n (item.name.startsWith(remote.alias) ||\n item.alias?.startsWith(remote.alias)),\n );\n assert(\n !findEqual,\n `The alias ${remote.alias} of remote ${\n remote.name\n } is not allowed to be the prefix of ${\n findEqual && findEqual.name\n } name or alias`,\n );\n }\n // Set the remote entry to a complete path\n if ('entry' in remote) {\n if (isBrowserEnv() && !remote.entry.startsWith('http')) {\n remote.entry = new URL(remote.entry, window.location.origin).href;\n }\n }\n if (!remote.shareScope) {\n remote.shareScope = DEFAULT_SCOPE;\n }\n if (!remote.type) {\n remote.type = DEFAULT_REMOTE_TYPE;\n }\n };\n this.hooks.lifecycle.beforeRegisterRemote.emit({ remote, origin: host });\n const registeredRemote = targetRemotes.find(\n (item) => item.name === remote.name,\n );\n if (!registeredRemote) {\n normalizeRemote();\n targetRemotes.push(remote);\n this.hooks.lifecycle.registerRemote.emit({ remote, origin: host });\n } else {\n const messages = [\n `The remote \"${remote.name}\" is already registered.`,\n 'Please note that overriding it may cause unexpected errors.',\n ];\n if (options?.force) {\n // remove registered remote\n this.removeRemote(registeredRemote);\n normalizeRemote();\n targetRemotes.push(remote);\n this.hooks.lifecycle.registerRemote.emit({ remote, origin: host });\n warn(messages.join(' '));\n }\n }\n }\n\n private removeRemote(remote: Remote): void {\n try {\n const { host } = this;\n const { name } = remote;\n const remoteIndex = host.options.remotes.findIndex(\n (item) => item.name === name,\n );\n if (remoteIndex !== -1) {\n host.options.remotes.splice(remoteIndex, 1);\n }\n const loadedModule = host.moduleCache.get(remote.name);\n if (loadedModule) {\n const remoteInfo = loadedModule.remoteInfo;\n const key = remoteInfo.entryGlobalName as keyof typeof CurrentGlobal;\n\n if (CurrentGlobal[key]) {\n if (\n Object.getOwnPropertyDescriptor(CurrentGlobal, key)?.configurable\n ) {\n delete CurrentGlobal[key];\n } else {\n // @ts-ignore\n CurrentGlobal[key] = undefined;\n }\n }\n const remoteEntryUniqueKey = getRemoteEntryUniqueKey(\n loadedModule.remoteInfo,\n );\n\n if (globalLoading[remoteEntryUniqueKey]) {\n delete globalLoading[remoteEntryUniqueKey];\n }\n\n host.snapshotHandler.manifestCache.delete(remoteInfo.entry);\n\n // delete unloaded shared and instance\n let remoteInsId = remoteInfo.buildVersion\n ? composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion)\n : remoteInfo.name;\n const remoteInsIndex =\n CurrentGlobal.__FEDERATION__.__INSTANCES__.findIndex((ins) => {\n if (remoteInfo.buildVersion) {\n return ins.options.id === remoteInsId;\n } else {\n return ins.name === remoteInsId;\n }\n });\n if (remoteInsIndex !== -1) {\n const remoteIns =\n CurrentGlobal.__FEDERATION__.__INSTANCES__[remoteInsIndex];\n remoteInsId = remoteIns.options.id || remoteInsId;\n const globalShareScopeMap = getGlobalShareScope();\n\n let isAllSharedNotUsed = true;\n const needDeleteKeys: Array<[string, string, string, string]> = [];\n Object.keys(globalShareScopeMap).forEach((instId) => {\n const shareScopeMap = globalShareScopeMap[instId];\n shareScopeMap &&\n Object.keys(shareScopeMap).forEach((shareScope) => {\n const shareScopeVal = shareScopeMap[shareScope];\n shareScopeVal &&\n Object.keys(shareScopeVal).forEach((shareName) => {\n const sharedPkgs = shareScopeVal[shareName];\n sharedPkgs &&\n Object.keys(sharedPkgs).forEach((shareVersion) => {\n const shared = sharedPkgs[shareVersion];\n if (\n shared &&\n typeof shared === 'object' &&\n shared.from === remoteInfo.name\n ) {\n if (shared.loaded || shared.loading) {\n shared.useIn = shared.useIn.filter(\n (usedHostName) =>\n usedHostName !== remoteInfo.name,\n );\n if (shared.useIn.length) {\n isAllSharedNotUsed = false;\n } else {\n needDeleteKeys.push([\n instId,\n shareScope,\n shareName,\n shareVersion,\n ]);\n }\n } else {\n needDeleteKeys.push([\n instId,\n shareScope,\n shareName,\n shareVersion,\n ]);\n }\n }\n });\n });\n });\n });\n\n if (isAllSharedNotUsed) {\n remoteIns.shareScopeMap = {};\n delete globalShareScopeMap[remoteInsId];\n }\n needDeleteKeys.forEach(\n ([insId, shareScope, shareName, shareVersion]) => {\n delete globalShareScopeMap[insId]?.[shareScope]?.[shareName]?.[\n shareVersion\n ];\n },\n );\n CurrentGlobal.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);\n }\n\n const { hostGlobalSnapshot } = getGlobalRemoteInfo(remote, host);\n if (hostGlobalSnapshot) {\n const remoteKey =\n hostGlobalSnapshot &&\n 'remotesInfo' in hostGlobalSnapshot &&\n hostGlobalSnapshot.remotesInfo &&\n getInfoWithoutType(hostGlobalSnapshot.remotesInfo, remote.name).key;\n if (remoteKey) {\n delete hostGlobalSnapshot.remotesInfo[remoteKey];\n if (\n //eslint-disable-next-line no-extra-boolean-cast\n Boolean(Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey])\n ) {\n delete Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey];\n }\n }\n }\n\n host.moduleCache.delete(remote.name);\n }\n } catch (err) {\n logger.log('removeRemote fail: ', err);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA6DA,IAAa,gBAAb,MAA2B;CAyGzB,YAAY,MAAwB;eArG5B,IAAIA,kCAAa;GACvB,sBAAsB,IAAIC,4CAGvB,uBAAuB;GAC1B,gBAAgB,IAAIA,4CAGjB,iBAAiB;GACpB,eAAe,IAAIC,+CAIhB,gBAAgB;GACnB,QAAQ,IAAIC,4BAeV,SAAS;GACX,qBAAqB,IAAIC,0BAYvB,sBAAsB;GACxB,iBAAiB,IAAID,4BAgBnB,kBAAkB;GACpB,qBAAqB,IAAIA,4BAQvB,sBAAsB;GACxB,uBAAuB,IAAIA,4BAYzB,wBAAwB;GAE1B,oBAAoB,IAAIA,6BAIpB;GAEJ,WAAW,IAAIA,6BASZ;GACJ,CAAC;AAGA,OAAK,OAAO;AACZ,OAAK,gBAAgB,EAAE;;CAGzB,wBAAwB,eA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AAC;AAClE,SAAI,WAAW;AACb,aAAO,mBAAmB,YAAY;AACtC,UAEE,QAAQC,sBAAO,eAAe,qBAAqB,WAAW,CAE9D,QAAOA,sBAAO,eAAe,qBAAqB;;;AAKxD,SAAK,YAAY,OAAO,OAAO,KAAK;;WAE/B,KAAK;AACZ,yBAAO,IAAI,uBAAuB,IAAI"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["PluginSystem","SyncWaterfallHook","AsyncWaterfallHook","AsyncHook","SyncHook","formatPreloadArgs","getRemoteInfo","matchRemoteWithNameAndExpose","RUNTIME_004","runtimeDescMap","optionsToMFContext","Module","isBrowserEnvValue","DEFAULT_SCOPE","DEFAULT_REMOTE_TYPE","CurrentGlobal","getRemoteEntryUniqueKey","globalLoading","getGlobalShareScope","getGlobalRemoteInfo","getInfoWithoutType","Global"],"sources":["../../src/remote/index.ts"],"sourcesContent":["import {\n isBrowserEnvValue,\n warn,\n composeKeyWithSeparator,\n ModuleInfo,\n GlobalModuleInfo,\n} from '@module-federation/sdk';\nimport { RUNTIME_004, runtimeDescMap } from '@module-federation/error-codes';\nimport {\n Global,\n getInfoWithoutType,\n globalLoading,\n CurrentGlobal,\n} from '../global';\nimport {\n Options,\n UserOptions,\n PreloadAssets,\n PreloadOptions,\n PreloadRemoteArgs,\n Remote,\n RemoteInfo,\n RemoteEntryExports,\n CallFrom,\n} from '../type';\nimport { ModuleFederation } from '../core';\nimport {\n PluginSystem,\n AsyncHook,\n AsyncWaterfallHook,\n SyncHook,\n SyncWaterfallHook,\n} from '../utils/hooks';\nimport {\n assert,\n error,\n getRemoteInfo,\n getRemoteEntryUniqueKey,\n matchRemoteWithNameAndExpose,\n optionsToMFContext,\n logger,\n} from '../utils';\nimport { DEFAULT_REMOTE_TYPE, DEFAULT_SCOPE } from '../constant';\nimport { Module, ModuleOptions } from '../module';\nimport { formatPreloadArgs, preloadAssets } from '../utils/preload';\nimport { getGlobalShareScope } from '../utils/share';\nimport { getGlobalRemoteInfo } from '../plugins/snapshot/SnapshotHandler';\n\nexport interface LoadRemoteMatch {\n id: string;\n pkgNameOrAlias: string;\n expose: string;\n remote: Remote;\n options: Options;\n origin: ModuleFederation;\n remoteInfo: RemoteInfo;\n remoteSnapshot?: ModuleInfo;\n}\n\nexport class RemoteHandler {\n host: ModuleFederation;\n idToRemoteMap: Record<string, { name: string; expose: string }>;\n\n hooks = new PluginSystem({\n beforeRegisterRemote: new SyncWaterfallHook<{\n remote: Remote;\n origin: ModuleFederation;\n }>('beforeRegisterRemote'),\n registerRemote: new SyncWaterfallHook<{\n remote: Remote;\n origin: ModuleFederation;\n }>('registerRemote'),\n beforeRequest: new AsyncWaterfallHook<{\n id: string;\n options: Options;\n origin: ModuleFederation;\n }>('beforeRequest'),\n onLoad: new AsyncHook<\n [\n {\n id: string;\n expose: string;\n pkgNameOrAlias: string;\n remote: Remote;\n options: ModuleOptions;\n origin: ModuleFederation;\n exposeModule: any;\n exposeModuleFactory: any;\n moduleInstance: Module;\n },\n ],\n void\n >('onLoad'),\n handlePreloadModule: new SyncHook<\n [\n {\n id: string;\n name: string;\n remote: Remote;\n remoteSnapshot: ModuleInfo;\n preloadConfig: PreloadRemoteArgs;\n origin: ModuleFederation;\n },\n ],\n void\n >('handlePreloadModule'),\n errorLoadRemote: new AsyncHook<\n [\n {\n id: string;\n error: unknown;\n options?: any;\n from: CallFrom;\n lifecycle:\n | 'beforeRequest'\n | 'beforeLoadShare'\n | 'afterResolve'\n | 'onLoad';\n origin: ModuleFederation;\n },\n ],\n void | unknown\n >('errorLoadRemote'),\n beforePreloadRemote: new AsyncHook<\n [\n {\n preloadOps: Array<PreloadRemoteArgs>;\n options: Options;\n origin: ModuleFederation;\n },\n ]\n >('beforePreloadRemote'),\n generatePreloadAssets: new AsyncHook<\n [\n {\n origin: ModuleFederation;\n preloadOptions: PreloadOptions[number];\n remote: Remote;\n remoteInfo: RemoteInfo;\n remoteSnapshot: ModuleInfo;\n globalSnapshot: GlobalModuleInfo;\n },\n ],\n Promise<PreloadAssets>\n >('generatePreloadAssets'),\n // not used yet\n afterPreloadRemote: new AsyncHook<{\n preloadOps: Array<PreloadRemoteArgs>;\n options: Options;\n origin: ModuleFederation;\n }>(),\n // TODO: Move to loaderHook\n loadEntry: new AsyncHook<\n [\n {\n loaderHook: ModuleFederation['loaderHook'];\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports;\n },\n ],\n Promise<RemoteEntryExports>\n >(),\n });\n\n constructor(host: ModuleFederation) {\n this.host = host;\n this.idToRemoteMap = {};\n }\n\n formatAndRegisterRemote(globalOptions: Options, userOptions: UserOptions) {\n const userRemotes = userOptions.remotes || [];\n\n return userRemotes.reduce((res, remote) => {\n this.registerRemote(remote, res, { force: false });\n return res;\n }, globalOptions.remotes);\n }\n\n setIdToRemoteMap(id: string, remoteMatchInfo: LoadRemoteMatch) {\n const { remote, expose } = remoteMatchInfo;\n const { name, alias } = remote;\n this.idToRemoteMap[id] = { name: remote.name, expose };\n if (alias && id.startsWith(name)) {\n const idWithAlias = id.replace(name, alias);\n this.idToRemoteMap[idWithAlias] = { name: remote.name, expose };\n return;\n }\n\n if (alias && id.startsWith(alias)) {\n const idWithName = id.replace(alias, name);\n this.idToRemoteMap[idWithName] = { name: remote.name, expose };\n }\n }\n\n // eslint-disable-next-line max-lines-per-function\n // eslint-disable-next-line @typescript-eslint/member-ordering\n async loadRemote<T>(\n id: string,\n options?: { loadFactory?: boolean; from: CallFrom },\n ): Promise<T | null> {\n const { host } = this;\n try {\n const { loadFactory = true } = options || {\n loadFactory: true,\n };\n // 1. Validate the parameters of the retrieved module. There are two module request methods: pkgName + expose and alias + expose.\n // 2. Request the snapshot information of the current host and globally store the obtained snapshot information. The retrieved module information is partially offline and partially online. The online module information will retrieve the modules used online.\n // 3. Retrieve the detailed information of the current module from global (remoteEntry address, expose resource address)\n // 4. After retrieving remoteEntry, call the init of the module, and then retrieve the exported content of the module through get\n // id: pkgName(@federation/app1) + expose(button) = @federation/app1/button\n // id: alias(app1) + expose(button) = app1/button\n // id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort\n const { module, moduleOptions, remoteMatchInfo } =\n await this.getRemoteModuleAndOptions({\n id,\n });\n const {\n pkgNameOrAlias,\n remote,\n expose,\n id: idRes,\n remoteSnapshot,\n } = remoteMatchInfo;\n\n const moduleOrFactory = (await module.get(\n idRes,\n expose,\n options,\n remoteSnapshot,\n )) as T;\n\n const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({\n id: idRes,\n pkgNameOrAlias,\n expose,\n exposeModule: loadFactory ? moduleOrFactory : undefined,\n exposeModuleFactory: loadFactory ? undefined : moduleOrFactory,\n remote,\n options: moduleOptions,\n moduleInstance: module,\n origin: host,\n });\n\n this.setIdToRemoteMap(id, remoteMatchInfo);\n if (typeof moduleWrapper === 'function') {\n return moduleWrapper as T;\n }\n\n return moduleOrFactory;\n } catch (error) {\n const { from = 'runtime' } = options || { from: 'runtime' };\n\n const failOver = await this.hooks.lifecycle.errorLoadRemote.emit({\n id,\n error,\n from,\n lifecycle: 'onLoad',\n origin: host,\n });\n\n if (!failOver) {\n throw error;\n }\n\n return failOver as T;\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/member-ordering\n async preloadRemote(preloadOptions: Array<PreloadRemoteArgs>): Promise<void> {\n const { host } = this;\n\n await this.hooks.lifecycle.beforePreloadRemote.emit({\n preloadOps: preloadOptions,\n options: host.options,\n origin: host,\n });\n\n const preloadOps: PreloadOptions = formatPreloadArgs(\n host.options.remotes,\n preloadOptions,\n );\n\n await Promise.all(\n preloadOps.map(async (ops) => {\n const { remote } = ops;\n const remoteInfo = getRemoteInfo(remote);\n const { globalSnapshot, remoteSnapshot } =\n await host.snapshotHandler.loadRemoteSnapshotInfo({\n moduleInfo: remote,\n });\n\n const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({\n origin: host,\n preloadOptions: ops,\n remote,\n remoteInfo,\n globalSnapshot,\n remoteSnapshot,\n });\n if (!assets) {\n return;\n }\n preloadAssets(remoteInfo, host, assets);\n }),\n );\n }\n\n registerRemotes(remotes: Remote[], options?: { force?: boolean }): void {\n const { host } = this;\n remotes.forEach((remote) => {\n this.registerRemote(remote, host.options.remotes, {\n force: options?.force,\n });\n });\n }\n\n async getRemoteModuleAndOptions(options: { id: string }): Promise<{\n module: Module;\n moduleOptions: ModuleOptions;\n remoteMatchInfo: LoadRemoteMatch;\n }> {\n const { host } = this;\n const { id } = options;\n let loadRemoteArgs;\n\n try {\n loadRemoteArgs = await this.hooks.lifecycle.beforeRequest.emit({\n id,\n options: host.options,\n origin: host,\n });\n } catch (error) {\n loadRemoteArgs = (await this.hooks.lifecycle.errorLoadRemote.emit({\n id,\n options: host.options,\n origin: host,\n from: 'runtime',\n error,\n lifecycle: 'beforeRequest',\n })) as {\n id: string;\n options: Options;\n origin: ModuleFederation;\n };\n\n if (!loadRemoteArgs) {\n throw error;\n }\n }\n\n const { id: idRes } = loadRemoteArgs;\n\n const remoteSplitInfo = matchRemoteWithNameAndExpose(\n host.options.remotes,\n idRes,\n );\n if (!remoteSplitInfo) {\n error(\n RUNTIME_004,\n runtimeDescMap,\n {\n hostName: host.options.name,\n requestId: idRes,\n },\n undefined,\n optionsToMFContext(host.options),\n );\n }\n\n const { remote: rawRemote } = remoteSplitInfo;\n const remoteInfo = getRemoteInfo(rawRemote);\n const matchInfo =\n await host.sharedHandler.hooks.lifecycle.afterResolve.emit({\n id: idRes,\n ...remoteSplitInfo,\n options: host.options,\n origin: host,\n remoteInfo,\n });\n\n const { remote, expose } = matchInfo;\n assert(\n remote && expose,\n `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`,\n );\n let module: Module | undefined = host.moduleCache.get(remote.name);\n\n const moduleOptions: ModuleOptions = {\n host: host,\n remoteInfo,\n };\n\n if (!module) {\n module = new Module(moduleOptions);\n host.moduleCache.set(remote.name, module);\n }\n return {\n module,\n moduleOptions,\n remoteMatchInfo: matchInfo,\n };\n }\n\n registerRemote(\n remote: Remote,\n targetRemotes: Remote[],\n options?: { force?: boolean },\n ): void {\n const { host } = this;\n const normalizeRemote = () => {\n if (remote.alias) {\n // Validate if alias equals the prefix of remote.name and remote.alias, if so, throw an error\n // As multi-level path references cannot guarantee unique names, alias being a prefix of remote.name is not supported\n const findEqual = targetRemotes.find(\n (item) =>\n remote.alias &&\n (item.name.startsWith(remote.alias) ||\n item.alias?.startsWith(remote.alias)),\n );\n assert(\n !findEqual,\n `The alias ${remote.alias} of remote ${\n remote.name\n } is not allowed to be the prefix of ${\n findEqual && findEqual.name\n } name or alias`,\n );\n }\n // Set the remote entry to a complete path\n if ('entry' in remote) {\n if (\n isBrowserEnvValue &&\n typeof window !== 'undefined' &&\n !remote.entry.startsWith('http')\n ) {\n remote.entry = new URL(remote.entry, window.location.origin).href;\n }\n }\n if (!remote.shareScope) {\n remote.shareScope = DEFAULT_SCOPE;\n }\n if (!remote.type) {\n remote.type = DEFAULT_REMOTE_TYPE;\n }\n };\n this.hooks.lifecycle.beforeRegisterRemote.emit({ remote, origin: host });\n const registeredRemote = targetRemotes.find(\n (item) => item.name === remote.name,\n );\n if (!registeredRemote) {\n normalizeRemote();\n targetRemotes.push(remote);\n this.hooks.lifecycle.registerRemote.emit({ remote, origin: host });\n } else {\n const messages = [\n `The remote \"${remote.name}\" is already registered.`,\n 'Please note that overriding it may cause unexpected errors.',\n ];\n if (options?.force) {\n // remove registered remote\n this.removeRemote(registeredRemote);\n normalizeRemote();\n targetRemotes.push(remote);\n this.hooks.lifecycle.registerRemote.emit({ remote, origin: host });\n warn(messages.join(' '));\n }\n }\n }\n\n private removeRemote(remote: Remote): void {\n try {\n const { host } = this;\n const { name } = remote;\n const remoteIndex = host.options.remotes.findIndex(\n (item) => item.name === name,\n );\n if (remoteIndex !== -1) {\n host.options.remotes.splice(remoteIndex, 1);\n }\n const loadedModule = host.moduleCache.get(remote.name);\n if (loadedModule) {\n const remoteInfo = loadedModule.remoteInfo;\n const key = remoteInfo.entryGlobalName as keyof typeof CurrentGlobal;\n\n if (CurrentGlobal[key]) {\n if (\n Object.getOwnPropertyDescriptor(CurrentGlobal, key)?.configurable\n ) {\n delete CurrentGlobal[key];\n } else {\n // @ts-ignore\n CurrentGlobal[key] = undefined;\n }\n }\n const remoteEntryUniqueKey = getRemoteEntryUniqueKey(\n loadedModule.remoteInfo,\n );\n\n if (globalLoading[remoteEntryUniqueKey]) {\n delete globalLoading[remoteEntryUniqueKey];\n }\n\n host.snapshotHandler.manifestCache.delete(remoteInfo.entry);\n\n // delete unloaded shared and instance\n let remoteInsId = remoteInfo.buildVersion\n ? composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion)\n : remoteInfo.name;\n const remoteInsIndex =\n CurrentGlobal.__FEDERATION__.__INSTANCES__.findIndex((ins) => {\n if (remoteInfo.buildVersion) {\n return ins.options.id === remoteInsId;\n } else {\n return ins.name === remoteInsId;\n }\n });\n if (remoteInsIndex !== -1) {\n const remoteIns =\n CurrentGlobal.__FEDERATION__.__INSTANCES__[remoteInsIndex];\n remoteInsId = remoteIns.options.id || remoteInsId;\n const globalShareScopeMap = getGlobalShareScope();\n\n let isAllSharedNotUsed = true;\n const needDeleteKeys: Array<[string, string, string, string]> = [];\n Object.keys(globalShareScopeMap).forEach((instId) => {\n const shareScopeMap = globalShareScopeMap[instId];\n shareScopeMap &&\n Object.keys(shareScopeMap).forEach((shareScope) => {\n const shareScopeVal = shareScopeMap[shareScope];\n shareScopeVal &&\n Object.keys(shareScopeVal).forEach((shareName) => {\n const sharedPkgs = shareScopeVal[shareName];\n sharedPkgs &&\n Object.keys(sharedPkgs).forEach((shareVersion) => {\n const shared = sharedPkgs[shareVersion];\n if (\n shared &&\n typeof shared === 'object' &&\n shared.from === remoteInfo.name\n ) {\n if (shared.loaded || shared.loading) {\n shared.useIn = shared.useIn.filter(\n (usedHostName) =>\n usedHostName !== remoteInfo.name,\n );\n if (shared.useIn.length) {\n isAllSharedNotUsed = false;\n } else {\n needDeleteKeys.push([\n instId,\n shareScope,\n shareName,\n shareVersion,\n ]);\n }\n } else {\n needDeleteKeys.push([\n instId,\n shareScope,\n shareName,\n shareVersion,\n ]);\n }\n }\n });\n });\n });\n });\n\n if (isAllSharedNotUsed) {\n remoteIns.shareScopeMap = {};\n delete globalShareScopeMap[remoteInsId];\n }\n needDeleteKeys.forEach(\n ([insId, shareScope, shareName, shareVersion]) => {\n delete globalShareScopeMap[insId]?.[shareScope]?.[shareName]?.[\n shareVersion\n ];\n },\n );\n CurrentGlobal.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);\n }\n\n const { hostGlobalSnapshot } = getGlobalRemoteInfo(remote, host);\n if (hostGlobalSnapshot) {\n const remoteKey =\n hostGlobalSnapshot &&\n 'remotesInfo' in hostGlobalSnapshot &&\n hostGlobalSnapshot.remotesInfo &&\n getInfoWithoutType(hostGlobalSnapshot.remotesInfo, remote.name).key;\n if (remoteKey) {\n delete hostGlobalSnapshot.remotesInfo[remoteKey];\n if (\n //eslint-disable-next-line no-extra-boolean-cast\n Boolean(Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey])\n ) {\n delete Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey];\n }\n }\n }\n\n host.moduleCache.delete(remote.name);\n }\n } catch (err) {\n logger.error(\n `removeRemote failed: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA2DA,IAAa,gBAAb,MAA2B;CAyGzB,YAAY,MAAwB;eArG5B,IAAIA,kCAAa;GACvB,sBAAsB,IAAIC,4CAGvB,uBAAuB;GAC1B,gBAAgB,IAAIA,4CAGjB,iBAAiB;GACpB,eAAe,IAAIC,+CAIhB,gBAAgB;GACnB,QAAQ,IAAIC,4BAeV,SAAS;GACX,qBAAqB,IAAIC,0BAYvB,sBAAsB;GACxB,iBAAiB,IAAID,4BAgBnB,kBAAkB;GACpB,qBAAqB,IAAIA,4BAQvB,sBAAsB;GACxB,uBAAuB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AGL;AACD,kCAAc,eAAe,cAAc,OAAO,gBAAgB,EAAE;;IAGtE,MAAM,EAAE,uBAAuBC,4CAAoB,QAAQ,KAAK;AAChE,QAAI,oBAAoB;KACtB,MAAM,YACJ,sBACA,iBAAiB,sBACjB,mBAAmB,eACnBC,kCAAmB,mBAAmB,aAAa,OAAO,KAAK,CAAC;AAClE,SAAI,WAAW;AACb,aAAO,mBAAmB,YAAY;AACtC,UAEE,QAAQC,sBAAO,eAAe,qBAAqB,WAAW,CAE9D,QAAOA,sBAAO,eAAe,qBAAqB;;;AAKxD,SAAK,YAAY,OAAO,OAAO,KAAK;;WAE/B,KAAK;AACZ,yBAAO,MACL,wBAAwB,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,GACzE"}
|
package/dist/remote/index.d.ts
CHANGED
|
@@ -4,11 +4,9 @@ import { AsyncHook } from "../utils/hooks/asyncHook.js";
|
|
|
4
4
|
import { SyncWaterfallHook } from "../utils/hooks/syncWaterfallHook.js";
|
|
5
5
|
import { AsyncWaterfallHook } from "../utils/hooks/asyncWaterfallHooks.js";
|
|
6
6
|
import { PluginSystem } from "../utils/hooks/pluginSystem.js";
|
|
7
|
-
import "../utils/hooks/index.js";
|
|
8
7
|
import { ModuleFederation } from "../core.js";
|
|
9
8
|
import { CallFrom, Options, Remote, RemoteEntryExports, RemoteInfo, UserOptions } from "../type/config.js";
|
|
10
9
|
import { PreloadAssets, PreloadOptions, PreloadRemoteArgs } from "../type/preload.js";
|
|
11
|
-
import "../type/index.js";
|
|
12
10
|
import { GlobalModuleInfo, ModuleInfo } from "@module-federation/sdk";
|
|
13
11
|
|
|
14
12
|
//#region src/remote/index.d.ts
|
package/dist/remote/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { assert, logger } from "../utils/logger.js";
|
|
1
|
+
import { assert, error, logger } from "../utils/logger.js";
|
|
2
2
|
import { CurrentGlobal, Global, getInfoWithoutType, globalLoading } from "../global.js";
|
|
3
3
|
import { DEFAULT_REMOTE_TYPE, DEFAULT_SCOPE } from "../constant.js";
|
|
4
4
|
import { getGlobalShareScope } from "../utils/share.js";
|
|
5
5
|
import { matchRemoteWithNameAndExpose } from "../utils/manifest.js";
|
|
6
6
|
import { getRemoteEntryUniqueKey, getRemoteInfo } from "../utils/load.js";
|
|
7
|
+
import { optionsToMFContext } from "../utils/context.js";
|
|
7
8
|
import "../utils/index.js";
|
|
8
9
|
import { formatPreloadArgs, preloadAssets } from "../utils/preload.js";
|
|
9
10
|
import { Module as Module$1 } from "../module/index.js";
|
|
@@ -14,8 +15,8 @@ import { AsyncWaterfallHook } from "../utils/hooks/asyncWaterfallHooks.js";
|
|
|
14
15
|
import { PluginSystem } from "../utils/hooks/pluginSystem.js";
|
|
15
16
|
import "../utils/hooks/index.js";
|
|
16
17
|
import { getGlobalRemoteInfo } from "../plugins/snapshot/SnapshotHandler.js";
|
|
17
|
-
import { composeKeyWithSeparator,
|
|
18
|
-
import { RUNTIME_004,
|
|
18
|
+
import { composeKeyWithSeparator, isBrowserEnvValue, warn } from "@module-federation/sdk";
|
|
19
|
+
import { RUNTIME_004, runtimeDescMap } from "@module-federation/error-codes";
|
|
19
20
|
|
|
20
21
|
//#region src/remote/index.ts
|
|
21
22
|
var RemoteHandler = class {
|
|
@@ -151,10 +152,10 @@ var RemoteHandler = class {
|
|
|
151
152
|
}
|
|
152
153
|
const { id: idRes } = loadRemoteArgs;
|
|
153
154
|
const remoteSplitInfo = matchRemoteWithNameAndExpose(host.options.remotes, idRes);
|
|
154
|
-
|
|
155
|
+
if (!remoteSplitInfo) error(RUNTIME_004, runtimeDescMap, {
|
|
155
156
|
hostName: host.options.name,
|
|
156
157
|
requestId: idRes
|
|
157
|
-
}));
|
|
158
|
+
}, void 0, optionsToMFContext(host.options));
|
|
158
159
|
const { remote: rawRemote } = remoteSplitInfo;
|
|
159
160
|
const remoteInfo = getRemoteInfo(rawRemote);
|
|
160
161
|
const matchInfo = await host.sharedHandler.hooks.lifecycle.afterResolve.emit({
|
|
@@ -189,7 +190,7 @@ var RemoteHandler = class {
|
|
|
189
190
|
assert(!findEqual, `The alias ${remote.alias} of remote ${remote.name} is not allowed to be the prefix of ${findEqual && findEqual.name} name or alias`);
|
|
190
191
|
}
|
|
191
192
|
if ("entry" in remote) {
|
|
192
|
-
if (
|
|
193
|
+
if (isBrowserEnvValue && typeof window !== "undefined" && !remote.entry.startsWith("http")) remote.entry = new URL(remote.entry, window.location.origin).href;
|
|
193
194
|
}
|
|
194
195
|
if (!remote.shareScope) remote.shareScope = DEFAULT_SCOPE;
|
|
195
196
|
if (!remote.type) remote.type = DEFAULT_REMOTE_TYPE;
|
|
@@ -293,7 +294,7 @@ var RemoteHandler = class {
|
|
|
293
294
|
host.moduleCache.delete(remote.name);
|
|
294
295
|
}
|
|
295
296
|
} catch (err) {
|
|
296
|
-
logger.
|
|
297
|
+
logger.error(`removeRemote failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
297
298
|
}
|
|
298
299
|
}
|
|
299
300
|
};
|
package/dist/remote/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Module"],"sources":["../../src/remote/index.ts"],"sourcesContent":["import {\n isBrowserEnv,\n warn,\n composeKeyWithSeparator,\n ModuleInfo,\n GlobalModuleInfo,\n} from '@module-federation/sdk';\nimport {\n getShortErrorMsg,\n RUNTIME_004,\n runtimeDescMap,\n} from '@module-federation/error-codes';\nimport {\n Global,\n getInfoWithoutType,\n globalLoading,\n CurrentGlobal,\n} from '../global';\nimport {\n Options,\n UserOptions,\n PreloadAssets,\n PreloadOptions,\n PreloadRemoteArgs,\n Remote,\n RemoteInfo,\n RemoteEntryExports,\n CallFrom,\n} from '../type';\nimport { ModuleFederation } from '../core';\nimport {\n PluginSystem,\n AsyncHook,\n AsyncWaterfallHook,\n SyncHook,\n SyncWaterfallHook,\n} from '../utils/hooks';\nimport {\n assert,\n getRemoteInfo,\n getRemoteEntryUniqueKey,\n matchRemoteWithNameAndExpose,\n logger,\n} from '../utils';\nimport { DEFAULT_REMOTE_TYPE, DEFAULT_SCOPE } from '../constant';\nimport { Module, ModuleOptions } from '../module';\nimport { formatPreloadArgs, preloadAssets } from '../utils/preload';\nimport { getGlobalShareScope } from '../utils/share';\nimport { getGlobalRemoteInfo } from '../plugins/snapshot/SnapshotHandler';\n\nexport interface LoadRemoteMatch {\n id: string;\n pkgNameOrAlias: string;\n expose: string;\n remote: Remote;\n options: Options;\n origin: ModuleFederation;\n remoteInfo: RemoteInfo;\n remoteSnapshot?: ModuleInfo;\n}\n\nexport class RemoteHandler {\n host: ModuleFederation;\n idToRemoteMap: Record<string, { name: string; expose: string }>;\n\n hooks = new PluginSystem({\n beforeRegisterRemote: new SyncWaterfallHook<{\n remote: Remote;\n origin: ModuleFederation;\n }>('beforeRegisterRemote'),\n registerRemote: new SyncWaterfallHook<{\n remote: Remote;\n origin: ModuleFederation;\n }>('registerRemote'),\n beforeRequest: new AsyncWaterfallHook<{\n id: string;\n options: Options;\n origin: ModuleFederation;\n }>('beforeRequest'),\n onLoad: new AsyncHook<\n [\n {\n id: string;\n expose: string;\n pkgNameOrAlias: string;\n remote: Remote;\n options: ModuleOptions;\n origin: ModuleFederation;\n exposeModule: any;\n exposeModuleFactory: any;\n moduleInstance: Module;\n },\n ],\n void\n >('onLoad'),\n handlePreloadModule: new SyncHook<\n [\n {\n id: string;\n name: string;\n remote: Remote;\n remoteSnapshot: ModuleInfo;\n preloadConfig: PreloadRemoteArgs;\n origin: ModuleFederation;\n },\n ],\n void\n >('handlePreloadModule'),\n errorLoadRemote: new AsyncHook<\n [\n {\n id: string;\n error: unknown;\n options?: any;\n from: CallFrom;\n lifecycle:\n | 'beforeRequest'\n | 'beforeLoadShare'\n | 'afterResolve'\n | 'onLoad';\n origin: ModuleFederation;\n },\n ],\n void | unknown\n >('errorLoadRemote'),\n beforePreloadRemote: new AsyncHook<\n [\n {\n preloadOps: Array<PreloadRemoteArgs>;\n options: Options;\n origin: ModuleFederation;\n },\n ]\n >('beforePreloadRemote'),\n generatePreloadAssets: new AsyncHook<\n [\n {\n origin: ModuleFederation;\n preloadOptions: PreloadOptions[number];\n remote: Remote;\n remoteInfo: RemoteInfo;\n remoteSnapshot: ModuleInfo;\n globalSnapshot: GlobalModuleInfo;\n },\n ],\n Promise<PreloadAssets>\n >('generatePreloadAssets'),\n // not used yet\n afterPreloadRemote: new AsyncHook<{\n preloadOps: Array<PreloadRemoteArgs>;\n options: Options;\n origin: ModuleFederation;\n }>(),\n // TODO: Move to loaderHook\n loadEntry: new AsyncHook<\n [\n {\n loaderHook: ModuleFederation['loaderHook'];\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports;\n },\n ],\n Promise<RemoteEntryExports>\n >(),\n });\n\n constructor(host: ModuleFederation) {\n this.host = host;\n this.idToRemoteMap = {};\n }\n\n formatAndRegisterRemote(globalOptions: Options, userOptions: UserOptions) {\n const userRemotes = userOptions.remotes || [];\n\n return userRemotes.reduce((res, remote) => {\n this.registerRemote(remote, res, { force: false });\n return res;\n }, globalOptions.remotes);\n }\n\n setIdToRemoteMap(id: string, remoteMatchInfo: LoadRemoteMatch) {\n const { remote, expose } = remoteMatchInfo;\n const { name, alias } = remote;\n this.idToRemoteMap[id] = { name: remote.name, expose };\n if (alias && id.startsWith(name)) {\n const idWithAlias = id.replace(name, alias);\n this.idToRemoteMap[idWithAlias] = { name: remote.name, expose };\n return;\n }\n\n if (alias && id.startsWith(alias)) {\n const idWithName = id.replace(alias, name);\n this.idToRemoteMap[idWithName] = { name: remote.name, expose };\n }\n }\n\n // eslint-disable-next-line max-lines-per-function\n // eslint-disable-next-line @typescript-eslint/member-ordering\n async loadRemote<T>(\n id: string,\n options?: { loadFactory?: boolean; from: CallFrom },\n ): Promise<T | null> {\n const { host } = this;\n try {\n const { loadFactory = true } = options || {\n loadFactory: true,\n };\n // 1. Validate the parameters of the retrieved module. There are two module request methods: pkgName + expose and alias + expose.\n // 2. Request the snapshot information of the current host and globally store the obtained snapshot information. The retrieved module information is partially offline and partially online. The online module information will retrieve the modules used online.\n // 3. Retrieve the detailed information of the current module from global (remoteEntry address, expose resource address)\n // 4. After retrieving remoteEntry, call the init of the module, and then retrieve the exported content of the module through get\n // id: pkgName(@federation/app1) + expose(button) = @federation/app1/button\n // id: alias(app1) + expose(button) = app1/button\n // id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort\n const { module, moduleOptions, remoteMatchInfo } =\n await this.getRemoteModuleAndOptions({\n id,\n });\n const {\n pkgNameOrAlias,\n remote,\n expose,\n id: idRes,\n remoteSnapshot,\n } = remoteMatchInfo;\n\n const moduleOrFactory = (await module.get(\n idRes,\n expose,\n options,\n remoteSnapshot,\n )) as T;\n\n const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({\n id: idRes,\n pkgNameOrAlias,\n expose,\n exposeModule: loadFactory ? moduleOrFactory : undefined,\n exposeModuleFactory: loadFactory ? undefined : moduleOrFactory,\n remote,\n options: moduleOptions,\n moduleInstance: module,\n origin: host,\n });\n\n this.setIdToRemoteMap(id, remoteMatchInfo);\n if (typeof moduleWrapper === 'function') {\n return moduleWrapper as T;\n }\n\n return moduleOrFactory;\n } catch (error) {\n const { from = 'runtime' } = options || { from: 'runtime' };\n\n const failOver = await this.hooks.lifecycle.errorLoadRemote.emit({\n id,\n error,\n from,\n lifecycle: 'onLoad',\n origin: host,\n });\n\n if (!failOver) {\n throw error;\n }\n\n return failOver as T;\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/member-ordering\n async preloadRemote(preloadOptions: Array<PreloadRemoteArgs>): Promise<void> {\n const { host } = this;\n\n await this.hooks.lifecycle.beforePreloadRemote.emit({\n preloadOps: preloadOptions,\n options: host.options,\n origin: host,\n });\n\n const preloadOps: PreloadOptions = formatPreloadArgs(\n host.options.remotes,\n preloadOptions,\n );\n\n await Promise.all(\n preloadOps.map(async (ops) => {\n const { remote } = ops;\n const remoteInfo = getRemoteInfo(remote);\n const { globalSnapshot, remoteSnapshot } =\n await host.snapshotHandler.loadRemoteSnapshotInfo({\n moduleInfo: remote,\n });\n\n const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({\n origin: host,\n preloadOptions: ops,\n remote,\n remoteInfo,\n globalSnapshot,\n remoteSnapshot,\n });\n if (!assets) {\n return;\n }\n preloadAssets(remoteInfo, host, assets);\n }),\n );\n }\n\n registerRemotes(remotes: Remote[], options?: { force?: boolean }): void {\n const { host } = this;\n remotes.forEach((remote) => {\n this.registerRemote(remote, host.options.remotes, {\n force: options?.force,\n });\n });\n }\n\n async getRemoteModuleAndOptions(options: { id: string }): Promise<{\n module: Module;\n moduleOptions: ModuleOptions;\n remoteMatchInfo: LoadRemoteMatch;\n }> {\n const { host } = this;\n const { id } = options;\n let loadRemoteArgs;\n\n try {\n loadRemoteArgs = await this.hooks.lifecycle.beforeRequest.emit({\n id,\n options: host.options,\n origin: host,\n });\n } catch (error) {\n loadRemoteArgs = (await this.hooks.lifecycle.errorLoadRemote.emit({\n id,\n options: host.options,\n origin: host,\n from: 'runtime',\n error,\n lifecycle: 'beforeRequest',\n })) as {\n id: string;\n options: Options;\n origin: ModuleFederation;\n };\n\n if (!loadRemoteArgs) {\n throw error;\n }\n }\n\n const { id: idRes } = loadRemoteArgs;\n\n const remoteSplitInfo = matchRemoteWithNameAndExpose(\n host.options.remotes,\n idRes,\n );\n assert(\n remoteSplitInfo,\n getShortErrorMsg(RUNTIME_004, runtimeDescMap, {\n hostName: host.options.name,\n requestId: idRes,\n }),\n );\n\n const { remote: rawRemote } = remoteSplitInfo;\n const remoteInfo = getRemoteInfo(rawRemote);\n const matchInfo =\n await host.sharedHandler.hooks.lifecycle.afterResolve.emit({\n id: idRes,\n ...remoteSplitInfo,\n options: host.options,\n origin: host,\n remoteInfo,\n });\n\n const { remote, expose } = matchInfo;\n assert(\n remote && expose,\n `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`,\n );\n let module: Module | undefined = host.moduleCache.get(remote.name);\n\n const moduleOptions: ModuleOptions = {\n host: host,\n remoteInfo,\n };\n\n if (!module) {\n module = new Module(moduleOptions);\n host.moduleCache.set(remote.name, module);\n }\n return {\n module,\n moduleOptions,\n remoteMatchInfo: matchInfo,\n };\n }\n\n registerRemote(\n remote: Remote,\n targetRemotes: Remote[],\n options?: { force?: boolean },\n ): void {\n const { host } = this;\n const normalizeRemote = () => {\n if (remote.alias) {\n // Validate if alias equals the prefix of remote.name and remote.alias, if so, throw an error\n // As multi-level path references cannot guarantee unique names, alias being a prefix of remote.name is not supported\n const findEqual = targetRemotes.find(\n (item) =>\n remote.alias &&\n (item.name.startsWith(remote.alias) ||\n item.alias?.startsWith(remote.alias)),\n );\n assert(\n !findEqual,\n `The alias ${remote.alias} of remote ${\n remote.name\n } is not allowed to be the prefix of ${\n findEqual && findEqual.name\n } name or alias`,\n );\n }\n // Set the remote entry to a complete path\n if ('entry' in remote) {\n if (isBrowserEnv() && !remote.entry.startsWith('http')) {\n remote.entry = new URL(remote.entry, window.location.origin).href;\n }\n }\n if (!remote.shareScope) {\n remote.shareScope = DEFAULT_SCOPE;\n }\n if (!remote.type) {\n remote.type = DEFAULT_REMOTE_TYPE;\n }\n };\n this.hooks.lifecycle.beforeRegisterRemote.emit({ remote, origin: host });\n const registeredRemote = targetRemotes.find(\n (item) => item.name === remote.name,\n );\n if (!registeredRemote) {\n normalizeRemote();\n targetRemotes.push(remote);\n this.hooks.lifecycle.registerRemote.emit({ remote, origin: host });\n } else {\n const messages = [\n `The remote \"${remote.name}\" is already registered.`,\n 'Please note that overriding it may cause unexpected errors.',\n ];\n if (options?.force) {\n // remove registered remote\n this.removeRemote(registeredRemote);\n normalizeRemote();\n targetRemotes.push(remote);\n this.hooks.lifecycle.registerRemote.emit({ remote, origin: host });\n warn(messages.join(' '));\n }\n }\n }\n\n private removeRemote(remote: Remote): void {\n try {\n const { host } = this;\n const { name } = remote;\n const remoteIndex = host.options.remotes.findIndex(\n (item) => item.name === name,\n );\n if (remoteIndex !== -1) {\n host.options.remotes.splice(remoteIndex, 1);\n }\n const loadedModule = host.moduleCache.get(remote.name);\n if (loadedModule) {\n const remoteInfo = loadedModule.remoteInfo;\n const key = remoteInfo.entryGlobalName as keyof typeof CurrentGlobal;\n\n if (CurrentGlobal[key]) {\n if (\n Object.getOwnPropertyDescriptor(CurrentGlobal, key)?.configurable\n ) {\n delete CurrentGlobal[key];\n } else {\n // @ts-ignore\n CurrentGlobal[key] = undefined;\n }\n }\n const remoteEntryUniqueKey = getRemoteEntryUniqueKey(\n loadedModule.remoteInfo,\n );\n\n if (globalLoading[remoteEntryUniqueKey]) {\n delete globalLoading[remoteEntryUniqueKey];\n }\n\n host.snapshotHandler.manifestCache.delete(remoteInfo.entry);\n\n // delete unloaded shared and instance\n let remoteInsId = remoteInfo.buildVersion\n ? composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion)\n : remoteInfo.name;\n const remoteInsIndex =\n CurrentGlobal.__FEDERATION__.__INSTANCES__.findIndex((ins) => {\n if (remoteInfo.buildVersion) {\n return ins.options.id === remoteInsId;\n } else {\n return ins.name === remoteInsId;\n }\n });\n if (remoteInsIndex !== -1) {\n const remoteIns =\n CurrentGlobal.__FEDERATION__.__INSTANCES__[remoteInsIndex];\n remoteInsId = remoteIns.options.id || remoteInsId;\n const globalShareScopeMap = getGlobalShareScope();\n\n let isAllSharedNotUsed = true;\n const needDeleteKeys: Array<[string, string, string, string]> = [];\n Object.keys(globalShareScopeMap).forEach((instId) => {\n const shareScopeMap = globalShareScopeMap[instId];\n shareScopeMap &&\n Object.keys(shareScopeMap).forEach((shareScope) => {\n const shareScopeVal = shareScopeMap[shareScope];\n shareScopeVal &&\n Object.keys(shareScopeVal).forEach((shareName) => {\n const sharedPkgs = shareScopeVal[shareName];\n sharedPkgs &&\n Object.keys(sharedPkgs).forEach((shareVersion) => {\n const shared = sharedPkgs[shareVersion];\n if (\n shared &&\n typeof shared === 'object' &&\n shared.from === remoteInfo.name\n ) {\n if (shared.loaded || shared.loading) {\n shared.useIn = shared.useIn.filter(\n (usedHostName) =>\n usedHostName !== remoteInfo.name,\n );\n if (shared.useIn.length) {\n isAllSharedNotUsed = false;\n } else {\n needDeleteKeys.push([\n instId,\n shareScope,\n shareName,\n shareVersion,\n ]);\n }\n } else {\n needDeleteKeys.push([\n instId,\n shareScope,\n shareName,\n shareVersion,\n ]);\n }\n }\n });\n });\n });\n });\n\n if (isAllSharedNotUsed) {\n remoteIns.shareScopeMap = {};\n delete globalShareScopeMap[remoteInsId];\n }\n needDeleteKeys.forEach(\n ([insId, shareScope, shareName, shareVersion]) => {\n delete globalShareScopeMap[insId]?.[shareScope]?.[shareName]?.[\n shareVersion\n ];\n },\n );\n CurrentGlobal.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);\n }\n\n const { hostGlobalSnapshot } = getGlobalRemoteInfo(remote, host);\n if (hostGlobalSnapshot) {\n const remoteKey =\n hostGlobalSnapshot &&\n 'remotesInfo' in hostGlobalSnapshot &&\n hostGlobalSnapshot.remotesInfo &&\n getInfoWithoutType(hostGlobalSnapshot.remotesInfo, remote.name).key;\n if (remoteKey) {\n delete hostGlobalSnapshot.remotesInfo[remoteKey];\n if (\n //eslint-disable-next-line no-extra-boolean-cast\n Boolean(Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey])\n ) {\n delete Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey];\n }\n }\n }\n\n host.moduleCache.delete(remote.name);\n }\n } catch (err) {\n logger.log('removeRemote fail: ', err);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA6DA,IAAa,gBAAb,MAA2B;CAyGzB,YAAY,MAAwB;eArG5B,IAAI,aAAa;GACvB,sBAAsB,IAAI,kBAGvB,uBAAuB;GAC1B,gBAAgB,IAAI,kBAGjB,iBAAiB;GACpB,eAAe,IAAI,mBAIhB,gBAAgB;GACnB,QAAQ,IAAI,UAeV,SAAS;GACX,qBAAqB,IAAI,SAYv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,SAAS,cAAc,aAChD;OAGL;AACD,mBAAc,eAAe,cAAc,OAAO,gBAAgB,EAAE;;IAGtE,MAAM,EAAE,uBAAuB,oBAAoB,QAAQ,KAAK;AAChE,QAAI,oBAAoB;KACtB,MAAM,YACJ,sBACA,iBAAiB,sBACjB,mBAAmB,eACnB,mBAAmB,mBAAmB,aAAa,OAAO,KAAK,CAAC;AAClE,SAAI,WAAW;AACb,aAAO,mBAAmB,YAAY;AACtC,UAEE,QAAQ,OAAO,eAAe,qBAAqB,WAAW,CAE9D,QAAO,OAAO,eAAe,qBAAqB;;;AAKxD,SAAK,YAAY,OAAO,OAAO,KAAK;;WAE/B,KAAK;AACZ,UAAO,IAAI,uBAAuB,IAAI"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["Module"],"sources":["../../src/remote/index.ts"],"sourcesContent":["import {\n isBrowserEnvValue,\n warn,\n composeKeyWithSeparator,\n ModuleInfo,\n GlobalModuleInfo,\n} from '@module-federation/sdk';\nimport { RUNTIME_004, runtimeDescMap } from '@module-federation/error-codes';\nimport {\n Global,\n getInfoWithoutType,\n globalLoading,\n CurrentGlobal,\n} from '../global';\nimport {\n Options,\n UserOptions,\n PreloadAssets,\n PreloadOptions,\n PreloadRemoteArgs,\n Remote,\n RemoteInfo,\n RemoteEntryExports,\n CallFrom,\n} from '../type';\nimport { ModuleFederation } from '../core';\nimport {\n PluginSystem,\n AsyncHook,\n AsyncWaterfallHook,\n SyncHook,\n SyncWaterfallHook,\n} from '../utils/hooks';\nimport {\n assert,\n error,\n getRemoteInfo,\n getRemoteEntryUniqueKey,\n matchRemoteWithNameAndExpose,\n optionsToMFContext,\n logger,\n} from '../utils';\nimport { DEFAULT_REMOTE_TYPE, DEFAULT_SCOPE } from '../constant';\nimport { Module, ModuleOptions } from '../module';\nimport { formatPreloadArgs, preloadAssets } from '../utils/preload';\nimport { getGlobalShareScope } from '../utils/share';\nimport { getGlobalRemoteInfo } from '../plugins/snapshot/SnapshotHandler';\n\nexport interface LoadRemoteMatch {\n id: string;\n pkgNameOrAlias: string;\n expose: string;\n remote: Remote;\n options: Options;\n origin: ModuleFederation;\n remoteInfo: RemoteInfo;\n remoteSnapshot?: ModuleInfo;\n}\n\nexport class RemoteHandler {\n host: ModuleFederation;\n idToRemoteMap: Record<string, { name: string; expose: string }>;\n\n hooks = new PluginSystem({\n beforeRegisterRemote: new SyncWaterfallHook<{\n remote: Remote;\n origin: ModuleFederation;\n }>('beforeRegisterRemote'),\n registerRemote: new SyncWaterfallHook<{\n remote: Remote;\n origin: ModuleFederation;\n }>('registerRemote'),\n beforeRequest: new AsyncWaterfallHook<{\n id: string;\n options: Options;\n origin: ModuleFederation;\n }>('beforeRequest'),\n onLoad: new AsyncHook<\n [\n {\n id: string;\n expose: string;\n pkgNameOrAlias: string;\n remote: Remote;\n options: ModuleOptions;\n origin: ModuleFederation;\n exposeModule: any;\n exposeModuleFactory: any;\n moduleInstance: Module;\n },\n ],\n void\n >('onLoad'),\n handlePreloadModule: new SyncHook<\n [\n {\n id: string;\n name: string;\n remote: Remote;\n remoteSnapshot: ModuleInfo;\n preloadConfig: PreloadRemoteArgs;\n origin: ModuleFederation;\n },\n ],\n void\n >('handlePreloadModule'),\n errorLoadRemote: new AsyncHook<\n [\n {\n id: string;\n error: unknown;\n options?: any;\n from: CallFrom;\n lifecycle:\n | 'beforeRequest'\n | 'beforeLoadShare'\n | 'afterResolve'\n | 'onLoad';\n origin: ModuleFederation;\n },\n ],\n void | unknown\n >('errorLoadRemote'),\n beforePreloadRemote: new AsyncHook<\n [\n {\n preloadOps: Array<PreloadRemoteArgs>;\n options: Options;\n origin: ModuleFederation;\n },\n ]\n >('beforePreloadRemote'),\n generatePreloadAssets: new AsyncHook<\n [\n {\n origin: ModuleFederation;\n preloadOptions: PreloadOptions[number];\n remote: Remote;\n remoteInfo: RemoteInfo;\n remoteSnapshot: ModuleInfo;\n globalSnapshot: GlobalModuleInfo;\n },\n ],\n Promise<PreloadAssets>\n >('generatePreloadAssets'),\n // not used yet\n afterPreloadRemote: new AsyncHook<{\n preloadOps: Array<PreloadRemoteArgs>;\n options: Options;\n origin: ModuleFederation;\n }>(),\n // TODO: Move to loaderHook\n loadEntry: new AsyncHook<\n [\n {\n loaderHook: ModuleFederation['loaderHook'];\n remoteInfo: RemoteInfo;\n remoteEntryExports?: RemoteEntryExports;\n },\n ],\n Promise<RemoteEntryExports>\n >(),\n });\n\n constructor(host: ModuleFederation) {\n this.host = host;\n this.idToRemoteMap = {};\n }\n\n formatAndRegisterRemote(globalOptions: Options, userOptions: UserOptions) {\n const userRemotes = userOptions.remotes || [];\n\n return userRemotes.reduce((res, remote) => {\n this.registerRemote(remote, res, { force: false });\n return res;\n }, globalOptions.remotes);\n }\n\n setIdToRemoteMap(id: string, remoteMatchInfo: LoadRemoteMatch) {\n const { remote, expose } = remoteMatchInfo;\n const { name, alias } = remote;\n this.idToRemoteMap[id] = { name: remote.name, expose };\n if (alias && id.startsWith(name)) {\n const idWithAlias = id.replace(name, alias);\n this.idToRemoteMap[idWithAlias] = { name: remote.name, expose };\n return;\n }\n\n if (alias && id.startsWith(alias)) {\n const idWithName = id.replace(alias, name);\n this.idToRemoteMap[idWithName] = { name: remote.name, expose };\n }\n }\n\n // eslint-disable-next-line max-lines-per-function\n // eslint-disable-next-line @typescript-eslint/member-ordering\n async loadRemote<T>(\n id: string,\n options?: { loadFactory?: boolean; from: CallFrom },\n ): Promise<T | null> {\n const { host } = this;\n try {\n const { loadFactory = true } = options || {\n loadFactory: true,\n };\n // 1. Validate the parameters of the retrieved module. There are two module request methods: pkgName + expose and alias + expose.\n // 2. Request the snapshot information of the current host and globally store the obtained snapshot information. The retrieved module information is partially offline and partially online. The online module information will retrieve the modules used online.\n // 3. Retrieve the detailed information of the current module from global (remoteEntry address, expose resource address)\n // 4. After retrieving remoteEntry, call the init of the module, and then retrieve the exported content of the module through get\n // id: pkgName(@federation/app1) + expose(button) = @federation/app1/button\n // id: alias(app1) + expose(button) = app1/button\n // id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort\n const { module, moduleOptions, remoteMatchInfo } =\n await this.getRemoteModuleAndOptions({\n id,\n });\n const {\n pkgNameOrAlias,\n remote,\n expose,\n id: idRes,\n remoteSnapshot,\n } = remoteMatchInfo;\n\n const moduleOrFactory = (await module.get(\n idRes,\n expose,\n options,\n remoteSnapshot,\n )) as T;\n\n const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({\n id: idRes,\n pkgNameOrAlias,\n expose,\n exposeModule: loadFactory ? moduleOrFactory : undefined,\n exposeModuleFactory: loadFactory ? undefined : moduleOrFactory,\n remote,\n options: moduleOptions,\n moduleInstance: module,\n origin: host,\n });\n\n this.setIdToRemoteMap(id, remoteMatchInfo);\n if (typeof moduleWrapper === 'function') {\n return moduleWrapper as T;\n }\n\n return moduleOrFactory;\n } catch (error) {\n const { from = 'runtime' } = options || { from: 'runtime' };\n\n const failOver = await this.hooks.lifecycle.errorLoadRemote.emit({\n id,\n error,\n from,\n lifecycle: 'onLoad',\n origin: host,\n });\n\n if (!failOver) {\n throw error;\n }\n\n return failOver as T;\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/member-ordering\n async preloadRemote(preloadOptions: Array<PreloadRemoteArgs>): Promise<void> {\n const { host } = this;\n\n await this.hooks.lifecycle.beforePreloadRemote.emit({\n preloadOps: preloadOptions,\n options: host.options,\n origin: host,\n });\n\n const preloadOps: PreloadOptions = formatPreloadArgs(\n host.options.remotes,\n preloadOptions,\n );\n\n await Promise.all(\n preloadOps.map(async (ops) => {\n const { remote } = ops;\n const remoteInfo = getRemoteInfo(remote);\n const { globalSnapshot, remoteSnapshot } =\n await host.snapshotHandler.loadRemoteSnapshotInfo({\n moduleInfo: remote,\n });\n\n const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({\n origin: host,\n preloadOptions: ops,\n remote,\n remoteInfo,\n globalSnapshot,\n remoteSnapshot,\n });\n if (!assets) {\n return;\n }\n preloadAssets(remoteInfo, host, assets);\n }),\n );\n }\n\n registerRemotes(remotes: Remote[], options?: { force?: boolean }): void {\n const { host } = this;\n remotes.forEach((remote) => {\n this.registerRemote(remote, host.options.remotes, {\n force: options?.force,\n });\n });\n }\n\n async getRemoteModuleAndOptions(options: { id: string }): Promise<{\n module: Module;\n moduleOptions: ModuleOptions;\n remoteMatchInfo: LoadRemoteMatch;\n }> {\n const { host } = this;\n const { id } = options;\n let loadRemoteArgs;\n\n try {\n loadRemoteArgs = await this.hooks.lifecycle.beforeRequest.emit({\n id,\n options: host.options,\n origin: host,\n });\n } catch (error) {\n loadRemoteArgs = (await this.hooks.lifecycle.errorLoadRemote.emit({\n id,\n options: host.options,\n origin: host,\n from: 'runtime',\n error,\n lifecycle: 'beforeRequest',\n })) as {\n id: string;\n options: Options;\n origin: ModuleFederation;\n };\n\n if (!loadRemoteArgs) {\n throw error;\n }\n }\n\n const { id: idRes } = loadRemoteArgs;\n\n const remoteSplitInfo = matchRemoteWithNameAndExpose(\n host.options.remotes,\n idRes,\n );\n if (!remoteSplitInfo) {\n error(\n RUNTIME_004,\n runtimeDescMap,\n {\n hostName: host.options.name,\n requestId: idRes,\n },\n undefined,\n optionsToMFContext(host.options),\n );\n }\n\n const { remote: rawRemote } = remoteSplitInfo;\n const remoteInfo = getRemoteInfo(rawRemote);\n const matchInfo =\n await host.sharedHandler.hooks.lifecycle.afterResolve.emit({\n id: idRes,\n ...remoteSplitInfo,\n options: host.options,\n origin: host,\n remoteInfo,\n });\n\n const { remote, expose } = matchInfo;\n assert(\n remote && expose,\n `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`,\n );\n let module: Module | undefined = host.moduleCache.get(remote.name);\n\n const moduleOptions: ModuleOptions = {\n host: host,\n remoteInfo,\n };\n\n if (!module) {\n module = new Module(moduleOptions);\n host.moduleCache.set(remote.name, module);\n }\n return {\n module,\n moduleOptions,\n remoteMatchInfo: matchInfo,\n };\n }\n\n registerRemote(\n remote: Remote,\n targetRemotes: Remote[],\n options?: { force?: boolean },\n ): void {\n const { host } = this;\n const normalizeRemote = () => {\n if (remote.alias) {\n // Validate if alias equals the prefix of remote.name and remote.alias, if so, throw an error\n // As multi-level path references cannot guarantee unique names, alias being a prefix of remote.name is not supported\n const findEqual = targetRemotes.find(\n (item) =>\n remote.alias &&\n (item.name.startsWith(remote.alias) ||\n item.alias?.startsWith(remote.alias)),\n );\n assert(\n !findEqual,\n `The alias ${remote.alias} of remote ${\n remote.name\n } is not allowed to be the prefix of ${\n findEqual && findEqual.name\n } name or alias`,\n );\n }\n // Set the remote entry to a complete path\n if ('entry' in remote) {\n if (\n isBrowserEnvValue &&\n typeof window !== 'undefined' &&\n !remote.entry.startsWith('http')\n ) {\n remote.entry = new URL(remote.entry, window.location.origin).href;\n }\n }\n if (!remote.shareScope) {\n remote.shareScope = DEFAULT_SCOPE;\n }\n if (!remote.type) {\n remote.type = DEFAULT_REMOTE_TYPE;\n }\n };\n this.hooks.lifecycle.beforeRegisterRemote.emit({ remote, origin: host });\n const registeredRemote = targetRemotes.find(\n (item) => item.name === remote.name,\n );\n if (!registeredRemote) {\n normalizeRemote();\n targetRemotes.push(remote);\n this.hooks.lifecycle.registerRemote.emit({ remote, origin: host });\n } else {\n const messages = [\n `The remote \"${remote.name}\" is already registered.`,\n 'Please note that overriding it may cause unexpected errors.',\n ];\n if (options?.force) {\n // remove registered remote\n this.removeRemote(registeredRemote);\n normalizeRemote();\n targetRemotes.push(remote);\n this.hooks.lifecycle.registerRemote.emit({ remote, origin: host });\n warn(messages.join(' '));\n }\n }\n }\n\n private removeRemote(remote: Remote): void {\n try {\n const { host } = this;\n const { name } = remote;\n const remoteIndex = host.options.remotes.findIndex(\n (item) => item.name === name,\n );\n if (remoteIndex !== -1) {\n host.options.remotes.splice(remoteIndex, 1);\n }\n const loadedModule = host.moduleCache.get(remote.name);\n if (loadedModule) {\n const remoteInfo = loadedModule.remoteInfo;\n const key = remoteInfo.entryGlobalName as keyof typeof CurrentGlobal;\n\n if (CurrentGlobal[key]) {\n if (\n Object.getOwnPropertyDescriptor(CurrentGlobal, key)?.configurable\n ) {\n delete CurrentGlobal[key];\n } else {\n // @ts-ignore\n CurrentGlobal[key] = undefined;\n }\n }\n const remoteEntryUniqueKey = getRemoteEntryUniqueKey(\n loadedModule.remoteInfo,\n );\n\n if (globalLoading[remoteEntryUniqueKey]) {\n delete globalLoading[remoteEntryUniqueKey];\n }\n\n host.snapshotHandler.manifestCache.delete(remoteInfo.entry);\n\n // delete unloaded shared and instance\n let remoteInsId = remoteInfo.buildVersion\n ? composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion)\n : remoteInfo.name;\n const remoteInsIndex =\n CurrentGlobal.__FEDERATION__.__INSTANCES__.findIndex((ins) => {\n if (remoteInfo.buildVersion) {\n return ins.options.id === remoteInsId;\n } else {\n return ins.name === remoteInsId;\n }\n });\n if (remoteInsIndex !== -1) {\n const remoteIns =\n CurrentGlobal.__FEDERATION__.__INSTANCES__[remoteInsIndex];\n remoteInsId = remoteIns.options.id || remoteInsId;\n const globalShareScopeMap = getGlobalShareScope();\n\n let isAllSharedNotUsed = true;\n const needDeleteKeys: Array<[string, string, string, string]> = [];\n Object.keys(globalShareScopeMap).forEach((instId) => {\n const shareScopeMap = globalShareScopeMap[instId];\n shareScopeMap &&\n Object.keys(shareScopeMap).forEach((shareScope) => {\n const shareScopeVal = shareScopeMap[shareScope];\n shareScopeVal &&\n Object.keys(shareScopeVal).forEach((shareName) => {\n const sharedPkgs = shareScopeVal[shareName];\n sharedPkgs &&\n Object.keys(sharedPkgs).forEach((shareVersion) => {\n const shared = sharedPkgs[shareVersion];\n if (\n shared &&\n typeof shared === 'object' &&\n shared.from === remoteInfo.name\n ) {\n if (shared.loaded || shared.loading) {\n shared.useIn = shared.useIn.filter(\n (usedHostName) =>\n usedHostName !== remoteInfo.name,\n );\n if (shared.useIn.length) {\n isAllSharedNotUsed = false;\n } else {\n needDeleteKeys.push([\n instId,\n shareScope,\n shareName,\n shareVersion,\n ]);\n }\n } else {\n needDeleteKeys.push([\n instId,\n shareScope,\n shareName,\n shareVersion,\n ]);\n }\n }\n });\n });\n });\n });\n\n if (isAllSharedNotUsed) {\n remoteIns.shareScopeMap = {};\n delete globalShareScopeMap[remoteInsId];\n }\n needDeleteKeys.forEach(\n ([insId, shareScope, shareName, shareVersion]) => {\n delete globalShareScopeMap[insId]?.[shareScope]?.[shareName]?.[\n shareVersion\n ];\n },\n );\n CurrentGlobal.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);\n }\n\n const { hostGlobalSnapshot } = getGlobalRemoteInfo(remote, host);\n if (hostGlobalSnapshot) {\n const remoteKey =\n hostGlobalSnapshot &&\n 'remotesInfo' in hostGlobalSnapshot &&\n hostGlobalSnapshot.remotesInfo &&\n getInfoWithoutType(hostGlobalSnapshot.remotesInfo, remote.name).key;\n if (remoteKey) {\n delete hostGlobalSnapshot.remotesInfo[remoteKey];\n if (\n //eslint-disable-next-line no-extra-boolean-cast\n Boolean(Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey])\n ) {\n delete Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey];\n }\n }\n }\n\n host.moduleCache.delete(remote.name);\n }\n } catch (err) {\n logger.error(\n `removeRemote failed: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA2DA,IAAa,gBAAb,MAA2B;CAyGzB,YAAY,MAAwB;eArG5B,IAAI,aAAa;GACvB,sBAAsB,IAAI,kBAGvB,uBAAuB;GAC1B,gBAAgB,IAAI,kBAGjB,iBAAiB;GACpB,eAAe,IAAI,mBAIhB,gBAAgB;GACnB,QAAQ,IAAI,UAeV,SAAS;GACX,qBAAqB,IAAI,SAYvB,sBAAsB;GACxB,iBAAiB,IAAI,UAgBnB,kBAAkB;GACpB,qBAAqB,IAAI,UAQvB,sBAAsB;GACxB,uBAAuB,IAAI,UAYzB,wBAAwB;GAE1B,oBAAoB,IAAI,WA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QAAQ,KAAK;AAChE,QAAI,oBAAoB;KACtB,MAAM,YACJ,sBACA,iBAAiB,sBACjB,mBAAmB,eACnB,mBAAmB,mBAAmB,aAAa,OAAO,KAAK,CAAC;AAClE,SAAI,WAAW;AACb,aAAO,mBAAmB,YAAY;AACtC,UAEE,QAAQ,OAAO,eAAe,qBAAqB,WAAW,CAE9D,QAAO,OAAO,eAAe,qBAAqB;;;AAKxD,SAAK,YAAY,OAAO,OAAO,KAAK;;WAE/B,KAAK;AACZ,UAAO,MACL,wBAAwB,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,GACzE"}
|
package/dist/shared/index.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const require_logger = require('../utils/logger.cjs');
|
|
2
2
|
const require_constant = require('../constant.cjs');
|
|
3
3
|
const require_share = require('../utils/share.cjs');
|
|
4
|
+
const require_context = require('../utils/context.cjs');
|
|
4
5
|
require('../utils/index.cjs');
|
|
5
6
|
const require_asyncHook = require('../utils/hooks/asyncHook.cjs');
|
|
6
7
|
const require_syncWaterfallHook = require('../utils/hooks/syncWaterfallHook.cjs');
|
|
@@ -67,7 +68,7 @@ var SharedHandler = class {
|
|
|
67
68
|
shared: host.options.shared,
|
|
68
69
|
origin: host
|
|
69
70
|
});
|
|
70
|
-
require_logger.assert(shareOptionsRes, `Cannot find ${pkgName}
|
|
71
|
+
require_logger.assert(shareOptionsRes, `Cannot find shared "${pkgName}" in host "${host.options.name}". Ensure the shared config for "${pkgName}" is declared in the federation plugin options and the host has been initialized before loading shares.`);
|
|
71
72
|
const { shared: registeredShared, useTreesShaking } = require_share.getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare) || {};
|
|
72
73
|
if (registeredShared) {
|
|
73
74
|
const targetShared = require_share.directShare(registeredShared, useTreesShaking);
|
|
@@ -175,6 +176,7 @@ var SharedHandler = class {
|
|
|
175
176
|
lifecycle: "beforeLoadShare",
|
|
176
177
|
origin: host
|
|
177
178
|
});
|
|
179
|
+
if (!remoteEntryExports) return;
|
|
178
180
|
} finally {
|
|
179
181
|
if (remoteEntryExports?.init && !module.initing) {
|
|
180
182
|
module.remoteEntryExports = remoteEntryExports;
|
|
@@ -233,13 +235,10 @@ var SharedHandler = class {
|
|
|
233
235
|
}
|
|
234
236
|
if (shareOptions.get) {
|
|
235
237
|
const module = shareOptions.get();
|
|
236
|
-
if (module instanceof Promise) {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
sharedPkgName: pkgName
|
|
241
|
-
}));
|
|
242
|
-
}
|
|
238
|
+
if (module instanceof Promise) require_logger.error(extraOptions?.from === "build" ? _module_federation_error_codes.RUNTIME_005 : _module_federation_error_codes.RUNTIME_006, _module_federation_error_codes.runtimeDescMap, {
|
|
239
|
+
hostName: host.options.name,
|
|
240
|
+
sharedPkgName: pkgName
|
|
241
|
+
}, void 0, require_context.optionsToMFContext(host.options));
|
|
243
242
|
shareOptions.lib = module;
|
|
244
243
|
this.setShared({
|
|
245
244
|
pkgName,
|
|
@@ -250,10 +249,10 @@ var SharedHandler = class {
|
|
|
250
249
|
});
|
|
251
250
|
return shareOptions.lib;
|
|
252
251
|
}
|
|
253
|
-
|
|
252
|
+
require_logger.error(_module_federation_error_codes.RUNTIME_006, _module_federation_error_codes.runtimeDescMap, {
|
|
254
253
|
hostName: host.options.name,
|
|
255
254
|
sharedPkgName: pkgName
|
|
256
|
-
}));
|
|
255
|
+
}, void 0, require_context.optionsToMFContext(host.options));
|
|
257
256
|
}
|
|
258
257
|
initShareScopeMap(scopeName, shareScope, extraOptions = {}) {
|
|
259
258
|
const { host } = this;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["PluginSystem","SyncWaterfallHook","AsyncWaterfallHook","AsyncHook","formatShareConfigs","getTargetSharedOptions","getRegisteredShare","directShare","shouldUseTreeShaking","DEFAULT_SCOPE","RUNTIME_005","RUNTIME_006","runtimeDescMap","getGlobalShareScope"],"sources":["../../src/shared/index.ts"],"sourcesContent":["import {\n getShortErrorMsg,\n RUNTIME_005,\n RUNTIME_006,\n runtimeDescMap,\n} from '@module-federation/error-codes';\nimport { Federation } from '../global';\nimport {\n Options,\n ShareScopeMap,\n ShareInfos,\n Shared,\n RemoteEntryExports,\n UserOptions,\n ShareStrategy,\n InitScope,\n InitTokens,\n CallFrom,\n TreeShakingArgs,\n} from '../type';\nimport { ModuleFederation } from '../core';\nimport {\n PluginSystem,\n AsyncHook,\n AsyncWaterfallHook,\n SyncWaterfallHook,\n} from '../utils/hooks';\nimport {\n formatShareConfigs,\n getRegisteredShare,\n getTargetSharedOptions,\n getGlobalShareScope,\n directShare,\n shouldUseTreeShaking,\n addUseIn,\n} from '../utils/share';\nimport { assert, addUniqueItem } from '../utils';\nimport { DEFAULT_SCOPE } from '../constant';\nimport { LoadRemoteMatch } from '../remote';\nimport { createRemoteEntryInitOptions } from '../module';\n\nexport class SharedHandler {\n host: ModuleFederation;\n shareScopeMap: ShareScopeMap;\n hooks = new PluginSystem({\n beforeRegisterShare: new SyncWaterfallHook<{\n pkgName: string;\n shared: Shared;\n origin: ModuleFederation;\n }>('beforeRegisterShare'),\n afterResolve: new AsyncWaterfallHook<LoadRemoteMatch>('afterResolve'),\n beforeLoadShare: new AsyncWaterfallHook<{\n pkgName: string;\n shareInfo?: Shared;\n shared: Options['shared'];\n origin: ModuleFederation;\n }>('beforeLoadShare'),\n // not used yet\n loadShare: new AsyncHook<[ModuleFederation, string, ShareInfos]>(),\n resolveShare: new SyncWaterfallHook<{\n shareScopeMap: ShareScopeMap;\n scope: string;\n pkgName: string;\n version: string;\n shareInfo: Shared;\n GlobalFederation: Federation;\n resolver: () => { shared: Shared; useTreesShaking: boolean } | undefined;\n }>('resolveShare'),\n // maybe will change, temporarily for internal use only\n initContainerShareScopeMap: new SyncWaterfallHook<{\n shareScope: ShareScopeMap[string];\n options: Options;\n origin: ModuleFederation;\n scopeName: string;\n hostShareScopeMap?: ShareScopeMap;\n }>('initContainerShareScopeMap'),\n });\n initTokens: InitTokens;\n constructor(host: ModuleFederation) {\n this.host = host;\n this.shareScopeMap = {};\n this.initTokens = {};\n this._setGlobalShareScopeMap(host.options);\n }\n\n // register shared in shareScopeMap\n registerShared(globalOptions: Options, userOptions: UserOptions) {\n const { newShareInfos, allShareInfos } = formatShareConfigs(\n globalOptions,\n userOptions,\n );\n\n const sharedKeys = Object.keys(newShareInfos);\n sharedKeys.forEach((sharedKey) => {\n const sharedVals = newShareInfos[sharedKey];\n sharedVals.forEach((sharedVal) => {\n sharedVal.scope.forEach((sc) => {\n this.hooks.lifecycle.beforeRegisterShare.emit({\n origin: this.host,\n pkgName: sharedKey,\n shared: sharedVal,\n });\n const registeredShared = this.shareScopeMap[sc]?.[sharedKey];\n if (!registeredShared) {\n this.setShared({\n pkgName: sharedKey,\n lib: sharedVal.lib,\n get: sharedVal.get,\n loaded: sharedVal.loaded || Boolean(sharedVal.lib),\n shared: sharedVal,\n from: userOptions.name,\n });\n }\n });\n });\n });\n\n return {\n newShareInfos,\n allShareInfos,\n };\n }\n\n async loadShare<T>(\n pkgName: string,\n extraOptions?: {\n customShareInfo?: Partial<Shared>;\n resolver?: (sharedOptions: ShareInfos[string]) => Shared;\n },\n ): Promise<false | (() => T | undefined)> {\n const { host } = this;\n // This function performs the following steps:\n // 1. Checks if the currently loaded share already exists, if not, it throws an error\n // 2. Searches globally for a matching share, if found, it uses it directly\n // 3. If not found, it retrieves it from the current share and stores the obtained share globally.\n\n const shareOptions = getTargetSharedOptions({\n pkgName,\n extraOptions,\n shareInfos: host.options.shared,\n });\n\n if (shareOptions?.scope) {\n await Promise.all(\n shareOptions.scope.map(async (shareScope) => {\n await Promise.all(\n this.initializeSharing(shareScope, {\n strategy: shareOptions.strategy,\n }),\n );\n return;\n }),\n );\n }\n const loadShareRes = await this.hooks.lifecycle.beforeLoadShare.emit({\n pkgName,\n shareInfo: shareOptions,\n shared: host.options.shared,\n origin: host,\n });\n\n const { shareInfo: shareOptionsRes } = loadShareRes;\n\n // Assert that shareInfoRes exists, if not, throw an error\n assert(\n shareOptionsRes,\n `Cannot find ${pkgName} Share in the ${host.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`,\n );\n\n const { shared: registeredShared, useTreesShaking } =\n getRegisteredShare(\n this.shareScopeMap,\n pkgName,\n shareOptionsRes,\n this.hooks.lifecycle.resolveShare,\n ) || {};\n\n if (registeredShared) {\n const targetShared = directShare(registeredShared, useTreesShaking);\n if (targetShared.lib) {\n addUseIn(targetShared, host.options.name);\n return targetShared.lib as () => T;\n } else if (targetShared.loading && !targetShared.loaded) {\n const factory = await targetShared.loading;\n targetShared.loaded = true;\n if (!targetShared.lib) {\n targetShared.lib = factory;\n }\n addUseIn(targetShared, host.options.name);\n return factory;\n } else {\n const asyncLoadProcess = async () => {\n const factory = await targetShared.get!();\n addUseIn(targetShared, host.options.name);\n targetShared.loaded = true;\n targetShared.lib = factory;\n return factory as () => T;\n };\n const loading = asyncLoadProcess();\n this.setShared({\n pkgName,\n loaded: false,\n shared: registeredShared,\n from: host.options.name,\n lib: null,\n loading,\n treeShaking: useTreesShaking\n ? (targetShared as TreeShakingArgs)\n : undefined,\n });\n return loading;\n }\n } else {\n if (extraOptions?.customShareInfo) {\n return false;\n }\n const _useTreeShaking = shouldUseTreeShaking(shareOptionsRes.treeShaking);\n const targetShared = directShare(shareOptionsRes, _useTreeShaking);\n\n const asyncLoadProcess = async () => {\n const factory = await targetShared.get!();\n targetShared.lib = factory;\n targetShared.loaded = true;\n addUseIn(targetShared, host.options.name);\n const { shared: gShared, useTreesShaking: gUseTreeShaking } =\n getRegisteredShare(\n this.shareScopeMap,\n pkgName,\n shareOptionsRes,\n this.hooks.lifecycle.resolveShare,\n ) || {};\n if (gShared) {\n const targetGShared = directShare(gShared, gUseTreeShaking);\n targetGShared.lib = factory;\n targetGShared.loaded = true;\n gShared.from = shareOptionsRes.from;\n }\n return factory as () => T;\n };\n const loading = asyncLoadProcess();\n this.setShared({\n pkgName,\n loaded: false,\n shared: shareOptionsRes,\n from: host.options.name,\n lib: null,\n loading,\n treeShaking: _useTreeShaking\n ? (targetShared as TreeShakingArgs)\n : undefined,\n });\n return loading;\n }\n }\n\n /**\n * This function initializes the sharing sequence (executed only once per share scope).\n * It accepts one argument, the name of the share scope.\n * If the share scope does not exist, it creates one.\n */\n // eslint-disable-next-line @typescript-eslint/member-ordering\n initializeSharing(\n shareScopeName = DEFAULT_SCOPE,\n extraOptions?: {\n initScope?: InitScope;\n from?: CallFrom;\n strategy?: ShareStrategy;\n },\n ): Array<Promise<void>> {\n const { host } = this;\n const from = extraOptions?.from;\n const strategy = extraOptions?.strategy;\n let initScope = extraOptions?.initScope;\n const promises: Promise<any>[] = [];\n if (from !== 'build') {\n const { initTokens } = this;\n if (!initScope) initScope = [];\n let initToken = initTokens[shareScopeName];\n if (!initToken)\n initToken = initTokens[shareScopeName] = { from: this.host.name };\n if (initScope.indexOf(initToken) >= 0) return promises;\n initScope.push(initToken);\n }\n\n const shareScope = this.shareScopeMap;\n const hostName = host.options.name;\n // Creates a new share scope if necessary\n if (!shareScope[shareScopeName]) {\n shareScope[shareScopeName] = {};\n }\n // Executes all initialization snippets from all accessible modules\n const scope = shareScope[shareScopeName];\n const register = (name: string, shared: Shared) => {\n const { version, eager } = shared;\n scope[name] = scope[name] || {};\n const versions = scope[name];\n const activeVersion: Shared =\n versions[version] && (directShare(versions[version]) as Shared);\n const activeVersionEager = Boolean(\n activeVersion &&\n (('eager' in activeVersion && activeVersion.eager) ||\n ('shareConfig' in activeVersion &&\n activeVersion.shareConfig?.eager)),\n );\n if (\n !activeVersion ||\n (activeVersion.strategy !== 'loaded-first' &&\n !activeVersion.loaded &&\n (Boolean(!eager) !== !activeVersionEager\n ? eager\n : hostName > versions[version].from))\n ) {\n versions[version] = shared;\n }\n };\n\n const initRemoteModule = async (key: string): Promise<void> => {\n const { module } = await host.remoteHandler.getRemoteModuleAndOptions({\n id: key,\n });\n let remoteEntryExports: RemoteEntryExports | undefined = undefined;\n try {\n remoteEntryExports = await module.getEntry();\n } catch (error) {\n remoteEntryExports =\n (await host.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({\n id: key,\n error,\n from: 'runtime',\n lifecycle: 'beforeLoadShare',\n origin: host,\n })) as RemoteEntryExports;\n } finally {\n // prevent self load loop: when host load self , the initTokens is not the same\n if (remoteEntryExports?.init && !module.initing) {\n module.remoteEntryExports = remoteEntryExports;\n await module.init(undefined, undefined, initScope);\n }\n }\n };\n Object.keys(host.options.shared).forEach((shareName) => {\n const sharedArr = host.options.shared[shareName];\n sharedArr.forEach((shared) => {\n if (shared.scope.includes(shareScopeName)) {\n register(shareName, shared);\n }\n });\n });\n // TODO: strategy==='version-first' need to be removed in the future\n if (\n host.options.shareStrategy === 'version-first' ||\n strategy === 'version-first'\n ) {\n host.options.remotes.forEach((remote) => {\n if (remote.shareScope === shareScopeName) {\n promises.push(initRemoteModule(remote.name));\n }\n });\n }\n\n return promises;\n }\n\n // The lib function will only be available if the shared set by eager or runtime init is set or the shared is successfully loaded.\n // 1. If the loaded shared already exists globally, then it will be reused\n // 2. If lib exists in local shared, it will be used directly\n // 3. If the local get returns something other than Promise, then it will be used directly\n loadShareSync<T>(\n pkgName: string,\n extraOptions?: {\n from?: 'build' | 'runtime';\n customShareInfo?: Partial<Shared>;\n resolver?: (sharedOptions: ShareInfos[string]) => Shared;\n },\n ): () => T | never {\n const { host } = this;\n const shareOptions = getTargetSharedOptions({\n pkgName,\n extraOptions,\n shareInfos: host.options.shared,\n });\n\n if (shareOptions?.scope) {\n shareOptions.scope.forEach((shareScope) => {\n this.initializeSharing(shareScope, { strategy: shareOptions.strategy });\n });\n }\n const { shared: registeredShared, useTreesShaking } =\n getRegisteredShare(\n this.shareScopeMap,\n pkgName,\n shareOptions,\n this.hooks.lifecycle.resolveShare,\n ) || {};\n\n if (registeredShared) {\n if (typeof registeredShared.lib === 'function') {\n addUseIn(registeredShared, host.options.name);\n if (!registeredShared.loaded) {\n registeredShared.loaded = true;\n if (registeredShared.from === host.options.name) {\n shareOptions.loaded = true;\n }\n }\n return registeredShared.lib as () => T;\n }\n if (typeof registeredShared.get === 'function') {\n const module = registeredShared.get();\n if (!(module instanceof Promise)) {\n addUseIn(registeredShared, host.options.name);\n this.setShared({\n pkgName,\n loaded: true,\n from: host.options.name,\n lib: module,\n shared: registeredShared,\n });\n return module;\n }\n }\n }\n\n if (shareOptions.lib) {\n if (!shareOptions.loaded) {\n shareOptions.loaded = true;\n }\n return shareOptions.lib as () => T;\n }\n\n if (shareOptions.get) {\n const module = shareOptions.get();\n\n if (module instanceof Promise) {\n const errorCode =\n extraOptions?.from === 'build' ? RUNTIME_005 : RUNTIME_006;\n throw new Error(\n getShortErrorMsg(errorCode, runtimeDescMap, {\n hostName: host.options.name,\n sharedPkgName: pkgName,\n }),\n );\n }\n\n shareOptions.lib = module;\n\n this.setShared({\n pkgName,\n loaded: true,\n from: host.options.name,\n lib: shareOptions.lib,\n shared: shareOptions,\n });\n return shareOptions.lib as () => T;\n }\n\n throw new Error(\n getShortErrorMsg(RUNTIME_006, runtimeDescMap, {\n hostName: host.options.name,\n sharedPkgName: pkgName,\n }),\n );\n }\n\n initShareScopeMap(\n scopeName: string,\n shareScope: ShareScopeMap[string],\n extraOptions: { hostShareScopeMap?: ShareScopeMap } = {},\n ): void {\n const { host } = this;\n this.shareScopeMap[scopeName] = shareScope;\n this.hooks.lifecycle.initContainerShareScopeMap.emit({\n shareScope,\n options: host.options,\n origin: host,\n scopeName,\n hostShareScopeMap: extraOptions.hostShareScopeMap,\n });\n }\n\n private setShared({\n pkgName,\n shared,\n from,\n lib,\n loading,\n loaded,\n get,\n treeShaking,\n }: {\n pkgName: string;\n shared: Shared;\n from: string;\n lib: any;\n loaded?: boolean;\n loading?: Shared['loading'];\n get?: Shared['get'];\n treeShaking?: TreeShakingArgs;\n }): void {\n const { version, scope = 'default', ...shareInfo } = shared;\n const scopes: string[] = Array.isArray(scope) ? scope : [scope];\n\n const mergeAttrs = (shared: Shared) => {\n const merge = <K extends keyof TreeShakingArgs>(\n s: TreeShakingArgs,\n key: K,\n val: TreeShakingArgs[K],\n ): void => {\n if (val && !s[key]) {\n s[key] = val;\n }\n };\n const targetShared = (\n treeShaking ? shared.treeShaking! : shared\n ) as TreeShakingArgs;\n merge(targetShared, 'loaded', loaded);\n merge(targetShared, 'loading', loading);\n merge(targetShared, 'get', get);\n };\n scopes.forEach((sc) => {\n if (!this.shareScopeMap[sc]) {\n this.shareScopeMap[sc] = {};\n }\n if (!this.shareScopeMap[sc][pkgName]) {\n this.shareScopeMap[sc][pkgName] = {};\n }\n\n if (!this.shareScopeMap[sc][pkgName][version]) {\n this.shareScopeMap[sc][pkgName][version] = {\n version,\n scope: [sc],\n ...shareInfo,\n lib,\n };\n }\n\n const registeredShared = this.shareScopeMap[sc][pkgName][version];\n mergeAttrs(registeredShared);\n if (from && registeredShared.from !== from) {\n registeredShared.from = from;\n }\n });\n }\n\n private _setGlobalShareScopeMap(hostOptions: Options): void {\n const globalShareScopeMap = getGlobalShareScope();\n const identifier = hostOptions.id || hostOptions.name;\n if (identifier && !globalShareScopeMap[identifier]) {\n globalShareScopeMap[identifier] = this.shareScopeMap;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;AAyCA,IAAa,gBAAb,MAA2B;CAqCzB,YAAY,MAAwB;eAlC5B,IAAIA,kCAAa;GACvB,qBAAqB,IAAIC,4CAItB,sBAAsB;GACzB,cAAc,IAAIC,+CAAoC,eAAe;GACrE,iBAAiB,IAAIA,+CAKlB,kBAAkB;GAErB,WAAW,IAAIC,6BAAmD;GAClE,cAAc,IAAIF,4CAQf,eAAe;GAElB,4BAA4B,IAAIA,4CAM7B,6BAA6B;GACjC,CAAC;AAGA,OAAK,OAAO;AACZ,OAAK,gBAAgB,EAAE;AACvB,OAAK,aAAa,EAAE;AACpB,OAAK,wBAAwB,KAAK,QAAQ;;CAI5C,eAAe,eAAwB,aAA0B;EAC/D,MAAM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AI;;AAEjC,SAAO,SAAS,OAAO;AACrB,OAAI,CAAC,KAAK,cAAc,IACtB,MAAK,cAAc,MAAM,EAAE;AAE7B,OAAI,CAAC,KAAK,cAAc,IAAI,SAC1B,MAAK,cAAc,IAAI,WAAW,EAAE;AAGtC,OAAI,CAAC,KAAK,cAAc,IAAI,SAAS,SACnC,MAAK,cAAc,IAAI,SAAS,WAAW;IACzC;IACA,OAAO,CAAC,GAAG;IACX,GAAG;IACH;IACD;GAGH,MAAM,mBAAmB,KAAK,cAAc,IAAI,SAAS;AACzD,cAAW,iBAAiB;AAC5B,OAAI,QAAQ,iBAAiB,SAAS,KACpC,kBAAiB,OAAO;IAE1B;;CAGJ,AAAQ,wBAAwB,aAA4B;EAC1D,MAAM,sBAAsBC,mCAAqB;EACjD,MAAM,aAAa,YAAY,MAAM,YAAY;AACjD,MAAI,cAAc,CAAC,oBAAoB,YACrC,qBAAoB,cAAc,KAAK"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["PluginSystem","SyncWaterfallHook","AsyncWaterfallHook","AsyncHook","formatShareConfigs","getTargetSharedOptions","getRegisteredShare","directShare","shouldUseTreeShaking","DEFAULT_SCOPE","RUNTIME_005","RUNTIME_006","runtimeDescMap","optionsToMFContext","getGlobalShareScope"],"sources":["../../src/shared/index.ts"],"sourcesContent":["import {\n RUNTIME_005,\n RUNTIME_006,\n runtimeDescMap,\n} from '@module-federation/error-codes';\nimport { Federation } from '../global';\nimport {\n Options,\n ShareScopeMap,\n ShareInfos,\n Shared,\n RemoteEntryExports,\n UserOptions,\n ShareStrategy,\n InitScope,\n InitTokens,\n CallFrom,\n TreeShakingArgs,\n} from '../type';\nimport { ModuleFederation } from '../core';\nimport {\n PluginSystem,\n AsyncHook,\n AsyncWaterfallHook,\n SyncWaterfallHook,\n} from '../utils/hooks';\nimport {\n formatShareConfigs,\n getRegisteredShare,\n getTargetSharedOptions,\n getGlobalShareScope,\n directShare,\n shouldUseTreeShaking,\n addUseIn,\n} from '../utils/share';\nimport { assert, error, addUniqueItem, optionsToMFContext } from '../utils';\nimport { DEFAULT_SCOPE } from '../constant';\nimport { LoadRemoteMatch } from '../remote';\nimport { createRemoteEntryInitOptions } from '../module';\n\nexport class SharedHandler {\n host: ModuleFederation;\n shareScopeMap: ShareScopeMap;\n hooks = new PluginSystem({\n beforeRegisterShare: new SyncWaterfallHook<{\n pkgName: string;\n shared: Shared;\n origin: ModuleFederation;\n }>('beforeRegisterShare'),\n afterResolve: new AsyncWaterfallHook<LoadRemoteMatch>('afterResolve'),\n beforeLoadShare: new AsyncWaterfallHook<{\n pkgName: string;\n shareInfo?: Shared;\n shared: Options['shared'];\n origin: ModuleFederation;\n }>('beforeLoadShare'),\n // not used yet\n loadShare: new AsyncHook<[ModuleFederation, string, ShareInfos]>(),\n resolveShare: new SyncWaterfallHook<{\n shareScopeMap: ShareScopeMap;\n scope: string;\n pkgName: string;\n version: string;\n shareInfo: Shared;\n GlobalFederation: Federation;\n resolver: () => { shared: Shared; useTreesShaking: boolean } | undefined;\n }>('resolveShare'),\n // maybe will change, temporarily for internal use only\n initContainerShareScopeMap: new SyncWaterfallHook<{\n shareScope: ShareScopeMap[string];\n options: Options;\n origin: ModuleFederation;\n scopeName: string;\n hostShareScopeMap?: ShareScopeMap;\n }>('initContainerShareScopeMap'),\n });\n initTokens: InitTokens;\n constructor(host: ModuleFederation) {\n this.host = host;\n this.shareScopeMap = {};\n this.initTokens = {};\n this._setGlobalShareScopeMap(host.options);\n }\n\n // register shared in shareScopeMap\n registerShared(globalOptions: Options, userOptions: UserOptions) {\n const { newShareInfos, allShareInfos } = formatShareConfigs(\n globalOptions,\n userOptions,\n );\n\n const sharedKeys = Object.keys(newShareInfos);\n sharedKeys.forEach((sharedKey) => {\n const sharedVals = newShareInfos[sharedKey];\n sharedVals.forEach((sharedVal) => {\n sharedVal.scope.forEach((sc) => {\n this.hooks.lifecycle.beforeRegisterShare.emit({\n origin: this.host,\n pkgName: sharedKey,\n shared: sharedVal,\n });\n const registeredShared = this.shareScopeMap[sc]?.[sharedKey];\n if (!registeredShared) {\n this.setShared({\n pkgName: sharedKey,\n lib: sharedVal.lib,\n get: sharedVal.get,\n loaded: sharedVal.loaded || Boolean(sharedVal.lib),\n shared: sharedVal,\n from: userOptions.name,\n });\n }\n });\n });\n });\n\n return {\n newShareInfos,\n allShareInfos,\n };\n }\n\n async loadShare<T>(\n pkgName: string,\n extraOptions?: {\n customShareInfo?: Partial<Shared>;\n resolver?: (sharedOptions: ShareInfos[string]) => Shared;\n },\n ): Promise<false | (() => T | undefined)> {\n const { host } = this;\n // This function performs the following steps:\n // 1. Checks if the currently loaded share already exists, if not, it throws an error\n // 2. Searches globally for a matching share, if found, it uses it directly\n // 3. If not found, it retrieves it from the current share and stores the obtained share globally.\n\n const shareOptions = getTargetSharedOptions({\n pkgName,\n extraOptions,\n shareInfos: host.options.shared,\n });\n\n if (shareOptions?.scope) {\n await Promise.all(\n shareOptions.scope.map(async (shareScope) => {\n await Promise.all(\n this.initializeSharing(shareScope, {\n strategy: shareOptions.strategy,\n }),\n );\n return;\n }),\n );\n }\n const loadShareRes = await this.hooks.lifecycle.beforeLoadShare.emit({\n pkgName,\n shareInfo: shareOptions,\n shared: host.options.shared,\n origin: host,\n });\n\n const { shareInfo: shareOptionsRes } = loadShareRes;\n\n // Assert that shareInfoRes exists, if not, throw an error\n assert(\n shareOptionsRes,\n `Cannot find shared \"${pkgName}\" in host \"${host.options.name}\". Ensure the shared config for \"${pkgName}\" is declared in the federation plugin options and the host has been initialized before loading shares.`,\n );\n\n const { shared: registeredShared, useTreesShaking } =\n getRegisteredShare(\n this.shareScopeMap,\n pkgName,\n shareOptionsRes,\n this.hooks.lifecycle.resolveShare,\n ) || {};\n\n if (registeredShared) {\n const targetShared = directShare(registeredShared, useTreesShaking);\n if (targetShared.lib) {\n addUseIn(targetShared, host.options.name);\n return targetShared.lib as () => T;\n } else if (targetShared.loading && !targetShared.loaded) {\n const factory = await targetShared.loading;\n targetShared.loaded = true;\n if (!targetShared.lib) {\n targetShared.lib = factory;\n }\n addUseIn(targetShared, host.options.name);\n return factory;\n } else {\n const asyncLoadProcess = async () => {\n const factory = await targetShared.get!();\n addUseIn(targetShared, host.options.name);\n targetShared.loaded = true;\n targetShared.lib = factory;\n return factory as () => T;\n };\n const loading = asyncLoadProcess();\n this.setShared({\n pkgName,\n loaded: false,\n shared: registeredShared,\n from: host.options.name,\n lib: null,\n loading,\n treeShaking: useTreesShaking\n ? (targetShared as TreeShakingArgs)\n : undefined,\n });\n return loading;\n }\n } else {\n if (extraOptions?.customShareInfo) {\n return false;\n }\n const _useTreeShaking = shouldUseTreeShaking(shareOptionsRes.treeShaking);\n const targetShared = directShare(shareOptionsRes, _useTreeShaking);\n\n const asyncLoadProcess = async () => {\n const factory = await targetShared.get!();\n targetShared.lib = factory;\n targetShared.loaded = true;\n addUseIn(targetShared, host.options.name);\n const { shared: gShared, useTreesShaking: gUseTreeShaking } =\n getRegisteredShare(\n this.shareScopeMap,\n pkgName,\n shareOptionsRes,\n this.hooks.lifecycle.resolveShare,\n ) || {};\n if (gShared) {\n const targetGShared = directShare(gShared, gUseTreeShaking);\n targetGShared.lib = factory;\n targetGShared.loaded = true;\n gShared.from = shareOptionsRes.from;\n }\n return factory as () => T;\n };\n const loading = asyncLoadProcess();\n this.setShared({\n pkgName,\n loaded: false,\n shared: shareOptionsRes,\n from: host.options.name,\n lib: null,\n loading,\n treeShaking: _useTreeShaking\n ? (targetShared as TreeShakingArgs)\n : undefined,\n });\n return loading;\n }\n }\n\n /**\n * This function initializes the sharing sequence (executed only once per share scope).\n * It accepts one argument, the name of the share scope.\n * If the share scope does not exist, it creates one.\n */\n // eslint-disable-next-line @typescript-eslint/member-ordering\n initializeSharing(\n shareScopeName = DEFAULT_SCOPE,\n extraOptions?: {\n initScope?: InitScope;\n from?: CallFrom;\n strategy?: ShareStrategy;\n },\n ): Array<Promise<void>> {\n const { host } = this;\n const from = extraOptions?.from;\n const strategy = extraOptions?.strategy;\n let initScope = extraOptions?.initScope;\n const promises: Promise<any>[] = [];\n if (from !== 'build') {\n const { initTokens } = this;\n if (!initScope) initScope = [];\n let initToken = initTokens[shareScopeName];\n if (!initToken)\n initToken = initTokens[shareScopeName] = { from: this.host.name };\n if (initScope.indexOf(initToken) >= 0) return promises;\n initScope.push(initToken);\n }\n\n const shareScope = this.shareScopeMap;\n const hostName = host.options.name;\n // Creates a new share scope if necessary\n if (!shareScope[shareScopeName]) {\n shareScope[shareScopeName] = {};\n }\n // Executes all initialization snippets from all accessible modules\n const scope = shareScope[shareScopeName];\n const register = (name: string, shared: Shared) => {\n const { version, eager } = shared;\n scope[name] = scope[name] || {};\n const versions = scope[name];\n const activeVersion: Shared =\n versions[version] && (directShare(versions[version]) as Shared);\n const activeVersionEager = Boolean(\n activeVersion &&\n (('eager' in activeVersion && activeVersion.eager) ||\n ('shareConfig' in activeVersion && activeVersion.shareConfig?.eager)),\n );\n if (\n !activeVersion ||\n (activeVersion.strategy !== 'loaded-first' &&\n !activeVersion.loaded &&\n (Boolean(!eager) !== !activeVersionEager\n ? eager\n : hostName > versions[version].from))\n ) {\n versions[version] = shared;\n }\n };\n\n const initRemoteModule = async (key: string): Promise<void> => {\n const { module } = await host.remoteHandler.getRemoteModuleAndOptions({\n id: key,\n });\n let remoteEntryExports: RemoteEntryExports | undefined = undefined;\n try {\n remoteEntryExports = await module.getEntry();\n } catch (error) {\n remoteEntryExports =\n (await host.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({\n id: key,\n error,\n from: 'runtime',\n lifecycle: 'beforeLoadShare',\n origin: host,\n })) as RemoteEntryExports;\n if (!remoteEntryExports) {\n return;\n }\n } finally {\n // prevent self load loop: when host load self , the initTokens is not the same\n if (remoteEntryExports?.init && !module.initing) {\n module.remoteEntryExports = remoteEntryExports;\n await module.init(undefined, undefined, initScope);\n }\n }\n };\n Object.keys(host.options.shared).forEach((shareName) => {\n const sharedArr = host.options.shared[shareName];\n sharedArr.forEach((shared) => {\n if (shared.scope.includes(shareScopeName)) {\n register(shareName, shared);\n }\n });\n });\n // TODO: strategy==='version-first' need to be removed in the future\n if (\n host.options.shareStrategy === 'version-first' ||\n strategy === 'version-first'\n ) {\n host.options.remotes.forEach((remote) => {\n if (remote.shareScope === shareScopeName) {\n promises.push(initRemoteModule(remote.name));\n }\n });\n }\n\n return promises;\n }\n\n // The lib function will only be available if the shared set by eager or runtime init is set or the shared is successfully loaded.\n // 1. If the loaded shared already exists globally, then it will be reused\n // 2. If lib exists in local shared, it will be used directly\n // 3. If the local get returns something other than Promise, then it will be used directly\n loadShareSync<T>(\n pkgName: string,\n extraOptions?: {\n from?: 'build' | 'runtime';\n customShareInfo?: Partial<Shared>;\n resolver?: (sharedOptions: ShareInfos[string]) => Shared;\n },\n ): () => T | never {\n const { host } = this;\n const shareOptions = getTargetSharedOptions({\n pkgName,\n extraOptions,\n shareInfos: host.options.shared,\n });\n\n if (shareOptions?.scope) {\n shareOptions.scope.forEach((shareScope) => {\n this.initializeSharing(shareScope, { strategy: shareOptions.strategy });\n });\n }\n const { shared: registeredShared, useTreesShaking } =\n getRegisteredShare(\n this.shareScopeMap,\n pkgName,\n shareOptions,\n this.hooks.lifecycle.resolveShare,\n ) || {};\n\n if (registeredShared) {\n if (typeof registeredShared.lib === 'function') {\n addUseIn(registeredShared, host.options.name);\n if (!registeredShared.loaded) {\n registeredShared.loaded = true;\n if (registeredShared.from === host.options.name) {\n shareOptions.loaded = true;\n }\n }\n return registeredShared.lib as () => T;\n }\n if (typeof registeredShared.get === 'function') {\n const module = registeredShared.get();\n if (!(module instanceof Promise)) {\n addUseIn(registeredShared, host.options.name);\n this.setShared({\n pkgName,\n loaded: true,\n from: host.options.name,\n lib: module,\n shared: registeredShared,\n });\n return module;\n }\n }\n }\n\n if (shareOptions.lib) {\n if (!shareOptions.loaded) {\n shareOptions.loaded = true;\n }\n return shareOptions.lib as () => T;\n }\n\n if (shareOptions.get) {\n const module = shareOptions.get();\n\n if (module instanceof Promise) {\n const errorCode =\n extraOptions?.from === 'build' ? RUNTIME_005 : RUNTIME_006;\n error(\n errorCode,\n runtimeDescMap,\n {\n hostName: host.options.name,\n sharedPkgName: pkgName,\n },\n undefined,\n optionsToMFContext(host.options),\n );\n }\n\n shareOptions.lib = module;\n\n this.setShared({\n pkgName,\n loaded: true,\n from: host.options.name,\n lib: shareOptions.lib,\n shared: shareOptions,\n });\n return shareOptions.lib as () => T;\n }\n\n error(\n RUNTIME_006,\n runtimeDescMap,\n {\n hostName: host.options.name,\n sharedPkgName: pkgName,\n },\n undefined,\n optionsToMFContext(host.options),\n );\n }\n\n initShareScopeMap(\n scopeName: string,\n shareScope: ShareScopeMap[string],\n extraOptions: { hostShareScopeMap?: ShareScopeMap } = {},\n ): void {\n const { host } = this;\n this.shareScopeMap[scopeName] = shareScope;\n this.hooks.lifecycle.initContainerShareScopeMap.emit({\n shareScope,\n options: host.options,\n origin: host,\n scopeName,\n hostShareScopeMap: extraOptions.hostShareScopeMap,\n });\n }\n\n private setShared({\n pkgName,\n shared,\n from,\n lib,\n loading,\n loaded,\n get,\n treeShaking,\n }: {\n pkgName: string;\n shared: Shared;\n from: string;\n lib: any;\n loaded?: boolean;\n loading?: Shared['loading'];\n get?: Shared['get'];\n treeShaking?: TreeShakingArgs;\n }): void {\n const { version, scope = 'default', ...shareInfo } = shared;\n const scopes: string[] = Array.isArray(scope) ? scope : [scope];\n\n const mergeAttrs = (shared: Shared) => {\n const merge = <K extends keyof TreeShakingArgs>(\n s: TreeShakingArgs,\n key: K,\n val: TreeShakingArgs[K],\n ): void => {\n if (val && !s[key]) {\n s[key] = val;\n }\n };\n const targetShared = (\n treeShaking ? shared.treeShaking! : shared\n ) as TreeShakingArgs;\n merge(targetShared, 'loaded', loaded);\n merge(targetShared, 'loading', loading);\n merge(targetShared, 'get', get);\n };\n scopes.forEach((sc) => {\n if (!this.shareScopeMap[sc]) {\n this.shareScopeMap[sc] = {};\n }\n if (!this.shareScopeMap[sc][pkgName]) {\n this.shareScopeMap[sc][pkgName] = {};\n }\n\n if (!this.shareScopeMap[sc][pkgName][version]) {\n this.shareScopeMap[sc][pkgName][version] = {\n version,\n scope: [sc],\n ...shareInfo,\n lib,\n };\n }\n\n const registeredShared = this.shareScopeMap[sc][pkgName][version];\n mergeAttrs(registeredShared);\n if (from && registeredShared.from !== from) {\n registeredShared.from = from;\n }\n });\n }\n\n private _setGlobalShareScopeMap(hostOptions: Options): void {\n const globalShareScopeMap = getGlobalShareScope();\n const identifier = hostOptions.id || hostOptions.name;\n if (identifier && !globalShareScopeMap[identifier]) {\n globalShareScopeMap[identifier] = this.shareScopeMap;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAwCA,IAAa,gBAAb,MAA2B;CAqCzB,YAAY,MAAwB;eAlC5B,IAAIA,kCAAa;GACvB,qBAAqB,IAAIC,4CAItB,sBAAsB;GACzB,cAAc,IAAIC,+CAAoC,eAAe;GACrE,iBAAiB,IAAIA,+CAKlB,kBAAkB;GAErB,WAAW,IAAIC,6BAAmD;GAClE,cAAc,IAAIF,4CAQf,eAAe;GAElB,4BAA4B,IAAIA,4CAM7B,6BAA6B;GACjC,CAAC;AAGA,OAAK,OAAO;AACZ,OAAK,gBAAgB,EAAE;AACvB,OAAK,aAAa,EAAE;AACpB,OAAK,wBAAwB,KAAK,QAAQ;;CAI5C,eAAe,eAAwB,aAA0B;EAC/D,MAAM,EAAE,eAAe,kBAAkBG,iCACvC,eACA,YACD;AAGD,EADmB,OAAO,KAAK,cAAc,CAClC,SAAS,cAAc;AAEhC,GADmB,cAAc,WACtB,SAAS,cAAc;AAChC,cAAU,MAAM,SAAS,OAAO;AAC9B,UAAK,MAAM,UAAU,oBAAoB,KAAK;MAC5C,QAAQ,KAAK;MACb,SAAS;MACT,QAAQ;MACT,CAAC;AAEF,SAAI,CADqB,KAAK,cAAc,MAAM,WAEhD,MAAK,UAAU;MACb,SAAS;MACT,KAAK,UAAU;MACf,KAAK,UAAU;MACf,QAAQ,UAAU,UAAU,Q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iBAAiB;AAC5B,OAAI,QAAQ,iBAAiB,SAAS,KACpC,kBAAiB,OAAO;IAE1B;;CAGJ,AAAQ,wBAAwB,aAA4B;EAC1D,MAAM,sBAAsBC,mCAAqB;EACjD,MAAM,aAAa,YAAY,MAAM,YAAY;AACjD,MAAI,cAAc,CAAC,oBAAoB,YACrC,qBAAoB,cAAc,KAAK"}
|
package/dist/shared/index.d.ts
CHANGED
|
@@ -2,13 +2,10 @@ import { AsyncHook } from "../utils/hooks/asyncHook.js";
|
|
|
2
2
|
import { SyncWaterfallHook } from "../utils/hooks/syncWaterfallHook.js";
|
|
3
3
|
import { AsyncWaterfallHook } from "../utils/hooks/asyncWaterfallHooks.js";
|
|
4
4
|
import { PluginSystem } from "../utils/hooks/pluginSystem.js";
|
|
5
|
-
import "../utils/hooks/index.js";
|
|
6
5
|
import { Federation } from "../global.js";
|
|
7
6
|
import { LoadRemoteMatch } from "../remote/index.js";
|
|
8
7
|
import { ModuleFederation } from "../core.js";
|
|
9
8
|
import { CallFrom, InitScope, InitTokens, Options, ShareInfos, ShareScopeMap, ShareStrategy, Shared, UserOptions } from "../type/config.js";
|
|
10
|
-
import "../type/index.js";
|
|
11
|
-
|
|
12
9
|
//#region src/shared/index.d.ts
|
|
13
10
|
declare class SharedHandler {
|
|
14
11
|
host: ModuleFederation;
|