{"version":3,"file":"angular-architects-module-federation-runtime.mjs","sources":["../../../../libs/mf-runtime/src/lib/loader/dynamic-federation.ts","../../../../libs/mf-runtime/src/angular-architects-module-federation-runtime.ts"],"sourcesContent":["type Scope = unknown;\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Factory = () => any;\n\ntype Container = {\n  init(shareScope: Scope): void;\n  get(module: string): Factory;\n};\n\nlet config: Manifest = {};\n\nexport type ManifestFile<T extends RemoteConfig = RemoteConfig> = {\n  [key: string]: string | T;\n};\n\nexport type Manifest<T extends RemoteConfig = RemoteConfig> = {\n  [key: string]: T;\n};\n\nexport type RemoteConfig = {\n  type: 'module' | 'script';\n  remoteEntry: string;\n  [key: string]: unknown;\n};\n\ndeclare const __webpack_init_sharing__: (shareScope: string) => Promise<void>;\ndeclare const __webpack_share_scopes__: { default: Scope };\n\ntype ContainerMap = { [key: string]: Container };\n\nconst containerMap: ContainerMap = {};\nconst remoteMap = {};\n\nlet isDefaultScopeInitialized = false;\n\nasync function lookupExposedModule<T>(\n  key: string,\n  exposedModule: string\n): Promise<T> {\n  const container = containerMap[key];\n  const factory = await container.get(exposedModule);\n  const Module = factory();\n  return Module as T;\n}\n\nasync function initRemote(container: Container, key: string) {\n  // const container = window[key] as Container;\n\n  // Do we still need to initialize the remote?\n  if (remoteMap[key]) {\n    return container;\n  }\n\n  // Do we still need to initialize the share scope?\n  if (!isDefaultScopeInitialized) {\n    await __webpack_init_sharing__('default');\n    isDefaultScopeInitialized = true;\n  }\n\n  await container.init(__webpack_share_scopes__.default);\n  remoteMap[key] = true;\n  return container;\n}\n\nexport type LoadRemoteEntryOptions =\n  | LoadRemoteEntryScriptOptions\n  | LoadRemoteEntryEsmOptions;\n\nexport type LoadRemoteEntryScriptOptions = {\n  type?: 'script';\n  remoteEntry: string;\n  remoteName: string;\n};\n\nexport type LoadRemoteEntryEsmOptions = {\n  type: 'module';\n  remoteEntry: string;\n};\n\nexport async function loadRemoteEntry(\n  remoteEntry: string,\n  remoteName: string\n): Promise<void>;\nexport async function loadRemoteEntry(\n  options: LoadRemoteEntryOptions\n): Promise<void>;\nexport async function loadRemoteEntry(\n  remoteEntryOrOptions: string | LoadRemoteEntryOptions,\n  remoteName?: string\n): Promise<void> {\n  if (typeof remoteEntryOrOptions === 'string') {\n    const remoteEntry = remoteEntryOrOptions;\n    return await loadRemoteScriptEntry(remoteEntry, remoteName);\n  } else if (remoteEntryOrOptions.type === 'script') {\n    const options = remoteEntryOrOptions;\n    return await loadRemoteScriptEntry(options.remoteEntry, options.remoteName);\n  } else if (remoteEntryOrOptions.type === 'module') {\n    const options = remoteEntryOrOptions;\n    await loadRemoteModuleEntry(options.remoteEntry);\n  }\n}\n\nasync function loadRemoteModuleEntry(remoteEntry: string): Promise<void> {\n  if (containerMap[remoteEntry]) {\n    return Promise.resolve();\n  }\n  return await import(/* webpackIgnore:true */ remoteEntry).then(\n    (container) => {\n      initRemote(container, remoteEntry);\n      containerMap[remoteEntry] = container;\n    }\n  );\n}\n\nasync function loadRemoteScriptEntry(\n  remoteEntry: string,\n  remoteName: string\n): Promise<void> {\n  return new Promise<void>((resolve, reject) => {\n    // Is remoteEntry already loaded?\n    if (containerMap[remoteName]) {\n      resolve();\n      return;\n    }\n\n    const script = document.createElement('script');\n    script.src = remoteEntry;\n\n    script.onerror = reject;\n\n    script.onload = () => {\n      const container = window[remoteName] as Container;\n      initRemote(container, remoteName);\n      containerMap[remoteName] = container;\n      resolve();\n    };\n\n    document.body.appendChild(script);\n  });\n}\n\nexport type LoadRemoteModuleOptions =\n  | LoadRemoteModuleScriptOptions\n  | LoadRemoteModuleEsmOptions\n  | LoadRemoteModuleManifestOptions;\n\nexport type LoadRemoteModuleScriptOptions = {\n  type?: 'script';\n  remoteEntry?: string;\n  remoteName: string;\n  exposedModule: string;\n};\n\nexport type LoadRemoteModuleEsmOptions = {\n  type: 'module';\n  remoteEntry: string;\n  exposedModule: string;\n};\n\nexport type LoadRemoteModuleManifestOptions = {\n  type: 'manifest';\n  remoteName: string;\n  exposedModule: string;\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport async function loadRemoteModule<T = any>(\n  remoteName: string,\n  exposedModule: string\n): Promise<T>;\nexport async function loadRemoteModule<T = any>(\n  options: LoadRemoteModuleOptions\n): Promise<T>;\nexport async function loadRemoteModule<T = any>(\n  optionsOrRemoteName: LoadRemoteModuleOptions | string,\n  exposedModule?: string\n): Promise<T> {\n  let loadRemoteEntryOptions: LoadRemoteEntryOptions;\n  let key: string;\n  let remoteEntry: string;\n  let options: LoadRemoteModuleOptions;\n\n  if (typeof optionsOrRemoteName === 'string') {\n    options = {\n      type: 'manifest',\n      remoteName: optionsOrRemoteName,\n      exposedModule: exposedModule,\n    };\n  } else {\n    options = optionsOrRemoteName;\n  }\n\n  // To support legacy API (< ng 13)\n  if (!options.type) {\n    const hasManifest = Object.keys(config).length > 0;\n    options.type = hasManifest ? 'manifest' : 'script';\n  }\n\n  if (options.type === 'manifest') {\n    const manifestEntry = config[options.remoteName];\n    if (!manifestEntry) {\n      throw new Error('Manifest does not contain ' + options.remoteName);\n    }\n    options = {\n      type: manifestEntry.type,\n      exposedModule: options.exposedModule,\n      remoteEntry: manifestEntry.remoteEntry,\n      remoteName:\n        manifestEntry.type === 'script' ? options.remoteName : undefined,\n    };\n    remoteEntry = manifestEntry.remoteEntry;\n  } else {\n    remoteEntry = options.remoteEntry;\n  }\n\n  if (options.type === 'script') {\n    loadRemoteEntryOptions = {\n      type: 'script',\n      remoteEntry: options.remoteEntry,\n      remoteName: options.remoteName,\n    };\n    key = options.remoteName;\n  } else if (options.type === 'module') {\n    loadRemoteEntryOptions = {\n      type: 'module',\n      remoteEntry: options.remoteEntry,\n    };\n    key = options.remoteEntry;\n  }\n\n  if (remoteEntry) {\n    await loadRemoteEntry(loadRemoteEntryOptions);\n  }\n\n  return await lookupExposedModule<T>(key, options.exposedModule);\n}\n\nexport async function setManifest(\n  manifest: ManifestFile,\n  skipRemoteEntries = false\n) {\n  config = parseConfig(manifest);\n\n  if (!skipRemoteEntries) {\n    await loadRemoteEntries();\n  }\n}\n\nexport function getManifest<T extends Manifest>(): T {\n  return config as T;\n}\n\nexport async function initFederation(\n  manifest: string | ManifestFile,\n  skipRemoteEntries = false\n): Promise<void> {\n  if (typeof manifest === 'string') {\n    return loadManifest(manifest, skipRemoteEntries);\n  } else {\n    return setManifest(manifest, skipRemoteEntries);\n  }\n}\n\nexport async function loadManifest(\n  configFile: string,\n  skipRemoteEntries = false\n): Promise<void> {\n  const result = await fetch(configFile);\n\n  if (!result.ok) {\n    throw Error('could not load configFile: ' + configFile);\n  }\n\n  config = parseConfig(await result.json());\n\n  if (!skipRemoteEntries) {\n    await loadRemoteEntries();\n  }\n}\n\nfunction parseConfig(config: ManifestFile): Manifest {\n  const result: Manifest = {};\n  for (const key in config) {\n    const value = config[key];\n\n    let entry: RemoteConfig;\n    if (typeof value === 'string') {\n      entry = {\n        remoteEntry: value,\n        type: 'module',\n      };\n    } else {\n      entry = {\n        ...value,\n        type: value.type || 'module',\n      };\n    }\n\n    result[key] = entry;\n  }\n  return result;\n}\n\nasync function loadRemoteEntries() {\n  const promises: Promise<void>[] = [];\n\n  for (const key in config) {\n    const entry = config[key];\n\n    if (entry.type === 'module') {\n      promises.push(\n        loadRemoteEntry({ type: 'module', remoteEntry: entry.remoteEntry })\n      );\n    } else {\n      promises.push(\n        loadRemoteEntry({\n          type: 'script',\n          remoteEntry: entry.remoteEntry,\n          remoteName: key,\n        })\n      );\n    }\n  }\n\n  await Promise.all(promises);\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":"AASA,IAAI,MAAM,GAAa,EAAE;AAqBzB,MAAM,YAAY,GAAiB,EAAE;AACrC,MAAM,SAAS,GAAG,EAAE;AAEpB,IAAI,yBAAyB,GAAG,KAAK;AAErC,eAAe,mBAAmB,CAChC,GAAW,EACX,aAAqB,EAAA;AAErB,IAAA,MAAM,SAAS,GAAG,YAAY,C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