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 { compareChunksNatural } = require("../util/comparators");
|
---|
9 | const {
|
---|
10 | getShortChunkName,
|
---|
11 | getLongChunkName,
|
---|
12 | assignNames,
|
---|
13 | getUsedChunkIds,
|
---|
14 | assignAscendingChunkIds
|
---|
15 | } = require("./IdHelpers");
|
---|
16 |
|
---|
17 | /** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} Output */
|
---|
18 | /** @typedef {import("../Chunk")} Chunk */
|
---|
19 | /** @typedef {import("../Compiler")} Compiler */
|
---|
20 | /** @typedef {import("../Module")} Module */
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * @typedef {object} NamedChunkIdsPluginOptions
|
---|
24 | * @property {string} [context] context
|
---|
25 | * @property {string} [delimiter] delimiter
|
---|
26 | */
|
---|
27 |
|
---|
28 | class NamedChunkIdsPlugin {
|
---|
29 | /**
|
---|
30 | * @param {NamedChunkIdsPluginOptions=} options options
|
---|
31 | */
|
---|
32 | constructor(options) {
|
---|
33 | this.delimiter = (options && options.delimiter) || "-";
|
---|
34 | this.context = options && options.context;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Apply the plugin
|
---|
39 | * @param {Compiler} compiler the compiler instance
|
---|
40 | * @returns {void}
|
---|
41 | */
|
---|
42 | apply(compiler) {
|
---|
43 | compiler.hooks.compilation.tap("NamedChunkIdsPlugin", compilation => {
|
---|
44 | const hashFunction =
|
---|
45 | /** @type {NonNullable<Output["hashFunction"]>} */
|
---|
46 | (compilation.outputOptions.hashFunction);
|
---|
47 | compilation.hooks.chunkIds.tap("NamedChunkIdsPlugin", chunks => {
|
---|
48 | const chunkGraph = compilation.chunkGraph;
|
---|
49 | const context = this.context ? this.context : compiler.context;
|
---|
50 | const delimiter = this.delimiter;
|
---|
51 |
|
---|
52 | const unnamedChunks = assignNames(
|
---|
53 | Array.from(chunks).filter(chunk => {
|
---|
54 | if (chunk.name) {
|
---|
55 | chunk.id = chunk.name;
|
---|
56 | chunk.ids = [chunk.name];
|
---|
57 | }
|
---|
58 | return chunk.id === null;
|
---|
59 | }),
|
---|
60 | chunk =>
|
---|
61 | getShortChunkName(
|
---|
62 | chunk,
|
---|
63 | chunkGraph,
|
---|
64 | context,
|
---|
65 | delimiter,
|
---|
66 | hashFunction,
|
---|
67 | compiler.root
|
---|
68 | ),
|
---|
69 | chunk =>
|
---|
70 | getLongChunkName(
|
---|
71 | chunk,
|
---|
72 | chunkGraph,
|
---|
73 | context,
|
---|
74 | delimiter,
|
---|
75 | hashFunction,
|
---|
76 | compiler.root
|
---|
77 | ),
|
---|
78 | compareChunksNatural(chunkGraph),
|
---|
79 | getUsedChunkIds(compilation),
|
---|
80 | (chunk, name) => {
|
---|
81 | chunk.id = name;
|
---|
82 | chunk.ids = [name];
|
---|
83 | }
|
---|
84 | );
|
---|
85 | if (unnamedChunks.length > 0) {
|
---|
86 | assignAscendingChunkIds(unnamedChunks, compilation);
|
---|
87 | }
|
---|
88 | });
|
---|
89 | });
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | module.exports = NamedChunkIdsPlugin;
|
---|