Skip to content

use nonNumericOnlyHash function for contentHash #15289

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

Merged
merged 2 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
use nonNumericOnlyHash function for contentHash
  • Loading branch information
vankop committed Feb 1, 2022
commit f1d329cc0e5241327575d3b3d6f491ff95773c8b
5 changes: 3 additions & 2 deletions lib/asset/AssetGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Generator = require("../Generator");
const RuntimeGlobals = require("../RuntimeGlobals");
const createHash = require("../util/createHash");
const { makePathsRelative } = require("../util/identifier");
const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");

/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../../declarations/WebpackOptions").AssetGeneratorOptions} AssetGeneratorOptions */
Expand Down Expand Up @@ -232,8 +233,8 @@ class AssetGenerator extends Generator {
const fullHash = /** @type {string} */ (
hash.digest(runtimeTemplate.outputOptions.hashDigest)
);
const contentHash = fullHash.slice(
0,
const contentHash = nonNumericOnlyHash(
fullHash,
runtimeTemplate.outputOptions.hashDigestLength
);
module.buildInfo.fullContentHash = fullHash;
Expand Down
3 changes: 2 additions & 1 deletion lib/css/CssModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const { compareModulesByIdentifier } = require("../util/comparators");
const createSchemaValidation = require("../util/create-schema-validation");
const createHash = require("../util/createHash");
const memoize = require("../util/memoize");
const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
const CssExportsGenerator = require("./CssExportsGenerator");
const CssGenerator = require("./CssGenerator");
const CssParser = require("./CssParser");
Expand Down Expand Up @@ -201,7 +202,7 @@ class CssModulesPlugin {
hash.update(chunkGraph.getModuleHash(module, chunk.runtime));
}
const digest = /** @type {string} */ (hash.digest(hashDigest));
chunk.contentHash.css = digest.substr(0, hashDigestLength);
chunk.contentHash.css = nonNumericOnlyHash(digest, hashDigestLength);
});
compilation.hooks.renderManifest.tap(plugin, (result, options) => {
const { chunkGraph } = compilation;
Expand Down
6 changes: 5 additions & 1 deletion lib/javascript/JavascriptModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const { last, someInIterable } = require("../util/IterableHelpers");
const StringXor = require("../util/StringXor");
const { compareModulesByIdentifier } = require("../util/comparators");
const createHash = require("../util/createHash");
const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
const { intersectRuntime } = require("../util/runtime");
const JavascriptGenerator = require("./JavascriptGenerator");
const JavascriptParser = require("./JavascriptParser");
Expand Down Expand Up @@ -398,7 +399,10 @@ class JavascriptModulesPlugin {
xor.updateHash(hash);
}
const digest = /** @type {string} */ (hash.digest(hashDigest));
chunk.contentHash.javascript = digest.substr(0, hashDigestLength);
chunk.contentHash.javascript = nonNumericOnlyHash(
digest,
hashDigestLength
);
});
compilation.hooks.additionalTreeRuntimeRequirements.tap(
"JavascriptModulesPlugin",
Expand Down
22 changes: 22 additions & 0 deletions lib/util/nonNumericOnlyHash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Ivan Kopeykin @vankop
*/

"use strict";

const A_CODE = "a".charCodeAt(0);

/**
* @param {string} hash hash
* @param {number} hashLength hash length
* @returns {string} returns hash that has at least one non numeric char
*/
module.exports = (hash, hashLength) => {
if (hashLength < 1) return "";
const slice = hash.slice(0, hashLength);
if (slice.match(/[^\d]/)) return slice;
return `${String.fromCharCode(A_CODE + parseInt(hash[0], 10))}${slice.slice(
1
)}`;
};
27 changes: 27 additions & 0 deletions test/nonNumericOnlyHash.unittest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

const nonNumericOnlyHash = require("../lib/util/nonNumericOnlyHash");

it("hashLength=0", () => {
expect(nonNumericOnlyHash("111", 0)).toBe("");
});

it("abc", () => {
expect(nonNumericOnlyHash("abc", 10)).toBe("abc");
});

it("abc1", () => {
expect(nonNumericOnlyHash("abc1", 3)).toBe("abc");
});

it("ab11", () => {
expect(nonNumericOnlyHash("ab11", 3)).toBe("ab1");
});

it("0111", () => {
expect(nonNumericOnlyHash("0111", 3)).toBe("a11");
});

it("911a", () => {
expect(nonNumericOnlyHash("911a", 3)).toBe("j11");
});