2018-09-17 22:09:58 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Florent Cailhol @ooflorent
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-12-07 19:26:35 +08:00
|
|
|
const RequestShortener = require("../RequestShortener");
|
2018-09-17 22:09:58 +08:00
|
|
|
const {
|
|
|
|
compareModulesByPreOrderIndexOrIdentifier
|
|
|
|
} = require("../util/comparators");
|
2018-12-07 19:26:35 +08:00
|
|
|
const { getUsedModuleIds, assignDeterministicIds } = require("./IdHelpers");
|
2018-09-17 22:09:58 +08:00
|
|
|
|
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
|
|
/** @typedef {import("../Module")} Module */
|
|
|
|
|
|
|
|
class DeterministicModuleIdsPlugin {
|
|
|
|
constructor(options) {
|
|
|
|
this.options = options || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Compiler} compiler the compiler instance
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
apply(compiler) {
|
|
|
|
compiler.hooks.compilation.tap(
|
|
|
|
"DeterministicModuleIdsPlugin",
|
|
|
|
compilation => {
|
|
|
|
compilation.hooks.moduleIds.tap(
|
|
|
|
"DeterministicModuleIdsPlugin",
|
|
|
|
modules => {
|
|
|
|
const chunkGraph = compilation.chunkGraph;
|
2018-12-07 19:26:35 +08:00
|
|
|
const requestShortener = this.options.context
|
|
|
|
? new RequestShortener(this.options.context)
|
|
|
|
: compilation.requestShortener;
|
|
|
|
|
|
|
|
assignDeterministicIds(
|
|
|
|
Array.from(modules).filter(module => {
|
|
|
|
if (chunkGraph.getNumberOfModuleChunks(module) === 0)
|
|
|
|
return false;
|
|
|
|
return chunkGraph.getModuleId(module) === null;
|
|
|
|
}),
|
|
|
|
module => requestShortener.shorten(module.identifier()),
|
|
|
|
compareModulesByPreOrderIndexOrIdentifier(
|
|
|
|
compilation.moduleGraph
|
2018-09-17 22:09:58 +08:00
|
|
|
),
|
2018-12-07 21:47:25 +08:00
|
|
|
this.options.maxLength || 3,
|
2018-12-07 19:26:35 +08:00
|
|
|
getUsedModuleIds(compilation),
|
|
|
|
(module, id) => {
|
2018-11-30 00:08:44 +08:00
|
|
|
chunkGraph.setModuleId(module, id);
|
|
|
|
}
|
2018-12-07 19:26:35 +08:00
|
|
|
);
|
2018-09-17 22:09:58 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DeterministicModuleIdsPlugin;
|