From 487a98230c34f4ab2d5233c390d5ec0b6755e1a5 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 19 Sep 2024 20:49:40 +0200 Subject: [PATCH 01/19] PICKME: Add build-module to GH actions --- .github/workflows/unit-test.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index c0f70070908c1c..fd4068db8b1140 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -135,7 +135,9 @@ jobs: uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3 with: name: build-assets - path: ./build/ + path: | + ./build/ + ./build-module/ test-php: name: PHP ${{ matrix.php }}${{ matrix.multisite && ' multisite' || '' }}${{ matrix.wordpress != '' && format( ' (WP {0}) ', matrix.wordpress ) || '' }} on ubuntu-latest @@ -212,7 +214,9 @@ jobs: uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 with: name: build-assets - path: ./build + path: | + ./build + ./build-module - name: Docker debug information run: | From 11c63fe7fc2471a772232a3e65935b8ddc5577d7 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 20 Sep 2024 11:51:52 +0200 Subject: [PATCH 02/19] Fix artifact download --- .github/workflows/unit-test.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index fd4068db8b1140..b80804b824e3cd 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -214,9 +214,6 @@ jobs: uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 with: name: build-assets - path: | - ./build - ./build-module - name: Docker debug information run: | From 44dd78f9d98ae759063e7024e25caa84fe47cad8 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 18 Sep 2024 16:40:04 +0200 Subject: [PATCH 03/19] Remove block-library view script module registration on render --- packages/block-library/src/file/index.php | 13 +---------- packages/block-library/src/image/index.php | 14 +----------- .../block-library/src/navigation/index.php | 13 +---------- packages/block-library/src/query/index.php | 22 +------------------ packages/block-library/src/search/index.php | 13 +---------- 5 files changed, 5 insertions(+), 70 deletions(-) diff --git a/packages/block-library/src/file/index.php b/packages/block-library/src/file/index.php index 85cc840201da59..8ea668d56d8545 100644 --- a/packages/block-library/src/file/index.php +++ b/packages/block-library/src/file/index.php @@ -19,18 +19,7 @@ function render_block_core_file( $attributes, $content ) { // If it's interactive, enqueue the script module and add the directives. if ( ! empty( $attributes['displayPreview'] ) ) { - $suffix = wp_scripts_get_suffix(); - if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) { - $module_url = gutenberg_url( '/build-module/block-library/file/view.min.js' ); - } - - wp_register_script_module( - '@wordpress/block-library/file', - isset( $module_url ) ? $module_url : includes_url( "blocks/file/view{$suffix}.js" ), - array( '@wordpress/interactivity' ), - defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ) - ); - wp_enqueue_script_module( '@wordpress/block-library/file' ); + wp_enqueue_script_module( '@wordpress/block-library/file/view' ); $processor = new WP_HTML_Tag_Processor( $content ); $processor->next_tag(); diff --git a/packages/block-library/src/image/index.php b/packages/block-library/src/image/index.php index abbb03c0952452..5d7815a1f2f3fb 100644 --- a/packages/block-library/src/image/index.php +++ b/packages/block-library/src/image/index.php @@ -70,19 +70,7 @@ function render_block_core_image( $attributes, $content, $block ) { isset( $lightbox_settings['enabled'] ) && true === $lightbox_settings['enabled'] ) { - $suffix = wp_scripts_get_suffix(); - if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) { - $module_url = gutenberg_url( '/build-module/block-library/image/view.min.js' ); - } - - wp_register_script_module( - '@wordpress/block-library/image', - isset( $module_url ) ? $module_url : includes_url( "blocks/image/view{$suffix}.js" ), - array( '@wordpress/interactivity' ), - defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ) - ); - - wp_enqueue_script_module( '@wordpress/block-library/image' ); + wp_enqueue_script_module( '@wordpress/block-library/image/view' ); /* * This render needs to happen in a filter with priority 15 to ensure that diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index ec72b03b6906f0..10fec84ed59d9c 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -622,18 +622,7 @@ private static function get_nav_element_directives( $is_interactive ) { */ private static function handle_view_script_module_loading( $attributes, $block, $inner_blocks ) { if ( static::is_interactive( $attributes, $inner_blocks ) ) { - $suffix = wp_scripts_get_suffix(); - if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) { - $module_url = gutenberg_url( '/build-module/block-library/navigation/view.min.js' ); - } - - wp_register_script_module( - '@wordpress/block-library/navigation', - isset( $module_url ) ? $module_url : includes_url( "blocks/navigation/view{$suffix}.js" ), - array( '@wordpress/interactivity' ), - defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ) - ); - wp_enqueue_script_module( '@wordpress/block-library/navigation' ); + wp_enqueue_script_module( '@wordpress/block-library/navigation/view' ); } } diff --git a/packages/block-library/src/query/index.php b/packages/block-library/src/query/index.php index d10db26529854e..043f351e11d7f1 100644 --- a/packages/block-library/src/query/index.php +++ b/packages/block-library/src/query/index.php @@ -24,27 +24,7 @@ function render_block_core_query( $attributes, $content, $block ) { // Enqueue the script module and add the necessary directives if the block is // interactive. if ( $is_interactive ) { - $suffix = wp_scripts_get_suffix(); - if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) { - $module_url = gutenberg_url( '/build-module/block-library/query/view.min.js' ); - } - - wp_register_script_module( - '@wordpress/block-library/query', - isset( $module_url ) ? $module_url : includes_url( "blocks/query/view{$suffix}.js" ), - array( - array( - 'id' => '@wordpress/interactivity', - 'import' => 'static', - ), - array( - 'id' => '@wordpress/interactivity-router', - 'import' => 'dynamic', - ), - ), - defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ) - ); - wp_enqueue_script_module( '@wordpress/block-library/query' ); + wp_enqueue_script_module( '@wordpress/block-library/query/view' ); $p = new WP_HTML_Tag_Processor( $content ); if ( $p->next_tag() ) { diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index fb09cdd36406e8..e4259bb0ce2c7f 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -80,18 +80,7 @@ function render_block_core_search( $attributes ) { // If it's interactive, enqueue the script module and add the directives. $is_expandable_searchfield = 'button-only' === $button_position; if ( $is_expandable_searchfield ) { - $suffix = wp_scripts_get_suffix(); - if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) { - $module_url = gutenberg_url( '/build-module/block-library/search/view.min.js' ); - } - - wp_register_script_module( - '@wordpress/block-library/search', - isset( $module_url ) ? $module_url : includes_url( "blocks/search/view{$suffix}.js" ), - array( '@wordpress/interactivity' ), - defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ) - ); - wp_enqueue_script_module( '@wordpress/block-library/search' ); + wp_enqueue_script_module( '@wordpress/block-library/search/view' ); $input->set_attribute( 'data-wp-bind--aria-hidden', '!context.isSearchInputVisible' ); $input->set_attribute( 'data-wp-bind--tabindex', 'state.tabindex' ); From 508cbba77613cdf915787464b20856819d03786e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 18 Sep 2024 16:46:42 +0200 Subject: [PATCH 04/19] Produce combined script modules assetfile --- tools/webpack/script-modules.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tools/webpack/script-modules.js b/tools/webpack/script-modules.js index 18287c96d83c8a..c548e1f4f91b48 100644 --- a/tools/webpack/script-modules.js +++ b/tools/webpack/script-modules.js @@ -89,11 +89,11 @@ module.exports = { }, output: { devtoolNamespace: 'wp', - filename: './build-module/[name].min.js', + filename: './[name].min.js', library: { type: 'module', }, - path: join( __dirname, '..', '..' ), + path: join( __dirname, '..', '..', 'build-module' ), environment: { module: true }, module: true, chunkFormat: 'module', @@ -102,7 +102,13 @@ module.exports = { resolve: { extensions: [ '.js', '.ts', '.tsx' ], }, - plugins: [ ...plugins, new DependencyExtractionWebpackPlugin() ], + plugins: [ + ...plugins, + new DependencyExtractionWebpackPlugin( { + combineAssets: true, + combinedOutputFile: `./assets.${ baseConfig.mode }.php`, + } ), + ], watchOptions: { ignored: [ '**/node_modules' ], aggregateTimeout: 500, From 00a196b74c9aefe4f212ef983d96e32ef9321fc8 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 18 Sep 2024 16:53:38 +0200 Subject: [PATCH 05/19] Fix script module webpack filenames --- tools/webpack/script-modules.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/webpack/script-modules.js b/tools/webpack/script-modules.js index c548e1f4f91b48..e93edb43167a4b 100644 --- a/tools/webpack/script-modules.js +++ b/tools/webpack/script-modules.js @@ -89,7 +89,9 @@ module.exports = { }, output: { devtoolNamespace: 'wp', - filename: './[name].min.js', + filename: `[name]${ + baseConfig.mode === 'production' ? '.min' : '' + }.js`, library: { type: 'module', }, From a148c74c0d2c4f39070e8f5cb41986e7e9a6f376 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 18 Sep 2024 16:55:15 +0200 Subject: [PATCH 06/19] Remove specific interactivity script module registration --- lib/interactivity-api.php | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/lib/interactivity-api.php b/lib/interactivity-api.php index c00d68bc70e8e2..e543793aa5263f 100644 --- a/lib/interactivity-api.php +++ b/lib/interactivity-api.php @@ -9,32 +9,7 @@ * Deregisters the Core Interactivity API Modules and replace them * with the ones from the Gutenberg plugin. */ -function gutenberg_reregister_interactivity_script_modules() { - $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time(); - wp_deregister_script_module( '@wordpress/interactivity' ); - wp_deregister_script_module( '@wordpress/interactivity-router' ); - - wp_register_script_module( - '@wordpress/interactivity', - gutenberg_url( '/build-module/' . ( SCRIPT_DEBUG ? 'interactivity/debug.min.js' : 'interactivity/index.min.js' ) ), - array(), - $default_version - ); - - wp_register_script_module( - '@wordpress/interactivity-router', - gutenberg_url( '/build-module/interactivity-router/index.min.js' ), - array( - array( - 'id' => '@wordpress/a11y', - 'import' => 'dynamic', - ), - '@wordpress/interactivity', - ), - $default_version - ); -} -add_action( 'init', 'gutenberg_reregister_interactivity_script_modules' ); +function gutenberg_reregister_interactivity_script_modules() {} /** * Adds script data to the interactivity-router script module. From 5015d56de26f777cec63cfeb43e4d5e2d7dcd089 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 18 Sep 2024 17:03:23 +0200 Subject: [PATCH 07/19] Handle re-registration of core script modules --- lib/experimental/script-modules.php | 53 +++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index f65bc1704dd890..8c94f93b17f901 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -246,10 +246,6 @@ function gutenberg_a11y_script_module_html() { * @since 19.3 */ function gutenberg_register_script_modules() { - // When in production, use the plugin's version as the default asset version; - // else (for development or test) default to use the current time. - $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time(); - wp_deregister_script_module( '@wordpress/a11y' ); wp_register_script_module( '@wordpress/a11y', @@ -258,7 +254,52 @@ function gutenberg_register_script_modules() { $default_version ); - add_action( 'wp_footer', 'gutenberg_a11y_script_module_html' ); - add_action( 'admin_footer', 'gutenberg_a11y_script_module_html' ); + $suffix = defined( 'WP_RUN_CORE_TESTS' ) ? '.min' : wp_scripts_get_suffix(); + + + /* + * Expects multidimensional array like: + * + * 'interactivity/index.min.js' => array('dependencies' => array(…), 'version' => '…'), + * 'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'), + * 'interactivity-router/index.min.js' => … + */ + $assets = include gutenberg_dir_path( 'build-module/assets.production.php' ); + + foreach ( $assets as $file_name => $script_module_data ) { + $package_name = dirname( $file_name ); + $package_sub_name = basename( $file_name, "{$suffix}.js" ); + + switch ( $package_name ) { + /* + * Interactivity exposes two entrypoints, `/index` and `/debug`. + * They're the production and development versions of the package. + */ + case 'interactivity': + if ( SCRIPT_DEBUG ) { + if ( 'index' === $package_sub_name ) { + continue 2; + } + } else { + if ( 'debug' === $package_sub_name ) { + continue 2; + } + } + $script_module_id = '@wordpress/interactivity'; + break; + + default: + $script_module_id = 'index' === $package_sub_name ? + "@wordpress/{$package_name}" : + "@wordpress/{$package_name}/{$package_sub_name}"; + } + + $path = gutenberg_url( "build-module/{$file_name}" ); + wp_deregister_style( $script_module_id ); + wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] ); + } } + add_action( 'init', 'gutenberg_register_script_modules' ); +add_action( 'wp_footer', 'gutenberg_a11y_script_module_html' ); +add_action( 'admin_footer', 'gutenberg_a11y_script_module_html' ); From b0b3e7229338a8e4dd0adb2d67c332685a06df51 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 18 Sep 2024 17:52:48 +0200 Subject: [PATCH 08/19] Fix script module re-registration --- lib/experimental/script-modules.php | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index 8c94f93b17f901..9cc1c535316f8a 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -246,17 +246,6 @@ function gutenberg_a11y_script_module_html() { * @since 19.3 */ function gutenberg_register_script_modules() { - wp_deregister_script_module( '@wordpress/a11y' ); - wp_register_script_module( - '@wordpress/a11y', - gutenberg_url( 'build-module/a11y/index.min.js' ), - array(), - $default_version - ); - - $suffix = defined( 'WP_RUN_CORE_TESTS' ) ? '.min' : wp_scripts_get_suffix(); - - /* * Expects multidimensional array like: * @@ -264,11 +253,11 @@ function gutenberg_register_script_modules() { * 'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'), * 'interactivity-router/index.min.js' => … */ - $assets = include gutenberg_dir_path( 'build-module/assets.production.php' ); + $assets = include gutenberg_dir_path() . '/build-module/assets.production.php'; foreach ( $assets as $file_name => $script_module_data ) { $package_name = dirname( $file_name ); - $package_sub_name = basename( $file_name, "{$suffix}.js" ); + $package_sub_name = basename( $file_name, '.min.js' ); switch ( $package_name ) { /* @@ -295,11 +284,10 @@ function gutenberg_register_script_modules() { } $path = gutenberg_url( "build-module/{$file_name}" ); - wp_deregister_style( $script_module_id ); + wp_deregister_script_module( $script_module_id ); wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] ); } } - -add_action( 'init', 'gutenberg_register_script_modules' ); +add_action( 'after_setup_theme', 'gutenberg_register_script_modules', 20 ); add_action( 'wp_footer', 'gutenberg_a11y_script_module_html' ); add_action( 'admin_footer', 'gutenberg_a11y_script_module_html' ); From 370f2e6e75d6a8b8a41f15d7495f9b60966f0e96 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 19 Sep 2024 13:38:07 +0200 Subject: [PATCH 09/19] Move script module registration out of experimental --- lib/client-assets.php | 49 ++++++++++++++++++++++++++++ lib/experimental/script-modules.php | 50 ----------------------------- 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 62e874d6b06c82..de344fe33f26c5 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -601,6 +601,55 @@ function gutenberg_register_vendor_scripts( $scripts ) { } add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' ); +/** + * Registers or re-registers Gutenberg Script Modules. + * + * Script modules that are registered by Core will be re-registered by Gutenberg. + */ +function gutenberg_register_script_modules() { + /* + * Expects multidimensional array like: + * + * 'interactivity/index.min.js' => array('dependencies' => array(…), 'version' => '…'), + * 'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'), + * 'interactivity-router/index.min.js' => … + */ + $assets = include gutenberg_dir_path() . '/build-module/assets.production.php'; + + foreach ( $assets as $file_name => $script_module_data ) { + $package_name = dirname( $file_name ); + $package_sub_name = basename( $file_name, '.min.js' ); + + switch ( $package_name ) { + /* + * Interactivity exposes two entrypoints, `/index` and `/debug`. + * They're the production and development versions of the package. + */ + case 'interactivity': + if ( SCRIPT_DEBUG ) { + if ( 'index' === $package_sub_name ) { + continue 2; + } + } else { + if ( 'debug' === $package_sub_name ) { + continue 2; + } + } + $script_module_id = '@wordpress/interactivity'; + break; + + default: + $script_module_id = 'index' === $package_sub_name ? + "@wordpress/{$package_name}" : + "@wordpress/{$package_name}/{$package_sub_name}"; + } + + $path = gutenberg_url( "build-module/{$file_name}" ); + wp_deregister_script_module( $script_module_id ); + wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] ); + } +} +add_action( 'after_setup_theme', 'gutenberg_register_script_modules', 20 ); /* * Always remove the Core action hook while gutenberg_enqueue_stored_styles() exists to avoid styles being printed twice. diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index 9cc1c535316f8a..fe23786fc03621 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -239,55 +239,5 @@ function gutenberg_a11y_script_module_html() { . '
' . ''; } - -/** - * Registers Gutenberg Script Modules. - * - * @since 19.3 - */ -function gutenberg_register_script_modules() { - /* - * Expects multidimensional array like: - * - * 'interactivity/index.min.js' => array('dependencies' => array(…), 'version' => '…'), - * 'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'), - * 'interactivity-router/index.min.js' => … - */ - $assets = include gutenberg_dir_path() . '/build-module/assets.production.php'; - - foreach ( $assets as $file_name => $script_module_data ) { - $package_name = dirname( $file_name ); - $package_sub_name = basename( $file_name, '.min.js' ); - - switch ( $package_name ) { - /* - * Interactivity exposes two entrypoints, `/index` and `/debug`. - * They're the production and development versions of the package. - */ - case 'interactivity': - if ( SCRIPT_DEBUG ) { - if ( 'index' === $package_sub_name ) { - continue 2; - } - } else { - if ( 'debug' === $package_sub_name ) { - continue 2; - } - } - $script_module_id = '@wordpress/interactivity'; - break; - - default: - $script_module_id = 'index' === $package_sub_name ? - "@wordpress/{$package_name}" : - "@wordpress/{$package_name}/{$package_sub_name}"; - } - - $path = gutenberg_url( "build-module/{$file_name}" ); - wp_deregister_script_module( $script_module_id ); - wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] ); - } -} -add_action( 'after_setup_theme', 'gutenberg_register_script_modules', 20 ); add_action( 'wp_footer', 'gutenberg_a11y_script_module_html' ); add_action( 'admin_footer', 'gutenberg_a11y_script_module_html' ); From 1bad15c5c371d239fd7d8669aad58162b2412b08 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 19 Sep 2024 13:53:06 +0200 Subject: [PATCH 10/19] Just use assets.php --- lib/client-assets.php | 2 +- tools/webpack/script-modules.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index de344fe33f26c5..5f25fa6c81c357 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -614,7 +614,7 @@ function gutenberg_register_script_modules() { * 'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'), * 'interactivity-router/index.min.js' => … */ - $assets = include gutenberg_dir_path() . '/build-module/assets.production.php'; + $assets = include gutenberg_dir_path() . '/build-module/assets.php'; foreach ( $assets as $file_name => $script_module_data ) { $package_name = dirname( $file_name ); diff --git a/tools/webpack/script-modules.js b/tools/webpack/script-modules.js index e93edb43167a4b..592a06116ed0f0 100644 --- a/tools/webpack/script-modules.js +++ b/tools/webpack/script-modules.js @@ -108,7 +108,7 @@ module.exports = { ...plugins, new DependencyExtractionWebpackPlugin( { combineAssets: true, - combinedOutputFile: `./assets.${ baseConfig.mode }.php`, + combinedOutputFile: `./assets.php`, } ), ], watchOptions: { From cb3a31b2c70525d32b3b69f006052a5624c590b2 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 19 Sep 2024 13:55:02 +0200 Subject: [PATCH 11/19] Replace WP modules action with GB modules Avoid registering modules twice. Anticipates changes in https://github.com/WordPress/wordpress-develop/pull/7360 --- lib/client-assets.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 5f25fa6c81c357..ed1d2ee4eb7d07 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -606,7 +606,7 @@ function gutenberg_register_vendor_scripts( $scripts ) { * * Script modules that are registered by Core will be re-registered by Gutenberg. */ -function gutenberg_register_script_modules() { +function gutenberg_default_script_modules() { /* * Expects multidimensional array like: * @@ -645,11 +645,11 @@ function gutenberg_register_script_modules() { } $path = gutenberg_url( "build-module/{$file_name}" ); - wp_deregister_script_module( $script_module_id ); wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] ); } } -add_action( 'after_setup_theme', 'gutenberg_register_script_modules', 20 ); +remove_action( 'wp_default_scripts', 'wp_default_script_modules' ); +add_action( 'wp_default_scripts', 'gutenberg_register_script_modules' ); /* * Always remove the Core action hook while gutenberg_enqueue_stored_styles() exists to avoid styles being printed twice. From 476183a7090ad3c31c3bf19e5be04197b027e35e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 19 Sep 2024 14:22:11 +0200 Subject: [PATCH 12/19] Update function name, add deprecation note --- lib/client-assets.php | 4 +++- lib/interactivity-api.php | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index ed1d2ee4eb7d07..53c59409c99478 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -605,6 +605,8 @@ function gutenberg_register_vendor_scripts( $scripts ) { * Registers or re-registers Gutenberg Script Modules. * * Script modules that are registered by Core will be re-registered by Gutenberg. + * + * @since 19.3.0 */ function gutenberg_default_script_modules() { /* @@ -649,7 +651,7 @@ function gutenberg_default_script_modules() { } } remove_action( 'wp_default_scripts', 'wp_default_script_modules' ); -add_action( 'wp_default_scripts', 'gutenberg_register_script_modules' ); +add_action( 'wp_default_scripts', 'gutenberg_default_script_modules' ); /* * Always remove the Core action hook while gutenberg_enqueue_stored_styles() exists to avoid styles being printed twice. diff --git a/lib/interactivity-api.php b/lib/interactivity-api.php index e543793aa5263f..d8780f3ba0c9c3 100644 --- a/lib/interactivity-api.php +++ b/lib/interactivity-api.php @@ -8,6 +8,8 @@ /** * Deregisters the Core Interactivity API Modules and replace them * with the ones from the Gutenberg plugin. + * + * @deprecated 19.3.0 Script module registration is handled by {@see gutenberg_default_script_modules()}. */ function gutenberg_reregister_interactivity_script_modules() {} From 3f457a5dc22525446d38cfe8f448ebc67d1bf80f Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 19 Sep 2024 14:37:21 +0200 Subject: [PATCH 13/19] Properly trim .min.js and .js --- lib/client-assets.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 53c59409c99478..293d87897fe8e7 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -619,8 +619,9 @@ function gutenberg_default_script_modules() { $assets = include gutenberg_dir_path() . '/build-module/assets.php'; foreach ( $assets as $file_name => $script_module_data ) { - $package_name = dirname( $file_name ); - $package_sub_name = basename( $file_name, '.min.js' ); + $package_name = dirname( $file_name ); + // Account for both `[filename].min.js` and `[filename].js` + $package_sub_name = basename( basename( $file_name, '.js' ), '.min' ); switch ( $package_name ) { /* From 6759367d5ce0f5bb8d25514e13e816ace38138c4 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 20 Sep 2024 10:26:10 +0200 Subject: [PATCH 14/19] Remove unused filter function --- lib/interactivity-api.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/lib/interactivity-api.php b/lib/interactivity-api.php index d8780f3ba0c9c3..ff68936f054a7e 100644 --- a/lib/interactivity-api.php +++ b/lib/interactivity-api.php @@ -5,14 +5,6 @@ * @package gutenberg */ -/** - * Deregisters the Core Interactivity API Modules and replace them - * with the ones from the Gutenberg plugin. - * - * @deprecated 19.3.0 Script module registration is handled by {@see gutenberg_default_script_modules()}. - */ -function gutenberg_reregister_interactivity_script_modules() {} - /** * Adds script data to the interactivity-router script module. * From ff352fc8be525eb2a87b96ea667fafc643135779 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 20 Sep 2024 11:04:42 +0200 Subject: [PATCH 15/19] Simplify module id creation and special casing --- lib/client-assets.php | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 293d87897fe8e7..cbedc0ed667187 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -619,32 +619,30 @@ function gutenberg_default_script_modules() { $assets = include gutenberg_dir_path() . '/build-module/assets.php'; foreach ( $assets as $file_name => $script_module_data ) { - $package_name = dirname( $file_name ); - // Account for both `[filename].min.js` and `[filename].js` - $package_sub_name = basename( basename( $file_name, '.js' ), '.min' ); - - switch ( $package_name ) { + /* + * Build the WordPress Script Module ID from the file name. + * Prepend `@wordpress/` and remove extensions and `/index` if present: + * - interactivity/index.min.js => @wordpress/interactivity + * - interactivity/debug.min.js => @wordpress/interactivity/debug + * - block-library/query/view.js => @wordpress/block-library/query/view + */ + $script_module_id = '@wordpress/' . preg_replace( '~(?:/index)?(?:\.min)?\.js$~iD', '', $file_name, 1 ); + switch ( $script_module_id ) { /* * Interactivity exposes two entrypoints, `/index` and `/debug`. - * They're the production and development versions of the package. + * `/debug` should replalce `/index` in devlopment. */ - case 'interactivity': - if ( SCRIPT_DEBUG ) { - if ( 'index' === $package_sub_name ) { - continue 2; - } - } else { - if ( 'debug' === $package_sub_name ) { - continue 2; - } + case '@wordpress/interactivity/debug': + if ( ! SCRIPT_DEBUG ) { + continue 2; } $script_module_id = '@wordpress/interactivity'; break; - - default: - $script_module_id = 'index' === $package_sub_name ? - "@wordpress/{$package_name}" : - "@wordpress/{$package_name}/{$package_sub_name}"; + case '@wordpress/interactivity': + if ( SCRIPT_DEBUG ) { + continue 2; + } + break; } $path = gutenberg_url( "build-module/{$file_name}" ); From 23bff877572171020cef6bb5978470c10c4f57e4 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 20 Sep 2024 11:08:59 +0200 Subject: [PATCH 16/19] Add core-backport file --- backport-changelog/6.7/7360.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 backport-changelog/6.7/7360.md diff --git a/backport-changelog/6.7/7360.md b/backport-changelog/6.7/7360.md new file mode 100644 index 00000000000000..b2fb8efd624b93 --- /dev/null +++ b/backport-changelog/6.7/7360.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/7360 + +* https://github.com/WordPress/gutenberg/pull/65460 From b8e11bc8f2a39b5811c171221e00b4407ccff48e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 20 Sep 2024 11:42:10 +0200 Subject: [PATCH 17/19] Use .min.js for webpack filename extension Readable .js is handled by webpack plugin. --- tools/webpack/script-modules.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/webpack/script-modules.js b/tools/webpack/script-modules.js index 592a06116ed0f0..021f11f5f5ed95 100644 --- a/tools/webpack/script-modules.js +++ b/tools/webpack/script-modules.js @@ -89,9 +89,7 @@ module.exports = { }, output: { devtoolNamespace: 'wp', - filename: `[name]${ - baseConfig.mode === 'production' ? '.min' : '' - }.js`, + filename: '[name].min.js', library: { type: 'module', }, From 44da0b0caee077fdc5318ebaa8a66eb33c42847b Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 20 Sep 2024 12:37:21 +0200 Subject: [PATCH 18/19] Use non-minified assets with SCRIPT_DEBUG --- lib/client-assets.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index cbedc0ed667187..3d89ab46b950b4 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -626,11 +626,11 @@ function gutenberg_default_script_modules() { * - interactivity/debug.min.js => @wordpress/interactivity/debug * - block-library/query/view.js => @wordpress/block-library/query/view */ - $script_module_id = '@wordpress/' . preg_replace( '~(?:/index)?(?:\.min)?\.js$~iD', '', $file_name, 1 ); + $script_module_id = '@wordpress/' . preg_replace( '~(?:/index)?\.min\.js$~D', '', $file_name, 1 ); switch ( $script_module_id ) { /* - * Interactivity exposes two entrypoints, `/index` and `/debug`. - * `/debug` should replalce `/index` in devlopment. + * Interactivity exposes two entrypoints, "/index" and "/debug". + * "/debug" should replalce "/index" in devlopment. */ case '@wordpress/interactivity/debug': if ( ! SCRIPT_DEBUG ) { @@ -645,6 +645,10 @@ function gutenberg_default_script_modules() { break; } + if ( SCRIPT_DEBUG ) { + // Replave ".min.js" with ".js" in devlopment. + $file_name = substr( $file_name, 0, -7 ) . '.js'; + } $path = gutenberg_url( "build-module/{$file_name}" ); wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] ); } From e99b96e5b2dc288cead10c621bd6911a71242b3b Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 20 Sep 2024 12:50:05 +0200 Subject: [PATCH 19/19] Remove usage of non-min .js files in Script Debug --- lib/client-assets.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 3d89ab46b950b4..2343530e5595a7 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -645,10 +645,6 @@ function gutenberg_default_script_modules() { break; } - if ( SCRIPT_DEBUG ) { - // Replave ".min.js" with ".js" in devlopment. - $file_name = substr( $file_name, 0, -7 ) . '.js'; - } $path = gutenberg_url( "build-module/{$file_name}" ); wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] ); }