From 5456631235641adce78c9f74c30ad0c691e73a60 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 20 Sep 2024 11:04:42 +0200 Subject: [PATCH] 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}" );