{"version":3,"sources":["../src/plugins/rspack/runtimePlugin/forceRemote.ts","../src/helper/config.ts"],"sourcesContent":["/**\n * 运行时插件：根据 window[EMP_FORCE_REMOTES] 配置，在注册 remote 前拦截并改写其 entry/url。\n * 支持两种方式二选一：指定完整 entry 地址，或仅指定 version 做 URL 内版本号替换。\n */\nimport {type ModuleFederationRuntimePlugin} from '@module-federation/runtime'\nimport {shareForceRemote} from 'src/helper/config'\nimport {type ForceRemoteItem, type ForceRemoteOptions, type RemoteInfoForForce} from '../types'\n\ndeclare global {\n  interface Window {\n    [shareForceRemote]?: ForceRemoteOptions\n  }\n}\n\n/** remote 上可能表示入口地址的字段，按优先级 */\nconst REMOTE_ENTRY_FIELD_KEYS = ['entry', 'url', 'manifest'] as const\n\n/**\n * 取当前 remote 用于表示入口的字段名（entry / url / manifest 中第一个为字符串的）\n */\nfunction getRemoteEntryFieldKey(remote: RemoteInfoForForce): (typeof REMOTE_ENTRY_FIELD_KEYS)[number] | undefined {\n  return REMOTE_ENTRY_FIELD_KEYS.find((k) => typeof remote[k] === 'string')\n}\n\nfunction hasEntryConfig(config: ForceRemoteItem): boolean {\n  return typeof config === 'object' && config !== null && 'entry' in config && typeof (config as {entry?: string}).entry === 'string'\n}\n\nfunction getVersionFromConfig(config: ForceRemoteItem): string | undefined {\n  if (typeof config === 'string') return config\n  const v = (config as {version?: string})?.version\n  return typeof v === 'string' ? v : undefined\n}\n\nfunction getEntryFromConfig(config: ForceRemoteItem | undefined): string | undefined {\n  return config && hasEntryConfig(config) ? (config as {entry: string}).entry : undefined\n}\n\n/**\n * 从 forceRemotes 提取「仅 version」项的映射（有 entry 的项不参与，二选一）。\n */\nfunction buildVersionReplaceMap(forceRemotes: ForceRemoteOptions): Record<string, string> {\n  return Object.fromEntries(\n    Object.entries(forceRemotes).flatMap(([remoteKey, config]) => {\n      if (hasEntryConfig(config)) return []\n      const version = getVersionFromConfig(config)\n      return version ? [[remoteKey, version]] : []\n    }),\n  )\n}\n\n/**\n * 在 URL 中按 versionMap 替换 common 版本号（匹配 `remoteKey@原版本` -> `remoteKey@新版本`）\n */\nfunction replaceUrlVersions(url: string, versionMap: Record<string, string>): string {\n  if (!Object.keys(versionMap).length) return url\n  let result = url\n  for (const [remoteKey, version] of Object.entries(versionMap)) {\n    if (!version) continue\n    const regex = new RegExp(`(${remoteKey.replace(/[-/\\\\^$*+?.()|[\\]{}]/g, '\\\\$&')}@)([^/]+)`)\n    result = result.replace(regex, `$1${version}`)\n  }\n  return result\n}\n\nexport default function (): ModuleFederationRuntimePlugin {\n  return {\n    name: 'emp-remotes-replacer',\n    beforeRegisterRemote(args) {\n      const forceRemotes = typeof window !== 'undefined' ? window[shareForceRemote] : undefined\n      if (!forceRemotes || typeof forceRemotes !== 'object') return args\n\n      const remote = args.remote as unknown as RemoteInfoForForce\n      if (!remote) return args\n\n      const entryFieldKey = getRemoteEntryFieldKey(remote)\n      if (!entryFieldKey) return args\n\n      const currentEntryUrl = remote[entryFieldKey] as string | undefined\n      const forceConfig = remote.alias ? forceRemotes[remote.alias] : undefined\n\n      // 优先 version：有则替换并直接 return，不再走 entry\n      const versionMap = buildVersionReplaceMap(forceRemotes)\n      const versionedEntryUrl = currentEntryUrl ? replaceUrlVersions(currentEntryUrl, versionMap) : undefined\n      if (versionedEntryUrl && versionedEntryUrl !== currentEntryUrl) {\n        remote[entryFieldKey] = versionedEntryUrl\n        return args\n      }\n\n      // 否则判断 entry：有则改写并 return\n      const forcedEntryUrl = getEntryFromConfig(forceConfig)\n      if (forcedEntryUrl) {\n        remote[entryFieldKey] = forcedEntryUrl\n        return args\n      }\n\n      return args\n    },\n  }\n}\n","export const shareGlobalName = 'EMP_SHARE_RUNTIME'\nexport const shareGlobalVal = 'EMPShareGlobalVal'\nexport const shareForceRemote = 'EMP_FORCE_REMOTES'\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GCEO,IAAMI,EAAmB,oBDahC,IAAMC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U,CAAgB,EAAI,OAChF,GAAI,CAACV,GAAgB,OAAOA,GAAiB,SAAU,OAAOS,EAE9D,IAAMjB,EAASiB,EAAK,OACpB,GAAI,CAACjB,EAAQ,OAAOiB,EAEpB,IAAME,EAAgBpB,EAAuBC,CAAM,EACnD,GAAI,CAACmB,EAAe,OAAOF,EAE3B,IAAMG,EAAkBpB,EAAOmB,CAAa,EACtCE,EAAcrB,EAAO,MAAQQ,EAAaR,EAAO,KAAK,EAAI,OAG1Da,EAAaN,EAAuBC,CAAY,EAChDc,EAAoBF,EAAkBT,EAAmBS,EAAiBP,CAAU,EAAI,OAC9F,GAAIS,GAAqBA,IAAsBF,EAC7C,OAAApB,EAAOmB,CAAa,EAAIG,EACjBL,EAIT,IAAMM,EAAiBjB,EAAmBe,CAAW,EACrD,OAAIE,IACFvB,EAAOmB,CAAa,EAAII,GACjBN,CAIX,CACF,CACF","names":["forceRemote_exports","__export","forceRemote_default","__toCommonJS","shareForceRemote","REMOTE_ENTRY_FIELD_KEYS","getRemoteEntryFieldKey","remote","k","hasEntryConfig","config","getVersionFromConfig","v","getEntryFromConfig","buildVersionReplaceMap","forceRemotes","remoteKey","version","replaceUrlVersions","url","versionMap","result","regex","forceRemote_default","args","shareForceRemote","entryFieldKey","currentEntryUrl","forceConfig","versionedEntryUrl","forcedEntryUrl"]}