@module-federation/runtime-core 2.0.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +19 -0
  2. package/dist/_virtual/_rolldown/runtime.js +18 -0
  3. package/dist/constant.cjs +9 -0
  4. package/dist/constant.cjs.map +1 -0
  5. package/dist/constant.js +7 -0
  6. package/dist/constant.js.map +1 -0
  7. package/dist/core.cjs +153 -0
  8. package/dist/core.cjs.map +1 -0
  9. package/dist/core.d.ts +129 -0
  10. package/dist/core.js +153 -0
  11. package/dist/core.js.map +1 -0
  12. package/dist/global.cjs +160 -0
  13. package/dist/global.cjs.map +1 -0
  14. package/dist/global.d.ts +45 -0
  15. package/dist/global.js +142 -0
  16. package/dist/global.js.map +1 -0
  17. package/dist/helpers.cjs +44 -0
  18. package/dist/helpers.cjs.map +1 -0
  19. package/dist/helpers.d.ts +30 -0
  20. package/dist/helpers.js +44 -0
  21. package/dist/helpers.js.map +1 -0
  22. package/dist/index.cjs +60 -0
  23. package/dist/index.cjs.map +1 -0
  24. package/dist/index.d.ts +29 -1
  25. package/dist/index.js +20 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/module/index.cjs +129 -0
  28. package/dist/module/index.cjs.map +1 -0
  29. package/dist/module/index.d.ts +31 -0
  30. package/dist/module/index.js +129 -0
  31. package/dist/module/index.js.map +1 -0
  32. package/dist/plugins/generate-preload-assets.cjs +182 -0
  33. package/dist/plugins/generate-preload-assets.cjs.map +1 -0
  34. package/dist/plugins/generate-preload-assets.js +182 -0
  35. package/dist/plugins/generate-preload-assets.js.map +1 -0
  36. package/dist/plugins/snapshot/SnapshotHandler.cjs +190 -0
  37. package/dist/plugins/snapshot/SnapshotHandler.cjs.map +1 -0
  38. package/dist/plugins/snapshot/SnapshotHandler.d.ts +66 -0
  39. package/dist/plugins/snapshot/SnapshotHandler.js +189 -0
  40. package/dist/plugins/snapshot/SnapshotHandler.js.map +1 -0
  41. package/dist/plugins/snapshot/index.cjs +63 -0
  42. package/dist/plugins/snapshot/index.cjs.map +1 -0
  43. package/dist/plugins/snapshot/index.js +62 -0
  44. package/dist/plugins/snapshot/index.js.map +1 -0
  45. package/dist/remote/index.cjs +304 -0
  46. package/dist/remote/index.cjs.map +1 -0
  47. package/dist/remote/index.d.ts +119 -0
  48. package/dist/remote/index.js +304 -0
  49. package/dist/remote/index.js.map +1 -0
  50. package/dist/shared/index.cjs +303 -0
  51. package/dist/shared/index.cjs.map +1 -0
  52. package/dist/shared/index.d.ts +82 -0
  53. package/dist/shared/index.js +303 -0
  54. package/dist/shared/index.js.map +1 -0
  55. package/dist/type/config.d.ts +130 -0
  56. package/dist/type/index.cjs +13 -0
  57. package/dist/type/index.cjs.map +1 -0
  58. package/dist/type/index.d.ts +11 -0
  59. package/dist/type/index.js +8 -0
  60. package/dist/type/index.js.map +1 -0
  61. package/dist/type/plugin.d.ts +27 -0
  62. package/dist/type/preload.d.ts +31 -0
  63. package/dist/types.cjs +0 -0
  64. package/dist/types.d.ts +4 -1
  65. package/dist/types.js +1 -0
  66. package/dist/utils/context.cjs +45 -0
  67. package/dist/utils/context.cjs.map +1 -0
  68. package/dist/utils/context.d.ts +1 -0
  69. package/dist/utils/context.js +44 -0
  70. package/dist/utils/context.js.map +1 -0
  71. package/dist/utils/env.cjs +10 -0
  72. package/dist/utils/env.cjs.map +1 -0
  73. package/dist/utils/env.d.ts +1 -0
  74. package/dist/utils/env.js +10 -0
  75. package/dist/utils/env.js.map +1 -0
  76. package/dist/utils/hooks/asyncHook.cjs +23 -0
  77. package/dist/utils/hooks/asyncHook.cjs.map +1 -0
  78. package/dist/utils/hooks/asyncHook.d.ts +10 -0
  79. package/dist/utils/hooks/asyncHook.js +23 -0
  80. package/dist/utils/hooks/asyncHook.js.map +1 -0
  81. package/dist/utils/hooks/asyncWaterfallHooks.cjs +42 -0
  82. package/dist/utils/hooks/asyncWaterfallHooks.cjs.map +1 -0
  83. package/dist/utils/hooks/asyncWaterfallHooks.d.ts +12 -0
  84. package/dist/utils/hooks/asyncWaterfallHooks.js +42 -0
  85. package/dist/utils/hooks/asyncWaterfallHooks.js.map +1 -0
  86. package/dist/utils/hooks/index.cjs +5 -0
  87. package/dist/utils/hooks/index.d.ts +5 -0
  88. package/dist/utils/hooks/index.js +7 -0
  89. package/dist/utils/hooks/pluginSystem.cjs +37 -0
  90. package/dist/utils/hooks/pluginSystem.cjs.map +1 -0
  91. package/dist/utils/hooks/pluginSystem.d.ts +19 -0
  92. package/dist/utils/hooks/pluginSystem.js +37 -0
  93. package/dist/utils/hooks/pluginSystem.js.map +1 -0
  94. package/dist/utils/hooks/syncHook.cjs +36 -0
  95. package/dist/utils/hooks/syncHook.cjs.map +1 -0
  96. package/dist/utils/hooks/syncHook.d.ts +16 -0
  97. package/dist/utils/hooks/syncHook.js +35 -0
  98. package/dist/utils/hooks/syncHook.js.map +1 -0
  99. package/dist/utils/hooks/syncWaterfallHook.cjs +39 -0
  100. package/dist/utils/hooks/syncWaterfallHook.cjs.map +1 -0
  101. package/dist/utils/hooks/syncWaterfallHook.d.ts +11 -0
  102. package/dist/utils/hooks/syncWaterfallHook.js +38 -0
  103. package/dist/utils/hooks/syncWaterfallHook.js.map +1 -0
  104. package/dist/utils/index.cjs +8 -0
  105. package/dist/utils/index.d.ts +4 -0
  106. package/dist/utils/index.js +9 -0
  107. package/dist/utils/load.cjs +179 -0
  108. package/dist/utils/load.cjs.map +1 -0
  109. package/dist/utils/load.d.ts +14 -0
  110. package/dist/utils/load.js +177 -0
  111. package/dist/utils/load.js.map +1 -0
  112. package/dist/utils/logger.cjs +34 -0
  113. package/dist/utils/logger.cjs.map +1 -0
  114. package/dist/utils/logger.d.ts +10 -0
  115. package/dist/utils/logger.js +31 -0
  116. package/dist/utils/logger.js.map +1 -0
  117. package/dist/utils/manifest.cjs +51 -0
  118. package/dist/utils/manifest.cjs.map +1 -0
  119. package/dist/utils/manifest.d.ts +10 -0
  120. package/dist/utils/manifest.js +49 -0
  121. package/dist/utils/manifest.js.map +1 -0
  122. package/dist/utils/plugin.cjs +27 -0
  123. package/dist/utils/plugin.cjs.map +1 -0
  124. package/dist/utils/plugin.d.ts +1 -0
  125. package/dist/utils/plugin.js +27 -0
  126. package/dist/utils/plugin.js.map +1 -0
  127. package/dist/utils/preload.cjs +146 -0
  128. package/dist/utils/preload.cjs.map +1 -0
  129. package/dist/utils/preload.d.ts +8 -0
  130. package/dist/utils/preload.js +143 -0
  131. package/dist/utils/preload.js.map +1 -0
  132. package/dist/utils/semver/compare.cjs +48 -0
  133. package/dist/utils/semver/compare.cjs.map +1 -0
  134. package/dist/utils/semver/compare.js +47 -0
  135. package/dist/utils/semver/compare.js.map +1 -0
  136. package/dist/utils/semver/constants.cjs +41 -0
  137. package/dist/utils/semver/constants.cjs.map +1 -0
  138. package/dist/utils/semver/constants.js +31 -0
  139. package/dist/utils/semver/constants.js.map +1 -0
  140. package/dist/utils/semver/index.cjs +68 -0
  141. package/dist/utils/semver/index.cjs.map +1 -0
  142. package/dist/utils/semver/index.d.ts +5 -0
  143. package/dist/utils/semver/index.js +68 -0
  144. package/dist/utils/semver/index.js.map +1 -0
  145. package/dist/utils/semver/parser.cjs +104 -0
  146. package/dist/utils/semver/parser.cjs.map +1 -0
  147. package/dist/utils/semver/parser.js +96 -0
  148. package/dist/utils/semver/parser.js.map +1 -0
  149. package/dist/utils/semver/utils.cjs +28 -0
  150. package/dist/utils/semver/utils.cjs.map +1 -0
  151. package/dist/utils/semver/utils.js +24 -0
  152. package/dist/utils/semver/utils.js.map +1 -0
  153. package/dist/utils/share.cjs +284 -0
  154. package/dist/utils/share.cjs.map +1 -0
  155. package/dist/utils/share.d.ts +23 -0
  156. package/dist/utils/share.js +278 -0
  157. package/dist/utils/share.js.map +1 -0
  158. package/dist/utils/tool.cjs +82 -0
  159. package/dist/utils/tool.cjs.map +1 -0
  160. package/dist/utils/tool.d.ts +8 -0
  161. package/dist/utils/tool.js +71 -0
  162. package/dist/utils/tool.js.map +1 -0
  163. package/package.json +16 -9
  164. package/dist/index.cjs.cjs +0 -3442
  165. package/dist/index.cjs.cjs.map +0 -1
  166. package/dist/index.cjs.d.ts +0 -1
  167. package/dist/index.esm.js +0 -3418
  168. package/dist/index.esm.js.map +0 -1
  169. package/dist/src/constant.d.ts +0 -2
  170. package/dist/src/core.d.ts +0 -119
  171. package/dist/src/global.d.ts +0 -42
  172. package/dist/src/helpers.d.ts +0 -38
  173. package/dist/src/index.d.ts +0 -14
  174. package/dist/src/module/index.d.ts +0 -25
  175. package/dist/src/plugins/generate-preload-assets.d.ts +0 -8
  176. package/dist/src/plugins/snapshot/SnapshotHandler.d.ts +0 -60
  177. package/dist/src/plugins/snapshot/index.d.ts +0 -5
  178. package/dist/src/remote/index.d.ts +0 -109
  179. package/dist/src/shared/index.d.ts +0 -75
  180. package/dist/src/type/config.d.ts +0 -128
  181. package/dist/src/type/index.d.ts +0 -3
  182. package/dist/src/type/plugin.d.ts +0 -35
  183. package/dist/src/type/preload.d.ts +0 -26
  184. package/dist/src/types.d.ts +0 -1
  185. package/dist/src/utils/env.d.ts +0 -3
  186. package/dist/src/utils/hooks/asyncHook.d.ts +0 -6
  187. package/dist/src/utils/hooks/asyncWaterfallHooks.d.ts +0 -10
  188. package/dist/src/utils/hooks/index.d.ts +0 -6
  189. package/dist/src/utils/hooks/pluginSystem.d.ts +0 -16
  190. package/dist/src/utils/hooks/syncHook.d.ts +0 -12
  191. package/dist/src/utils/hooks/syncWaterfallHook.d.ts +0 -9
  192. package/dist/src/utils/index.d.ts +0 -6
  193. package/dist/src/utils/load.d.ts +0 -11
  194. package/dist/src/utils/logger.d.ts +0 -6
  195. package/dist/src/utils/manifest.d.ts +0 -7
  196. package/dist/src/utils/plugin.d.ts +0 -3
  197. package/dist/src/utils/preload.d.ts +0 -6
  198. package/dist/src/utils/semver/compare.d.ts +0 -9
  199. package/dist/src/utils/semver/constants.d.ts +0 -10
  200. package/dist/src/utils/semver/index.d.ts +0 -2
  201. package/dist/src/utils/semver/parser.d.ts +0 -9
  202. package/dist/src/utils/semver/utils.d.ts +0 -11
  203. package/dist/src/utils/share.d.ts +0 -45
  204. package/dist/src/utils/tool.d.ts +0 -18
  205. package/dist/types.cjs.cjs +0 -3
  206. package/dist/types.cjs.cjs.map +0 -1
  207. package/dist/types.cjs.d.ts +0 -1
  208. package/dist/types.esm.js +0 -2
  209. package/dist/types.esm.js.map +0 -1
