Dependency Extraction Webpack Plugin: improved script module externalization#78886
Conversation
…mport() and emit module_dependencies Packages like @wordpress/abilities and @wordpress/core-abilities are script modules (registered via wpScriptModuleExports) that don't expose window globals. The plugin currently treats all @wordpress/* packages as window-global scripts, which breaks at runtime. This adds these packages to the existing defaultRequestToExternalModule switch and makes non-module (IIFE) builds also consult that function via a try-catch bridge. Script module externals are detected in processModule via m.externalType === 'import' and emitted as module_dependencies in the asset file, matching the format WordPress PHP expects.
This adds handling of mixed usage of module imports + detection of dynamic imports in functions
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Can you add a changelog entry? Thanks! |
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
@simison I added a changelog entry, but this is a PR on a PR, so it doesn't actually do much. |
ca5c600 to
48af2e1
Compare
What?
Enhances PR #76397
This fixes the handling modules that are imported both statically and dynamically in the same project and adds detection of dynamic imports that are inside functions.
Why?
Script module externalization would be incomplete without this
How?
I modified the index.js file based on the work done by @jorgefilipecosta and a bunch of brute-force testing of how webpack records dynamic dependencies of my own
Testing Instructions
Build a project with the following files:
File 1:
File 2:
File 3:
File 4:
The files should have the following dependencies:
{"dependencies": ["@wordpress/interactivity"]}{"dependencies": ["@wordpress/a11y", {"id": "@wordpress/interactivity", "import": "dynamic"}]}{"dependencies": ["wp-blocks"], "module_dependencies": ["@wordpress/a11y", "@wordpress/interactivity"]}{"dependencies": ["wp-blocks"], "module_dependencies": ["@wordpress/a11y", {"id": "@wordpress/interactivity", "import": "dynamic"}]}Use of AI Tools
I don't use AI tools