Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build: Enable React Fast Refresh for block development #2519

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ module.exports = function(grunt) {
'webpack-assets': [
WORKING_DIR + 'wp-includes/assets/*',
WORKING_DIR + 'wp-includes/css/dist/',
'!' + WORKING_DIR + 'wp-includes/assets/script-loader-packages.php'
'!' + WORKING_DIR + 'wp-includes/assets/script-loader-*.php'
],
dynamic: {
dot: true,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"last 2 Opera versions"
],
"devDependencies": {
"@pmmmwh/react-refresh-webpack-plugin": "0.5.5",
"@wordpress/babel-preset-default": "6.8.0",
"@wordpress/dependency-extraction-webpack-plugin": "3.4.1",
"@wordpress/e2e-test-utils": "5.4.10",
Expand Down Expand Up @@ -62,6 +63,7 @@
"matchdep": "~2.0.0",
"prettier": "npm:[email protected]",
"qunit": "~2.18.1",
"react-refresh": "0.10.0",
"sass": "^1.50.0",
"sinon": "~13.0.1",
"sinon-test": "~3.1.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('wp-react-refresh-runtime'), 'version' => '8151afc94a5ebc73b7a8229f0d7ee352');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array(), 'version' => '4fb86f241c3b2d9d9e0411b507079823');
40 changes: 40 additions & 0 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,45 @@ function wp_get_script_polyfill( $scripts, $tests ) {
return $polyfill;
}

/**
* Registers development scripts that integrate with `@wordpress/scripts`.
*
* @see https://github.com/WordPress/gutenberg/tree/trunk/packages/scripts#start
*
* @since 6.0.0
*
* @param WP_Scripts $scripts WP_Scripts object.
*/
function wp_register_development_scripts( $scripts ) {
if (
! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG
|| empty( $scripts->registered['react'] )
) {
return;
}

$development_scripts = array(
'react-refresh-entry',
'react-refresh-runtime',
);

foreach ( $development_scripts as $script_name ) {
$assets = include ABSPATH . WPINC . '/assets/script-loader-' . $script_name . '.php';
if ( ! is_array( $assets ) ) {
return;
}
$scripts->add(
'wp-' . $script_name,
gziolo marked this conversation as resolved.
Show resolved Hide resolved
'/wp-includes/js/dist/development/' . $script_name . '.js',
$assets['dependencies'],
$assets['version']
);
}

// See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react.
$scripts->registered['react']->deps[] = 'wp-react-refresh-entry';
}

/**
* Registers all the WordPress packages scripts that are in the standardized
* `js/dist/` location.
Expand Down Expand Up @@ -560,6 +599,7 @@ function wp_tinymce_inline_scripts() {
*/
function wp_default_packages( $scripts ) {
wp_default_packages_vendor( $scripts );
wp_register_development_scripts( $scripts );
wp_register_tinymce_scripts( $scripts );
wp_default_packages_scripts( $scripts );

Expand Down
61 changes: 61 additions & 0 deletions tools/webpack/development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* External dependencies
*/
const { join } = require( 'path' );

/**
* WordPress dependencies
*/
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );

const baseDir = join( __dirname, '../../' );

module.exports = function( env = { environment: 'production', buildTarget: false } ) {
const mode = env.environment;
const suffix = mode === 'production' ? '.min' : '';
let buildTarget = env.buildTarget ? env.buildTarget : ( mode === 'production' ? 'build' : 'src' );
buildTarget = buildTarget + '/wp-includes';

const sharedConfig = {
mode: 'development',
target: 'browserslist',
output: {
filename: `[name]${ suffix }.js`,
path: join( baseDir, `${ buildTarget }/js/dist/development` ),
},
};

// See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react.
return [
{
...sharedConfig,
name: 'react-refresh-entry',
entry: {
'react-refresh-entry':
'@pmmmwh/react-refresh-webpack-plugin/client/ReactRefreshEntry.js',
},
plugins: [ new DependencyExtractionWebpackPlugin( {
outputFilename: '../../../assets/script-loader-[name].php',
} ) ],
},
{
...sharedConfig,
name: 'react-refresh-runtime',
entry: {
'react-refresh-runtime': {
import: 'react-refresh/runtime.js',
library: {
name: 'ReactRefreshRuntime',
type: 'window',
},
},
},
plugins: [
new DependencyExtractionWebpackPlugin( {
useDefaults: false,
outputFilename: '../../../assets/script-loader-[name].php'
} ),
],
},
];
};
2 changes: 2 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const blocksConfig = require( './tools/webpack/blocks' );
const developmentConfig = require( './tools/webpack/development' );
const mediaConfig = require( './tools/webpack/media' );
const packagesConfig = require( './tools/webpack/packages' );

Expand All @@ -13,6 +14,7 @@ module.exports = function( env = { environment: "production", watch: false, buil

const config = [
blocksConfig( env ),
...developmentConfig( env ),
mediaConfig( env ),
packagesConfig( env ),
];
Expand Down