Skip to content

Commit

Permalink
Merge pull request #14884 from colacadstink/main
Browse files Browse the repository at this point in the history
Proposed fix to module federation to stop ignoring the singleton flag when requiredVersion is false
  • Loading branch information
sokra committed Dec 3, 2021
2 parents dc70535 + bbe72b3 commit 0bc3a2e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/sharing/ConsumeSharedModule.js
Expand Up @@ -210,6 +210,10 @@ class ConsumeSharedModule extends Module {
}
args.push(stringifyHoley(requiredVersion));
fn += "VersionCheck";
} else {
if (singleton) {
fn += "Singleton";
}
}
if (fallbackCode) {
fn += "Fallback";
Expand Down
21 changes: 21 additions & 0 deletions lib/sharing/ConsumeSharedRuntimeModule.js
Expand Up @@ -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",
[
Expand Down Expand Up @@ -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",
[
Expand Down Expand Up @@ -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",
[
Expand Down
27 changes: 27 additions & 0 deletions test/configCases/sharing/consume-module/index.js
Expand Up @@ -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"
}
}
};
{
Expand Down Expand Up @@ -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");
}
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/configCases/sharing/consume-module/webpack.config.js
Expand Up @@ -54,6 +54,10 @@ module.exports = {
requiredVersion: "1.1.0",
singleton: true,
strictVersion: false
},
singletonWithoutVersion: {
requiredVersion: false,
singleton: true
}
}
})
Expand Down

0 comments on commit 0bc3a2e

Please sign in to comment.