source: frontend/node_modules/webpack/lib/prefetch/ChunkPrefetchPreloadPlugin.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("../RuntimeGlobals");
9const ChunkPrefetchFunctionRuntimeModule = require("./ChunkPrefetchFunctionRuntimeModule");
10const ChunkPrefetchStartupRuntimeModule = require("./ChunkPrefetchStartupRuntimeModule");
11const ChunkPrefetchTriggerRuntimeModule = require("./ChunkPrefetchTriggerRuntimeModule");
12const ChunkPreloadTriggerRuntimeModule = require("./ChunkPreloadTriggerRuntimeModule");
13
14/** @typedef {import("../Compiler")} Compiler */
15
16const PLUGIN_NAME = "ChunkPrefetchPreloadPlugin";
17
18/**
19 * Adds runtime support for chunk prefetch and preload relationships discovered
20 * in the chunk graph.
21 */
22class ChunkPrefetchPreloadPlugin {
23 /**
24 * Registers compilation hooks that emit the runtime modules responsible for
25 * scheduling chunk prefetch and preload requests.
26 * @param {Compiler} compiler the compiler
27 * @returns {void}
28 */
29 apply(compiler) {
30 compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
31 compilation.hooks.additionalChunkRuntimeRequirements.tap(
32 PLUGIN_NAME,
33 (chunk, set, { chunkGraph }) => {
34 if (chunkGraph.getNumberOfEntryModules(chunk) === 0) return;
35 const startupChildChunks = chunk.getChildrenOfTypeInOrder(
36 chunkGraph,
37 "prefetchOrder"
38 );
39 if (startupChildChunks) {
40 set.add(RuntimeGlobals.prefetchChunk);
41 set.add(RuntimeGlobals.onChunksLoaded);
42 set.add(RuntimeGlobals.exports);
43 compilation.addRuntimeModule(
44 chunk,
45 new ChunkPrefetchStartupRuntimeModule(startupChildChunks)
46 );
47 }
48 }
49 );
50 compilation.hooks.additionalTreeRuntimeRequirements.tap(
51 PLUGIN_NAME,
52 (chunk, set, { chunkGraph }) => {
53 const chunkMap = chunk.getChildIdsByOrdersMap(chunkGraph);
54
55 if (chunkMap.prefetch) {
56 set.add(RuntimeGlobals.prefetchChunk);
57 compilation.addRuntimeModule(
58 chunk,
59 new ChunkPrefetchTriggerRuntimeModule(chunkMap.prefetch)
60 );
61 }
62 if (chunkMap.preload) {
63 set.add(RuntimeGlobals.preloadChunk);
64 compilation.addRuntimeModule(
65 chunk,
66 new ChunkPreloadTriggerRuntimeModule(chunkMap.preload)
67 );
68 }
69 }
70 );
71 compilation.hooks.runtimeRequirementInTree
72 .for(RuntimeGlobals.prefetchChunk)
73 .tap(PLUGIN_NAME, (chunk, set) => {
74 compilation.addRuntimeModule(
75 chunk,
76 new ChunkPrefetchFunctionRuntimeModule(
77 "prefetch",
78 RuntimeGlobals.prefetchChunk,
79 RuntimeGlobals.prefetchChunkHandlers
80 )
81 );
82 set.add(RuntimeGlobals.prefetchChunkHandlers);
83 });
84 compilation.hooks.runtimeRequirementInTree
85 .for(RuntimeGlobals.preloadChunk)
86 .tap(PLUGIN_NAME, (chunk, set) => {
87 compilation.addRuntimeModule(
88 chunk,
89 new ChunkPrefetchFunctionRuntimeModule(
90 "preload",
91 RuntimeGlobals.preloadChunk,
92 RuntimeGlobals.preloadChunkHandlers
93 )
94 );
95 set.add(RuntimeGlobals.preloadChunkHandlers);
96 });
97 });
98 }
99}
100
101module.exports = ChunkPrefetchPreloadPlugin;
Note: See TracBrowser for help on using the repository browser.