Skip to content

Commit c363f1c

Browse files
authored
Build: Fix script_debug modules and scripts (WordPress#72485)
1 parent 44a00b6 commit c363f1c

File tree

5 files changed

+45
-48
lines changed

5 files changed

+45
-48
lines changed

bin/packages/build.mjs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ function getAllPackages() {
5757

5858
const PACKAGES = getAllPackages();
5959

60-
// Define global variables for feature flagging, matching webpack's DefinePlugin behavior
61-
const define = {
60+
const baseDefine = {
6261
'globalThis.IS_GUTENBERG_PLUGIN': JSON.stringify(
6362
Boolean( process.env.npm_package_config_IS_GUTENBERG_PLUGIN )
6463
),
6564
'globalThis.IS_WORDPRESS_CORE': JSON.stringify(
6665
Boolean( process.env.npm_package_config_IS_WORDPRESS_CORE )
6766
),
68-
'globalThis.SCRIPT_DEBUG': JSON.stringify(
69-
process.env.NODE_ENV === 'development'
70-
),
7167
};
68+
const getDefine = ( scriptDebug ) => ( {
69+
...baseDefine,
70+
'globalThis.SCRIPT_DEBUG': JSON.stringify( scriptDebug ),
71+
} );
7272

7373
/**
7474
* Create emotion babel plugin for esbuild.
@@ -561,14 +561,14 @@ async function bundlePackage( packageName ) {
561561
...baseConfig,
562562
outfile: path.join( outputDir, 'index.min.js' ),
563563
minify: true,
564-
define,
564+
define: getDefine( false ),
565565
plugins: bundlePlugins,
566566
} ),
567567
esbuild.build( {
568568
...baseConfig,
569569
outfile: path.join( outputDir, 'index.js' ),
570570
minify: false,
571-
define,
571+
define: getDefine( true ),
572572
plugins: bundlePlugins,
573573
} )
574574
);
@@ -596,7 +596,6 @@ async function bundlePackage( packageName ) {
596596
: exportName.replace( /^\.\//, '' );
597597
const entryPoint = path.join( packageDir, exportPath );
598598
const baseFileName = path.basename( fileName );
599-
600599
const modulePlugins = [
601600
wordpressExternalsPlugin( `${ baseFileName }.min`, 'esm' ),
602601
];
@@ -614,7 +613,22 @@ async function bundlePackage( packageName ) {
614613
target,
615614
platform: 'browser',
616615
minify: true,
617-
define,
616+
define: getDefine( false ),
617+
plugins: modulePlugins,
618+
} ),
619+
esbuild.build( {
620+
entryPoints: [ entryPoint ],
621+
outfile: path.join(
622+
rootBuildModuleDir,
623+
`${ fileName }.js`
624+
),
625+
bundle: true,
626+
sourcemap: true,
627+
format: 'esm',
628+
target,
629+
platform: 'browser',
630+
minify: false,
631+
define: getDefine( true ),
618632
plugins: modulePlugins,
619633
} )
620634
);
@@ -1115,7 +1129,9 @@ async function buildAll() {
11151129
const isBundled = await bundlePackage( packageName );
11161130
const buildTime = Date.now() - startBundleTime;
11171131
if ( isBundled ) {
1118-
console.log( ` ✔ Bundled ${ packageName } (${ buildTime }ms)` );
1132+
console.log(
1133+
` ✔ Bundled ${ packageName } (${ buildTime }ms)`
1134+
);
11191135
}
11201136
} )
11211137
);

lib/client-assets.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -628,18 +628,24 @@ function gutenberg_default_script_modules() {
628628
/*
629629
* Load individual asset files for esbuild-built packages.
630630
* Follows the same pattern as regular scripts in gutenberg_register_packages_scripts().
631-
* Uses RecursiveDirectoryIterator to find all *.min.js files at any nesting depth.
631+
* Uses RecursiveDirectoryIterator to find all *.js files at any nesting depth.
632+
* Only processes files matching the SCRIPT_DEBUG setting (either .js or .min.js).
632633
*/
634+
$extension = SCRIPT_DEBUG ? '.js' : '.min.js';
633635
$all_assets = array();
634636
$build_module_dir = gutenberg_dir_path() . 'build/modules';
635637
if ( is_dir( $build_module_dir ) ) {
636638
$iterator = new RecursiveIteratorIterator(
637639
new RecursiveDirectoryIterator( $build_module_dir, RecursiveDirectoryIterator::SKIP_DOTS )
638640
);
639641
foreach ( $iterator as $file ) {
640-
if ( $file->isFile() && preg_match( '/\.min\.js$/', $file->getFilename() ) ) {
641-
$path = $file->getPathname();
642-
$asset_file = substr( $path, 0, -3 ) . '.asset.php';
642+
if ( $file->isFile() && preg_match( '/\.(min\.)?js$/', $file->getFilename() ) ) {
643+
$path = $file->getPathname();
644+
if ( ! str_ends_with( $path, $extension ) ) {
645+
continue;
646+
}
647+
648+
$asset_file = substr( $path, 0, -strlen( $extension ) ) . '.min.asset.php';
643649
if ( ! file_exists( $asset_file ) ) {
644650
continue;
645651
}
@@ -656,28 +662,10 @@ function gutenberg_default_script_modules() {
656662
/*
657663
* Build the WordPress Script Module ID from the file name.
658664
* Prepend `@wordpress/` and remove extensions and `/index` if present:
659-
* - interactivity/index.min.js => @wordpress/interactivity
660-
* - interactivity/debug.min.js => @wordpress/interactivity/debug
661-
* - block-library/query/view.js => @wordpress/block-library/query/view
665+
* - interactivity/index.min.js or interactivity/index.js => @wordpress/interactivity
666+
* - block-library/query/view.min.js or block-library/query/view.js => @wordpress/block-library/query/view
662667
*/
663-
$script_module_id = '@wordpress/' . preg_replace( '~(?:/index)?\.min\.js$~D', '', $file_name, 1 );
664-
switch ( $script_module_id ) {
665-
/*
666-
* Interactivity exposes two entrypoints, "/index" and "/debug".
667-
* "/debug" should replace "/index" in development.
668-
*/
669-
case '@wordpress/interactivity/debug':
670-
if ( ! SCRIPT_DEBUG ) {
671-
continue 2;
672-
}
673-
$script_module_id = '@wordpress/interactivity';
674-
break;
675-
case '@wordpress/interactivity':
676-
if ( SCRIPT_DEBUG ) {
677-
continue 2;
678-
}
679-
break;
680-
}
668+
$script_module_id = '@wordpress/' . preg_replace( '~(?:/index)?\.(min\.)?js$~D', '', $file_name, 1 );
681669

682670
/*
683671
* All script modules in Gutenberg are (currently) related to the Interactivity API which prioritizes server-side rendering.

packages/interactivity/package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,16 @@
2222
"node": ">=18.12.0",
2323
"npm": ">=8.19.2"
2424
},
25-
"main": "build/index.js",
2625
"module": "build-module/index.js",
2726
"exports": {
2827
".": {
2928
"types": "./build-types/index.d.ts",
30-
"import": "./build-module/index.js",
31-
"require": "./build/index.js"
29+
"import": "./build-module/index.js"
3230
},
3331
"./package.json": "./package.json"
3432
},
3533
"react-native": "src/index",
36-
"wpScriptModuleExports": {
37-
".": "./build-module/index.js",
38-
"./debug": "./build-module/debug.js"
39-
},
34+
"wpScriptModuleExports": "./build-module/index.js",
4035
"types": "build-types",
4136
"dependencies": {
4237
"@preact/signals": "^1.3.0",

packages/interactivity/src/debug.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/interactivity/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
if ( globalThis.SCRIPT_DEBUG ) {
2+
await import( 'preact/debug' );
3+
}
4+
15
/**
26
* External dependencies
37
*/

0 commit comments

Comments
 (0)