This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (47 loc) · 1.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var crypto = require("crypto");
var PLUGIN_NAME = 'HashAllModulesPlugin';
function HashAllModulesPlugin(options) {
this.options = Object.assign({
hashFunction: "md4",
hashDigest: "base64",
hashDigestLength: 4
}, options);
}
HashAllModulesPlugin.prototype.apply = function (compiler) {
var self = this;
// webpack 4
if (compiler.hooks) {
compiler.hooks.compilation.tap(PLUGIN_NAME, function (compilation) {
const usedIds = new Set();
compilation.hooks.beforeModuleIds.tap(PLUGIN_NAME, function (modules) {
self.hash(usedIds, modules);
});
});
}
else {
compiler.plugin("compilation", function (compilation) {
const usedIds = new Set();
compilation.plugin("before-module-ids", function (modules) {
self.hash(usedIds, modules);
});
});
}
}
HashAllModulesPlugin.prototype.hash = function (usedIds, modules) {
var options = this.options;
modules.forEach(function (module) {
if (module.id !== null) {
return;
}
const id = module.identifier();
const hash = crypto.createHash(options.hashFunction);
hash.update(id);
const hashId = hash.digest(options.hashDigest);
let len = options.hashDigestLength;
while (usedIds.has(hashId.substr(0, len)))
len++;
module.id = hashId.substr(0, len);
usedIds.add(module.id);
});
}
module.exports = HashAllModulesPlugin;