From b1f706591c976358b5c3b1f1ef6dc5ddfb506c9e Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Mon, 11 Apr 2022 15:39:06 +0200 Subject: [PATCH] Improve webpack configuration for the development scripts --- .../assets/development/react-refresh-entry.php | 1 + .../assets/development/react-refresh-runtime.php | 1 + src/wp-includes/script-loader.php | 12 +++++++++--- tools/webpack/development.js | 5 ++++- 4 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 src/wp-includes/assets/development/react-refresh-entry.php create mode 100644 src/wp-includes/assets/development/react-refresh-runtime.php diff --git a/src/wp-includes/assets/development/react-refresh-entry.php b/src/wp-includes/assets/development/react-refresh-entry.php new file mode 100644 index 0000000000000..f595680f32219 --- /dev/null +++ b/src/wp-includes/assets/development/react-refresh-entry.php @@ -0,0 +1 @@ + array('wp-react-refresh-runtime'), 'version' => '8151afc94a5ebc73b7a8229f0d7ee352'); \ No newline at end of file diff --git a/src/wp-includes/assets/development/react-refresh-runtime.php b/src/wp-includes/assets/development/react-refresh-runtime.php new file mode 100644 index 0000000000000..80fcd1cd2f87a --- /dev/null +++ b/src/wp-includes/assets/development/react-refresh-runtime.php @@ -0,0 +1 @@ + array(), 'version' => '4fb86f241c3b2d9d9e0411b507079823'); \ No newline at end of file diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index eeffee1d257aa..94d456ab644ce 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -224,7 +224,10 @@ function wp_get_script_polyfill( $scripts, $tests ) { * @param WP_Scripts $scripts WP_Scripts object. */ function wp_register_development_scripts( $scripts ) { - if ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) { + if ( + ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG + || empty( $scripts->registered['react'] ) + ) { return; } @@ -234,7 +237,10 @@ function wp_register_development_scripts( $scripts ) { ); foreach ( $development_scripts as $script_name ) { - $assets = include ABSPATH . WPINC . "/js/dist/development/$script_name.php"; + $assets = include ABSPATH . WPINC . "/assets/development/$script_name.php"; + if ( ! is_array( $assets ) ) { + return; + } $scripts->add( 'wp-' . $script_name, "/wp-includes/js/dist/development/$script_name.js", @@ -244,7 +250,7 @@ function wp_register_development_scripts( $scripts ) { } // See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react. - $scripts->query( 'react' )->deps[] = 'wp-react-refresh-entry'; + $scripts->registered['react']->deps[] = 'wp-react-refresh-entry'; } /** diff --git a/tools/webpack/development.js b/tools/webpack/development.js index c0fbd722bbaf0..82e5320eb7106 100644 --- a/tools/webpack/development.js +++ b/tools/webpack/development.js @@ -34,7 +34,9 @@ module.exports = function( env = { environment: 'production', buildTarget: false 'react-refresh-entry': '@pmmmwh/react-refresh-webpack-plugin/client/ReactRefreshEntry.js', }, - plugins: [ new DependencyExtractionWebpackPlugin() ], + plugins: [ new DependencyExtractionWebpackPlugin( { + outputFilename: '../../../assets/development/[name].php', + } ) ], }, { ...sharedConfig, @@ -51,6 +53,7 @@ module.exports = function( env = { environment: 'production', buildTarget: false plugins: [ new DependencyExtractionWebpackPlugin( { useDefaults: false, + outputFilename: '../../../assets/development/[name].php' } ), ], },