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

Conversation

colacadstink
Copy link
Contributor

@colacadstink colacadstink commented Dec 2, 2021

This addresses (fixes #14795) . Previously, when using module federation, if requiredVersion was false, the singleton flag would be entirely ignored. Since it's entirely valid to require one instance of a package be instantiated, but not care at all what version it is, this seems like an oversight.

What kind of change does this PR introduce?

The core of this change is in ConsumeSharedModule.codeGeneration, which now includes logic for when requiredVersion is false but singleton is true. Specifically, it causes the generated code to call one of two new functions - loadSingleton, or loadSingletonFallback if a fallback is present. The code for these new functions is located in ConsumeSharedRuntimeModule.generate, along with a helper function, getSingleton, which acts similarly to getSingletonVersion but without doing a semver check.

Did you add tests for your changes?

I did not. Pardon my ignorance, but I can't figure out where or how to add a test for this.

Does this PR introduce a breaking change?

No, or at least, I wouldn't think so. This technically changes the behavior of webpack, but I'd be shocked if anyone was depending on the existing behavior, given how this appears to be a bug instead of an intended feature.

What needs to be documented once your changes are merged?

Nothing, I don't think - this code change makes the code work as I'd expect from the current documentation, so I don't think any changes would be needed.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 2, 2021

CLA Signed

The committers are authorized under a signed CLA.

@webpack-bot
Copy link
Contributor

For maintainers only:

  • This needs to be documented (issue in webpack/webpack.js.org will be filed when merged)
  • This needs to be backported to webpack 4 (issue will be created when merged)

Copy link
Member

@vankop vankop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls add test cases

@vankop
Copy link
Member

vankop commented Dec 2, 2021

You can edit test case in test/configCases/container/* (there are some with ConsumeSharedPlugin) or add your one in this directory

to run only your test case node node_modules/.bin/jest test/ConfigTestCases.basictest.js -t=directory-name

@sokra
Copy link
Member

sokra commented Dec 2, 2021

You can edit test case in test/configCases/container/* (there are some with ConsumeSharedPlugin) or add your one in this directory

And there are more regarding sharing in test/configCases/sharing

to run only your test case node node_modules/.bin/jest test/ConfigTestCases.basictest.js -t=directory-name

yarn jest ConfigTest -t directory-name

@sokra
Copy link
Member

sokra commented Dec 2, 2021

Code changes look good.

@webpack-bot
Copy link
Contributor

@colacadstink Thanks for your update.

I labeled the Pull Request so reviewers will review it again.

@vankop Please review the new changes.

@colacadstink
Copy link
Contributor Author

Massive thank you @sokra for pointing me to the sharing tests - I was able to very easily modify that to test what I added (and ended up catching a typo in the process - this is why we test!)

@webpack-bot
Copy link
Contributor

Hi @colacadstink.

Just a little hint from a friendly bot about the best practice when submitting pull requests:

Don't submit pull request from your own main branch. It's recommended to create a feature branch for the PR.

You don't have to change it for this PR, just make sure to follow this hint the next time you submit a PR.

@sokra sokra merged commit 0bc3a2e into webpack:main Dec 3, 2021
@sokra
Copy link
Member

sokra commented Dec 3, 2021

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ModuleFederationPlugin ignores singleton: true when requiredVersion is false
4 participants