Skip to content

Commit 5aeef4a

Browse files
committed
added HashedModuleIdsPlugin
1 parent c76c527 commit 5aeef4a

File tree

19 files changed

+81
-1
lines changed

19 files changed

+81
-1
lines changed

lib/HashedModuleIdsPlugin.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
function HashedModuleIdsPlugin(options) {
6+
this.options = options || {};
7+
this.options.hashFunction = this.options.hashFunction || "md5";
8+
this.options.hashDigest = this.options.hashDigest || "base64";
9+
this.options.hashDigestLength = this.options.hashDigestLength || 4;
10+
}
11+
module.exports = HashedModuleIdsPlugin;
12+
HashedModuleIdsPlugin.prototype.apply = function(compiler) {
13+
var options = this.options;
14+
compiler.plugin("compilation", function(compilation) {
15+
var usedIds = {};
16+
compilation.plugin("before-module-ids", function(modules) {
17+
modules.forEach(function(module) {
18+
if(module.id === null && module.libIdent) {
19+
var id = module.libIdent({
20+
context: this.options.context || compiler.options.context
21+
});
22+
var hash = require("crypto").createHash(options.hashFunction);
23+
hash.update(id);
24+
id = hash.digest(options.hashDigest);
25+
var len = options.hashDigestLength;
26+
while(usedIds[id.substr(0, len)])
27+
len++;
28+
module.id = id.substr(0, len);
29+
usedIds[module.id] = true;
30+
}
31+
}, this);
32+
}.bind(this));
33+
}.bind(this));
34+
};

lib/webpack.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ exportPlugins(exports, ".", [
8787
"DllPlugin",
8888
"DllReferencePlugin",
8989
"LoaderOptionsPlugin",
90-
"NamedModulesPlugin"
90+
"NamedModulesPlugin",
91+
"HashedModuleIdsPlugin"
9192
]);
9293
exportPlugins(exports.optimize = {}, "./optimize", [
9394
"AggressiveMergingPlugin",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = module.id;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = module.id;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = module.id;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = module.id;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = module.id;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = module.id;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = module.id;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = module.id;

0 commit comments

Comments
 (0)