From 316b936f35eb8c1ed3d52837dc30cd526c7fe9d6 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Fri, 3 Jun 2022 12:04:02 +0100 Subject: [PATCH] Compute content hash from all chunk assets --- .../lib/index.js | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/packages/dependency-extraction-webpack-plugin/lib/index.js b/packages/dependency-extraction-webpack-plugin/lib/index.js index 543d924869fe90..4e18b4e9bb09c6 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/index.js +++ b/packages/dependency-extraction-webpack-plugin/lib/index.js @@ -173,11 +173,11 @@ class DependencyExtractionWebpackPlugin { // Process each entrypoint chunk independently for ( const chunk of entrypointChunks ) { - const chunkFilename = Array.from( chunk.files ).find( ( f ) => - /\.js$/i.test( f ) - ); - if ( ! chunkFilename ) { - // There's no JS file in this chunk, there's no work for us. + const chunkFiles = Array.from( chunk.files ); + + const chunkJSFile = chunkFiles.find( ( f ) => /\.js$/i.test( f ) ); + if ( ! chunkJSFile ) { + // There's no JS file in this chunk, no work for us. Typically a `style.css` from cache group. continue; } @@ -217,8 +217,12 @@ class DependencyExtractionWebpackPlugin { hashDigest, hashDigestLength, } = compilation.outputOptions; - const contentHash = createHash( hashFunction ) - .update( compilation.getAsset( chunkFilename ).source.buffer() ) + + const contentHash = chunkFiles + .reduce( ( hash, filename ) => { + const asset = compilation.getAsset( filename ); + return hash.update( asset.source.buffer() ); + }, createHash( hashFunction ) ) .digest( hashDigest ) .slice( 0, hashDigestLength ); @@ -229,21 +233,24 @@ class DependencyExtractionWebpackPlugin { }; if ( combineAssets ) { - combinedAssetsData[ chunkFilename ] = assetData; + combinedAssetsData[ chunkJSFile ] = assetData; continue; } - const assetFilename = outputFilename - ? compilation.getPath( outputFilename, { - chunk, - filename: chunkFilename, - basename: basename( chunkFilename ), - contentHash, - } ) - : chunkFilename.replace( - /\.js$/i, - '.asset.' + ( outputFormat === 'php' ? 'php' : 'json' ) - ); + let assetFilename; + if ( outputFilename ) { + assetFilename = compilation.getPath( outputFilename, { + chunk, + filename: chunkJSFile, + contentHash, + } ); + } else { + const suffix = + '.asset.' + ( outputFormat === 'php' ? 'php' : 'json' ); + assetFilename = compilation + .getPath( '[file]', { filename: chunkJSFile } ) + .replace( /\.js$/i, suffix ); + } // Add source and file into compilation for webpack to output. compilation.assets[ assetFilename ] = new RawSource( @@ -273,11 +280,4 @@ class DependencyExtractionWebpackPlugin { } } -function basename( name ) { - if ( ! name.includes( '/' ) ) { - return name; - } - return name.substr( name.lastIndexOf( '/' ) + 1 ); -} - module.exports = DependencyExtractionWebpackPlugin;