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("../Chunk")} Chunk */
|
---|
18 | /** @typedef {import("../Compiler")} Compiler */
|
---|
19 | /** @typedef {import("../Module")} Module */
|
---|
20 |
|
---|
21 | class NamedChunkIdsPlugin {
|
---|
22 | constructor(options) {
|
---|
23 | this.delimiter = (options && options.delimiter) || "-";
|
---|
24 | this.context = options && options.context;
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Apply the plugin
|
---|
29 | * @param {Compiler} compiler the compiler instance
|
---|
30 | * @returns {void}
|
---|
31 | */
|
---|
32 | apply(compiler) {
|
---|
33 | compiler.hooks.compilation.tap("NamedChunkIdsPlugin", compilation => {
|
---|
34 | compilation.hooks.chunkIds.tap("NamedChunkIdsPlugin", chunks => {
|
---|
35 | const chunkGraph = compilation.chunkGraph;
|
---|
36 | const context = this.context ? this.context : compiler.context;
|
---|
37 | const delimiter = this.delimiter;
|
---|
38 |
|
---|
39 | const unnamedChunks = assignNames(
|
---|
40 | Array.from(chunks).filter(chunk => {
|
---|
41 | if (chunk.name) {
|
---|
42 | chunk.id = chunk.name;
|
---|
43 | chunk.ids = [chunk.name];
|
---|
44 | }
|
---|
45 | return chunk.id === null;
|
---|
46 | }),
|
---|
47 | chunk =>
|
---|
48 | getShortChunkName(
|
---|
49 | chunk,
|
---|
50 | chunkGraph,
|
---|
51 | context,
|
---|
52 | delimiter,
|
---|
53 | compiler.root
|
---|
54 | ),
|
---|
55 | chunk =>
|
---|
56 | getLongChunkName(
|
---|
57 | chunk,
|
---|
58 | chunkGraph,
|
---|
59 | context,
|
---|
60 | delimiter,
|
---|
61 | compiler.root
|
---|
62 | ),
|
---|
63 | compareChunksNatural(chunkGraph),
|
---|
64 | getUsedChunkIds(compilation),
|
---|
65 | (chunk, name) => {
|
---|
66 | chunk.id = name;
|
---|
67 | chunk.ids = [name];
|
---|
68 | }
|
---|
69 | );
|
---|
70 | if (unnamedChunks.length > 0) {
|
---|
71 | assignAscendingChunkIds(unnamedChunks, compilation);
|
---|
72 | }
|
---|
73 | });
|
---|
74 | });
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | module.exports = NamedChunkIdsPlugin;
|
---|