From ebcfbb18e084d9193530f1a44e12240f86fa8d0e Mon Sep 17 00:00:00 2001 From: zhangHongEn Date: Tue, 29 Oct 2024 12:03:42 +0800 Subject: [PATCH] fix: base #159 --- examples/vite-vite/vite-host/vite.config.js | 2 +- examples/vite-vite/vite-remote/vite.config.js | 2 +- src/plugins/pluginAddEntry.ts | 10 +++++++--- src/plugins/pluginMFManifest.ts | 12 ++++++++---- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/examples/vite-vite/vite-host/vite.config.js b/examples/vite-vite/vite-host/vite.config.js index 062e38d..00c4372 100644 --- a/examples/vite-vite/vite-host/vite.config.js +++ b/examples/vite-vite/vite-host/vite.config.js @@ -22,7 +22,7 @@ export default defineConfig({ remote2: 'mfapp02@https://unpkg.com/mf-app-02/dist/remoteEntry.js', remote3: 'remote1@https://unpkg.com/react-manifest-example_remote1@1.0.6/dist/mf-manifest.json', - '@namespace/viteViteRemote': 'http://localhost:5176/mf-manifest.json', + '@namespace/viteViteRemote': 'http://localhost:5176/testbase/mf-manifest.json', }, filename: 'remoteEntry-[hash].js', manifest: true, diff --git a/examples/vite-vite/vite-remote/vite.config.js b/examples/vite-vite/vite-remote/vite.config.js index fb5e6b7..92b0aaf 100644 --- a/examples/vite-vite/vite-remote/vite.config.js +++ b/examples/vite-vite/vite-remote/vite.config.js @@ -13,7 +13,7 @@ export default defineConfig({ preview: { port: 5176, }, - base: 'http://localhost:5176', + base: 'http://localhost:5176/testbase', plugins: [ react({ jsxImportSource: '@emotion/react' }), federation({ diff --git a/src/plugins/pluginAddEntry.ts b/src/plugins/pluginAddEntry.ts index 93e3f07..fa71d18 100644 --- a/src/plugins/pluginAddEntry.ts +++ b/src/plugins/pluginAddEntry.ts @@ -15,7 +15,7 @@ const addEntry = ({ fileName, inject = 'entry', }: AddEntryOptions): Plugin[] => { - const devEntryPath = entryPath.startsWith('virtual:mf') ? '/@id/' + entryPath : entryPath; + let devEntryPath = entryPath.startsWith('virtual:mf') ? '@id/' + entryPath : entryPath; let entryFiles: string[] = []; let htmlFilePath: string; let _command: string; @@ -29,17 +29,21 @@ const addEntry = ({ config(config, { command }) { _command = command; }, + configResolved(config) { + viteConfig = config; + devEntryPath = config.base + devEntryPath.replace(/^\//, ''); + }, configureServer(server) { server.httpServer?.once?.('listening', () => { const { port } = server.config.server; - fetch(path.join(`http://localhost:${port}`, `${devEntryPath}`)).catch((e) => {}); + fetch(`http://localhost:${port}${devEntryPath}`).catch((e) => { }); }); server.middlewares.use((req, res, next) => { if (!fileName) { next(); return; } - if (req.url && req.url.startsWith(fileName.replace(/^\/?/, '/'))) { + if (req.url && req.url.startsWith((viteConfig.base + fileName).replace(/^\/?/, '/'))) { req.url = devEntryPath; } next(); diff --git a/src/plugins/pluginMFManifest.ts b/src/plugins/pluginMFManifest.ts index 61029e6..2c1f5a2 100644 --- a/src/plugins/pluginMFManifest.ts +++ b/src/plugins/pluginMFManifest.ts @@ -28,17 +28,21 @@ const Manifest = (): Plugin[] => { let remoteEntryFile: string; let publicPath: string; let _command: string; + let viteConfig: any; return [ { name: 'module-federation-manifest', apply: 'serve', + configResolved(config) { + viteConfig = config; + }, configureServer(server) { server.middlewares.use((req, res, next) => { if (!mfManifestName) { next(); return; } - if (req.url === mfManifestName.replace(/^\/?/, '/')) { + if (req.url === (viteConfig.base + mfManifestName).replace(/^\/?/, '/')) { res.setHeader('Content-Type', 'application/json'); res.setHeader('Access-Control-Allow-Origin', '*'); res.end( @@ -93,11 +97,11 @@ const Manifest = (): Plugin[] => { '.tsx', '.json', ]; - publicPath = config.base ? config.base.replace(/\/?$/, '/') : 'auto'; + let base = config.base; if (_command === 'serve') { - const origin = config.server.origin; - publicPath = origin ? origin.replace(/\/?$/, '/') : 'auto'; + base = (config.server.origin || '') + config.base; } + publicPath = base ? base.replace(/\/?$/, '/') : 'auto'; }, async generateBundle(options, bundle) { if (!mfManifestName) return;