Skip to content

Commit

Permalink
Compute content hash from all chunk assets
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Jun 3, 2022
1 parent e051f98 commit 316b936
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions packages/dependency-extraction-webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 );

Expand All @@ -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(
Expand Down Expand Up @@ -273,11 +280,4 @@ class DependencyExtractionWebpackPlugin {
}
}

function basename( name ) {
if ( ! name.includes( '/' ) ) {
return name;
}
return name.substr( name.lastIndexOf( '/' ) + 1 );
}

module.exports = DependencyExtractionWebpackPlugin;

0 comments on commit 316b936

Please sign in to comment.