{"version":3,"file":"index.cjs","names":["getRemoteEntryInfoFromSnapshot","RUNTIME_011","runtimeDescMap","isBrowserEnvValue","isRemoteInfoWithEntry","isPureRemoteEntry","composeRemoteRequestId"],"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  composeRemoteRequestId,\n  isPureRemoteEntry,\n  isRemoteInfoWithEntry,\n  getRemoteEntryInfoFromSnapshot,\n} from '../../utils';\nimport { PreloadOptions, PreloadRemoteArgs, 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: composeRemoteRequestId(remote.name, expose),\n          });\n\n        assignRemoteInfo(remoteInfo, remoteSnapshot);\n        // preloading assets\n        const preloadOps: PreloadRemoteArgs[] = [\n          {\n            nameOrAlias: pkgNameOrAlias,\n            exposes: [expose],\n            resourceCategory: 'sync',\n            share: false,\n            depsRemote: false,\n            recordPreloadedAssets: true,\n          },\n        ];\n        await origin.remoteHandler.hooks.lifecycle.beforePreloadRemote.emit({\n          preloadOps,\n          options: origin.options,\n          origin,\n        });\n\n        const [preloadConfig] = preloadOps;\n        if (preloadConfig) {\n          const preloadOptions: PreloadOptions[0] = {\n            remote,\n            preloadConfig,\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(\n              remoteInfo,\n              origin,\n              assets,\n              false,\n              {\n                initiator: 'loadRemote',\n                id,\n              },\n              preloadConfig.recordPreloadedAssets,\n            ).catch(() => undefined);\n          }\n        }\n\n        return {\n          ...args,\n          remoteSnapshot,\n        };\n      }\n\n      return args;\n    },\n  };\n}\n"],"mappings":";;;;;;;;;AAiBA,SAAgB,iBACd,YACA,gBACM;CACN,MAAM,kBAAkBA,4CAA+B,eAAe;AACtE,KAAI,CAAC,gBAAgB,IACnB,sBAAMC,4CAAaC,+CAAgB,EAAE,YAAY,WAAW,MAAM,CAAC;CAGrE,IAAI,sDAA0B,gBAAgB,gBAAgB,IAAI;AAElE,KAAI,CAACC,4CAAqB,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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