{"version":3,"file":"global.cjs","names":["getFMId"],"sources":["../src/global.ts"],"sourcesContent":["import { ModuleFederation } from './core';\nimport {\n  RemoteEntryExports,\n  GlobalShareScopeMap,\n  Remote,\n  Optional,\n} from './type';\nimport { getFMId } from './utils/tool';\nimport {\n  GlobalModuleInfo,\n  ModuleInfo,\n  isDebugMode,\n} from '@module-federation/sdk';\nimport { warn, error } from './utils/logger';\nimport { ModuleFederationRuntimePlugin } from './type/plugin';\n\nexport interface Federation {\n  __GLOBAL_PLUGIN__: Array<ModuleFederationRuntimePlugin>;\n  __DEBUG_CONSTRUCTOR_VERSION__?: string;\n  moduleInfo: GlobalModuleInfo;\n  __DEBUG_CONSTRUCTOR__?: typeof ModuleFederation;\n  __INSTANCES__: Array<ModuleFederation>;\n  __SHARE__: GlobalShareScopeMap;\n  __MANIFEST_LOADING__: Record<string, Promise<ModuleInfo>>;\n  __PRELOADED_MAP__: Map<string, boolean>;\n}\nexport const CurrentGlobal =\n  typeof globalThis === 'object' ? globalThis : window;\nexport const nativeGlobal: typeof global = (() => {\n  try {\n    // get real window (incase of sandbox)\n    return document.defaultView;\n  } catch {\n    // node env\n    return CurrentGlobal;\n  }\n})() as typeof global;\n\nexport const Global = nativeGlobal;\n\ndeclare global {\n  // eslint-disable-next-line no-var\n  var __FEDERATION__: Federation,\n    __VMOK__: Federation,\n    // eslint-disable-next-line no-var\n    __GLOBAL_LOADING_REMOTE_ENTRY__: Record<\n      string,\n      undefined | Promise<RemoteEntryExports | void>\n    >;\n}\n\nfunction definePropertyGlobalVal(\n  target: typeof CurrentGlobal,\n  key: string,\n  val: any,\n) {\n  Object.defineProperty(target, key, {\n    value: val,\n    configurable: false,\n    writable: true,\n  });\n}\n\nfunction includeOwnProperty(target: typeof CurrentGlobal, key: string) {\n  return Object.hasOwnProperty.call(target, key);\n}\n\n// This section is to prevent encapsulation by certain microfrontend frameworks. Due to reuse policies, sandbox escapes.\n// The sandbox in the microfrontend does not replicate the value of 'configurable'.\n// If there is no loading content on the global object, this section defines the loading object.\nif (!includeOwnProperty(CurrentGlobal, '__GLOBAL_LOADING_REMOTE_ENTRY__')) {\n  definePropertyGlobalVal(CurrentGlobal, '__GLOBAL_LOADING_REMOTE_ENTRY__', {});\n}\n\nexport const globalLoading = CurrentGlobal.__GLOBAL_LOADING_REMOTE_ENTRY__;\n\nfunction setGlobalDefaultVal(target: typeof CurrentGlobal) {\n  if (\n    includeOwnProperty(target, '__VMOK__') &&\n    !includeOwnProperty(target, '__FEDERATION__')\n  ) {\n    definePropertyGlobalVal(target, '__FEDERATION__', target.__VMOK__);\n  }\n\n  if (!includeOwnProperty(target, '__FEDERATION__')) {\n    definePropertyGlobalVal(target, '__FEDERATION__', {\n      __GLOBAL_PLUGIN__: [],\n      __INSTANCES__: [],\n      moduleInfo: {},\n      __SHARE__: {},\n      __MANIFEST_LOADING__: {},\n      __PRELOADED_MAP__: new Map(),\n    });\n\n    definePropertyGlobalVal(target, '__VMOK__', target.__FEDERATION__);\n  }\n\n  target.__FEDERATION__.__GLOBAL_PLUGIN__ ??= [];\n  target.__FEDERATION__.__INSTANCES__ ??= [];\n  target.__FEDERATION__.moduleInfo ??= {};\n  target.__FEDERATION__.__SHARE__ ??= {};\n  target.__FEDERATION__.__MANIFEST_LOADING__ ??= {};\n  target.__FEDERATION__.__PRELOADED_MAP__ ??= new Map();\n}\n\nsetGlobalDefaultVal(CurrentGlobal);\nsetGlobalDefaultVal(nativeGlobal);\n\nexport function resetFederationGlobalInfo(): void {\n  CurrentGlobal.__FEDERATION__.__GLOBAL_PLUGIN__ = [];\n  CurrentGlobal.__FEDERATION__.__INSTANCES__ = [];\n  CurrentGlobal.__FEDERATION__.moduleInfo = {};\n  CurrentGlobal.__FEDERATION__.__SHARE__ = {};\n  CurrentGlobal.__FEDERATION__.__MANIFEST_LOADING__ = {};\n\n  Object.keys(globalLoading).forEach((key) => {\n    delete globalLoading[key];\n  });\n}\n\nexport function setGlobalFederationInstance(\n  FederationInstance: ModuleFederation,\n): void {\n  CurrentGlobal.__FEDERATION__.__INSTANCES__.push(FederationInstance);\n}\n\nexport function getGlobalFederationConstructor():\n  | typeof ModuleFederation\n  | undefined {\n  return CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__;\n}\n\nexport function setGlobalFederationConstructor(\n  FederationConstructor: typeof ModuleFederation | undefined,\n  isDebug = isDebugMode(),\n): void {\n  if (isDebug) {\n    CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;\n    CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = __VERSION__;\n  }\n}\n\nexport function getInfoWithoutType<T extends object>(\n  target: T,\n  key: keyof T,\n): { value: T[keyof T] | undefined; key: string } {\n  if (typeof key === 'string') {\n    const keyRes = target[key];\n    if (keyRes) {\n      return {\n        value: target[key],\n        key: key as string,\n      };\n    } else {\n      const targetKeys = Object.keys(target);\n      for (const targetKey of targetKeys) {\n        const [targetTypeOrName, _] = targetKey.split(':');\n        const nKey = `${targetTypeOrName}:${key}` as unknown as keyof T;\n        const typeWithKeyRes = target[nKey];\n        if (typeWithKeyRes) {\n          return {\n            value: typeWithKeyRes,\n            key: nKey as string,\n          };\n        }\n      }\n      return {\n        value: undefined,\n        key: key as string,\n      };\n    }\n  } else {\n    error(\n      `getInfoWithoutType: \"key\" must be a string, got ${typeof key} (${JSON.stringify(key)}).`,\n    );\n  }\n}\n\nexport const getGlobalSnapshot = (): GlobalModuleInfo =>\n  nativeGlobal.__FEDERATION__.moduleInfo;\n\nexport const getTargetSnapshotInfoByModuleInfo = (\n  moduleInfo: Optional<Remote, 'alias'>,\n  snapshot: GlobalModuleInfo,\n): GlobalModuleInfo[string] | undefined => {\n  // Check if the remote is included in the hostSnapshot\n  const moduleKey = getFMId(moduleInfo);\n  const getModuleInfo = getInfoWithoutType(snapshot, moduleKey).value;\n\n  // The remoteSnapshot might not include a version\n  if (\n    getModuleInfo &&\n    !getModuleInfo.version &&\n    'version' in moduleInfo &&\n    moduleInfo['version']\n  ) {\n    getModuleInfo.version = moduleInfo['version'];\n  }\n\n  if (getModuleInfo) {\n    return getModuleInfo;\n  }\n\n  // If the remote is not included in the hostSnapshot, deploy a micro app snapshot\n  if ('version' in moduleInfo && moduleInfo['version']) {\n    const { version, ...resModuleInfo } = moduleInfo;\n    const moduleKeyWithoutVersion = getFMId(resModuleInfo);\n    const getModuleInfoWithoutVersion = getInfoWithoutType(\n      nativeGlobal.__FEDERATION__.moduleInfo,\n      moduleKeyWithoutVersion,\n    ).value;\n\n    if (getModuleInfoWithoutVersion?.version === version) {\n      return getModuleInfoWithoutVersion;\n    }\n  }\n\n  return;\n};\n\nexport const getGlobalSnapshotInfoByModuleInfo = (\n  moduleInfo: Optional<Remote, 'alias'>,\n): GlobalModuleInfo[string] | undefined =>\n  getTargetSnapshotInfoByModuleInfo(\n    moduleInfo,\n    nativeGlobal.__FEDERATION__.moduleInfo,\n  );\n\nexport const setGlobalSnapshotInfoByModuleInfo = (\n  remoteInfo: Remote,\n  moduleDetailInfo: GlobalModuleInfo[string],\n): GlobalModuleInfo => {\n  const moduleKey = getFMId(remoteInfo);\n  nativeGlobal.__FEDERATION__.moduleInfo[moduleKey] = moduleDetailInfo;\n  return nativeGlobal.__FEDERATION__.moduleInfo;\n};\n\nexport const addGlobalSnapshot = (\n  moduleInfos: GlobalModuleInfo,\n): (() => void) => {\n  nativeGlobal.__FEDERATION__.moduleInfo = {\n    ...nativeGlobal.__FEDERATION__.moduleInfo,\n    ...moduleInfos,\n  };\n  return () => {\n    const keys = Object.keys(moduleInfos);\n    for (const key of keys) {\n      delete nativeGlobal.__FEDERATION__.moduleInfo[key];\n    }\n  };\n};\n\nexport const getRemoteEntryExports = (\n  name: string,\n  globalName: string | undefined,\n): {\n  remoteEntryKey: string;\n  entryExports: RemoteEntryExports | undefined;\n} => {\n  const remoteEntryKey = globalName || `__FEDERATION_${name}:custom__`;\n  const entryExports = (CurrentGlobal as any)[remoteEntryKey];\n  return {\n    remoteEntryKey,\n    entryExports,\n  };\n};\n\n// This function is used to register global plugins.\n// It iterates over the provided plugins and checks if they are already registered.\n// If a plugin is not registered, it is added to the global plugins.\n// If a plugin is already registered, a warning message is logged.\nexport const registerGlobalPlugins = (\n  plugins: Array<ModuleFederationRuntimePlugin>,\n): void => {\n  const { __GLOBAL_PLUGIN__ } = nativeGlobal.__FEDERATION__;\n\n  plugins.forEach((plugin) => {\n    if (__GLOBAL_PLUGIN__.findIndex((p) => p.name === plugin.name) === -1) {\n      __GLOBAL_PLUGIN__.push(plugin);\n    } else {\n      warn(`The plugin ${plugin.name} has been registered.`);\n    }\n  });\n};\n\nexport const getGlobalHostPlugins = (): Array<ModuleFederationRuntimePlugin> =>\n  nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__;\n\nexport const getPreloaded = (id: string) =>\n  CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.get(id);\n\nexport const setPreloaded = (id: string) =>\n  CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.set(id, true);\n"],"mappings":";;;;;AA0BA,MAAa,gBACX,OAAO,eAAe,WAAW,aAAa;AAChD,MAAa,sBAAqC;AAChD,KAAI;AAEF,SAAO,SAAS;SACV;AAEN,SAAO;;IAEP;AAEJ,MAAa,SAAS;AAatB,SAAS,wBACP,QACA,KACA,KACA;AACA,QAAO,eAAe,QAAQ,KAAK;EACjC,OAAO;EACP,cAAc;EACd,UAAU;EACX,CAAC;;AAGJ,SAAS,mBAAmB,QAA8B,KAAa;AACrE,QAAO,OAAO,eAAe,KAAK,QAAQ,IAAI;;AAMhD,IAAI,CAAC,mBAAmB,eAAe,kCAAkC,CACvE,yBAAwB,eAAe,mCAAmC,EAAE,CAAC;AAG/E,MAAa,gBAAgB,cAAc;AAE3C,SAAS,oBAAoB,QAA8B;AACzD,KACE,mBAAmB,QAAQ,WAAW,IACtC,CAAC,mBAAmB,QAAQ,iBAAiB,CAE7C,yBAAwB,QAAQ,kBAAkB,OAAO,SAAS;AAGp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