From 03149f96b63c24b7810b029ab75b612318f16ae6 Mon Sep 17 00:00:00 2001 From: Stefan Fisk Date: Wed, 30 Dec 2020 10:41:17 +0100 Subject: [PATCH] Use entrypoint chunk instead of runtime chunk This results in the asset files being named after the entry points, instead of the runtime chunk when using `runtimeChunk = 'single'`. Fixes #24352 --- .../lib/index.js | 12 ++++++---- .../test/__snapshots__/build.js.snap | 22 +++++++++++++++++++ .../test/fixtures/runtime-chunk-single/a.js | 12 ++++++++++ .../test/fixtures/runtime-chunk-single/b.js | 11 ++++++++++ .../runtime-chunk-single/webpack.config.js | 15 +++++++++++++ 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/a.js create mode 100644 packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/b.js create mode 100644 packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/webpack.config.js diff --git a/packages/dependency-extraction-webpack-plugin/lib/index.js b/packages/dependency-extraction-webpack-plugin/lib/index.js index 047289b4855284..242a21fdc71abe 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/index.js +++ b/packages/dependency-extraction-webpack-plugin/lib/index.js @@ -196,12 +196,14 @@ class DependencyExtractionWebpackPlugin { } } - const runtimeChunk = entrypoint.getRuntimeChunk(); + const entrypointChunk = isWebpack4 + ? entrypoint.chunks.find( ( c ) => c.name === entrypointName ) + : entrypoint.getEntrypointChunk(); const assetData = { // Get a sorted array so we can produce a stable, stringified representation. dependencies: Array.from( entrypointExternalizedWpDeps ).sort(), - version: runtimeChunk.hash, + version: entrypointChunk.hash, }; const assetString = this.stringify( assetData ); @@ -211,7 +213,7 @@ class DependencyExtractionWebpackPlugin { const buildFilename = compilation.getPath( compiler.options.output.filename, { - chunk: runtimeChunk, + chunk: entrypointChunk, filename, query, basename: basename( filename ), @@ -233,7 +235,9 @@ class DependencyExtractionWebpackPlugin { // Add source and file into compilation for webpack to output. compilation.assets[ assetFilename ] = new RawSource( assetString ); - runtimeChunk.files[ isWebpack4 ? 'push' : 'add' ]( assetFilename ); + entrypointChunk.files[ isWebpack4 ? 'push' : 'add' ]( + assetFilename + ); } if ( combineAssets ) { diff --git a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap index 0fbd543b862644..df2e2b3425b5ce 100644 --- a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap +++ b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap @@ -144,6 +144,28 @@ Array [ ] `; +exports[`Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'a.asset.php' should match snapshot 1`] = `" array('wp-blob'), 'version' => '8f74c54ba0be0fd357a8594c9eda1f35');"`; + +exports[`Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'b.asset.php' should match snapshot 1`] = `" array('lodash', 'wp-blob'), 'version' => '4ad01639f1af1b6624a0d18cbb94e4e6');"`; + +exports[`Webpack \`runtime-chunk-single\` should produce expected output: External modules should match snapshot 1`] = ` +Array [ + Object { + "externalType": "window", + "request": "lodash", + "userRequest": "lodash", + }, + Object { + "externalType": "window", + "request": Array [ + "wp", + "blob", + ], + "userRequest": "@wordpress/blob", + }, +] +`; + exports[`Webpack \`wordpress\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = `" array('lodash', 'wp-blob'), 'version' => '844c4b1f2a6db3a13416e9ea339e6334');"`; exports[`Webpack \`wordpress\` should produce expected output: External modules should match snapshot 1`] = ` diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/a.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/a.js new file mode 100644 index 00000000000000..465018684ca426 --- /dev/null +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/a.js @@ -0,0 +1,12 @@ +/** + * WordPress dependencies + */ +import { isBlobURL } from '@wordpress/blob'; + +/** + * External dependencies + */ +import atob from 'atob'; + +isBlobURL( '' ); +atob( 'SGVsbG8sIFdvcmxkIQ==' ); diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/b.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/b.js new file mode 100644 index 00000000000000..917b4cd7e204bf --- /dev/null +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/b.js @@ -0,0 +1,11 @@ +/** + * WordPress dependencies + */ +import { isBlobURL } from '@wordpress/blob'; + +/** + * External dependencies + */ +import _ from 'lodash'; + +_.isEmpty( isBlobURL( '' ) ); diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/webpack.config.js new file mode 100644 index 00000000000000..e16f6b6b0fe70a --- /dev/null +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/runtime-chunk-single/webpack.config.js @@ -0,0 +1,15 @@ +/** + * Internal dependencies + */ +const DependencyExtractionWebpackPlugin = require( '../../..' ); + +module.exports = { + entry: { + a: './a', + b: './b', + }, + plugins: [ new DependencyExtractionWebpackPlugin() ], + optimization: { + runtimeChunk: 'single', + }, +};