Skip to content

Commit 23e96e9

Browse files
authored
Merge pull request webpack#6006 from webpack/refactor/hash
util/createHash, call hash.update in bulks
2 parents 69e56b0 + 7e8936c commit 23e96e9

File tree

8 files changed

+63
-15
lines changed

8 files changed

+63
-15
lines changed

lib/Compilation.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"use strict";
66

77
const asyncLib = require("async");
8-
const crypto = require("crypto");
98
const util = require("util");
109
const Tapable = require("tapable");
1110
const EntryModuleNotFoundError = require("./EntryModuleNotFoundError");
@@ -25,6 +24,7 @@ const AsyncDependencyToInitialChunkWarning = require("./AsyncDependencyToInitial
2524
const CachedSource = require("webpack-sources").CachedSource;
2625
const Stats = require("./Stats");
2726
const Semaphore = require("./util/Semaphore");
27+
const createHash = require("./util/createHash");
2828
const Queue = require("./util/Queue");
2929
const SortableSet = require("./util/SortableSet");
3030

@@ -1357,7 +1357,7 @@ class Compilation extends Tapable {
13571357
const hashFunction = outputOptions.hashFunction;
13581358
const hashDigest = outputOptions.hashDigest;
13591359
const hashDigestLength = outputOptions.hashDigestLength;
1360-
const hash = crypto.createHash(hashFunction);
1360+
const hash = createHash(hashFunction);
13611361
if(outputOptions.hashSalt)
13621362
hash.update(outputOptions.hashSalt);
13631363
this.mainTemplate.updateHash(hash);
@@ -1369,7 +1369,7 @@ class Compilation extends Tapable {
13691369
const modules = this.modules;
13701370
for(let i = 0; i < modules.length; i++) {
13711371
const module = modules[i];
1372-
const moduleHash = crypto.createHash(hashFunction);
1372+
const moduleHash = createHash(hashFunction);
13731373
module.updateHash(moduleHash);
13741374
module.hash = moduleHash.digest(hashDigest);
13751375
module.renderedHash = module.hash.substr(0, hashDigestLength);
@@ -1390,7 +1390,7 @@ class Compilation extends Tapable {
13901390
});
13911391
for(let i = 0; i < chunks.length; i++) {
13921392
const chunk = chunks[i];
1393-
const chunkHash = crypto.createHash(hashFunction);
1393+
const chunkHash = createHash(hashFunction);
13941394
if(outputOptions.hashSalt)
13951395
chunkHash.update(outputOptions.hashSalt);
13961396
chunk.updateHash(chunkHash);
@@ -1413,7 +1413,7 @@ class Compilation extends Tapable {
14131413
const hashFunction = outputOptions.hashFunction;
14141414
const hashDigest = outputOptions.hashDigest;
14151415
const hashDigestLength = outputOptions.hashDigestLength;
1416-
const hash = crypto.createHash(hashFunction);
1416+
const hash = createHash(hashFunction);
14171417
hash.update(this.fullHash);
14181418
hash.update(update);
14191419
this.fullHash = hash.digest(hashDigest);

lib/HashedModuleIdsPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Author Tobias Koppers @sokra
44
*/
55
"use strict";
6-
const createHash = require("crypto").createHash;
6+
const createHash = require("./util/createHash");
77

88
class HashedModuleIdsPlugin {
99
constructor(options) {

lib/HotModuleReplacementPlugin.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const RawSource = require("webpack-sources").RawSource;
1010
const ConstDependency = require("./dependencies/ConstDependency");
1111
const NullFactory = require("./NullFactory");
1212
const ParserHelpers = require("./ParserHelpers");
13+
const createHash = require("./util/createHash");
1314

1415
module.exports = class HotModuleReplacementPlugin {
1516
constructor(options) {
@@ -46,7 +47,7 @@ module.exports = class HotModuleReplacementPlugin {
4647
records.moduleHashs = {};
4748
compilation.modules.forEach(module => {
4849
const identifier = module.identifier();
49-
const hash = require("crypto").createHash("md5");
50+
const hash = createHash(compilation.outputOptions.hashFunction);
5051
module.updateHash(hash);
5152
records.moduleHashs[identifier] = hash.digest("hex");
5253
});
@@ -99,7 +100,7 @@ module.exports = class HotModuleReplacementPlugin {
99100
if(!records.moduleHashs || !records.chunkHashs || !records.chunkModuleIds) return;
100101
compilation.modules.forEach(module => {
101102
const identifier = module.identifier();
102-
let hash = require("crypto").createHash("md5");
103+
let hash = createHash(compilation.outputOptions.hashFunction);
103104
module.updateHash(hash);
104105
hash = hash.digest("hex");
105106
module.hotUpdate = records.moduleHashs[identifier] !== hash;

lib/ModuleFilenameHelpers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
"use strict";
66

7+
const createHash = require("./util/createHash");
8+
79
const ModuleFilenameHelpers = exports;
810

911
ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
@@ -40,7 +42,7 @@ const getBefore = (str, token) => {
4042
};
4143

4244
const getHash = str => {
43-
const hash = require("crypto").createHash("md5");
45+
const hash = createHash("md5");
4446
hash.update(str);
4547
return hash.digest("hex").substr(0, 4);
4648
};

lib/NormalModule.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
const path = require("path");
88
const NativeModule = require("module");
9-
const crypto = require("crypto");
109

1110
const SourceMapSource = require("webpack-sources").SourceMapSource;
1211
const OriginalSource = require("webpack-sources").OriginalSource;
@@ -21,6 +20,7 @@ const ModuleParseError = require("./ModuleParseError");
2120
const ModuleBuildError = require("./ModuleBuildError");
2221
const ModuleError = require("./ModuleError");
2322
const ModuleWarning = require("./ModuleWarning");
23+
const createHash = require("./util/createHash");
2424

2525
const runLoaders = require("loader-runner").runLoaders;
2626
const getContext = require("loader-runner").getContext;
@@ -328,7 +328,7 @@ class NormalModule extends Module {
328328

329329
getHashDigest(dependencyTemplates) {
330330
let dtHash = dependencyTemplatesHashMap.get("hash");
331-
const hash = crypto.createHash("md5");
331+
const hash = createHash("md5");
332332
this.updateHash(hash);
333333
hash.update(`${dtHash}`);
334334
return hash.digest("hex");

lib/SourceMapDevToolPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"use strict";
66

77
const path = require("path");
8-
const crypto = require("crypto");
98
const RequestShortener = require("./RequestShortener");
109
const ConcatSource = require("webpack-sources").ConcatSource;
1110
const RawSource = require("webpack-sources").RawSource;
1211
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
1312
const SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin");
13+
const createHash = require("./util/createHash");
1414

1515
const basename = (name) => {
1616
if(name.indexOf("/") < 0) return name;
@@ -190,7 +190,7 @@ class SourceMapDevToolPlugin {
190190
basename: basename(filename)
191191
});
192192
if(sourceMapFile.indexOf("[contenthash]") !== -1) {
193-
sourceMapFile = sourceMapFile.replace(/\[contenthash\]/g, crypto.createHash("md5").update(sourceMapString).digest("hex"));
193+
sourceMapFile = sourceMapFile.replace(/\[contenthash\]/g, createHash("md5").update(sourceMapString).digest("hex"));
194194
}
195195
const sourceMapUrl = path.relative(path.dirname(file), sourceMapFile).replace(/\\/g, "/");
196196
if(currentSourceMappingURLComment !== false) {

lib/optimize/ConcatenatedModule.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const Module = require("../Module");
88
const Template = require("../Template");
99
const Parser = require("../Parser");
10-
const crypto = require("crypto");
1110
const acorn = require("acorn");
1211
const escope = require("escope");
1312
const ReplaceSource = require("webpack-sources").ReplaceSource;
@@ -19,6 +18,7 @@ const HarmonyExportSpecifierDependency = require("../dependencies/HarmonyExportS
1918
const HarmonyExportExpressionDependency = require("../dependencies/HarmonyExportExpressionDependency");
2019
const HarmonyExportImportedSpecifierDependency = require("../dependencies/HarmonyExportImportedSpecifierDependency");
2120
const HarmonyCompatibilityDependency = require("../dependencies/HarmonyCompatibilityDependency");
21+
const createHash = require("../util/createHash");
2222

2323
const ensureNsObjSource = (info, moduleToInfoMap, requestShortener) => {
2424
if(!info.hasNamespaceObject) {
@@ -315,7 +315,7 @@ class ConcatenatedModule extends Module {
315315
return info.module.identifier();
316316
}
317317
}).filter(Boolean).join(" ");
318-
const hash = crypto.createHash("md5");
318+
const hash = createHash("md5");
319319
hash.update(orderedConcatenationListIdentifiers);
320320
return this.rootModule.identifier() + " " + hash.digest("hex");
321321
}

lib/util/createHash.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
"use strict";
6+
7+
const BULK_SIZE = 1000;
8+
9+
class BulkUpdateDecorator {
10+
constructor(hash) {
11+
this.hash = hash;
12+
this.buffer = "";
13+
}
14+
15+
update(data, inputEncoding) {
16+
if(inputEncoding !== undefined || typeof data !== "string" || data.length > BULK_SIZE) {
17+
if(this.buffer.length > 0) {
18+
this.hash.update(this.buffer);
19+
this.buffer = "";
20+
}
21+
this.hash.update(data, inputEncoding);
22+
} else {
23+
this.buffer += data;
24+
if(this.buffer.length > BULK_SIZE) {
25+
this.hash.update(this.buffer);
26+
this.buffer = "";
27+
}
28+
}
29+
return this;
30+
}
31+
32+
digest(encoding) {
33+
if(this.buffer.length > 0) {
34+
this.hash.update(this.buffer);
35+
}
36+
return this.hash.digest(encoding);
37+
}
38+
}
39+
40+
module.exports = algorithm => {
41+
switch(algorithm) {
42+
// TODO add non-cryptographic algorithm here
43+
default: return new BulkUpdateDecorator(require("crypto").createHash(algorithm));
44+
}
45+
};

0 commit comments

Comments
 (0)