Skip to content

Commit

Permalink
Use entrypoint chunk instead of runtime chunk
Browse files Browse the repository at this point in the history
This results in the asset files being named after the entry points,
instead of the runtime chunk when using `runtimeChunk = 'single'`.

Fixes #24352
  • Loading branch information
stefanfisk committed Nov 24, 2021
1 parent 051f187 commit 03149f9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/dependency-extraction-webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -211,7 +213,7 @@ class DependencyExtractionWebpackPlugin {
const buildFilename = compilation.getPath(
compiler.options.output.filename,
{
chunk: runtimeChunk,
chunk: entrypointChunk,
filename,
query,
basename: basename( filename ),
Expand All @@ -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 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,28 @@ Array [
]
`;

exports[`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' => '8f74c54ba0be0fd357a8594c9eda1f35');"`;

exports[`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' => '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`] = `"<?php return array('dependencies' => array('lodash', 'wp-blob'), 'version' => '844c4b1f2a6db3a13416e9ea339e6334');"`;

exports[`Webpack \`wordpress\` should produce expected output: External modules should match snapshot 1`] = `
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* WordPress dependencies
*/
import { isBlobURL } from '@wordpress/blob';

/**
* External dependencies
*/
import atob from 'atob';

isBlobURL( '' );
atob( 'SGVsbG8sIFdvcmxkIQ==' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* WordPress dependencies
*/
import { isBlobURL } from '@wordpress/blob';

/**
* External dependencies
*/
import _ from 'lodash';

_.isEmpty( isBlobURL( '' ) );
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Internal dependencies
*/
const DependencyExtractionWebpackPlugin = require( '../../..' );

module.exports = {
entry: {
a: './a',
b: './b',
},
plugins: [ new DependencyExtractionWebpackPlugin() ],
optimization: {
runtimeChunk: 'single',
},
};

0 comments on commit 03149f9

Please sign in to comment.