Make WordPress Core

Changeset 53140


Ignore:
Timestamp:
04/11/2022 04:08:12 PM (2 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].

Location:
trunk
Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Gruntfile.js

    r52657 r53140  
    125125                WORKING_DIR + 'wp-includes/assets/*',
    126126                WORKING_DIR + 'wp-includes/css/dist/',
    127                 '!' + WORKING_DIR + 'wp-includes/assets/script-loader-packages.php'
     127                '!' + WORKING_DIR + 'wp-includes/assets/script-loader-*.php'
    128128            ],
    129129            dynamic: {
  • trunk/package.json

    r53135 r53140  
    2525    ],
    2626    "devDependencies": {
     27        "@pmmmwh/react-refresh-webpack-plugin": "0.5.5",
    2728        "@wordpress/babel-preset-default": "6.8.0",
    2829        "@wordpress/dependency-extraction-webpack-plugin": "3.4.1",
     
    6364        "prettier": "npm:wp-prettier@2.0.5",
    6465        "qunit": "~2.18.1",
     66        "react-refresh": "0.10.0",
    6567        "sass": "^1.50.0",
    6668        "sinon": "~13.0.1",
  • 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 );
  • trunk/webpack.config.js

    r51501 r53140  
    11const blocksConfig = require( './tools/webpack/blocks' );
     2const developmentConfig = require( './tools/webpack/development' );
    23const mediaConfig = require( './tools/webpack/media' );
    34const packagesConfig = require( './tools/webpack/packages' );
     
    1415    const config = [
    1516        blocksConfig( env ),
     17        ...developmentConfig( env ),
    1618        mediaConfig( env ),
    1719        packagesConfig( env ),
Note: See TracChangeset for help on using the changeset viewer.