-
-
Notifications
You must be signed in to change notification settings - Fork 370
fix(webpack-bundler-runtime): fix the bug that mjs is wrongly handled #4239
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
base: main
Are you sure you want to change the base?
fix(webpack-bundler-runtime): fix the bug that mjs is wrongly handled #4239
Conversation
🦋 Changeset detectedLatest commit: d317461 The changes in this PR will be included in the next version bump. This PR includes changesets to release 37 packages
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 |
✅ Deploy Preview for module-federation-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
ef448c7 to
eb21d4c
Compare
… mjs files Fix ESM interop issue where .mjs files received module namespace objects instead of default exports when using Module Federation with remotes. The runtime now intelligently unwraps ESM namespace objects for object/function default exports while preserving the namespace for primitive defaults to maintain named export accessibility.
fix mjs handling issue by introducing dynamic override
eb21d4c to
696a637
Compare
| }, | ||
| ); | ||
|
|
||
| // Add finishModules hook to copy buildMeta/buildInfo from fallback modules *after* webpack's export analysis |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason for this change is that:
- since I introduced
"dynamic", this tells webpack to generate__wepack_require__.n()at compile time, which properly handles the ESM/CJS interop. The deleted codes are to inherit the ESM/CJS detection from the actual shared module, which is no longer necessary - As explained in the description section, this can't deal with federation-remote case
- Instead of copying
buildMetaat runtime, it's better to deal with it at compilation time
Description
When using Module Federation with .mjs files, default exports are incorrectly resolved. Instead of receiving the actual default export value, consumers receive the ESM namespace object.
Example
Testing with the reproduction codes,

Root Cause
Webpack normally inspects
buildMetato determine the exact export type at compile time and generates appropriate code to accessmodule.default.However, in Module Federation contexts:
Shared modules (
ConsumeSharedModule)The actual module is resolved at runtime, so webpack cannot determine the export type at compile time.
Remote modules (
RemoteModule)The module comes from a federated remote, making compile-time analysis impossible.
Without knowing the export type, webpack defaults to treating these as strict ESM modules and returns the namespace object instead of unwrapping the default export.
Previous Approach (Removed)
The previous fix attempted to copy
buildMeta/buildInfofrom fallback modules at runtime inConsumeSharedPlugin.ts.This worked for shared modules with fallbacks but failed for remote modules, which have no fallback to copy metadata from.
Solution
Override
getExportsType()in bothRemoteModuleandConsumeSharedModuleto return"dynamic".This tells webpack to generate
__webpack_require__.n()at compile time, which properly handles ESM/CJS interoperability by:__esModuleflag at runtimemodule.defaultfor ESM modulesThis approach works for both shared modules and remote modules, since it doesn't depend on fallback metadata.
Related Issue
#4238
In webpack,
webpack/webpack#16125
webpack/webpack#20189
Types of changes
Checklist