Skip to content

Commit

Permalink
Merge pull request #14782 from tanhauhau/tanhauhau/show-which-contain…
Browse files Browse the repository at this point in the history
…er-loaded-singleton-version

feat: show which singleton loaded version
  • Loading branch information
sokra committed Nov 25, 2021
2 parents 68c4a2a + ce0f0b3 commit ac9a2c8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/sharing/ConsumeSharedRuntimeModule.js
Expand Up @@ -103,17 +103,17 @@ class ConsumeSharedRuntimeModule extends RuntimeModule {
]
)};`,
`var getInvalidSingletonVersionMessage = ${runtimeTemplate.basicFunction(
"key, version, requiredVersion",
"scope, key, version, requiredVersion",
[
`return "Unsatisfied version " + version + " 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(
"scope, scopeName, key, requiredVersion",
[
"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(scope, key, version, requiredVersion));',
"return get(scope[key][version]);"
]
)};`,
Expand All @@ -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(scope, key, version, requiredVersion));",
"return get(scope[key][version]);"
]
)};`,
Expand Down
Expand Up @@ -7,7 +7,8 @@ module.exports = {
bundle0: "./index.js"
},
output: {
filename: "[name].js"
filename: "[name].js",
uniqueName: "1-container-full"
},
plugins: [
new ModuleFederationPlugin({
Expand Down
2 changes: 1 addition & 1 deletion test/configCases/container/2-container-full/index.js
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions test/configCases/container/2-container-full/webpack.config.js
Expand Up @@ -3,6 +3,9 @@ const { ModuleFederationPlugin } = require("../../../../").container;

/** @type {import("../../../../").Configuration} */
module.exports = {
output: {
uniqueName: "2-container-full"
},
plugins: [
new ModuleFederationPlugin({
name: "main",
Expand Down
5 changes: 3 additions & 2 deletions test/configCases/sharing/consume-module/index.js
Expand Up @@ -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'
}
}
};
Expand Down Expand Up @@ -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\)/
);
}
});

0 comments on commit ac9a2c8

Please sign in to comment.