diff --git a/projects/js-packages/webpack-config/README.md b/projects/js-packages/webpack-config/README.md index 3db2347aa2e6..5e901c2115f5 100644 --- a/projects/js-packages/webpack-config/README.md +++ b/projects/js-packages/webpack-config/README.md @@ -226,9 +226,10 @@ By default, the following additional dependencies are extracted: - `@automattic/jetpack-connection`: Handle `jetpack-connection` provided by PHP package [automattic/jetpack-connection](https://packagist.org/packages/automattic/jetpack-connection). - `@automattic/jetpack-shared-stores`: Handle `jetpack-shared-stores` provided by PHP package [automattic/jetpack-assets](https://packagist.org/packages/automattic/jetpack-assets). The shared data stores resolve to one externalized bundle so they register only once. -One additional option is recognized: +Two additional options are recognized: - `requestMap`: An easier way to specify additional dependencies to extract, rather than redefining `requestToHandle` and `requestToExternal`. Key is the dependency, value is an object with `handle` and `external` keys corresponding to the return values of `requestToHandle` and `requestToExternal`. +- `bundleWpUiDeps`: Bundle `@wordpress/theme` and `@wordpress/private-apis` instead of externalizing them to the `wp-theme` and `wp-private-apis` script handles. Defaults to `false`. It is meant for entries that bundle `@wordpress/ui`, which is built against a newer `@wordpress/theme` than the one WordPress core registers. A bundled `@wordpress/private-apis` can only unlock private APIs that were locked inside the same bundle. So do not use this option on an entry that unlocks private APIs from an external `wp-*` script, for example bundled `@wordpress/dataviews` with an external `wp-components`. Your own `requestMap` still wins over it. ##### `DuplicatePackageCheckerPlugin( options )` diff --git a/projects/js-packages/webpack-config/changelog/update-webpack-config-default-request-map b/projects/js-packages/webpack-config/changelog/update-webpack-config-default-request-map new file mode 100644 index 000000000000..f2da20c85878 --- /dev/null +++ b/projects/js-packages/webpack-config/changelog/update-webpack-config-default-request-map @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +DependencyExtractionPlugin: Add a `bundleWpUiDeps` option to bundle `@wordpress/theme` and `@wordpress/private-apis`. diff --git a/projects/js-packages/webpack-config/src/webpack.js b/projects/js-packages/webpack-config/src/webpack.js index 5b7102e17310..085f9788c533 100644 --- a/projects/js-packages/webpack-config/src/webpack.js +++ b/projects/js-packages/webpack-config/src/webpack.js @@ -146,8 +146,30 @@ const defaultRequestMap = { }, }; -const DependencyExtractionPlugin = ( { requestMap, ...options } = {} ) => { - const finalRequestMap = { ...defaultRequestMap, ...requestMap }; +// A bundled @wordpress/private-apis unlocks only what its own bundle locked. So never bundle it +// without @wordpress/theme, or in an entry that unlocks private APIs of an external wp-* script. +const wpUiRequestMap = { + '@wordpress/theme': { external: false }, + '@wordpress/private-apis': { external: false }, +}; + +/** + * Build the DependencyExtractionWebpackPlugin. + * + * @param {object} options - Plugin options, passed through except for the two below. + * @param {object} options.requestMap - Per-request `{ external, handle }` overrides. Wins over every default. + * @param {boolean} options.bundleWpUiDeps - Bundle `@wordpress/theme` and `@wordpress/private-apis` instead of + * externalizing them. Off by default. For an entry that bundles + * `@wordpress/ui`, which is built against a newer `@wordpress/theme` + * than core's `wp-theme`. + * @return {object[]} The plugin. + */ +const DependencyExtractionPlugin = ( { requestMap, bundleWpUiDeps = false, ...options } = {} ) => { + const finalRequestMap = { + ...defaultRequestMap, + ...( bundleWpUiDeps ? wpUiRequestMap : {} ), + ...requestMap, + }; const requestToExternal = request => { return finalRequestMap[ request ]?.external; diff --git a/projects/packages/search/changelog/update-webpack-config-default-request-map b/projects/packages/search/changelog/update-webpack-config-default-request-map new file mode 100644 index 000000000000..f262392c91bb --- /dev/null +++ b/projects/packages/search/changelog/update-webpack-config-default-request-map @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Replace the duplicated DependencyExtractionPlugin requestMap block with an opt-in bundleWpUiDeps option. No build output changes. + + diff --git a/projects/packages/search/tools/webpack.dashboard.config.js b/projects/packages/search/tools/webpack.dashboard.config.js index 00e03958c618..acdf27a4000b 100644 --- a/projects/packages/search/tools/webpack.dashboard.config.js +++ b/projects/packages/search/tools/webpack.dashboard.config.js @@ -36,15 +36,7 @@ module.exports = { ...jetpackWebpackConfig.StandardPlugins( { DependencyExtractionPlugin: { injectPolyfill: true, - // Match Boost / Jetpack AI admin: @wordpress/ui pulls these in transitively; - // they are not reliably registered as WP script handles in all contexts, so - // bundle them together instead of externalizing. Both must be bundled jointly - // so @wordpress/theme's module-init lock() lands on the same private-apis - // consent map. See PR #48173. - requestMap: { - '@wordpress/theme': { external: false }, - '@wordpress/private-apis': { external: false }, - }, + bundleWpUiDeps: true, }, } ), ], diff --git a/projects/plugins/boost/changelog/update-webpack-config-default-request-map b/projects/plugins/boost/changelog/update-webpack-config-default-request-map new file mode 100644 index 000000000000..f262392c91bb --- /dev/null +++ b/projects/plugins/boost/changelog/update-webpack-config-default-request-map @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Replace the duplicated DependencyExtractionPlugin requestMap block with an opt-in bundleWpUiDeps option. No build output changes. + + diff --git a/projects/plugins/boost/webpack.config.js b/projects/plugins/boost/webpack.config.js index 6aacb76dec6c..1c816a31cb0e 100644 --- a/projects/plugins/boost/webpack.config.js +++ b/projects/plugins/boost/webpack.config.js @@ -83,13 +83,7 @@ module.exports = [ filename: 'jetpack-boost.css', }, DependencyExtractionPlugin: { - requestMap: { - // Bundle @wordpress/theme and @wordpress/private-apis inline — - // they're transitive deps of @wordpress/ui but aren't registered - // as script handles in WP core, so externalizing them breaks enqueue. - '@wordpress/theme': { external: false }, - '@wordpress/private-apis': { external: false }, - }, + bundleWpUiDeps: true, }, } ), new webpack.ProvidePlugin( { diff --git a/projects/plugins/jetpack/changelog/update-webpack-config-default-request-map b/projects/plugins/jetpack/changelog/update-webpack-config-default-request-map new file mode 100644 index 000000000000..cd7f9000fed9 --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-webpack-config-default-request-map @@ -0,0 +1,5 @@ +Significance: patch +Type: other +Comment: Replace the duplicated DependencyExtractionPlugin requestMap block with an opt-in bundleWpUiDeps option. No build output changes. + + diff --git a/projects/plugins/jetpack/tools/webpack.config.js b/projects/plugins/jetpack/tools/webpack.config.js index cbd6977ef13a..22aabdbde4d2 100644 --- a/projects/plugins/jetpack/tools/webpack.config.js +++ b/projects/plugins/jetpack/tools/webpack.config.js @@ -231,16 +231,7 @@ module.exports = [ plugins: [ ...sharedWebpackConfig.plugins, ...jetpackWebpackConfig.DependencyExtractionPlugin( { - // Match the AI admin build: @wordpress/ui (pulled in via the licensing - // activation screen) drags in @wordpress/theme and @wordpress/private-apis. - // They are not registered as WP script handles on the main Jetpack admin - // path (WP < 7.0 has no core wp-theme, and this page does not load the - // wp-build-polyfills shim), so bundle them instead of externalizing to - // avoid the whole dashboard script failing to enqueue. - requestMap: { - '@wordpress/theme': { external: false }, - '@wordpress/private-apis': { external: false }, - }, + bundleWpUiDeps: true, } ), ], externals: { diff --git a/projects/plugins/protect/changelog/update-webpack-config-default-request-map b/projects/plugins/protect/changelog/update-webpack-config-default-request-map new file mode 100644 index 000000000000..f262392c91bb --- /dev/null +++ b/projects/plugins/protect/changelog/update-webpack-config-default-request-map @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Replace the duplicated DependencyExtractionPlugin requestMap block with an opt-in bundleWpUiDeps option. No build output changes. + + diff --git a/projects/plugins/protect/webpack.config.js b/projects/plugins/protect/webpack.config.js index e236cfe34c7e..439b542c081a 100644 --- a/projects/plugins/protect/webpack.config.js +++ b/projects/plugins/protect/webpack.config.js @@ -22,11 +22,7 @@ module.exports = [ plugins: [ ...jetpackWebpackConfig.StandardPlugins( { DependencyExtractionPlugin: { - // Bundled, never one without the other — see #48173. - requestMap: { - '@wordpress/theme': { external: false }, - '@wordpress/private-apis': { external: false }, - }, + bundleWpUiDeps: true, }, } ), ],