Skip to content

Commit

Permalink
Respect output.hashSalt in RealContentHashPlugin
Browse files Browse the repository at this point in the history
Fix #16788

Update RealContentHashPlugin to initialize hash instances with
the value of `output.hashSalt`, if provided.
  • Loading branch information
dmichon-msft committed Mar 8, 2023
1 parent c98e9e0 commit 07283fa
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/optimize/RealContentHashPlugin.js
Expand Up @@ -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);
}
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/configCases/contenthash/salt/index.js
@@ -0,0 +1,5 @@
import img from "./img.jpg";

it("should compile", () => {
expect(typeof img).toBe("string");
});
24 changes: 24 additions & 0 deletions 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);
}
};
48 changes: 48 additions & 0 deletions 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"
}
]
}
}
];

0 comments on commit 07283fa

Please sign in to comment.