fix: module-import get fallback from externalsPresets #18660
Merged
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.
What kind of change does this PR introduce?
Adding fallback external type for
"module-import"
type when"module"
and"import"
are neither applicable.In #18620, implement "module-import", but the premise is that we're using
import
orimport()
for external request, However, for example, if we userequire('...')
, "module-import" will failed to know which externals type to adopt ("import", "module", or even "node-commonjs"), and it will crash.The possible options we may want to fall back to:
node-commonjs
: to letrequire('path')
code could run in Node.js + ESM (this is widely used).import
: possibly for some specific reason.module
: possibly for some specific reason.commonjs
: level therequire
there, to be bundled by a bundler.In this PR, using
externalsPresets
to indicate which external to use as the fallback:exteranlsPresets
is node.js related (electrons, node, nwjs): usenode-commonjs
exteranlsPresets
is web: usemodule
exteranlsPresets
is webAsync: useimport
exteranlsPresets
is not assigned or derived, usecommonjs
Since the implementation coupled the fallback with externalsPresets, when users want to set all ESM import/import() to
module-import
and leave other CJS require tonode-commonjs
, they need to override the node.js built-in externals type tomodule-import
defined byexternalsPresets
.There's a left TODO is when user setting multiple
externalsPresets
category, e.g.{ node: true, web: true }
. Which one should we choose to use as fallback. Currently using a simple prioritizationmodule > import > node-commonjs
.Did you add tests for your changes?
Yes.
Does this PR introduce a breaking change?
No.
What needs to be documented once your changes are merged?
Update webpack/webpack.js.org#7345 after merged.