|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 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; |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +class HashedModuleIdsPlugin { |
| 8 | + constructor(options) { |
| 9 | + this.options = Object.assign({ |
| 10 | + hashFunction: "md5", |
| 11 | + hashDigest: "base64", |
| 12 | + hashDigestLength: 4 |
| 13 | + }, options); |
| 14 | + } |
| 15 | + |
| 16 | + apply(compiler) { |
| 17 | + const options = this.options; |
| 18 | + compiler.plugin("compilation", (compilation) => { |
| 19 | + const usedIds = new Set(); |
| 20 | + compilation.plugin("before-module-ids", (modules) => { |
| 21 | + modules.forEach((module) => { |
| 22 | + if(module.id === null && module.libIdent) { |
| 23 | + let id = module.libIdent({ |
| 24 | + context: this.options.context || compiler.options.context |
| 25 | + }); |
| 26 | + const hash = require("crypto").createHash(options.hashFunction); |
| 27 | + hash.update(id); |
| 28 | + id = hash.digest(options.hashDigest); |
| 29 | + let len = options.hashDigestLength; |
| 30 | + while(usedIds.has(id.substr(0, len))) |
| 31 | + len++; |
| 32 | + module.id = id.substr(0, len); |
| 33 | + usedIds.add(module.id); |
| 34 | + } |
| 35 | + }); |
| 36 | + }); |
| 37 | + }); |
| 38 | + } |
10 | 39 | } |
| 40 | + |
11 | 41 | 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 | | -}; |
|
0 commit comments