From c624f2d968e12528dd5fb48ff85a072674df8663 Mon Sep 17 00:00:00 2001 From: zhangHongEn Date: Sat, 12 Oct 2024 11:44:13 +0800 Subject: [PATCH] fix: hotfix important bugs --- src/utils/normalizeModuleFederationOptions.ts | 68 +++++++++---------- src/virtualModules/virtualRemoteEntry.ts | 35 ++++++---- 2 files changed, 54 insertions(+), 49 deletions(-) diff --git a/src/utils/normalizeModuleFederationOptions.ts b/src/utils/normalizeModuleFederationOptions.ts index 6fc071a..2bed5f8 100644 --- a/src/utils/normalizeModuleFederationOptions.ts +++ b/src/utils/normalizeModuleFederationOptions.ts @@ -122,13 +122,13 @@ function normalizeShareItem( shareItem: | string | { - name: string; - version?: string; - shareScope?: string; - singleton?: boolean; - requiredVersion?: string; - strictVersion?: boolean; - } + name: string; + version?: string; + shareScope?: string; + singleton?: boolean; + requiredVersion?: string; + strictVersion?: boolean; + } ): ShareItem { let version: string | undefined; try { @@ -165,17 +165,17 @@ function normalizeShared( shared: | string[] | Record< - string, - | string - | { - name?: string; - version?: string; - shareScope?: string; - singleton?: boolean; - requiredVersion?: string; - strictVersion?: boolean; - } - > + string, + | string + | { + name?: string; + version?: string; + shareScope?: string; + singleton?: boolean; + requiredVersion?: string; + strictVersion?: boolean; + } + > | undefined ): NormalizedShared { if (!shared) return {}; @@ -229,20 +229,20 @@ export type ModuleFederationOptions = { runtime?: any; shareScope?: string; shared?: - | string[] - | Record< - string, - | string - | { - name?: string; - version?: string; - shareScope?: string; - singleton?: boolean; - requiredVersion?: string; - strictVersion?: boolean; - } - > - | undefined; + | string[] + | Record< + string, + | string + | { + name?: string; + version?: string; + shareScope?: string; + singleton?: boolean; + requiredVersion?: string; + strictVersion?: boolean; + } + > + | undefined; runtimePlugins?: string[]; getPublicPath?: string; implementation?: any; @@ -267,7 +267,7 @@ export interface NormalizedModuleFederationOptions { manifest: ManifestOptions | boolean; dev?: boolean | PluginDevOptions; dts?: boolean | PluginDtsOptions; - shareStrategy?: ShareStrategy; + shareStrategy: ShareStrategy; getPublicPath?: string; } @@ -343,6 +343,6 @@ export function normalizeModuleFederationOptions( dev: options.dev, dts: options.dts, getPublicPath: options.getPublicPath, - shareStrategy: options.shareStrategy, + shareStrategy: options.shareStrategy || "version-first", }); } diff --git a/src/virtualModules/virtualRemoteEntry.ts b/src/virtualModules/virtualRemoteEntry.ts index a96f33f..bfcee9e 100644 --- a/src/virtualModules/virtualRemoteEntry.ts +++ b/src/virtualModules/virtualRemoteEntry.ts @@ -40,21 +40,21 @@ export function generateLocalSharedImportMap() { return ` const importMap = { ${Array.from(getUsedShares()) - .map( - (pkg) => ` + .map( + (pkg) => ` ${JSON.stringify(pkg)}: async () => { let pkg = await import("${getPreBuildLibImportId(pkg)}") return pkg } ` - ) - .join(',')} + ) + .join(',')} } const usedShared = { ${Array.from(getUsedShares()) - .map((key) => { - const shareItem = getNormalizeShareItem(key); - return ` + .map((key) => { + const shareItem = getNormalizeShareItem(key); + return ` ${JSON.stringify(key)}: { name: ${JSON.stringify(key)}, version: ${JSON.stringify(shareItem.version)}, @@ -81,13 +81,13 @@ export function generateLocalSharedImportMap() { } } `; - }) - .join(',')} + }) + .join(',')} } const usedRemotes = [${Object.keys(getUsedRemotesMap()) - .map((key) => { - const remote = options.remotes[key]; - return ` + .map((key) => { + const remote = options.remotes[key]; + return ` { entryGlobalName: ${JSON.stringify(remote.entryGlobalName)}, name: ${JSON.stringify(remote.name)}, @@ -95,8 +95,8 @@ export function generateLocalSharedImportMap() { entry: ${JSON.stringify(remote.entry)}, } `; - }) - .join(',')} + }) + .join(',')} ] export { usedShared, @@ -126,9 +126,14 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions): remotes: usedRemotes, shared: usedShared, plugins: [${pluginImportNames.map((item) => `${item[0]}()`).join(', ')}], - ${options.shareStrategy ? `shareStrategy: ${options.shareStrategy}` : ''} + ${options.shareStrategy ? `shareStrategy: '${options.shareStrategy}'` : ''} }); initRes.initShareScopeMap('${options.shareScope}', shared); + try { + await Promise.all(await initRes.initializeSharing('${options.shareScope}', {strategy: '${options.shareStrategy}'})); + } catch (e) { + console.error(e) + } initResolve(initRes) return initRes }