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 82f6103
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions packages/dependency-extraction-webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,6 @@ 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.
continue;
}

const chunkDeps = new Set();
if ( injectPolyfill ) {
chunkDeps.add( 'wp-polyfill' );
Expand Down Expand Up @@ -217,8 +209,13 @@ class DependencyExtractionWebpackPlugin {
hashDigest,
hashDigestLength,
} = compilation.outputOptions;
const contentHash = createHash( hashFunction )
.update( compilation.getAsset( chunkFilename ).source.buffer() )

const chunkFiles = Array.from( chunk.files );
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 @@ -228,22 +225,27 @@ class DependencyExtractionWebpackPlugin {
version: contentHash,
};

const filename = chunkFiles.find( ( f ) => /\.js$/i.test( f ) );

if ( combineAssets ) {
combinedAssetsData[ chunkFilename ] = assetData;
combinedAssetsData[ filename ] = 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,
contentHash,
} );
} else {
const suffix =
'.asset.' + ( outputFormat === 'php' ? 'php' : 'json' );
assetFilename = compilation
.getPath( '[file]', { filename } )
.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 +275,4 @@ class DependencyExtractionWebpackPlugin {
}
}

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

module.exports = DependencyExtractionWebpackPlugin;

0 comments on commit 82f6103

Please sign in to comment.