-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Dependency Extraction Webpack Plugin: Use import
for module externals
#57577
Conversation
When using `module` externals, webpack always produces a static import: import x from 'external-module'; That's undesirable in the case of dynamic imports where module loading could be deferred or avoided altogether. Instead, use the `import` external type which causes webpack use always use `import()` to load these external modules.
0377de5
to
7cfb82e
Compare
Size Change: 0 B Total Size: 1.69 MB ℹ️ View Unchanged
|
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.
Looks good to me!
Maybe add an entry to the changelog before merging?
Ups, I didn't notice the auto-merge was enabled. My bad 😄 |
I love having relevant changelogs, but since this feature hasn't shipped I think it's covered under the new feature. I can add one in another PR if desired. |
true! |
…ivity (#57602) Use an explicit module external type by default for the @wordpress/interactivity external module. In #57577, we switched the default external type from module (externals are always hoisted to static imports) to import (externals are always dynamic import()s). That's likely desirable for most external modules, but @wordpress/interactivity has some known issues where it may not behave as expected if it's not initialized before DOMContentLoaded (#56986). By declaring an explicit module external type for @wordpress/interactivity, webpack will always hoist this import to a static import, preventing issues with dynamic imports of the package.
What?
Use
import
instead ofmodule
as the external type. This prevents webpack from hoisting all external modules to static imports —even ones that were written as dynamic/asyncimport()
!Why?
See https://webpack.js.org/configuration/externals/#externalstype
With
module
, all external modules were hoisted to static imports. That has some downsides, in particular making it completely irrelevant to use dynamic imports in code to defer or avoid loading some modules.import
transforms all the externals to dynamicimport(…)
.Webpack behavior
Input
Notice the dynamic
import()
:Before
Notice the import is now hoisted to a static import:
After
Here we restore the dynamic
import()
: