From 0e2337dfb7ad88ebaac40ee7a8071a3364ba2387 Mon Sep 17 00:00:00 2001 From: tanhauhau Date: Sun, 21 Nov 2021 10:57:17 +0800 Subject: [PATCH 1/3] feat: show which singleton loaded version --- lib/sharing/ConsumeSharedRuntimeModule.js | 8 ++++---- test/configCases/sharing/consume-module/index.js | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/sharing/ConsumeSharedRuntimeModule.js b/lib/sharing/ConsumeSharedRuntimeModule.js index 9a868f4f705..6f4d3210d57 100644 --- a/lib/sharing/ConsumeSharedRuntimeModule.js +++ b/lib/sharing/ConsumeSharedRuntimeModule.js @@ -103,9 +103,9 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { ] )};`, `var getInvalidSingletonVersionMessage = ${runtimeTemplate.basicFunction( - "key, version, requiredVersion", + "key, version, requiredVersion, from", [ - `return "Unsatisfied version " + version + " of shared singleton module " + key + " (required " + rangeToString(requiredVersion) + ")"` + `return "Unsatisfied version " + version + (from ? " from " + from: '') + " of shared singleton module " + key + " (required " + rangeToString(requiredVersion) + ")"` ] )};`, `var getSingletonVersion = ${runtimeTemplate.basicFunction( @@ -113,7 +113,7 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { [ "var version = findSingletonVersionKey(scope, key);", "if (!satisfy(requiredVersion, version)) " + - 'typeof console !== "undefined" && console.warn && console.warn(getInvalidSingletonVersionMessage(key, version, requiredVersion));', + 'typeof console !== "undefined" && console.warn && console.warn(getInvalidSingletonVersionMessage(key, version, requiredVersion, scope[key][version].from));', "return get(scope[key][version]);" ] )};`, @@ -122,7 +122,7 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { [ "var version = findSingletonVersionKey(scope, key);", "if (!satisfy(requiredVersion, version)) " + - "throw new Error(getInvalidSingletonVersionMessage(key, version, requiredVersion));", + "throw new Error(getInvalidSingletonVersionMessage(key, version, requiredVersion, scope[key][version].from));", "return get(scope[key][version]);" ] )};`, diff --git a/test/configCases/sharing/consume-module/index.js b/test/configCases/sharing/consume-module/index.js index b812be3957d..e5b826490f1 100644 --- a/test/configCases/sharing/consume-module/index.js +++ b/test/configCases/sharing/consume-module/index.js @@ -193,7 +193,8 @@ it("should handle version matching correctly in strict and singleton mode", asyn }, singleton: { "1.1.1": { - get: () => () => "shared singleton" + get: () => () => "shared singleton", + from: 'container-a' } } }; @@ -230,7 +231,7 @@ it("should handle version matching correctly in strict and singleton mode", asyn const result = await import("singleton"); expect(result.default).toBe("shared singleton"); expectWarning( - /Unsatisfied version 1\.1\.1 of shared singleton module singleton \(required =1\.1\.0\)/ + /Unsatisfied version 1\.1\.1 from container-a of shared singleton module singleton \(required =1\.1\.0\)/ ); } }); From 6fbcd415d45933010249d31e28d657cce028e5a5 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 25 Nov 2021 09:57:48 +0100 Subject: [PATCH 2/3] improve runtime code --- lib/sharing/ConsumeSharedRuntimeModule.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/sharing/ConsumeSharedRuntimeModule.js b/lib/sharing/ConsumeSharedRuntimeModule.js index 6f4d3210d57..457881a0f33 100644 --- a/lib/sharing/ConsumeSharedRuntimeModule.js +++ b/lib/sharing/ConsumeSharedRuntimeModule.js @@ -103,9 +103,9 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { ] )};`, `var getInvalidSingletonVersionMessage = ${runtimeTemplate.basicFunction( - "key, version, requiredVersion, from", + "scope, key, version, requiredVersion", [ - `return "Unsatisfied version " + version + (from ? " from " + from: '') + " of shared singleton module " + key + " (required " + rangeToString(requiredVersion) + ")"` + `return "Unsatisfied version " + version + " from " + (version && scope[key][version].from) + " of shared singleton module " + key + " (required " + rangeToString(requiredVersion) + ")"` ] )};`, `var getSingletonVersion = ${runtimeTemplate.basicFunction( @@ -113,7 +113,7 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { [ "var version = findSingletonVersionKey(scope, key);", "if (!satisfy(requiredVersion, version)) " + - 'typeof console !== "undefined" && console.warn && console.warn(getInvalidSingletonVersionMessage(key, version, requiredVersion, scope[key][version].from));', + 'typeof console !== "undefined" && console.warn && console.warn(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));', "return get(scope[key][version]);" ] )};`, @@ -122,7 +122,7 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { [ "var version = findSingletonVersionKey(scope, key);", "if (!satisfy(requiredVersion, version)) " + - "throw new Error(getInvalidSingletonVersionMessage(key, version, requiredVersion, scope[key][version].from));", + "throw new Error(getInvalidSingletonVersionMessage(scope, key, version, requiredVersion));", "return get(scope[key][version]);" ] )};`, From ce0f0b3a164591da1b8edcee038b9e81d0bf4909 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 25 Nov 2021 10:23:24 +0100 Subject: [PATCH 3/3] update test case --- test/configCases/container/1-container-full/webpack.config.js | 3 ++- test/configCases/container/2-container-full/index.js | 2 +- test/configCases/container/2-container-full/webpack.config.js | 3 +++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/test/configCases/container/1-container-full/webpack.config.js b/test/configCases/container/1-container-full/webpack.config.js index 24128c78978..0717682bc7c 100644 --- a/test/configCases/container/1-container-full/webpack.config.js +++ b/test/configCases/container/1-container-full/webpack.config.js @@ -7,7 +7,8 @@ module.exports = { bundle0: "./index.js" }, output: { - filename: "[name].js" + filename: "[name].js", + uniqueName: "1-container-full" }, plugins: [ new ModuleFederationPlugin({ diff --git a/test/configCases/container/2-container-full/index.js b/test/configCases/container/2-container-full/index.js index 942f87c5a5b..4dcf93a5a1e 100644 --- a/test/configCases/container/2-container-full/index.js +++ b/test/configCases/container/2-container-full/index.js @@ -30,7 +30,7 @@ const expectWarning = regexp => { it("should load the component from container", () => { return import("./App").then(({ default: App }) => { expectWarning( - /Unsatisfied version 8 of shared singleton module react \(required \^2\)/ + /Unsatisfied version 8 from 2-container-full of shared singleton module react \(required \^2\)/ ); const rendered = App(); expect(rendered).toBe( diff --git a/test/configCases/container/2-container-full/webpack.config.js b/test/configCases/container/2-container-full/webpack.config.js index 16e44280410..72e7ba1eb5a 100644 --- a/test/configCases/container/2-container-full/webpack.config.js +++ b/test/configCases/container/2-container-full/webpack.config.js @@ -3,6 +3,9 @@ const { ModuleFederationPlugin } = require("../../../../").container; /** @type {import("../../../../").Configuration} */ module.exports = { + output: { + uniqueName: "2-container-full" + }, plugins: [ new ModuleFederationPlugin({ name: "main",