Skip to content

Commit 710eaf4

Browse files
authored
Merge pull request #16789 from dmichon-msft/contenthash-hashsalt
Respect output.hashSalt in RealContentHashPlugin
2 parents 5d64468 + 07283fa commit 710eaf4

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

lib/optimize/RealContentHashPlugin.js

+3
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ ${referencingAssets
393393
let newHash = hooks.updateHash.call(assetsContent, oldHash);
394394
if (!newHash) {
395395
const hash = createHash(this._hashFunction);
396+
if (compilation.outputOptions.hashSalt) {
397+
hash.update(compilation.outputOptions.hashSalt);
398+
}
396399
for (const content of assetsContent) {
397400
hash.update(content);
398401
}

test/configCases/contenthash/salt/img.jpg

Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import img from "./img.jpg";
2+
3+
it("should compile", () => {
4+
expect(typeof img).toBe("string");
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const findOutputFiles = require("../../../helpers/findOutputFiles");
2+
3+
const allAssets = new Set();
4+
const allBundles = new Set();
5+
6+
module.exports = {
7+
findBundle: function(i, options) {
8+
const bundle = findOutputFiles(options, new RegExp(`^bundle${i}`))[0];
9+
allBundles.add(/\.([^.]+)\./.exec(bundle)[1]);
10+
11+
const assets = findOutputFiles(options, /^img/);
12+
for (const asset of assets) {
13+
allAssets.add(asset);
14+
}
15+
16+
return `./${bundle}`;
17+
},
18+
afterExecute: () => {
19+
// Since there are exactly 2 unique values of output.hashSalt,
20+
// there should be exactly 2 unique output hashes for each file.
21+
expect(allBundles.size).toBe(2);
22+
expect(allAssets.size).toBe(2);
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/** @type {import("../../../../").Configuration[]} */
2+
module.exports = [
3+
{
4+
output: {
5+
filename: "bundle0.[contenthash].js",
6+
assetModuleFilename: "[name].[contenthash][ext]",
7+
hashSalt: "1"
8+
},
9+
module: {
10+
rules: [
11+
{
12+
test: /\.jpg$/,
13+
type: "asset/resource"
14+
}
15+
]
16+
}
17+
},
18+
{
19+
output: {
20+
filename: "bundle1.[contenthash].js",
21+
assetModuleFilename: "[name].[contenthash][ext]",
22+
hashSalt: "1"
23+
},
24+
module: {
25+
rules: [
26+
{
27+
test: /\.jpg$/,
28+
type: "asset/resource"
29+
}
30+
]
31+
}
32+
},
33+
{
34+
output: {
35+
filename: "bundle2.[contenthash].js",
36+
assetModuleFilename: "[name].[contenthash][ext]",
37+
hashSalt: "2"
38+
},
39+
module: {
40+
rules: [
41+
{
42+
test: /\.jpg$/,
43+
type: "asset/resource"
44+
}
45+
]
46+
}
47+
}
48+
];

0 commit comments

Comments
 (0)