Make WordPress Core


Ignore:
Timestamp:
04/11/2022 04:08:12 PM (5 years ago)
Author:
gziolo
Message:

Build: Enable React Fast Refresh for block development

Brings the same functionality introduced in the Gutenberg plugin with ​https://github.com/WordPress/gutenberg/pull/28273. In effect, it brings React Fast Refresh support to WordPress core for block development with @wordpress/scripts.

Props walbo, antonvlasenko.
See #51750, #55505.
Follow-up [53135].

File:
1 edited

Legend:

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

    r53124 r53140  
    213213
    214214        return $polyfill;
     215}
     216
     217/**
     218 * Registers development scripts that integrate with `@wordpress/scripts`.
     219 *
     220 * @see https://github.com/WordPress/gutenberg/tree/trunk/packages/scripts#start
     221 *
     222 * @since 6.0.0
     223 *
     224 * @param WP_Scripts $scripts WP_Scripts object.
     225 */
     226function wp_register_development_scripts( $scripts ) {
     227        if (
     228                ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG
     229                || empty( $scripts->registered['react'] )
     230        ) {
     231                return;
     232        }
     233
     234        $development_scripts = array(
     235                'react-refresh-entry',
     236                'react-refresh-runtime',
     237        );
     238
     239        foreach ( $development_scripts as $script_name ) {
     240                $assets = include ABSPATH . WPINC . '/assets/script-loader-' . $script_name . '.php';
     241                if ( ! is_array( $assets ) ) {
     242                        return;
     243                }
     244                $scripts->add(
     245                        'wp-' . $script_name,
     246                        '/wp-includes/js/dist/development/' . $script_name . '.js',
     247                        $assets['dependencies'],
     248                        $assets['version']
     249                );
     250        }
     251
     252        // See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react.
     253        $scripts->registered['react']->deps[] = 'wp-react-refresh-entry';
    215254}
    216255
    … …  
    561600function wp_default_packages( $scripts ) {
    562601        wp_default_packages_vendor( $scripts );
     602        wp_register_development_scripts( $scripts );
    563603        wp_register_tinymce_scripts( $scripts );
    564604        wp_default_packages_scripts( $scripts );
Note: See TracChangeset for help on using the changeset viewer.