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..78edabd60a5 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 getSingleton(scope, scopeName, key);" + ] + )});`, `var loadSingletonVersionCheckFallback = /*#__PURE__*/ init(${runtimeTemplate.basicFunction( "scopeName, scope, key, version, fallback", [ 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 } } })