Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect output.hashSalt in RealContentHashPlugin #16789

Merged
merged 1 commit into from Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
}
]
}
}
];