From 2ad29a6189b355e7088fdd3cc5204bc7d7227aad Mon Sep 17 00:00:00 2001 From: Zhang Hang Date: Mon, 15 Jan 2024 15:01:33 +0800 Subject: [PATCH] fix: webpackRequire.S has been attached with instance.shareScopeMap (#1962) --- .changeset/shy-spoons-cheer.md | 9 +++++++++ .../src/lib/container/ModuleFederationPlugin.ts | 7 ++++++- .../enhanced/src/lib/sharing/ConsumeSharedPlugin.ts | 13 +++++++++++-- packages/runtime/src/utils/share.ts | 13 +++++++++---- .../src/initContainerEntry.ts | 1 - 5 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 .changeset/shy-spoons-cheer.md diff --git a/.changeset/shy-spoons-cheer.md b/.changeset/shy-spoons-cheer.md new file mode 100644 index 00000000000..369f209a955 --- /dev/null +++ b/.changeset/shy-spoons-cheer.md @@ -0,0 +1,9 @@ +--- +'@module-federation/webpack-bundler-runtime': patch +'@module-federation/enhanced': patch +'@module-federation/runtime': patch +--- + +fix: remove duplicate init shareScopeMap +fix: normalize schemas path +fix: shared is loaded if it has lib attr diff --git a/packages/enhanced/src/lib/container/ModuleFederationPlugin.ts b/packages/enhanced/src/lib/container/ModuleFederationPlugin.ts index 5a64634f975..ba86967d09e 100644 --- a/packages/enhanced/src/lib/container/ModuleFederationPlugin.ts +++ b/packages/enhanced/src/lib/container/ModuleFederationPlugin.ts @@ -7,7 +7,6 @@ import type { Compiler, WebpackPluginInstance } from 'webpack'; import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; -import isValidExternalsType from 'webpack/schemas/plugins/container/ExternalsType.check.js'; import type { ModuleFederationPluginOptions } from './ModuleFederationPluginTypes'; import SharePlugin from '../sharing/SharePlugin'; import ContainerPlugin from './ContainerPlugin'; @@ -16,6 +15,12 @@ import checkOptions from '../../schemas/container/ModuleFederationPlugin.check'; import schema from '../../schemas/container/ModuleFederationPlugin'; import FederationRuntimePlugin from './runtime/FederationRuntimePlugin'; +const isValidExternalsType = require( + normalizeWebpackPath( + 'webpack/schemas/plugins/container/ExternalsType.check.js', + ), +) as typeof import('webpack/schemas/plugins/container/ExternalsType.check.js'); + const createSchemaValidation = require( normalizeWebpackPath('webpack/lib/util/create-schema-validation'), ) as typeof import('webpack/lib/util/create-schema-validation'); diff --git a/packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts b/packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts index 4b1b4f9e9db..89fbfa46749 100644 --- a/packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts +++ b/packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts @@ -48,8 +48,17 @@ const createSchemaValidation = require( const validate = createSchemaValidation( //eslint-disable-next-line - require('webpack/schemas/plugins/sharing/ConsumeSharedPlugin.check.js'), - () => require('webpack/schemas/plugins/sharing/ConsumeSharedPlugin.json'), + require( + normalizeWebpackPath( + 'webpack/schemas/plugins/sharing/ConsumeSharedPlugin.check.js', + ), + ), + () => + require( + normalizeWebpackPath( + 'webpack/schemas/plugins/sharing/ConsumeSharedPlugin.json', + ), + ), { name: 'Consume Shared Plugin', baseDataPath: 'options', diff --git a/packages/runtime/src/utils/share.ts b/packages/runtime/src/utils/share.ts index c9bc8fdf082..a3ea0cedc97 100644 --- a/packages/runtime/src/utils/share.ts +++ b/packages/runtime/src/utils/share.ts @@ -105,6 +105,10 @@ const findVersion = ( }, 0) as string; }; +const isLoaded = (shared: Shared) => { + return Boolean(shared.loaded) || typeof shared.lib === 'function'; +}; + function findSingletonVersionOrderByVersion( shareScopeMap: ShareScopeMap, scope: string, @@ -112,7 +116,7 @@ function findSingletonVersionOrderByVersion( ): string { const versions = shareScopeMap[scope][pkgName]; const callback = function (prev: string, cur: string): boolean { - return !versions[prev].loaded && versionLt(prev, cur); + return !isLoaded(versions[prev]) && versionLt(prev, cur); }; return findVersion(shareScopeMap, scope, pkgName, callback); @@ -124,15 +128,16 @@ function findSingletonVersionOrderByLoaded( pkgName: string, ): string { const versions = shareScopeMap[scope][pkgName]; + const callback = function (prev: string, cur: string): boolean { - if (versions[cur].loaded) { - if (versions[prev].loaded) { + if (isLoaded(versions[cur])) { + if (isLoaded(versions[prev])) { return Boolean(versionLt(prev, cur)); } else { return true; } } - if (versions[prev].loaded) { + if (isLoaded(versions[prev])) { return false; } return versionLt(prev, cur); diff --git a/packages/webpack-bundler-runtime/src/initContainerEntry.ts b/packages/webpack-bundler-runtime/src/initContainerEntry.ts index 55b9086f6f0..ffacc203f93 100644 --- a/packages/webpack-bundler-runtime/src/initContainerEntry.ts +++ b/packages/webpack-bundler-runtime/src/initContainerEntry.ts @@ -52,7 +52,6 @@ export function initContainerEntry( federationInstance.initShareScopeMap(name, shareScope); - webpackRequire.S[name] = shareScope; if (webpackRequire.federation.attachShareScopeMap) { webpackRequire.federation.attachShareScopeMap(webpackRequire); }