[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const { compareModulesByIdentifier } = require("../util/comparators");
|
---|
| 9 | const {
|
---|
| 10 | getShortModuleName,
|
---|
| 11 | getLongModuleName,
|
---|
| 12 | assignNames,
|
---|
| 13 | getUsedModuleIds,
|
---|
| 14 | assignAscendingModuleIds
|
---|
| 15 | } = require("./IdHelpers");
|
---|
| 16 |
|
---|
| 17 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 18 | /** @typedef {import("../Module")} Module */
|
---|
| 19 |
|
---|
| 20 | class NamedModuleIdsPlugin {
|
---|
| 21 | constructor(options) {
|
---|
| 22 | this.options = options || {};
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Apply the plugin
|
---|
| 27 | * @param {Compiler} compiler the compiler instance
|
---|
| 28 | * @returns {void}
|
---|
| 29 | */
|
---|
| 30 | apply(compiler) {
|
---|
| 31 | const { root } = compiler;
|
---|
| 32 | compiler.hooks.compilation.tap("NamedModuleIdsPlugin", compilation => {
|
---|
| 33 | compilation.hooks.moduleIds.tap("NamedModuleIdsPlugin", modules => {
|
---|
| 34 | const chunkGraph = compilation.chunkGraph;
|
---|
| 35 | const context = this.options.context
|
---|
| 36 | ? this.options.context
|
---|
| 37 | : compiler.context;
|
---|
| 38 |
|
---|
| 39 | const unnamedModules = assignNames(
|
---|
| 40 | Array.from(modules).filter(module => {
|
---|
| 41 | if (!module.needId) return false;
|
---|
| 42 | if (chunkGraph.getNumberOfModuleChunks(module) === 0) return false;
|
---|
| 43 | return chunkGraph.getModuleId(module) === null;
|
---|
| 44 | }),
|
---|
| 45 | m => getShortModuleName(m, context, root),
|
---|
| 46 | (m, shortName) => getLongModuleName(shortName, m, context, root),
|
---|
| 47 | compareModulesByIdentifier,
|
---|
| 48 | getUsedModuleIds(compilation),
|
---|
| 49 | (m, name) => chunkGraph.setModuleId(m, name)
|
---|
| 50 | );
|
---|
| 51 | if (unnamedModules.length > 0) {
|
---|
| 52 | assignAscendingModuleIds(unnamedModules, compilation);
|
---|
| 53 | }
|
---|
| 54 | });
|
---|
| 55 | });
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | module.exports = NamedModuleIdsPlugin;
|
---|