From 92e373028d6831e7e276bd5e8dad8a3b7f5ef9f4 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Wed, 1 Dec 2021 21:07:29 -0500 Subject: [PATCH 1/2] Proposed fix to module federation to stop ignoring the singleton flag when requiredVersion is false --- lib/sharing/ConsumeSharedModule.js | 4 ++++ lib/sharing/ConsumeSharedRuntimeModule.js | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/sharing/ConsumeSharedModule.js b/lib/sharing/ConsumeSharedModule.js index cb6881331b1..675fa6cf4eb 100644 --- a/lib/sharing/ConsumeSharedModule.js +++ b/lib/sharing/ConsumeSharedModule.js @@ -210,6 +210,10 @@ class ConsumeSharedModule extends Module { } args.push(stringifyHoley(requiredVersion)); fn += "VersionCheck"; + } else { + if (singleton) { + fn += "Singleton"; + } } if (fallbackCode) { fn += "Fallback"; diff --git a/lib/sharing/ConsumeSharedRuntimeModule.js b/lib/sharing/ConsumeSharedRuntimeModule.js index 457881a0f33..cbaf839f537 100644 --- a/lib/sharing/ConsumeSharedRuntimeModule.js +++ b/lib/sharing/ConsumeSharedRuntimeModule.js @@ -108,6 +108,13 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { `return "Unsatisfied version " + version + " from " + (version && scope[key][version].from) + " of shared singleton module " + key + " (required " + rangeToString(requiredVersion) + ")"` ] )};`, + `var getSingleton = ${runtimeTemplate.basicFunction( + "scope, scopeName, key, requiredVersion", + [ + "var version = findSingletonVersionKey(scope, key);", + "return get(scope[key][version]);" + ] + )};`, `var getSingletonVersion = ${runtimeTemplate.basicFunction( "scope, scopeName, key, requiredVersion", [ @@ -202,6 +209,13 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { "return get(findValidVersion(scope, key, version) || warnInvalidVersion(scope, scopeName, key, version) || findVersion(scope, key));" ] )});`, + `var loadSingleton = /*#__PURE__*/ init(${runtimeTemplate.basicFunction( + "scopeName, scope, key", + [ + "ensureExistence(scopeName, key);", + "return getSingleton(scope, scopeName, key);" + ] + )});`, `var loadSingletonVersionCheck = /*#__PURE__*/ init(${runtimeTemplate.basicFunction( "scopeName, scope, key, version", [ @@ -230,6 +244,13 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { "return get(findValidVersion(scope, key, version) || warnInvalidVersion(scope, scopeName, key, version) || findVersion(scope, key));" ] )});`, + `var loadSingletonFallback = /*#__PURE__*/ init(${runtimeTemplate.basicFunction( + "scopeName, scope, key, fallback", + [ + `if(!scope || !${RuntimeGlobals.hasOwnProperty}(scope, key)) return fallback();`, + "return getSingletonVersion(scope, scopeName, key);" + ] + )});`, `var loadSingletonVersionCheckFallback = /*#__PURE__*/ init(${runtimeTemplate.basicFunction( "scopeName, scope, key, version, fallback", [ From bbe72b3eae520813a2bdfb176e4c31fe9b4504f3 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Thu, 2 Dec 2021 11:44:23 -0500 Subject: [PATCH 2/2] Added testing for new singleton logic (and caught a typo in the process!) --- lib/sharing/ConsumeSharedRuntimeModule.js | 2 +- .../sharing/consume-module/index.js | 27 +++++++++++++++++++ .../node_modules/singletonWithoutVersion.js | 1 + .../sharing/consume-module/webpack.config.js | 4 +++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/configCases/sharing/consume-module/node_modules/singletonWithoutVersion.js diff --git a/lib/sharing/ConsumeSharedRuntimeModule.js b/lib/sharing/ConsumeSharedRuntimeModule.js index cbaf839f537..78edabd60a5 100644 --- a/lib/sharing/ConsumeSharedRuntimeModule.js +++ b/lib/sharing/ConsumeSharedRuntimeModule.js @@ -248,7 +248,7 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { "scopeName, scope, key, fallback", [ `if(!scope || !${RuntimeGlobals.hasOwnProperty}(scope, key)) return fallback();`, - "return getSingletonVersion(scope, scopeName, key);" + "return getSingleton(scope, scopeName, key);" ] )});`, `var loadSingletonVersionCheckFallback = /*#__PURE__*/ init(${runtimeTemplate.basicFunction( diff --git a/test/configCases/sharing/consume-module/index.js b/test/configCases/sharing/consume-module/index.js index e5b826490f1..442fdecde73 100644 --- a/test/configCases/sharing/consume-module/index.js +++ b/test/configCases/sharing/consume-module/index.js @@ -196,6 +196,15 @@ it("should handle version matching correctly in strict and singleton mode", asyn get: () => () => "shared singleton", from: 'container-a' } + }, + singletonWithoutVersion: { + "1.0.0": { + get: () => () => "shared singleton v1.0.0", + loaded: true + }, + "2.0.0": { + get: () => () => "shared singleton v2.0.0" + } } }; { @@ -235,3 +244,21 @@ it("should handle version matching correctly in strict and singleton mode", asyn ); } }); + +it("should not instantiate multiple singletons even if a higher version exists", async () => { + __webpack_share_scopes__["default"] = { + singletonWithoutVersion: { + "1.0.0": { + get: () => () => "shared singleton v1.0.0", + loaded: true + }, + "2.0.0": { + get: () => () => "shared singleton v2.0.0" + } + } + }; + { + const result = await import("singletonWithoutVersion"); + expect(result.default).toBe("shared singleton v1.0.0"); + } +}); diff --git a/test/configCases/sharing/consume-module/node_modules/singletonWithoutVersion.js b/test/configCases/sharing/consume-module/node_modules/singletonWithoutVersion.js new file mode 100644 index 00000000000..eb02ddc0628 --- /dev/null +++ b/test/configCases/sharing/consume-module/node_modules/singletonWithoutVersion.js @@ -0,0 +1 @@ +module.exports = "singleton without version"; diff --git a/test/configCases/sharing/consume-module/webpack.config.js b/test/configCases/sharing/consume-module/webpack.config.js index 5354e8f8d2d..e64023cbe63 100644 --- a/test/configCases/sharing/consume-module/webpack.config.js +++ b/test/configCases/sharing/consume-module/webpack.config.js @@ -54,6 +54,10 @@ module.exports = { requiredVersion: "1.1.0", singleton: true, strictVersion: false + }, + singletonWithoutVersion: { + requiredVersion: false, + singleton: true } } })