{"version":3,"file":"index.cjs","names":["getRemoteEntry","composeRemoteRequestId","RUNTIME_002","runtimeDescMap","optionsToMFContext","RUNTIME_015","RUNTIME_014","processModuleAlias"],"sources":["../../src/module/index.ts"],"sourcesContent":["import {\n  assert,\n  error,\n  processModuleAlias,\n  optionsToMFContext,\n  composeRemoteRequestId,\n} from '../utils';\nimport { safeToString, ModuleInfo } from '@module-federation/sdk';\nimport {\n  RUNTIME_002,\n  RUNTIME_014,\n  RUNTIME_015,\n  runtimeDescMap,\n} from '@module-federation/error-codes';\nimport { getRemoteEntry } from '../utils/load';\nimport { ModuleFederation } from '../core';\nimport {\n  RemoteEntryExports,\n  RemoteInfo,\n  InitScope,\n  ShareScopeMap,\n} from '../type';\n\nexport type ModuleOptions = ConstructorParameters<typeof Module>[0];\nexport type RemoteModuleFactory = () => unknown | Promise<unknown>;\n\nfunction getAvailableExposeNames(\n  remoteSnapshot?: ModuleInfo,\n): string | undefined {\n  if (\n    !remoteSnapshot ||\n    !('modules' in remoteSnapshot) ||\n    !Array.isArray(remoteSnapshot.modules)\n  ) {\n    return undefined;\n  }\n\n  const exposes = remoteSnapshot.modules\n    .map((module) => module.moduleName)\n    .filter(Boolean);\n\n  return exposes.length ? exposes.join(',') : undefined;\n}\n\nexport function createRemoteEntryInitOptions(\n  remoteInfo: RemoteInfo,\n  hostShareScopeMap: ShareScopeMap,\n  rawInitScope?: InitScope,\n): Record<string, any> {\n  const localShareScopeMap = hostShareScopeMap;\n\n  const shareScopeKeys = Array.isArray(remoteInfo.shareScope)\n    ? remoteInfo.shareScope\n    : [remoteInfo.shareScope];\n\n  if (!shareScopeKeys.length) {\n    shareScopeKeys.push('default');\n  }\n  shareScopeKeys.forEach((shareScopeKey) => {\n    if (!localShareScopeMap[shareScopeKey]) {\n      localShareScopeMap[shareScopeKey] = {};\n    }\n  });\n\n  const remoteEntryInitOptions = {\n    version: remoteInfo.version || '',\n    shareScopeKeys: Array.isArray(remoteInfo.shareScope)\n      ? shareScopeKeys\n      : remoteInfo.shareScope || 'default',\n  };\n\n  // Help to find host instance\n  Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {\n    value: localShareScopeMap,\n    // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed\n    enumerable: false,\n  });\n\n  // TODO: compate legacy init params, should use shareScopeMap if exist\n  const shareScope = localShareScopeMap[shareScopeKeys[0]];\n  const initScope: InitScope = rawInitScope ?? [];\n\n  return {\n    remoteEntryInitOptions,\n    shareScope,\n    initScope,\n  };\n}\n\nclass Module {\n  remoteInfo: RemoteInfo;\n  inited = false;\n  initing = false;\n  initPromise?: Promise<void>;\n  remoteEntryExports?: RemoteEntryExports;\n  lib: RemoteEntryExports | undefined = undefined;\n  host: ModuleFederation;\n\n  constructor({\n    remoteInfo,\n    host,\n  }: {\n    remoteInfo: RemoteInfo;\n    host: ModuleFederation;\n  }) {\n    this.remoteInfo = remoteInfo;\n    this.host = host;\n  }\n\n  async getEntry(expose?: string): Promise<RemoteEntryExports> {\n    if (this.remoteEntryExports) {\n      return this.remoteEntryExports;\n    }\n\n    const remoteEntryExports = await getRemoteEntry({\n      origin: this.host,\n      remoteInfo: this.remoteInfo,\n      remoteEntryExports: this.remoteEntryExports,\n      resourceContext: {\n        initiator: 'loadRemote',\n        id: composeRemoteRequestId(this.remoteInfo.name, expose),\n        resourceType: 'remoteEntry',\n      },\n    });\n\n    assert(\n      remoteEntryExports,\n      `remoteEntryExports is undefined \\n ${safeToString(this.remoteInfo)}`,\n    );\n\n    this.remoteEntryExports = remoteEntryExports;\n    return this.remoteEntryExports;\n  }\n\n  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\n\n  async init(\n    id?: string,\n    remoteSnapshot?: ModuleInfo,\n    rawInitScope?: InitScope,\n    expose?: string,\n  ) {\n    // Get remoteEntry.js\n    const remoteEntryExports = await this.getEntry(expose);\n\n    if (this.inited) {\n      await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n        id,\n        remoteInfo: this.remoteInfo,\n        remoteSnapshot,\n        remoteEntryExports,\n        cached: true,\n        origin: this.host,\n      });\n      return remoteEntryExports;\n    }\n\n    if (this.initPromise) {\n      try {\n        await this.initPromise;\n        await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n          id,\n          remoteInfo: this.remoteInfo,\n          remoteSnapshot,\n          remoteEntryExports,\n          cached: true,\n          origin: this.host,\n        });\n      } catch (initError) {\n        await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n          id,\n          remoteInfo: this.remoteInfo,\n          remoteSnapshot,\n          remoteEntryExports,\n          error: initError,\n          cached: true,\n          origin: this.host,\n        });\n        throw initError;\n      }\n      return remoteEntryExports;\n    }\n\n    this.initing = true;\n    this.initPromise = (async () => {\n      await this.host.loaderHook.lifecycle.beforeInitRemote.emit({\n        id,\n        remoteInfo: this.remoteInfo,\n        remoteSnapshot,\n        origin: this.host,\n      });\n\n      const { remoteEntryInitOptions, shareScope, initScope } =\n        createRemoteEntryInitOptions(\n          this.remoteInfo,\n          this.host.shareScopeMap,\n          rawInitScope,\n        );\n\n      const initContainerOptions =\n        await this.host.hooks.lifecycle.beforeInitContainer.emit({\n          shareScope,\n          // @ts-ignore shareScopeMap will be set by Object.defineProperty\n          remoteEntryInitOptions,\n          initScope,\n          remoteInfo: this.remoteInfo,\n          origin: this.host,\n        });\n\n      if (typeof remoteEntryExports?.init === 'undefined') {\n        error(\n          RUNTIME_002,\n          runtimeDescMap,\n          {\n            hostName: this.host.name,\n            remoteName: this.remoteInfo.name,\n            remoteEntryUrl: this.remoteInfo.entry,\n            remoteEntryKey: this.remoteInfo.entryGlobalName,\n          },\n          undefined,\n          optionsToMFContext(this.host.options),\n        );\n      }\n\n      try {\n        await remoteEntryExports.init(\n          initContainerOptions.shareScope,\n          initContainerOptions.initScope,\n          initContainerOptions.remoteEntryInitOptions,\n        );\n      } catch (initError) {\n        error(\n          RUNTIME_015,\n          runtimeDescMap,\n          {\n            hostName: this.host.name,\n            remoteName: this.remoteInfo.name,\n            remoteEntryUrl: this.remoteInfo.entry,\n            remoteEntryKey: this.remoteInfo.entryGlobalName,\n            shareScope: this.remoteInfo.shareScope,\n          },\n          `${initError}`,\n          optionsToMFContext(this.host.options),\n        );\n      }\n\n      await this.host.hooks.lifecycle.initContainer.emit({\n        ...initContainerOptions,\n        id,\n        remoteSnapshot,\n        remoteEntryExports,\n      });\n      this.inited = true;\n    })();\n\n    try {\n      await this.initPromise;\n      await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n        id,\n        remoteInfo: this.remoteInfo,\n        remoteSnapshot,\n        remoteEntryExports,\n        origin: this.host,\n      });\n    } catch (initError) {\n      await this.host.loaderHook.lifecycle.afterInitRemote.emit({\n        id,\n        remoteInfo: this.remoteInfo,\n        remoteSnapshot,\n        remoteEntryExports,\n        error: initError,\n        origin: this.host,\n      });\n      throw initError;\n    } finally {\n      this.initing = false;\n      this.initPromise = undefined;\n    }\n\n    return remoteEntryExports;\n  }\n\n  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\n  async get(\n    id: string,\n    expose: string,\n    options?: { loadFactory?: boolean },\n    remoteSnapshot?: ModuleInfo,\n  ) {\n    const { loadFactory = true } = options || { loadFactory: true };\n\n    const remoteEntryExports = await this.init(\n      id,\n      remoteSnapshot,\n      undefined,\n      expose,\n    );\n    this.lib = remoteEntryExports;\n\n    await this.host.loaderHook.lifecycle.beforeGetExpose.emit({\n      id,\n      expose,\n      moduleInfo: this.remoteInfo,\n      remoteEntryExports,\n      origin: this.host,\n    });\n\n    let moduleFactory: RemoteModuleFactory | undefined;\n    try {\n      const hookModuleFactory =\n        await this.host.loaderHook.lifecycle.getModuleFactory.emit({\n          remoteEntryExports,\n          expose,\n          moduleInfo: this.remoteInfo,\n        });\n      moduleFactory =\n        typeof hookModuleFactory === 'function' ? hookModuleFactory : undefined;\n\n      // get exposeGetter\n      if (!moduleFactory) {\n        moduleFactory = await remoteEntryExports.get(expose);\n      }\n\n      if (!moduleFactory) {\n        error(\n          RUNTIME_014,\n          runtimeDescMap,\n          {\n            hostName: this.host.name,\n            remoteName: this.remoteInfo.name,\n            remoteEntryUrl: this.remoteInfo.entry,\n            expose,\n            requestId: id,\n            availableExposes: getAvailableExposeNames(remoteSnapshot),\n          },\n          undefined,\n          optionsToMFContext(this.host.options),\n        );\n      }\n\n      await this.host.loaderHook.lifecycle.afterGetExpose.emit({\n        id,\n        expose,\n        moduleInfo: this.remoteInfo,\n        remoteEntryExports,\n        moduleFactory,\n        origin: this.host,\n      });\n    } catch (getExposeError) {\n      await this.host.loaderHook.lifecycle.afterGetExpose.emit({\n        id,\n        expose,\n        moduleInfo: this.remoteInfo,\n        remoteEntryExports,\n        error: getExposeError,\n        origin: this.host,\n      });\n      throw getExposeError;\n    }\n\n    // keep symbol for module name always one format\n    const symbolName = processModuleAlias(this.remoteInfo.name, expose);\n    const wrapModuleFactory = this.wraperFactory(moduleFactory, symbolName);\n\n    if (!loadFactory) {\n      return wrapModuleFactory;\n    }\n\n    await this.host.loaderHook.lifecycle.beforeExecuteFactory.emit({\n      id,\n      expose,\n      moduleInfo: this.remoteInfo,\n      loadFactory,\n      origin: this.host,\n    });\n\n    try {\n      const exposeContent = await wrapModuleFactory();\n\n      await this.host.loaderHook.lifecycle.afterExecuteFactory.emit({\n        id,\n        expose,\n        moduleInfo: this.remoteInfo,\n        loadFactory,\n        exposeModule: exposeContent,\n        origin: this.host,\n      });\n\n      return exposeContent;\n    } catch (executeFactoryError) {\n      await this.host.loaderHook.lifecycle.afterExecuteFactory.emit({\n        id,\n        expose,\n        moduleInfo: this.remoteInfo,\n        loadFactory,\n        error: executeFactoryError,\n        origin: this.host,\n      });\n      throw executeFactoryError;\n    }\n  }\n\n  private wraperFactory(moduleFactory: RemoteModuleFactory, id: string) {\n    function defineModuleId(res: unknown, id: string) {\n      if (\n        res &&\n        typeof res === 'object' &&\n        Object.isExtensible(res) &&\n        !Object.getOwnPropertyDescriptor(res, Symbol.for('mf_module_id'))\n      ) {\n        Object.defineProperty(res, Symbol.for('mf_module_id'), {\n          value: id,\n          enumerable: false,\n        });\n      }\n    }\n\n    return () => {\n      const res = moduleFactory();\n\n      if (res instanceof Promise) {\n        return res.then((asyncRes) => {\n          // This parameter is used for bridge debugging\n          defineModuleId(asyncRes, id);\n          return asyncRes;\n        });\n      }\n\n      // This parameter is used for bridge debugging\n      defineModuleId(res, id);\n      return res;\n    };\n  }\n}\n\nexport { Module };\n"],"mappings":";;;;;;;;;;AA0BA,SAAS,wBACP,gBACoB;AACpB,KACE,CAAC,kBACD,EAAE,aAAa,mBACf,CAAC,MAAM,QAAQ,eAAe,QAAQ,CAEtC;CAGF,MAAM,UAAU,eAAe,QAC5B,KAAK,WAAW,OAAO,WAAW,CAClC,OAAO,QAAQ;AAElB,QAAO,QAAQ,SAAS,QAAQ,KAAK,IAAI,GAAG;;AAG9C,SAAgB,6BACd,YACA,mBACA,cACqB;CACrB,MAAM,qBAAqB;CAE3B,MAAM,iBAAiB,MAAM,QAAQ,WAAW,WAAW,GACvD,WAAW,aACX,CAAC,WAAW,WAAW;AAE3B,KAAI,CAAC,eAAe,OAClB,gBAAe,KAAK,UAAU;AAEhC,gBAAe,SAAS,kBAAkB;AACxC,MAAI,CAAC,mBAAmB,eACtB,oBAAmB,iBAAiB,EAAE;GAExC;CAEF,MAAM,yBAAyB;EAC7B,SAAS,WAAW,WAAW;EAC/B,gBAAgB,MAAM,QAAQ,WAAW,WAAW,GAChD,iBACA,WAAW,cAAc;EAC9B;AAGD,QAAO,eAAe,wBAAwB,iBAAiB;EAC7D,OAAO;EAEP,YAAY;EACb,CAAC;AAMF,QAAO;EACL;EACA,YALiB,mBAAmB,eAAe;EAMnD,WAL2B,gBAAgB,EAAE;EAM9C;;AAGH,IAAM,SAAN,MAAa;CASX,YAAY,EACV,YACA,QAIC;gBAbM;iBACC;aAG4B;AAUpC,OAAK,aAAa;AAClB,OAAK,OAAO;;CAGd,MAAM,SAAS,QAA8C;AAC3D,MAAI,KAAK,mBACP,QAAO,K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