1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { STAGE_BASIC, STAGE_ADVANCED } = require("../OptimizationStages");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | class RemoveEmptyChunksPlugin {
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 | apply(compiler) {
|
20 | compiler.hooks.compilation.tap("RemoveEmptyChunksPlugin", compilation => {
|
21 | |
22 |
|
23 |
|
24 |
|
25 | const handler = chunks => {
|
26 | const chunkGraph = compilation.chunkGraph;
|
27 | for (const chunk of chunks) {
|
28 | if (
|
29 | chunkGraph.getNumberOfChunkModules(chunk) === 0 &&
|
30 | !chunk.hasRuntime() &&
|
31 | chunkGraph.getNumberOfEntryModules(chunk) === 0
|
32 | ) {
|
33 | compilation.chunkGraph.disconnectChunk(chunk);
|
34 | compilation.chunks.delete(chunk);
|
35 | }
|
36 | }
|
37 | };
|
38 |
|
39 |
|
40 | compilation.hooks.optimizeChunks.tap(
|
41 | {
|
42 | name: "RemoveEmptyChunksPlugin",
|
43 | stage: STAGE_BASIC
|
44 | },
|
45 | handler
|
46 | );
|
47 | compilation.hooks.optimizeChunks.tap(
|
48 | {
|
49 | name: "RemoveEmptyChunksPlugin",
|
50 | stage: STAGE_ADVANCED
|
51 | },
|
52 | handler
|
53 | );
|
54 | });
|
55 | }
|
56 | }
|
57 | module.exports = RemoveEmptyChunksPlugin;
|