1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const RuntimeGlobals = require("../RuntimeGlobals");
|
9 | const ChunkPrefetchFunctionRuntimeModule = require("./ChunkPrefetchFunctionRuntimeModule");
|
10 | const ChunkPrefetchStartupRuntimeModule = require("./ChunkPrefetchStartupRuntimeModule");
|
11 | const ChunkPrefetchTriggerRuntimeModule = require("./ChunkPrefetchTriggerRuntimeModule");
|
12 | const ChunkPreloadTriggerRuntimeModule = require("./ChunkPreloadTriggerRuntimeModule");
|
13 |
|
14 |
|
15 |
|
16 | class ChunkPrefetchPreloadPlugin {
|
17 | |
18 |
|
19 |
|
20 |
|
21 | apply(compiler) {
|
22 | compiler.hooks.compilation.tap(
|
23 | "ChunkPrefetchPreloadPlugin",
|
24 | compilation => {
|
25 | compilation.hooks.additionalChunkRuntimeRequirements.tap(
|
26 | "ChunkPrefetchPreloadPlugin",
|
27 | (chunk, set, { chunkGraph }) => {
|
28 | if (chunkGraph.getNumberOfEntryModules(chunk) === 0) return;
|
29 | const startupChildChunks = chunk.getChildrenOfTypeInOrder(
|
30 | chunkGraph,
|
31 | "prefetchOrder"
|
32 | );
|
33 | if (startupChildChunks) {
|
34 | set.add(RuntimeGlobals.prefetchChunk);
|
35 | set.add(RuntimeGlobals.onChunksLoaded);
|
36 | compilation.addRuntimeModule(
|
37 | chunk,
|
38 | new ChunkPrefetchStartupRuntimeModule(startupChildChunks)
|
39 | );
|
40 | }
|
41 | }
|
42 | );
|
43 | compilation.hooks.additionalTreeRuntimeRequirements.tap(
|
44 | "ChunkPrefetchPreloadPlugin",
|
45 | (chunk, set, { chunkGraph }) => {
|
46 | const chunkMap = chunk.getChildIdsByOrdersMap(chunkGraph, false);
|
47 |
|
48 | if (chunkMap.prefetch) {
|
49 | set.add(RuntimeGlobals.prefetchChunk);
|
50 | compilation.addRuntimeModule(
|
51 | chunk,
|
52 | new ChunkPrefetchTriggerRuntimeModule(chunkMap.prefetch)
|
53 | );
|
54 | }
|
55 | if (chunkMap.preload) {
|
56 | set.add(RuntimeGlobals.preloadChunk);
|
57 | compilation.addRuntimeModule(
|
58 | chunk,
|
59 | new ChunkPreloadTriggerRuntimeModule(chunkMap.preload)
|
60 | );
|
61 | }
|
62 | }
|
63 | );
|
64 | compilation.hooks.runtimeRequirementInTree
|
65 | .for(RuntimeGlobals.prefetchChunk)
|
66 | .tap("ChunkPrefetchPreloadPlugin", (chunk, set) => {
|
67 | compilation.addRuntimeModule(
|
68 | chunk,
|
69 | new ChunkPrefetchFunctionRuntimeModule(
|
70 | "prefetch",
|
71 | RuntimeGlobals.prefetchChunk,
|
72 | RuntimeGlobals.prefetchChunkHandlers
|
73 | )
|
74 | );
|
75 | set.add(RuntimeGlobals.prefetchChunkHandlers);
|
76 | });
|
77 | compilation.hooks.runtimeRequirementInTree
|
78 | .for(RuntimeGlobals.preloadChunk)
|
79 | .tap("ChunkPrefetchPreloadPlugin", (chunk, set) => {
|
80 | compilation.addRuntimeModule(
|
81 | chunk,
|
82 | new ChunkPrefetchFunctionRuntimeModule(
|
83 | "preload",
|
84 | RuntimeGlobals.preloadChunk,
|
85 | RuntimeGlobals.preloadChunkHandlers
|
86 | )
|
87 | );
|
88 | set.add(RuntimeGlobals.preloadChunkHandlers);
|
89 | });
|
90 | }
|
91 | );
|
92 | }
|
93 | }
|
94 |
|
95 | module.exports = ChunkPrefetchPreloadPlugin;
|