From 4e3a61b8a8e124fd8f942f9a5389c71331ae796e Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 8 May 2025 23:08:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E5=85=B1=E4=BA=AB=E4=BD=9C?= =?UTF-8?q?=E7=94=A8=E5=9F=9F=E7=AE=A1=E7=90=86=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E5=92=8Cdummy=E8=BF=9C=E7=A8=8B?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E5=8A=9F=E8=83=BD=EF=BC=8C=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E7=94=9F=E4=BA=A7=E7=8E=AF=E5=A2=83=E4=B8=AD=E7=9A=84=E5=85=B1?= =?UTF-8?q?=E4=BA=AB=E4=BD=9C=E7=94=A8=E5=9F=9F=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E4=B8=BA=E8=BF=9C=E7=A8=8B=E6=A8=A1=E5=9D=97?= =?UTF-8?q?URL=E6=B7=BB=E5=8A=A0=E7=89=88=E6=9C=AC=E6=A0=87=E8=AE=B0?= =?UTF-8?q?=E4=BB=A5=E9=98=B2=E6=AD=A2=E7=BC=93=E5=AD=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/federationLoader.ts | 65 ++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/src/utils/federationLoader.ts b/src/utils/federationLoader.ts index 1b368373..52febc3b 100644 --- a/src/utils/federationLoader.ts +++ b/src/utils/federationLoader.ts @@ -6,12 +6,55 @@ import { // @ts-ignore } from 'virtual:__federation__' +// 扩展全局接口,添加federation所需的共享作用域 +declare global { + interface Window { + __rf_placeholder__shareScope?: Record + vue?: any + vuetify?: any + pinia?: any + 'vue-i18n'?: any + 'vue-router'?: any + axios?: any + } +} + // 定义远程模块接口 interface RemoteModule { id: string url: string } +/** + * 初始化共享作用域 + */ +function initShareScope() { + // 确保全局共享作用域存在 + if (!window.__rf_placeholder__shareScope) { + window.__rf_placeholder__shareScope = {} + } + // 为共享模块设置默认作用域 + const shared = ['vue', 'vuetify', 'pinia', 'vue-i18n', 'vue-router', 'axios'] + shared.forEach(lib => { + if (window.__rf_placeholder__shareScope) { + window.__rf_placeholder__shareScope[lib] = { default: { get: () => (window as any)[lib] } } + } + }) + console.log('已初始化共享作用域:', window.__rf_placeholder__shareScope) +} + +/** + * 添加一个dummy远程模块以解决生产环境中的共享作用域问题 + */ +function addDummyRemote() { + __federation_method_setRemote('dummy', { + url: () => Promise.resolve(''), + format: 'esm', + from: 'vite', + shareScope: 'default', + }) +} + /** * 加载远程组件 * @param id 远程模块ID @@ -40,6 +83,13 @@ async function fetchRemoteModules(): Promise { } } +/** + * 生成唯一的版本标记用于防止缓存 + */ +function generateVersionTag(): string { + return `v=${Date.now()}` +} + /** * 动态注入Federation Remote模块 * @param modules 远程模块列表 @@ -55,10 +105,17 @@ function injectRemoteModule(module: RemoteModule): void { if (apiBase.endsWith('/')) { apiBase = apiBase.slice(0, -1) } + + // 添加版本标记防止缓存 + const versionTag = generateVersionTag() + const remoteUrl = `${baseUrl.origin}/${apiBase}${module.url}` + const urlWithVersion = remoteUrl.includes('?') ? `${remoteUrl}&${versionTag}` : `${remoteUrl}?${versionTag}` + __federation_method_setRemote(module.id, { - url: () => Promise.resolve(`${baseUrl.origin}/${apiBase}${module.url}`), + url: () => Promise.resolve(urlWithVersion), format: 'esm', from: 'vite', + shareScope: 'default', }) console.log('已注入远程模块:', module) } @@ -68,6 +125,12 @@ function injectRemoteModule(module: RemoteModule): void { */ export async function loadRemoteComponents(): Promise { try { + // 初始化共享作用域 + initShareScope() + + // 添加dummy远程模块解决生产环境问题 + addDummyRemote() + // 获取远程模块列表 const modules = await fetchRemoteModules()