From 31993926372ea8d5d6c43c0793c93d898605062d Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 8 May 2025 23:32:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20Vite=20=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=EF=BC=8C=E6=9B=B4=E6=96=B0=E5=85=B1=E4=BA=AB=E5=BA=93=E5=88=97?= =?UTF-8?q?=E8=A1=A8=EF=BC=8C=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E7=9A=84=20Rollup=20=E9=80=89=E9=A1=B9=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E7=AE=80=E5=8C=96=E8=BF=9C=E7=A8=8B=E6=A8=A1=E5=9D=97=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E9=80=BB=E8=BE=91=EF=BC=8C=E6=8F=90=E5=8D=87=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=8F=AF=E8=AF=BB=E6=80=A7=E5=92=8C=E7=BB=B4=E6=8A=A4?= =?UTF-8?q?=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/plugin-component/vite.config.js | 7 --- src/utils/federationLoader.ts | 65 +----------------------- vite.config.ts | 2 +- 3 files changed, 2 insertions(+), 72 deletions(-) diff --git a/examples/plugin-component/vite.config.js b/examples/plugin-component/vite.config.js index e3a20781..6d3fb025 100644 --- a/examples/plugin-component/vite.config.js +++ b/examples/plugin-component/vite.config.js @@ -36,13 +36,6 @@ export default defineConfig({ target: 'esnext', // 必须设置为esnext以支持顶层await minify: false, // 开发阶段建议关闭混淆 cssCodeSplit: true, // 改为true以便能分离样式文件 - rollupOptions: { - output: { - manualChunks: { - 'vuetify-lib': ['vuetify'] // 将vuetify单独分离出来 - } - } - } }, css: { preprocessorOptions: { diff --git a/src/utils/federationLoader.ts b/src/utils/federationLoader.ts index 52febc3b..1b368373 100644 --- a/src/utils/federationLoader.ts +++ b/src/utils/federationLoader.ts @@ -6,55 +6,12 @@ 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 @@ -83,13 +40,6 @@ async function fetchRemoteModules(): Promise { } } -/** - * 生成唯一的版本标记用于防止缓存 - */ -function generateVersionTag(): string { - return `v=${Date.now()}` -} - /** * 动态注入Federation Remote模块 * @param modules 远程模块列表 @@ -105,17 +55,10 @@ 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(urlWithVersion), + url: () => Promise.resolve(`${baseUrl.origin}/${apiBase}${module.url}`), format: 'esm', from: 'vite', - shareScope: 'default', }) console.log('已注入远程模块:', module) } @@ -125,12 +68,6 @@ function injectRemoteModule(module: RemoteModule): void { */ export async function loadRemoteComponents(): Promise { try { - // 初始化共享作用域 - initShareScope() - - // 添加dummy远程模块解决生产环境问题 - addDummyRemote() - // 获取远程模块列表 const modules = await fetchRemoteModules() diff --git a/vite.config.ts b/vite.config.ts index 535d2304..a4085876 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -38,7 +38,7 @@ export default defineConfig({ remotes: { // 动态remotes将在运行时注入 }, - shared: ['vue', 'vuetify', 'pinia', 'vue-i18n', 'vue-router', 'axios'], + shared: ['vue', 'vuetify', 'vuetify/styles'], }), VitePWA({ injectRegister: 'script',