Enhancement: Improve CJS → ESM support#14998
Merged
sokra merged 5 commits intowebpack:mainfrom Jan 10, 2022
Merged
Conversation
|
|
Contributor
|
For maintainers only:
|
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
@sokra requested/suggested |
fix externals add test case
sokra
approved these changes
Jan 10, 2022
Contributor
Author
|
Ohhh, now I see what you meant. Thanks! I just ran it locally with the |
Member
|
Thanks |
Contributor
|
I've created an issue to document this in webpack/webpack.js.org. |
| case "commonjs-module": | ||
| case "commonjs-static": | ||
| return getSourceForCommonJsExternal(request); | ||
| case "node-commonjs": |
Contributor
Author
This was referenced Feb 19, 2025
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
In order for Node.js to detect named exports in CJS, they must be statically available. However, without this change, webpack does not produce exports that way—webpack will respect source code that is originally written in such a way, but it won't itself produce it (ex if the source code is ESM).
The outputted code will now look like:
And when imported into an ESM file:
What kind of change does this PR introduce?
Enhancement
Did you add tests for your changes?
Will do once approach etc are confirmed.
Does this PR introduce a breaking change?
No: It requires the user to opt-in via configuration flag. Also, Node.js still automatically adds the named exports to
defaultanyway (in addition to exposing them as named exports).What needs to be documented once your changes are merged?
New
output.library.typeoptioncommonjs-staticwhich is similar tocommonjs-modulebut emits code in a statically analyse-able way, so Node.js is able to construct named exports in it's commonjs esm compat layer.Exports must be statically known to webpack for this to work.
optimization.providedExportsmust not be disabled.