-
-
Notifications
You must be signed in to change notification settings - Fork 372
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
Open
y-okt
wants to merge
3
commits into
module-federation:main
Choose a base branch
from
y-okt:fix-resolve-mjs-on-federation-remote
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@module-federation/enhanced': patch | ||
| --- | ||
|
|
||
| Fix ESM default export handling for .mjs files by overriding getExportsType() in ConsumeSharedModule and RemoteModule to return "dynamic" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/enhanced/test/configCases/container/remote-module-mjs-default-export/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| it('should correctly handle default imports in .mjs files from remote modules', async () => { | ||
| const { testDefaultImport } = await import('./pure-esm-consumer.mjs'); | ||
| const result = testDefaultImport(); | ||
| expect(result.defaultType).toBe('function'); | ||
| expect(result.defaultValue).toBe('remote default export'); | ||
| expect(result.namedExportValue).toBe('remote named export'); | ||
| }); |
10 changes: 10 additions & 0 deletions
10
...nhanced/test/configCases/container/remote-module-mjs-default-export/pure-esm-consumer.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import something from 'remote-esm-pkg/module'; | ||
| import { namedExport } from 'remote-esm-pkg/module'; | ||
|
|
||
| export function testDefaultImport() { | ||
| return { | ||
| defaultType: typeof something, | ||
| defaultValue: typeof something === 'function' ? something() : something, | ||
| namedExportValue: namedExport, | ||
| }; | ||
| } |
19 changes: 19 additions & 0 deletions
19
packages/enhanced/test/configCases/container/remote-module-mjs-default-export/test.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| module.exports = { | ||
| moduleScope(scope) { | ||
| scope.REMOTE_ESM_PKG = { | ||
| get(module) { | ||
| return new Promise((resolve) => { | ||
| setTimeout(() => { | ||
| resolve(() => ({ | ||
| __esModule: true, | ||
| default: function remoteFunction() { | ||
| return 'remote default export'; | ||
| }, | ||
| namedExport: 'remote named export', | ||
| })); | ||
| }, 100); | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; |
14 changes: 14 additions & 0 deletions
14
...es/enhanced/test/configCases/container/remote-module-mjs-default-export/webpack.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const { ContainerReferencePlugin } = require('../../../../dist/src'); | ||
|
|
||
| module.exports = { | ||
| mode: 'development', | ||
| devtool: false, | ||
| plugins: [ | ||
| new ContainerReferencePlugin({ | ||
| remoteType: 'var', | ||
| remotes: { | ||
| 'remote-esm-pkg': 'REMOTE_ESM_PKG', | ||
| }, | ||
| }), | ||
| ], | ||
| }; |
8 changes: 8 additions & 0 deletions
8
packages/enhanced/test/configCases/sharing/consume-module-mjs-default-export/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| it('should correctly handle default imports in .mjs files from shared modules', async () => { | ||
| await __webpack_init_sharing__('default'); | ||
| const { testDefaultImport } = await import('./pure-esm-consumer.mjs'); | ||
| const result = testDefaultImport(); | ||
| expect(result.defaultType).toBe('function'); | ||
| expect(result.defaultValue).toBe('shared default export'); | ||
| expect(result.namedExportValue).toBe('shared named export'); | ||
| }); |
5 changes: 5 additions & 0 deletions
5
...onfigCases/sharing/consume-module-mjs-default-export/node_modules/shared-esm-pkg/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
...gCases/sharing/consume-module-mjs-default-export/node_modules/shared-esm-pkg/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
...enhanced/test/configCases/sharing/consume-module-mjs-default-export/pure-esm-consumer.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import something from 'shared-esm-pkg'; | ||
| import { namedExport } from 'shared-esm-pkg'; | ||
|
|
||
| export function testDefaultImport() { | ||
| return { | ||
| defaultType: typeof something, | ||
| defaultValue: typeof something === 'function' ? something() : something, | ||
| namedExportValue: namedExport, | ||
| }; | ||
| } |
30 changes: 30 additions & 0 deletions
30
...ges/enhanced/test/configCases/sharing/consume-module-mjs-default-export/webpack.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| const { | ||
| ConsumeSharedPlugin, | ||
| ProvideSharedPlugin, | ||
| } = require('../../../../dist/src'); | ||
|
|
||
| module.exports = { | ||
| mode: 'development', | ||
| devtool: false, | ||
| plugins: [ | ||
| new ProvideSharedPlugin({ | ||
| provides: { | ||
| 'shared-esm-pkg': { | ||
| shareKey: 'shared-esm-pkg', | ||
| version: '1.0.0', | ||
| eager: true, | ||
| }, | ||
| }, | ||
| }), | ||
| new ConsumeSharedPlugin({ | ||
| consumes: { | ||
| 'shared-esm-pkg': { | ||
| shareKey: 'shared-esm-pkg', | ||
| requiredVersion: '^1.0.0', | ||
| strictVersion: false, | ||
| eager: true, | ||
| }, | ||
| }, | ||
| }), | ||
| ], | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
"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 necessarybuildMetaat runtime, it's better to deal with it at compilation time