Make WordPress Core


Ignore:
Timestamp:
01/15/2026 12:55:21 PM (10 months ago)
Author:
youknowriad
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/script-loader.php

    r61485 r61487  
    164164                'after'
    165165        );
     166}
     167
     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 */
     180function 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';
    166210}
    167211
    … …  
    619663function 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 );
Note: See TracChangeset for help on using the changeset viewer.