From 07283fabc43a440db046037f7231ee362f31a21c Mon Sep 17 00:00:00 2001 From: David Michon Date: Tue, 7 Mar 2023 16:15:36 -0800 Subject: [PATCH] Respect output.hashSalt in RealContentHashPlugin Fix #16788 Update RealContentHashPlugin to initialize hash instances with the value of `output.hashSalt`, if provided. --- lib/optimize/RealContentHashPlugin.js | 3 ++ test/configCases/contenthash/salt/img.jpg | 0 test/configCases/contenthash/salt/index.js | 5 ++ .../contenthash/salt/test.config.js | 24 ++++++++++ .../contenthash/salt/webpack.config.js | 48 +++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 test/configCases/contenthash/salt/img.jpg create mode 100644 test/configCases/contenthash/salt/index.js create mode 100644 test/configCases/contenthash/salt/test.config.js create mode 100644 test/configCases/contenthash/salt/webpack.config.js diff --git a/lib/optimize/RealContentHashPlugin.js b/lib/optimize/RealContentHashPlugin.js index 7ab9c46fb8b..9ceb157781d 100644 --- a/lib/optimize/RealContentHashPlugin.js +++ b/lib/optimize/RealContentHashPlugin.js @@ -363,6 +363,9 @@ ${referencingAssets let newHash = hooks.updateHash.call(assetsContent, oldHash); if (!newHash) { const hash = createHash(this._hashFunction); + if (compilation.outputOptions.hashSalt) { + hash.update(compilation.outputOptions.hashSalt); + } for (const content of assetsContent) { hash.update(content); } diff --git a/test/configCases/contenthash/salt/img.jpg b/test/configCases/contenthash/salt/img.jpg new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/configCases/contenthash/salt/index.js b/test/configCases/contenthash/salt/index.js new file mode 100644 index 00000000000..2d2b98703ee --- /dev/null +++ b/test/configCases/contenthash/salt/index.js @@ -0,0 +1,5 @@ +import img from "./img.jpg"; + +it("should compile", () => { + expect(typeof img).toBe("string"); +}); diff --git a/test/configCases/contenthash/salt/test.config.js b/test/configCases/contenthash/salt/test.config.js new file mode 100644 index 00000000000..530c9147c05 --- /dev/null +++ b/test/configCases/contenthash/salt/test.config.js @@ -0,0 +1,24 @@ +const findOutputFiles = require("../../../helpers/findOutputFiles"); + +const allAssets = new Set(); +const allBundles = new Set(); + +module.exports = { + findBundle: function(i, options) { + const bundle = findOutputFiles(options, new RegExp(`^bundle${i}`))[0]; + allBundles.add(/\.([^.]+)\./.exec(bundle)[1]); + + const assets = findOutputFiles(options, /^img/); + for (const asset of assets) { + allAssets.add(asset); + } + + return `./${bundle}`; + }, + afterExecute: () => { + // Since there are exactly 2 unique values of output.hashSalt, + // there should be exactly 2 unique output hashes for each file. + expect(allBundles.size).toBe(2); + expect(allAssets.size).toBe(2); + } +}; diff --git a/test/configCases/contenthash/salt/webpack.config.js b/test/configCases/contenthash/salt/webpack.config.js new file mode 100644 index 00000000000..1ec1c83b9d9 --- /dev/null +++ b/test/configCases/contenthash/salt/webpack.config.js @@ -0,0 +1,48 @@ +/** @type {import("../../../../").Configuration[]} */ +module.exports = [ + { + output: { + filename: "bundle0.[contenthash].js", + assetModuleFilename: "[name].[contenthash][ext]", + hashSalt: "1" + }, + module: { + rules: [ + { + test: /\.jpg$/, + type: "asset/resource" + } + ] + } + }, + { + output: { + filename: "bundle1.[contenthash].js", + assetModuleFilename: "[name].[contenthash][ext]", + hashSalt: "1" + }, + module: { + rules: [ + { + test: /\.jpg$/, + type: "asset/resource" + } + ] + } + }, + { + output: { + filename: "bundle2.[contenthash].js", + assetModuleFilename: "[name].[contenthash][ext]", + hashSalt: "2" + }, + module: { + rules: [ + { + test: /\.jpg$/, + type: "asset/resource" + } + ] + } + } +];