@@ -0,0 +1 @@
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,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BAAe,SACZ,CAAC,OAAO,YAAY,WAAW,kBAAkB;AAChD,aAAO,oBAAoB,SAAS,cAAc,aAChD;OAGL;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"}
@@ -0,0 +1,119 @@
1
+ import { Module as Module$1, ModuleOptions } from "../module/index.js";
2
+ import { SyncHook } from "../utils/hooks/syncHook.js";
3
+ import { AsyncHook } from "../utils/hooks/asyncHook.js";
4
+ import { SyncWaterfallHook } from "../utils/hooks/syncWaterfallHook.js";
5
+ import { AsyncWaterfallHook } from "../utils/hooks/asyncWaterfallHooks.js";
6
+ import { PluginSystem } from "../utils/hooks/pluginSystem.js";
7
+ import { ModuleFederation } from "../core.js";
8
+ import { CallFrom, Options, Remote, RemoteEntryExports, RemoteInfo, UserOptions } from "../type/config.js";
9
+ import { PreloadAssets, PreloadOptions, PreloadRemoteArgs } from "../type/preload.js";
10
+ import { GlobalModuleInfo, ModuleInfo } from "@module-federation/sdk";
11
+
12
+ //#region src/remote/index.d.ts
13
+ interface LoadRemoteMatch {
14
+ id: string;
15
+ pkgNameOrAlias: string;
16
+ expose: string;
17
+ remote: Remote;
18
+ options: Options;
19
+ origin: ModuleFederation;
20
+ remoteInfo: RemoteInfo;
21
+ remoteSnapshot?: ModuleInfo;
22
+ }
23
+ declare class RemoteHandler {
24
+ host: ModuleFederation;
25
+ idToRemoteMap: Record<string, {
26
+ name: string;
27
+ expose: string;
28
+ }>;
29
+ hooks: PluginSystem<{
30
+ beforeRegisterRemote: SyncWaterfallHook<{
31
+ remote: Remote;
32
+ origin: ModuleFederation;
33
+ }>;
34
+ registerRemote: SyncWaterfallHook<{
35
+ remote: Remote;
36
+ origin: ModuleFederation;
37
+ }>;
38
+ beforeRequest: AsyncWaterfallHook<{
39
+ id: string;
40
+ options: Options;
41
+ origin: ModuleFederation;
42
+ }>;
43
+ onLoad: AsyncHook<[{
44
+ id: string;
45
+ expose: string;
46
+ pkgNameOrAlias: string;
47
+ remote: Remote;
48
+ options: ModuleOptions;
49
+ origin: ModuleFederation;
50
+ exposeModule: any;
51
+ exposeModuleFactory: any;
52
+ moduleInstance: Module$1;
53
+ }], void>;
54
+ handlePreloadModule: SyncHook<[{
55
+ id: string;
56
+ name: string;
57
+ remote: Remote;
58
+ remoteSnapshot: ModuleInfo;
59
+ preloadConfig: PreloadRemoteArgs;
60
+ origin: ModuleFederation;
61
+ }], void>;
62
+ errorLoadRemote: AsyncHook<[{
63
+ id: string;
64
+ error: unknown;
65
+ options?: any;
66
+ from: CallFrom;
67
+ lifecycle: "beforeRequest" | "beforeLoadShare" | "afterResolve" | "onLoad";
68
+ origin: ModuleFederation;
69
+ }], unknown>;
70
+ beforePreloadRemote: AsyncHook<[{
71
+ preloadOps: Array<PreloadRemoteArgs>;
72
+ options: Options;
73
+ origin: ModuleFederation;
74
+ }], false | void | Promise<false | void>>;
75
+ generatePreloadAssets: AsyncHook<[{
76
+ origin: ModuleFederation;
77
+ preloadOptions: PreloadOptions[number];
78
+ remote: Remote;
79
+ remoteInfo: RemoteInfo;
80
+ remoteSnapshot: ModuleInfo;
81
+ globalSnapshot: GlobalModuleInfo;
82
+ }], Promise<PreloadAssets>>;
83
+ afterPreloadRemote: AsyncHook<{
84
+ preloadOps: Array<PreloadRemoteArgs>;
85
+ options: Options;
86
+ origin: ModuleFederation;
87
+ }, false | void | Promise<false | void>>;
88
+ loadEntry: AsyncHook<[{
89
+ loaderHook: ModuleFederation["loaderHook"];
90
+ remoteInfo: RemoteInfo;
91
+ remoteEntryExports?: RemoteEntryExports;
92
+ }], Promise<RemoteEntryExports>>;
93
+ }>;
94
+ constructor(host: ModuleFederation);
95
+ formatAndRegisterRemote(globalOptions: Options, userOptions: UserOptions): Remote[];
96
+ setIdToRemoteMap(id: string, remoteMatchInfo: LoadRemoteMatch): void;
97
+ loadRemote<T>(id: string, options?: {
98
+ loadFactory?: boolean;
99
+ from: CallFrom;
100
+ }): Promise<T | null>;
101
+ preloadRemote(preloadOptions: Array<PreloadRemoteArgs>): Promise<void>;
102
+ registerRemotes(remotes: Remote[], options?: {
103
+ force?: boolean;
104
+ }): void;
105
+ getRemoteModuleAndOptions(options: {
106
+ id: string;
107
+ }): Promise<{
108
+ module: Module$1;
109
+ moduleOptions: ModuleOptions;
110
+ remoteMatchInfo: LoadRemoteMatch;
111
+ }>;
112
+ registerRemote(remote: Remote, targetRemotes: Remote[], options?: {
113
+ force?: boolean;
114
+ }): void;
115
+ private removeRemote;
116
+ }
117
+ //#endregion
118
+ export { LoadRemoteMatch, RemoteHandler };
119
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,304 @@
1
+ import { assert, error, logger } from "../utils/logger.js";
2
+ import { CurrentGlobal, Global, getInfoWithoutType, globalLoading } from "../global.js";
3
+ import { DEFAULT_REMOTE_TYPE, DEFAULT_SCOPE } from "../constant.js";
4
+ import { getGlobalShareScope } from "../utils/share.js";
5
+ import { matchRemoteWithNameAndExpose } from "../utils/manifest.js";
6
+ import { getRemoteEntryUniqueKey, getRemoteInfo } from "../utils/load.js";
7
+ import { optionsToMFContext } from "../utils/context.js";
8
+ import "../utils/index.js";
9
+ import { formatPreloadArgs, preloadAssets } from "../utils/preload.js";
10
+ import { Module as Module$1 } from "../module/index.js";
11
+ import { SyncHook } from "../utils/hooks/syncHook.js";
12
+ import { AsyncHook } from "../utils/hooks/asyncHook.js";
13
+ import { SyncWaterfallHook } from "../utils/hooks/syncWaterfallHook.js";
14
+ import { AsyncWaterfallHook } from "../utils/hooks/asyncWaterfallHooks.js";
15
+ import { PluginSystem } from "../utils/hooks/pluginSystem.js";
16
+ import "../utils/hooks/index.js";
17
+ import { getGlobalRemoteInfo } from "../plugins/snapshot/SnapshotHandler.js";
18
+ import { composeKeyWithSeparator, isBrowserEnvValue, warn } from "@module-federation/sdk";
19
+ import { RUNTIME_004, runtimeDescMap } from "@module-federation/error-codes";
20
+
21
+ //#region src/remote/index.ts
22
+ var RemoteHandler = class {
23
+ constructor(host) {
24
+ this.hooks = new PluginSystem({
25
+ beforeRegisterRemote: new SyncWaterfallHook("beforeRegisterRemote"),
26
+ registerRemote: new SyncWaterfallHook("registerRemote"),
27
+ beforeRequest: new AsyncWaterfallHook("beforeRequest"),
28
+ onLoad: new AsyncHook("onLoad"),
29
+ handlePreloadModule: new SyncHook("handlePreloadModule"),
30
+ errorLoadRemote: new AsyncHook("errorLoadRemote"),
31
+ beforePreloadRemote: new AsyncHook("beforePreloadRemote"),
32
+ generatePreloadAssets: new AsyncHook("generatePreloadAssets"),
33
+ afterPreloadRemote: new AsyncHook(),
34
+ loadEntry: new AsyncHook()
35
+ });
36
+ this.host = host;
37
+ this.idToRemoteMap = {};
38
+ }
39
+ formatAndRegisterRemote(globalOptions, userOptions) {
40
+ return (userOptions.remotes || []).reduce((res, remote) => {
41
+ this.registerRemote(remote, res, { force: false });
42
+ return res;
43
+ }, globalOptions.remotes);
44
+ }
45
+ setIdToRemoteMap(id, remoteMatchInfo) {
46
+ const { remote, expose } = remoteMatchInfo;
47
+ const { name, alias } = remote;
48
+ this.idToRemoteMap[id] = {
49
+ name: remote.name,
50
+ expose
51
+ };
52
+ if (alias && id.startsWith(name)) {
53
+ const idWithAlias = id.replace(name, alias);
54
+ this.idToRemoteMap[idWithAlias] = {
55
+ name: remote.name,
56
+ expose
57
+ };
58
+ return;
59
+ }
60
+ if (alias && id.startsWith(alias)) {
61
+ const idWithName = id.replace(alias, name);
62
+ this.idToRemoteMap[idWithName] = {
63
+ name: remote.name,
64
+ expose
65
+ };
66
+ }
67
+ }
68
+ async loadRemote(id, options) {
69
+ const { host } = this;
70
+ try {
71
+ const { loadFactory = true } = options || { loadFactory: true };
72
+ const { module, moduleOptions, remoteMatchInfo } = await this.getRemoteModuleAndOptions({ id });
73
+ const { pkgNameOrAlias, remote, expose, id: idRes, remoteSnapshot } = remoteMatchInfo;
74
+ const moduleOrFactory = await module.get(idRes, expose, options, remoteSnapshot);
75
+ const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
76
+ id: idRes,
77
+ pkgNameOrAlias,
78
+ expose,
79
+ exposeModule: loadFactory ? moduleOrFactory : void 0,
80
+ exposeModuleFactory: loadFactory ? void 0 : moduleOrFactory,
81
+ remote,
82
+ options: moduleOptions,
83
+ moduleInstance: module,
84
+ origin: host
85
+ });
86
+ this.setIdToRemoteMap(id, remoteMatchInfo);
87
+ if (typeof moduleWrapper === "function") return moduleWrapper;
88
+ return moduleOrFactory;
89
+ } catch (error) {
90
+ const { from = "runtime" } = options || { from: "runtime" };
91
+ const failOver = await this.hooks.lifecycle.errorLoadRemote.emit({
92
+ id,
93
+ error,
94
+ from,
95
+ lifecycle: "onLoad",
96
+ origin: host
97
+ });
98
+ if (!failOver) throw error;
99
+ return failOver;
100
+ }
101
+ }
102
+ async preloadRemote(preloadOptions) {
103
+ const { host } = this;
104
+ await this.hooks.lifecycle.beforePreloadRemote.emit({
105
+ preloadOps: preloadOptions,
106
+ options: host.options,
107
+ origin: host
108
+ });
109
+ const preloadOps = formatPreloadArgs(host.options.remotes, preloadOptions);
110
+ await Promise.all(preloadOps.map(async (ops) => {
111
+ const { remote } = ops;
112
+ const remoteInfo = getRemoteInfo(remote);
113
+ const { globalSnapshot, remoteSnapshot } = await host.snapshotHandler.loadRemoteSnapshotInfo({ moduleInfo: remote });
114
+ const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({
115
+ origin: host,
116
+ preloadOptions: ops,
117
+ remote,
118
+ remoteInfo,
119
+ globalSnapshot,
120
+ remoteSnapshot
121
+ });
122
+ if (!assets) return;
123
+ preloadAssets(remoteInfo, host, assets);
124
+ }));
125
+ }
126
+ registerRemotes(remotes, options) {
127
+ const { host } = this;
128
+ remotes.forEach((remote) => {
129
+ this.registerRemote(remote, host.options.remotes, { force: options?.force });
130
+ });
131
+ }
132
+ async getRemoteModuleAndOptions(options) {
133
+ const { host } = this;
134
+ const { id } = options;
135
+ let loadRemoteArgs;
136
+ try {
137
+ loadRemoteArgs = await this.hooks.lifecycle.beforeRequest.emit({
138
+ id,
139
+ options: host.options,
140
+ origin: host
141
+ });
142
+ } catch (error) {
143
+ loadRemoteArgs = await this.hooks.lifecycle.errorLoadRemote.emit({
144
+ id,
145
+ options: host.options,
146
+ origin: host,
147
+ from: "runtime",
148
+ error,
149
+ lifecycle: "beforeRequest"
150
+ });
151
+ if (!loadRemoteArgs) throw error;
152
+ }
153
+ const { id: idRes } = loadRemoteArgs;
154
+ const remoteSplitInfo = matchRemoteWithNameAndExpose(host.options.remotes, idRes);
155
+ if (!remoteSplitInfo) error(RUNTIME_004, runtimeDescMap, {
156
+ hostName: host.options.name,
157
+ requestId: idRes
158
+ }, void 0, optionsToMFContext(host.options));
159
+ const { remote: rawRemote } = remoteSplitInfo;
160
+ const remoteInfo = getRemoteInfo(rawRemote);
161
+ const matchInfo = await host.sharedHandler.hooks.lifecycle.afterResolve.emit({
162
+ id: idRes,
163
+ ...remoteSplitInfo,
164
+ options: host.options,
165
+ origin: host,
166
+ remoteInfo
167
+ });
168
+ const { remote, expose } = matchInfo;
169
+ assert(remote && expose, `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`);
170
+ let module = host.moduleCache.get(remote.name);
171
+ const moduleOptions = {
172
+ host,
173
+ remoteInfo
174
+ };
175
+ if (!module) {
176
+ module = new Module$1(moduleOptions);
177
+ host.moduleCache.set(remote.name, module);
178
+ }
179
+ return {
180
+ module,
181
+ moduleOptions,
182
+ remoteMatchInfo: matchInfo
183
+ };
184
+ }
185
+ registerRemote(remote, targetRemotes, options) {
186
+ const { host } = this;
187
+ const normalizeRemote = () => {
188
+ if (remote.alias) {
189
+ const findEqual = targetRemotes.find((item) => remote.alias && (item.name.startsWith(remote.alias) || item.alias?.startsWith(remote.alias)));
190
+ assert(!findEqual, `The alias ${remote.alias} of remote ${remote.name} is not allowed to be the prefix of ${findEqual && findEqual.name} name or alias`);
191
+ }
192
+ if ("entry" in remote) {
193
+ if (isBrowserEnvValue && typeof window !== "undefined" && !remote.entry.startsWith("http")) remote.entry = new URL(remote.entry, window.location.origin).href;
194
+ }
195
+ if (!remote.shareScope) remote.shareScope = DEFAULT_SCOPE;
196
+ if (!remote.type) remote.type = DEFAULT_REMOTE_TYPE;
197
+ };
198
+ this.hooks.lifecycle.beforeRegisterRemote.emit({
199
+ remote,
200
+ origin: host
201
+ });
202
+ const registeredRemote = targetRemotes.find((item) => item.name === remote.name);
203
+ if (!registeredRemote) {
204
+ normalizeRemote();
205
+ targetRemotes.push(remote);
206
+ this.hooks.lifecycle.registerRemote.emit({
207
+ remote,
208
+ origin: host
209
+ });
210
+ } else {
211
+ const messages = [`The remote "${remote.name}" is already registered.`, "Please note that overriding it may cause unexpected errors."];
212
+ if (options?.force) {
213
+ this.removeRemote(registeredRemote);
214
+ normalizeRemote();
215
+ targetRemotes.push(remote);
216
+ this.hooks.lifecycle.registerRemote.emit({
217
+ remote,
218
+ origin: host
219
+ });
220
+ warn(messages.join(" "));
221
+ }
222
+ }
223
+ }
224
+ removeRemote(remote) {
225
+ try {
226
+ const { host } = this;
227
+ const { name } = remote;
228
+ const remoteIndex = host.options.remotes.findIndex((item) => item.name === name);
229
+ if (remoteIndex !== -1) host.options.remotes.splice(remoteIndex, 1);
230
+ const loadedModule = host.moduleCache.get(remote.name);
231
+ if (loadedModule) {
232
+ const remoteInfo = loadedModule.remoteInfo;
233
+ const key = remoteInfo.entryGlobalName;
234
+ if (CurrentGlobal[key]) if (Object.getOwnPropertyDescriptor(CurrentGlobal, key)?.configurable) delete CurrentGlobal[key];
235
+ else CurrentGlobal[key] = void 0;
236
+ const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
237
+ if (globalLoading[remoteEntryUniqueKey]) delete globalLoading[remoteEntryUniqueKey];
238
+ host.snapshotHandler.manifestCache.delete(remoteInfo.entry);
239
+ let remoteInsId = remoteInfo.buildVersion ? composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion) : remoteInfo.name;
240
+ const remoteInsIndex = CurrentGlobal.__FEDERATION__.__INSTANCES__.findIndex((ins) => {
241
+ if (remoteInfo.buildVersion) return ins.options.id === remoteInsId;
242
+ else return ins.name === remoteInsId;
243
+ });
244
+ if (remoteInsIndex !== -1) {
245
+ const remoteIns = CurrentGlobal.__FEDERATION__.__INSTANCES__[remoteInsIndex];
246
+ remoteInsId = remoteIns.options.id || remoteInsId;
247
+ const globalShareScopeMap = getGlobalShareScope();
248
+ let isAllSharedNotUsed = true;
249
+ const needDeleteKeys = [];
250
+ Object.keys(globalShareScopeMap).forEach((instId) => {
251
+ const shareScopeMap = globalShareScopeMap[instId];
252
+ shareScopeMap && Object.keys(shareScopeMap).forEach((shareScope) => {
253
+ const shareScopeVal = shareScopeMap[shareScope];
254
+ shareScopeVal && Object.keys(shareScopeVal).forEach((shareName) => {
255
+ const sharedPkgs = shareScopeVal[shareName];
256
+ sharedPkgs && Object.keys(sharedPkgs).forEach((shareVersion) => {
257
+ const shared = sharedPkgs[shareVersion];
258
+ if (shared && typeof shared === "object" && shared.from === remoteInfo.name) if (shared.loaded || shared.loading) {
259
+ shared.useIn = shared.useIn.filter((usedHostName) => usedHostName !== remoteInfo.name);
260
+ if (shared.useIn.length) isAllSharedNotUsed = false;
261
+ else needDeleteKeys.push([
262
+ instId,
263
+ shareScope,
264
+ shareName,
265
+ shareVersion
266
+ ]);
267
+ } else needDeleteKeys.push([
268
+ instId,
269
+ shareScope,
270
+ shareName,
271
+ shareVersion
272
+ ]);
273
+ });
274
+ });
275
+ });
276
+ });
277
+ if (isAllSharedNotUsed) {
278
+ remoteIns.shareScopeMap = {};
279
+ delete globalShareScopeMap[remoteInsId];
280
+ }
281
+ needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion]) => {
282
+ delete globalShareScopeMap[insId]?.[shareScope]?.[shareName]?.[shareVersion];
283
+ });
284
+ CurrentGlobal.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
285
+ }
286
+ const { hostGlobalSnapshot } = getGlobalRemoteInfo(remote, host);
287
+ if (hostGlobalSnapshot) {
288
+ const remoteKey = hostGlobalSnapshot && "remotesInfo" in hostGlobalSnapshot && hostGlobalSnapshot.remotesInfo && getInfoWithoutType(hostGlobalSnapshot.remotesInfo, remote.name).key;
289
+ if (remoteKey) {
290
+ delete hostGlobalSnapshot.remotesInfo[remoteKey];
291
+ if (Boolean(Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey])) delete Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey];
292
+ }
293
+ }
294
+ host.moduleCache.delete(remote.name);
295
+ }
296
+ } catch (err) {
297
+ logger.error(`removeRemote failed: ${err instanceof Error ? err.message : String(err)}`);
298
+ }
299
+ }
300
+ };
301
+
302
+ //#endregion
303
+ export { RemoteHandler };
304
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
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 `removeR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