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 { STAGE_BASIC } = require("../OptimizationStages");
|
---|
9 |
|
---|
10 | /** @typedef {import("../Chunk")} Chunk */
|
---|
11 | /** @typedef {import("../ChunkGroup")} ChunkGroup */
|
---|
12 | /** @typedef {import("../Compiler")} Compiler */
|
---|
13 |
|
---|
14 | class EnsureChunkConditionsPlugin {
|
---|
15 | /**
|
---|
16 | * Apply the plugin
|
---|
17 | * @param {Compiler} compiler the compiler instance
|
---|
18 | * @returns {void}
|
---|
19 | */
|
---|
20 | apply(compiler) {
|
---|
21 | compiler.hooks.compilation.tap(
|
---|
22 | "EnsureChunkConditionsPlugin",
|
---|
23 | compilation => {
|
---|
24 | const handler = chunks => {
|
---|
25 | const chunkGraph = compilation.chunkGraph;
|
---|
26 | // These sets are hoisted here to save memory
|
---|
27 | // They are cleared at the end of every loop
|
---|
28 | /** @type {Set<Chunk>} */
|
---|
29 | const sourceChunks = new Set();
|
---|
30 | /** @type {Set<ChunkGroup>} */
|
---|
31 | const chunkGroups = new Set();
|
---|
32 | for (const module of compilation.modules) {
|
---|
33 | for (const chunk of chunkGraph.getModuleChunksIterable(module)) {
|
---|
34 | if (!module.chunkCondition(chunk, compilation)) {
|
---|
35 | sourceChunks.add(chunk);
|
---|
36 | for (const group of chunk.groupsIterable) {
|
---|
37 | chunkGroups.add(group);
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|
41 | if (sourceChunks.size === 0) continue;
|
---|
42 | /** @type {Set<Chunk>} */
|
---|
43 | const targetChunks = new Set();
|
---|
44 | chunkGroupLoop: for (const chunkGroup of chunkGroups) {
|
---|
45 | // Can module be placed in a chunk of this group?
|
---|
46 | for (const chunk of chunkGroup.chunks) {
|
---|
47 | if (module.chunkCondition(chunk, compilation)) {
|
---|
48 | targetChunks.add(chunk);
|
---|
49 | continue chunkGroupLoop;
|
---|
50 | }
|
---|
51 | }
|
---|
52 | // We reached the entrypoint: fail
|
---|
53 | if (chunkGroup.isInitial()) {
|
---|
54 | throw new Error(
|
---|
55 | "Cannot fullfil chunk condition of " + module.identifier()
|
---|
56 | );
|
---|
57 | }
|
---|
58 | // Try placing in all parents
|
---|
59 | for (const group of chunkGroup.parentsIterable) {
|
---|
60 | chunkGroups.add(group);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | for (const sourceChunk of sourceChunks) {
|
---|
64 | chunkGraph.disconnectChunkAndModule(sourceChunk, module);
|
---|
65 | }
|
---|
66 | for (const targetChunk of targetChunks) {
|
---|
67 | chunkGraph.connectChunkAndModule(targetChunk, module);
|
---|
68 | }
|
---|
69 | sourceChunks.clear();
|
---|
70 | chunkGroups.clear();
|
---|
71 | }
|
---|
72 | };
|
---|
73 | compilation.hooks.optimizeChunks.tap(
|
---|
74 | {
|
---|
75 | name: "EnsureChunkConditionsPlugin",
|
---|
76 | stage: STAGE_BASIC
|
---|
77 | },
|
---|
78 | handler
|
---|
79 | );
|
---|
80 | }
|
---|
81 | );
|
---|
82 | }
|
---|
83 | }
|
---|
84 | module.exports = EnsureChunkConditionsPlugin;
|
---|