ChunkPrefetchPreloadPlugin.js 2.9 KB

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