From 694a27de143d3591a2fee82342b732d0f563d840 Mon Sep 17 00:00:00 2001 From: zhn Date: Sun, 4 Aug 2024 03:50:29 +0800 Subject: [PATCH] fix: Ensure @module-federation/runtime singleton (#31) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Ensure @module-federation/runtime singleton * fix: build preview --------- Co-authored-by: 张洪恩 --- README.md | 1 - examples/vite/vite.config.js | 1 + src/index.ts | 3 +- src/utils/normalizeBuild.ts | 12 +++----- src/utils/normalizeModuleFederationOptions.ts | 30 +++++++++---------- 5 files changed, 22 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 815e02b..824959c 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,6 @@ export default defineConfig({ ## roadmap - fix: vitePluginAddEntry.ts This plugin may need to support the case where base in vite.config.js is not configured as "/" -- fix: Ensure @module-federation/runtime is a singleton, split it into a separate chunk - fix: remoteEntry and hostInit file names support hash generation - feat: generate mf-manifest.json - feat: support chrome plugin diff --git a/examples/vite/vite.config.js b/examples/vite/vite.config.js index 8dd468c..fb1e16f 100644 --- a/examples/vite/vite.config.js +++ b/examples/vite/vite.config.js @@ -8,6 +8,7 @@ export default defineConfig({ server: { open: true, }, + base: 'http://localhost:5173', plugins: [ react(), federation({ diff --git a/src/index.ts b/src/index.ts index c83378e..c415d38 100644 --- a/src/index.ts +++ b/src/index.ts @@ -183,7 +183,7 @@ function federation(mfUserOptions: ModuleFederationOptions): Plugin[] { return [ aliasToArrayPlugin, normalizeOptimizeDepsPlugin, - normalizeBuildPlugin(shared), + normalizeBuildPlugin([...Object.keys(shared), "@module-federation/runtime"]), addEntry({ entryName: 'remoteEntry', entryPath: emptyPath + '?__mf__wrapRemoteEntry__', @@ -272,3 +272,4 @@ function federation(mfUserOptions: ModuleFederationOptions): Plugin[] { } export { federation }; +export default federation \ No newline at end of file diff --git a/src/utils/normalizeBuild.ts b/src/utils/normalizeBuild.ts index 1bceb6d..d51bbd8 100644 --- a/src/utils/normalizeBuild.ts +++ b/src/utils/normalizeBuild.ts @@ -1,28 +1,24 @@ import { UserConfig } from 'vite'; -interface Shared { - [key: string]: any; -} - interface Output { manualChunks?: { [key: string]: any; }; } -export default (shared: Shared) => ({ +export default (singleChunkModules: string[]) => ({ name: 'normalizeBuild', config: (config: UserConfig, { command }: { command: string }) => { if (!config.build) config.build = {}; if (!config.build.rollupOptions) config.build.rollupOptions = {}; let { rollupOptions } = config.build; if (!rollupOptions.output) rollupOptions.output = {}; - normalizeManualChunks(rollupOptions.output as any, shared); + normalizeManualChunks(rollupOptions.output as any, singleChunkModules); }, }); -function normalizeManualChunks(output: Output, shared: Shared = {}): void { - const pattern = new RegExp(`node_modules/(${Object.keys(shared).join('|')})/`); +function normalizeManualChunks(output: Output, singleChunkModules: string[]): void { + const pattern = new RegExp(`node_modules/(${singleChunkModules.join('|')})/`); if (!output.manualChunks) output.manualChunks = {}; const wrapManualChunks = (original: any) => diff --git a/src/utils/normalizeModuleFederationOptions.ts b/src/utils/normalizeModuleFederationOptions.ts index 40abffa..5873765 100644 --- a/src/utils/normalizeModuleFederationOptions.ts +++ b/src/utils/normalizeModuleFederationOptions.ts @@ -166,7 +166,7 @@ function normalizeShared( string, | string | { - name: string; + name?: string; version?: string; shareScope?: string; singleton?: boolean; @@ -186,7 +186,7 @@ function normalizeShared( } if (typeof shared === 'object') { Object.keys(shared).forEach((key) => { - result[key] = normalizeShareItem(key, shared[key]); + result[key] = normalizeShareItem(key, shared[key] as any); }); } @@ -199,21 +199,21 @@ function normalizeLibrary(library: any): any { } export type ModuleFederationOptions = { - exposes: Record | undefined; + exposes?: Record | undefined; filename?: string; - library: any; + library?: any; name: string; - remoteType: string; - remotes: + // remoteType?: string; + remotes?: | Record< string, | string | { type: string; name: string; entry: string; entryGlobalName: string; shareScope: string } > | undefined; - runtime: any; + runtime?: any; shareScope?: string; - shared: + shared?: | string[] | Record< string, @@ -229,11 +229,11 @@ export type ModuleFederationOptions = { > | undefined; runtimePlugins?: string[]; - getPublicPath: any; - implementation: any; - manifest: any; - dev: any; - dts: any; + getPublicPath?: any; + implementation?: any; + manifest?: any; + dev?: any; + dts?: any; }; export interface NormalizedModuleFederationOptions { @@ -241,7 +241,7 @@ export interface NormalizedModuleFederationOptions { filename: string; library: any; name: string; - remoteType: string; + // remoteType: string; remotes: Record< string, { type: string; name: string; entry: string; entryGlobalName: string; shareScope: string } @@ -265,7 +265,7 @@ export function normalizeModuleFederationOptions( filename: options.filename || 'remoteEntry.js', library: normalizeLibrary(options.library), name: options.name, - remoteType: options.remoteType, + // remoteType: options.remoteType, remotes: normalizeRemotes(options.remotes), runtime: options.runtime, shareScope: options.shareScope || 'default',