2015-06-28 04:47:51 +08:00
|
|
|
/*
|
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
|
*/
|
2016-12-29 15:05:11 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
2018-05-01 03:45:55 +08:00
|
|
|
const createHash = require("./util/createHash");
|
|
|
|
|
const RequestShortener = require("./RequestShortener");
|
|
|
|
|
|
|
|
|
|
const getHash = str => {
|
|
|
|
|
const hash = createHash("md4");
|
|
|
|
|
hash.update(str);
|
|
|
|
|
return hash.digest("hex").substr(0, 4);
|
|
|
|
|
};
|
|
|
|
|
|
2016-12-29 15:05:11 +08:00
|
|
|
class NamedModulesPlugin {
|
|
|
|
|
constructor(options) {
|
|
|
|
|
this.options = options || {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
apply(compiler) {
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.compilation.tap("NamedModulesPlugin", compilation => {
|
|
|
|
|
compilation.hooks.beforeModuleIds.tap("NamedModulesPlugin", modules => {
|
2018-05-01 03:45:55 +08:00
|
|
|
const namedModules = new Map();
|
2018-04-30 01:29:38 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
for (const module of modules) {
|
|
|
|
|
if (module.id === null && module.libIdent) {
|
2016-12-29 15:05:11 +08:00
|
|
|
module.id = module.libIdent({
|
|
|
|
|
context: this.options.context || compiler.options.context
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-04-30 01:29:38 +08:00
|
|
|
|
2018-05-01 03:45:55 +08:00
|
|
|
if (module.id !== null) {
|
|
|
|
|
if (namedModules.has(module.id)) {
|
|
|
|
|
namedModules.get(module.id).push(module);
|
|
|
|
|
} else {
|
|
|
|
|
namedModules.set(module.id, [module]);
|
|
|
|
|
}
|
2018-04-30 01:29:38 +08:00
|
|
|
}
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2018-04-30 01:29:38 +08:00
|
|
|
|
2018-05-01 03:45:55 +08:00
|
|
|
namedModules.forEach(namedModule => {
|
2018-04-30 01:29:38 +08:00
|
|
|
if (namedModule.length > 1) {
|
|
|
|
|
namedModule.forEach(module => {
|
|
|
|
|
if (module.issuer && module.issuer.id) {
|
2018-05-01 03:45:55 +08:00
|
|
|
const requestShortener = new RequestShortener(module.context);
|
|
|
|
|
module.id = `${module.id}?${getHash(
|
|
|
|
|
requestShortener.shorten(module.identifier())
|
|
|
|
|
)}`;
|
2018-04-30 01:29:38 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-12-29 15:05:11 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-06-28 04:47:51 +08:00
|
|
|
}
|
2016-12-29 15:05:11 +08:00
|
|
|
|
2015-06-28 04:47:51 +08:00
|
|
|
module.exports = NamedModulesPlugin;
|