GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||
GeezerVison is showing github.com — fix ESM default export handling for .mjs files in Module Federation by y-okt · Pull Request #20189 · webpack/webpack Conversation
Contributor
Summary Fixes: #16125 When .mjs files import a default export from a shared/remote module in Module Federation, they receive the ESM namespace object instead of the actual default export value. The root cause was that ConsumeSharedModule and RemoteModule have empty buildMeta (no exportsType). When .mjs files (which have strictHarmonyModule: true) import from these modules, getExportsType() returns "default-with-named", causing webpack to generate code that expects .default to be pre-unwrapped. But the federation runtime simply passes through the namespace object. Explaining in more detail,
// "default-with-named" var _pkg = __webpack_require__("shared-pkg"); // "dynamic" var _pkg = __webpack_require__("shared-pkg"); var _pkg_default = __webpack_require__.n(_pkg); // ← This is added
{ __esModule: true, default: function actualFunction() { ... }, namedExport: "value" }
// "default-with-named" var _pkg = __webpack_require__("shared-pkg"); var something = _pkg; // ← entire namespace object // "dynamic" var _pkg = __webpack_require__("shared-pkg"); var _pkg_default = __webpack_require__.n(_pkg); var something = _pkg_default(); // ← calls function that returns .default What kind of change does this PR introduce? As aforementioned in the detailed section, Another solution is to unwrap the default export at runtime, but this breaks the original webpack's approach to process at compilation time, and also can complicate the implementation due to its complexity. Did you add tests for your changes? Yes Does this PR introduce a breaking change? No. This will fix the bug reported in #16125 If relevant, what needs to be documented once your changes are merged or what have you already documented?
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
There was an error while loading. Please reload this page.
🦋 Changeset detectedLatest commit: 76c88e4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
CodSpeed Performance ReportMerging #20189 will not alter performanceComparing Summary
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
d32be4e to
b7a7123
Compare
|
@alexander-akait Thank you for approving! It seems that CI is saying "Waiting for status to be reported". Do I need to do anything here? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@y-okt No, I am waiting for review from other developers and we can merge |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@alexander-akait I see, thank you for letting me know! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Looks like the issue involves strict ESM module (.mjs) importing another strict ESM module (.mjs). In this case, using the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@hai-x Hi, thank you for reviewing. My understanding on what happens on module federation is that: The format can differ when the host has a different package version that uses a different module format (e.g., older version before ESM was added), or when resolve conditions differ between host and consumer. Furthermore, I think this matches how For these reasons, my guess is that Could you share your opinion? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
I think the root reason is that we can’t determine whether the shared module is actually CJS or ESM, so we don’t assign
Yeah, in most scenarios, But when a module(assumed CJS here) is imported by a strict ESM file (.mjs), webpack’s interop behavior aligned with
We have Back to the origin issue, it’s |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
use namespace rather than dynamic in js importing mjs case Fixes: webpack#20189
b7a7123 to
64ddd69
Compare
|
@hai-x Thank you, I now understand your concern. I updated my implementation as follows.
Could you please review again? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
For 1, I think we can handle it in a separate PR and we also need some input from @ScriptedAlchemy . To be honest, I’m not fully understand it, and concerned it might break interop. For 2 and 3 look good to me. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@y-okt Yeah, let's remove changes for 1 and will wait feedback from @ScriptedAlchemy here (will solve it in a separate PR) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
remove module federation implementation to separately do so in another MR Fixes: webpack#20189
|
@hai-x @alexander-akait Thank you, changed the handling for 1 to deal with The logic:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
When .mjs files import a default export from a shared/remote module in Module Federation, they receive the ESM namespace object instead of the actual default export value. The solution of this approach is to override the getExportsType() method in ConsumeSharedModule and RemoteModule to always return "dynamic".
fix failed linting for lib classes
as pointed out in PR review, their values are less so remove these
use namespace rather than dynamic in js importing mjs case Fixes: webpack#20189
remove module federation implementation to separately do so in another MR Fixes: webpack#20189
d57edf1 to
76c88e4
Compare
|
Sorry, rebased because codecov was failing - could you retrigger the pipeline? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@y-okt Sometimes codecov has false positive reports due our codebase is complex and many things are async, so ignore it, I always check it before merge and ask to adding test cases if found something uncovered that should be tested |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thank you @alexander-akait for the information🙇 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@y-okt @alexander-akait Sorry for the delay. I just checked the logic of And we could probably refine it further, but that can go into a separate PR. For now, we can go ahead and merge this one; at least the output won’t be any worse. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@y-okt Thanks for fix |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This PR is packaged and the instant preview is available (50e0997). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@50e0997
yarn add -D webpack@https://pkg.pr.new/webpack@50e0997
pnpm add -D webpack@https://pkg.pr.new/webpack@50e0997 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@alexander-akait @hai-x Thank you for reviewing and merging! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Resolve module (mjs) incorrectly when using Module Federation Plugin
There was an error while loading. Please reload this page.