From 160cf834d0314662e2359deb446543137484c3f4 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Wed, 11 May 2022 08:58:59 +0200 Subject: [PATCH] Dependency extraction: respect hashDigestLength option for version field (#40977) --- .../lib/index.js | 13 +++++++-- .../test/__snapshots__/build.js.snap | 28 +++++++++---------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/packages/dependency-extraction-webpack-plugin/lib/index.js b/packages/dependency-extraction-webpack-plugin/lib/index.js index 2d577ab41dcb10..88d2cf6a0a3d2f 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/index.js +++ b/packages/dependency-extraction-webpack-plugin/lib/index.js @@ -198,7 +198,11 @@ class DependencyExtractionWebpackPlugin { } } - const { hashFunction, hashDigest } = compilation.outputOptions; + const { + hashFunction, + hashDigest, + hashDigestLength, + } = compilation.outputOptions; // Go through the assets and hash the sources. We can't just use // `entrypointChunk.contentHash` because that's not updated when @@ -211,7 +215,9 @@ class DependencyExtractionWebpackPlugin { const asset = compilation.getAsset( filename ); hash.update( asset.source.buffer() ); } - const version = hash.digest( hashDigest ); + const version = hash + .digest( hashDigest ) + .slice( 0, hashDigestLength ); const entrypointChunk = isWebpack4 ? entrypoint.chunks.find( ( c ) => c.name === entrypointName ) @@ -226,7 +232,8 @@ class DependencyExtractionWebpackPlugin { const assetString = this.stringify( assetData ); const contentHash = createHash( hashFunction ) .update( assetString ) - .digest( hashDigest ); + .digest( hashDigest ) + .slice( 0, hashDigestLength ); // Determine a filename for the asset file. const [ filename, query ] = entrypointName.split( '?', 2 ); 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 bb6ad6049087b8..a9605afe54e4d0 100644 --- a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap +++ b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`DependencyExtractionWebpackPlugin Webpack \`combine-assets\` should produce expected output: Asset file 'assets.php' should match snapshot 1`] = ` -" array('dependencies' => array('lodash', 'wp-blob'), 'version' => 'bf200ecb3dcb6881a1f39d63cd243206'), 'fileB.js' => array('dependencies' => array('wp-token-list'), 'version' => '0af6c51a8e6ac934b85a78835a93cb5c')); +" array('dependencies' => array('lodash', 'wp-blob'), 'version' => 'bf200ecb3dcb6881a1f3'), 'fileB.js' => array('dependencies' => array('wp-token-list'), 'version' => '0af6c51a8e6ac934b85a')); " `; @@ -32,7 +32,7 @@ Array [ `; exports[`DependencyExtractionWebpackPlugin Webpack \`dynamic-import\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = ` -" array('lodash', 'wp-blob'), 'version' => '1166b9d1044bb1eded0a4193fadf87f8'); +" array('lodash', 'wp-blob'), 'version' => '1166b9d1044bb1eded0a'); " `; @@ -55,7 +55,7 @@ Array [ `; exports[`DependencyExtractionWebpackPlugin Webpack \`function-output-filename\` should produce expected output: Asset file 'chunk--main--main.asset.php' should match snapshot 1`] = ` -" array('lodash', 'wp-blob'), 'version' => '4c78134607e6ed966df3de5d3d38b15f'); +" array('lodash', 'wp-blob'), 'version' => '4c78134607e6ed966df3'); " `; @@ -78,7 +78,7 @@ Array [ `; exports[`DependencyExtractionWebpackPlugin Webpack \`has-extension-suffix\` should produce expected output: Asset file 'index.min.asset.php' should match snapshot 1`] = ` -" array('lodash', 'wp-blob'), 'version' => 'dabeb91f3cb9dd73d48d044af194a158'); +" array('lodash', 'wp-blob'), 'version' => 'dabeb91f3cb9dd73d48d'); " `; @@ -101,21 +101,21 @@ Array [ `; exports[`DependencyExtractionWebpackPlugin Webpack \`no-default\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = ` -" array(), 'version' => 'bb85a9737103c7054b00770c09034c6e'); +" array(), 'version' => 'bb85a9737103c7054b00'); " `; exports[`DependencyExtractionWebpackPlugin Webpack \`no-default\` should produce expected output: External modules should match snapshot 1`] = `Array []`; exports[`DependencyExtractionWebpackPlugin Webpack \`no-deps\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = ` -" array(), 'version' => '091ffcd70d94dd16e773c0ddca465fdc'); +" array(), 'version' => '091ffcd70d94dd16e773'); " `; exports[`DependencyExtractionWebpackPlugin Webpack \`no-deps\` should produce expected output: External modules should match snapshot 1`] = `Array []`; exports[`DependencyExtractionWebpackPlugin Webpack \`option-function-output-filename\` should produce expected output: Asset file 'chunk--main--main.asset.php' should match snapshot 1`] = ` -" array('lodash', 'wp-blob'), 'version' => '4c78134607e6ed966df3de5d3d38b15f'); +" array('lodash', 'wp-blob'), 'version' => '4c78134607e6ed966df3'); " `; @@ -138,7 +138,7 @@ Array [ `; exports[`DependencyExtractionWebpackPlugin Webpack \`option-output-filename\` should produce expected output: Asset file 'main-foo.asset.php' should match snapshot 1`] = ` -" array('lodash', 'wp-blob'), 'version' => '4c78134607e6ed966df3de5d3d38b15f'); +" array('lodash', 'wp-blob'), 'version' => '4c78134607e6ed966df3'); " `; @@ -160,7 +160,7 @@ Array [ ] `; -exports[`DependencyExtractionWebpackPlugin Webpack \`output-format-json\` should produce expected output: Asset file 'main.asset.json' should match snapshot 1`] = `"{\\"dependencies\\":[\\"lodash\\"],\\"version\\":\\"a8f35bfc9f46482cc48a78d50e7099da\\"}"`; +exports[`DependencyExtractionWebpackPlugin Webpack \`output-format-json\` should produce expected output: Asset file 'main.asset.json' should match snapshot 1`] = `"{\\"dependencies\\":[\\"lodash\\"],\\"version\\":\\"a8f35bfc9f46482cc48a\\"}"`; exports[`DependencyExtractionWebpackPlugin Webpack \`output-format-json\` should produce expected output: External modules should match snapshot 1`] = ` Array [ @@ -173,7 +173,7 @@ Array [ `; exports[`DependencyExtractionWebpackPlugin Webpack \`overrides\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = ` -" array('wp-blob', 'wp-script-handle-for-rxjs', 'wp-url'), 'version' => '2a29b245fc3d0509b5a8039bb4cde515'); +" array('wp-blob', 'wp-script-handle-for-rxjs', 'wp-url'), 'version' => '2a29b245fc3d0509b5a8'); " `; @@ -212,12 +212,12 @@ Array [ `; exports[`DependencyExtractionWebpackPlugin Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'a.asset.php' should match snapshot 1`] = ` -" array('wp-blob'), 'version' => 'afb97a54745e17cee9638e4d9d2322f9'); +" array('wp-blob'), 'version' => 'afb97a54745e17cee963'); " `; exports[`DependencyExtractionWebpackPlugin Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'b.asset.php' should match snapshot 1`] = ` -" array('lodash', 'wp-blob'), 'version' => 'ed750e4e046b77c317cb852294984ef3'); +" array('lodash', 'wp-blob'), 'version' => 'ed750e4e046b77c317cb'); " `; @@ -240,7 +240,7 @@ Array [ `; exports[`DependencyExtractionWebpackPlugin Webpack \`wordpress\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = ` -" array('lodash', 'wp-blob'), 'version' => '4c78134607e6ed966df3de5d3d38b15f'); +" array('lodash', 'wp-blob'), 'version' => '4c78134607e6ed966df3'); " `; @@ -263,7 +263,7 @@ Array [ `; exports[`DependencyExtractionWebpackPlugin Webpack \`wordpress-require\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = ` -" array('lodash', 'wp-blob'), 'version' => 'ed2bd4e7df46768bb3c23c8dc0ba5c50'); +" array('lodash', 'wp-blob'), 'version' => 'ed2bd4e7df46768bb3c2'); " `;