Skip to content

Commit

Permalink
Scripts: Make React Fast Refresh work with multiple blocks (WordPress…
Browse files Browse the repository at this point in the history
…#64924)

* Scripts: Make React Fast Refresh Work with multiple blocks

* Properly pass the unique name for the build and updates test snapshots

* Apply suggestions from code review

* Add changelog entry

Co-authored-by: gziolo <[email protected]>
Co-authored-by: sirreal <[email protected]>
  • Loading branch information
3 people authored and im3dabasia committed Dec 4, 2024
1 parent b900be2 commit 6d0868b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
10 changes: 10 additions & 0 deletions packages/dependency-extraction-webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,16 @@ class DependencyExtractionWebpackPlugin {
assetData.type = 'module';
}

if ( compilation.options?.optimization?.runtimeChunk !== false ) {
// Sets the script handle for the shared runtime file so WordPress registers it only once when using the asset file.
assetData.handle =
compilation.name +
'-' +
chunkJSFile
.replace( /\\/g, '/' )
.replace( jsExtensionRegExp, '' );
}

if ( combineAssets ) {
combinedAssetsData[ chunkJSFile ] = assetData;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,17 @@ exports[`DependencyExtractionWebpackPlugin modules Webpack \`polyfill-magic-comm
exports[`DependencyExtractionWebpackPlugin modules Webpack \`polyfill-magic-comment-minified\` should produce expected output: External modules should match snapshot 1`] = `[]`;

exports[`DependencyExtractionWebpackPlugin modules Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'a.asset.php' should match snapshot 1`] = `
"<?php return array('dependencies' => array('@wordpress/blob'), 'version' => 'ee5ac21a1f0003d732e6', 'type' => 'module');
"<?php return array('dependencies' => array('@wordpress/blob'), 'version' => 'ee5ac21a1f0003d732e6', 'type' => 'module', 'handle' => 'runtime-chunk-single-modules-a');
"
`;

exports[`DependencyExtractionWebpackPlugin modules Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'b.asset.php' should match snapshot 1`] = `
"<?php return array('dependencies' => array('@wordpress/blob', 'lodash'), 'version' => '5b112b32c6db548c2997', 'type' => 'module');
"<?php return array('dependencies' => array('@wordpress/blob', 'lodash'), 'version' => '5b112b32c6db548c2997', 'type' => 'module', 'handle' => 'runtime-chunk-single-modules-b');
"
`;

exports[`DependencyExtractionWebpackPlugin modules Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'runtime.asset.php' should match snapshot 1`] = `
"<?php return array('dependencies' => array(), 'version' => 'b1ca4106075e0bd94f9c', 'type' => 'module');
"<?php return array('dependencies' => array(), 'version' => 'b1ca4106075e0bd94f9c', 'type' => 'module', 'handle' => 'runtime-chunk-single-modules-runtime');
"
`;

Expand Down Expand Up @@ -681,17 +681,17 @@ exports[`DependencyExtractionWebpackPlugin scripts Webpack \`polyfill-magic-comm
exports[`DependencyExtractionWebpackPlugin scripts Webpack \`polyfill-magic-comment-minified\` should produce expected output: External modules should match snapshot 1`] = `[]`;

exports[`DependencyExtractionWebpackPlugin scripts Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'a.asset.php' should match snapshot 1`] = `
"<?php return array('dependencies' => array('wp-blob'), 'version' => 'd091f1cbbf7603d6e12c');
"<?php return array('dependencies' => array('wp-blob'), 'version' => 'd091f1cbbf7603d6e12c', 'handle' => 'runtime-chunk-single-scripts-a');
"
`;

exports[`DependencyExtractionWebpackPlugin scripts Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'b.asset.php' should match snapshot 1`] = `
"<?php return array('dependencies' => array('lodash', 'wp-blob'), 'version' => '845bc6d4ffbdb9419ebd');
"<?php return array('dependencies' => array('lodash', 'wp-blob'), 'version' => '845bc6d4ffbdb9419ebd', 'handle' => 'runtime-chunk-single-scripts-b');
"
`;

exports[`DependencyExtractionWebpackPlugin scripts Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'runtime.asset.php' should match snapshot 1`] = `
"<?php return array('dependencies' => array(), 'version' => '717eb779e609d175a7dd');
"<?php return array('dependencies' => array(), 'version' => '717eb779e609d175a7dd', 'handle' => 'runtime-chunk-single-scripts-runtime');
"
`;

Expand Down
4 changes: 4 additions & 0 deletions packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fix

- Make React Fast Refresh in the `start` command work with multiple blocks ([64924](https://github.com/WordPress/gutenberg/pull/64924)).

## 30.6.0 (2024-11-27)

## 30.5.1 (2024-11-18)
Expand Down
29 changes: 28 additions & 1 deletion packages/scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const webpack = require( 'webpack' );
const browserslist = require( 'browserslist' );
const MiniCSSExtractPlugin = require( 'mini-css-extract-plugin' );
const { basename, dirname, resolve } = require( 'path' );
const { basename, dirname, relative, resolve, sep } = require( 'path' );
const ReactRefreshWebpackPlugin = require( '@pmmmwh/react-refresh-webpack-plugin' );
const RtlCssPlugin = require( 'rtlcss-webpack-plugin' );
const TerserPlugin = require( 'terser-webpack-plugin' );
Expand Down Expand Up @@ -115,6 +115,7 @@ const baseConfig = {
optimization: {
// Only concatenate modules in production, when not analyzing bundles.
concatenateModules: isProduction && ! process.env.WP_BUNDLE_ANALYZER,
runtimeChunk: hasReactFastRefresh && 'single',
splitChunks: {
cacheGroups: {
style: {
Expand Down Expand Up @@ -340,6 +341,32 @@ const scriptConfig = {
}
} );

if ( hasReactFastRefresh ) {
// Prepends the file reference to the shared runtime chunk to every script type defined for the block.
const runtimePath = relative(
dirname( absoluteFrom ),
fromProjectRoot(
getWordPressSrcDirectory() +
sep +
'runtime.js'
)
);
const fields =
getBlockJsonScriptFields( blockJson );
for ( const [ fieldName ] of Object.entries(
fields
) ) {
blockJson[ fieldName ] = [
`file:${ runtimePath }`,
...( Array.isArray(
blockJson[ fieldName ]
)
? blockJson[ fieldName ]
: [ blockJson[ fieldName ] ] ),
];
}
}

return JSON.stringify( blockJson, null, 2 );
}

Expand Down

0 comments on commit 6d0868b

Please sign in to comment.