Dependency Extraction Webpack Plugin: Externalize script modules as import() and emit module_dependencies#76395
Dependency Extraction Webpack Plugin: Externalize script modules as import() and emit module_dependencies#76395jorgefilipecosta wants to merge 1 commit intotrunkfrom
Conversation
|
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. |
|
Size Change: 0 B Total Size: 6.89 MB ℹ️ View Unchanged
|
|
Flaky tests detected in fb21c19. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/22953762617
|
Summary
FIxes: #75196
WordPress packages that declare
wpScriptModuleExportsin theirpackage.json(e.g.,@wordpress/abilities) are script modules — they don't registerwindowglobals. The@wordpress/dependency-extraction-webpack-plugincurrently treats all@wordpress/*packages as window-global scripts, which causes two problems:window.wp.abilities(which doesn't exist at runtime). During the build the dynamic import is not respected.wp-abilitiesindependenciesinstead ofmodule_dependenciesin the asset fileThis PR fixes the webpack plugin to match the behavior already implemented in the esbuild-based
wp-buildplugin:package.jsonand checkingwpScriptModuleExportsimporttype (generatingimport()at runtime) instead ofwindowglobalmodule_dependenciesin the asset file, matching the format WordPress PHP expects (script-loader.php,class-wp-scripts.php)wpScript+wpScriptModuleExports) are treated as regular scripts in non-module buildsThis solution has a drawback packages need to be published on npm so the build can get their package.json. On #76397 we address that, but it is a hardcoded list.
cc: @youknowriad, @sirreal, @jonathanbossenger
Changes
util.js:getPackageInfo()— resolves and reads a package'spackage.jsonusing Node's module resolution (with caching)isScriptModuleImport()— checks if a package/subpath import is a script module (ported fromwp-build'swordpress-externals-plugin.mjs)index.js:isScriptModuleRequest()method that combines package resolution with script module detectionexternalizeWpDeps()to externalize script module requests withimporttype prefixaddAssets()to track script module dependencies separately and emit them asmodule_dependenciesTest plan
npm install && npm run build. Check out https://github.com/jonathanbossenger/wp-ai-client-demo/tree/debug-wp-build (branch debug-wp-build ).On this plugin it should probably use routes and we would not face that problem, but a solution is still needed for scripts using wp-build that consume script modules but are not a wp-module.