Skip to content

Commit e34ed7c

Browse files
committed
Build/Test Tools: Restore React Refresh scripts for hot reloading.
Restores the `wp_register_development_scripts()` function and associated build infrastructure to enable hot module replacement (HMR) when using `@wordpress/scripts` with the `--hot` flag. The React Refresh scripts were removed in [61438] as part of the Gutenberg build restructuring, but they are still needed for plugin developers using `wp-scripts start --hot` for block development. Props jsnajdr, wildworks, manzoorwanijk. See #64393. git-svn-id: https://develop.svn.wordpress.org/trunk@61487 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 101a21f commit e34ed7c

File tree

5 files changed

+177
-16
lines changed

5 files changed

+177
-16
lines changed

package-lock.json

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
],
2929
"devDependencies": {
3030
"@lodder/grunt-postcss": "^3.1.1",
31+
"@pmmmwh/react-refresh-webpack-plugin": "0.6.1",
3132
"@playwright/test": "1.56.1",
3233
"@wordpress/e2e-test-utils-playwright": "1.33.2",
3334
"@wordpress/prettier-config": "4.33.1",
@@ -61,6 +62,7 @@
6162
"postcss": "8.5.6",
6263
"prettier": "npm:[email protected]",
6364
"qunit": "~2.24.2",
65+
"react-refresh": "0.14.0",
6466
"sass": "1.94.0",
6567
"sinon": "16.1.3",
6668
"sinon-test": "~3.1.6",

src/wp-includes/deprecated.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6479,17 +6479,3 @@ function wp_print_auto_sizes_contain_css_fix() {
64796479
<style>img:is([sizes="auto" i], [sizes^="auto," i]) { contain-intrinsic-size: 3000px 1500px }</style>
64806480
<?php
64816481
}
6482-
6483-
/**
6484-
* Registers development scripts that integrate with `@wordpress/scripts`.
6485-
*
6486-
* @see https://github.com/WordPress/gutenberg/tree/trunk/packages/scripts#start
6487-
*
6488-
* @since 6.0.0
6489-
* @deprecated 7.0.0 Obsolete due to a change in how Gutenberg is included in Core. See #64393.
6490-
*
6491-
* @param WP_Scripts $scripts WP_Scripts object.
6492-
*/
6493-
function wp_register_development_scripts( $scripts ) {
6494-
_deprecated_function( __FUNCTION__, '7.0.0' );
6495-
}

src/wp-includes/script-loader.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,50 @@ function wp_default_packages_vendor( $scripts ) {
165165
);
166166
}
167167

168+
/**
169+
* Registers development scripts that integrate with `@wordpress/scripts`.
170+
*
171+
* These scripts enable hot module replacement (HMR) for block development
172+
* when using `wp-scripts start --hot`.
173+
*
174+
* @see https://github.com/WordPress/gutenberg/tree/trunk/packages/scripts#start
175+
*
176+
* @since 6.0.0
177+
*
178+
* @param WP_Scripts $scripts WP_Scripts object.
179+
*/
180+
function wp_register_development_scripts( $scripts ) {
181+
if (
182+
! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG
183+
|| empty( $scripts->registered['react'] )
184+
|| defined( 'WP_RUN_CORE_TESTS' )
185+
) {
186+
return;
187+
}
188+
189+
// React Refresh runtime - exposes ReactRefreshRuntime global.
190+
// No dependencies.
191+
$scripts->add(
192+
'wp-react-refresh-runtime',
193+
'/wp-includes/js/dist/development/react-refresh-runtime.js',
194+
array(),
195+
'0.14.0'
196+
);
197+
198+
// React Refresh entry - injects runtime into global hook.
199+
// Must load before React to set up hooks.
200+
$scripts->add(
201+
'wp-react-refresh-entry',
202+
'/wp-includes/js/dist/development/react-refresh-entry.js',
203+
array( 'wp-react-refresh-runtime' ),
204+
'0.14.0'
205+
);
206+
207+
// Add entry as a dependency of React so it loads first.
208+
// See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react.
209+
$scripts->registered['react']->deps[] = 'wp-react-refresh-entry';
210+
}
211+
168212
/**
169213
* Returns contents of an inline script used in appending polyfill scripts for
170214
* browsers which fail the provided tests. The provided array is a mapping from
@@ -618,6 +662,7 @@ function wp_tinymce_inline_scripts() {
618662
*/
619663
function wp_default_packages( $scripts ) {
620664
wp_default_packages_vendor( $scripts );
665+
wp_register_development_scripts( $scripts );
621666
wp_register_tinymce_scripts( $scripts );
622667
wp_default_packages_scripts( $scripts );
623668

webpack.config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const mediaConfig = require( './tools/webpack/media' );
2+
const developmentConfig = require( './tools/webpack/development' );
23

34
module.exports = function (
45
env = { environment: 'production', watch: false, buildTarget: false }
@@ -11,10 +12,13 @@ module.exports = function (
1112
env.buildTarget = env.mode === 'production' ? 'build/' : 'src/';
1213
}
1314

14-
// Only building Core-specific media files.
15+
// Only building Core-specific media files and development scripts.
1516
// Blocks, packages, script modules, and vendors are now sourced from
1617
// the Gutenberg build (see tools/gutenberg/copy-gutenberg-build.js).
17-
const config = [ mediaConfig( env ) ];
18+
const config = [
19+
mediaConfig( env ),
20+
developmentConfig( env ),
21+
];
1822

1923
return config;
2024
};

0 commit comments

Comments
 (0)