Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed fix to module federation to stop ignoring the singleton flag when requiredVersion is false #14884

Merged
merged 2 commits into from Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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