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 | getUsedModuleIdsAndModules,
|
---|
14 | assignAscendingModuleIds
|
---|
15 | } = require("./IdHelpers");
|
---|
16 |
|
---|
17 | /** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} Output */
|
---|
18 | /** @typedef {import("../Compiler")} Compiler */
|
---|
19 | /** @typedef {import("../Module")} Module */
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * @typedef {object} NamedModuleIdsPluginOptions
|
---|
23 | * @property {string} [context] context
|
---|
24 | */
|
---|
25 |
|
---|
26 | class NamedModuleIdsPlugin {
|
---|
27 | /**
|
---|
28 | * @param {NamedModuleIdsPluginOptions} [options] options
|
---|
29 | */
|
---|
30 | constructor(options = {}) {
|
---|
31 | this.options = options;
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Apply the plugin
|
---|
36 | * @param {Compiler} compiler the compiler instance
|
---|
37 | * @returns {void}
|
---|
38 | */
|
---|
39 | apply(compiler) {
|
---|
40 | const { root } = compiler;
|
---|
41 | compiler.hooks.compilation.tap("NamedModuleIdsPlugin", compilation => {
|
---|
42 | const hashFunction =
|
---|
43 | /** @type {NonNullable<Output["hashFunction"]>} */
|
---|
44 | (compilation.outputOptions.hashFunction);
|
---|
45 | compilation.hooks.moduleIds.tap("NamedModuleIdsPlugin", () => {
|
---|
46 | const chunkGraph = compilation.chunkGraph;
|
---|
47 | const context = this.options.context
|
---|
48 | ? this.options.context
|
---|
49 | : compiler.context;
|
---|
50 |
|
---|
51 | const [usedIds, modules] = getUsedModuleIdsAndModules(compilation);
|
---|
52 | const unnamedModules = assignNames(
|
---|
53 | modules,
|
---|
54 | m => getShortModuleName(m, context, root),
|
---|
55 | (m, shortName) =>
|
---|
56 | getLongModuleName(shortName, m, context, hashFunction, root),
|
---|
57 | compareModulesByIdentifier,
|
---|
58 | usedIds,
|
---|
59 | (m, name) => chunkGraph.setModuleId(m, name)
|
---|
60 | );
|
---|
61 | if (unnamedModules.length > 0) {
|
---|
62 | assignAscendingModuleIds(usedIds, unnamedModules, compilation);
|
---|
63 | }
|
---|
64 | });
|
---|
65 | });
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | module.exports = NamedModuleIdsPlugin;
|
---|