1 |
3a515b92
|
cagy
|
/*
|
2 |
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3 |
|
|
Author Tobias Koppers @sokra
|
4 |
|
|
*/
|
5 |
|
|
"use strict";
|
6 |
|
|
|
7 |
|
|
const createHash = require("./util/createHash");
|
8 |
|
|
const RequestShortener = require("./RequestShortener");
|
9 |
|
|
|
10 |
|
|
const getHash = str => {
|
11 |
|
|
const hash = createHash("md4");
|
12 |
|
|
hash.update(str);
|
13 |
|
|
const digest = /** @type {string} */ (hash.digest("hex"));
|
14 |
|
|
return digest.substr(0, 4);
|
15 |
|
|
};
|
16 |
|
|
|
17 |
|
|
class NamedModulesPlugin {
|
18 |
|
|
constructor(options) {
|
19 |
|
|
this.options = options || {};
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
apply(compiler) {
|
23 |
|
|
compiler.hooks.compilation.tap("NamedModulesPlugin", compilation => {
|
24 |
|
|
compilation.hooks.beforeModuleIds.tap("NamedModulesPlugin", modules => {
|
25 |
|
|
const namedModules = new Map();
|
26 |
|
|
const context = this.options.context || compiler.options.context;
|
27 |
|
|
|
28 |
|
|
for (const module of modules) {
|
29 |
|
|
if (module.id === null && module.libIdent) {
|
30 |
|
|
module.id = module.libIdent({ context });
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
if (module.id !== null) {
|
34 |
|
|
const namedModule = namedModules.get(module.id);
|
35 |
|
|
if (namedModule !== undefined) {
|
36 |
|
|
namedModule.push(module);
|
37 |
|
|
} else {
|
38 |
|
|
namedModules.set(module.id, [module]);
|
39 |
|
|
}
|
40 |
|
|
}
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
for (const namedModule of namedModules.values()) {
|
44 |
|
|
if (namedModule.length > 1) {
|
45 |
|
|
for (const module of namedModule) {
|
46 |
|
|
const requestShortener = new RequestShortener(context);
|
47 |
|
|
module.id = `${module.id}?${getHash(
|
48 |
|
|
requestShortener.shorten(module.identifier())
|
49 |
|
|
)}`;
|
50 |
|
|
}
|
51 |
|
|
}
|
52 |
|
|
}
|
53 |
|
|
});
|
54 |
|
|
});
|
55 |
|
|
}
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
module.exports = NamedModulesPlugin;
|