ChunkPrefetchPreloadPlugin.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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("../Chunk")} Chunk */
  12. /** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
  13. /** @typedef {import("../Compiler")} Compiler */
  14. class ChunkPrefetchPreloadPlugin {
  15. /**
  16. * @param {Compiler} compiler the compiler
  17. * @returns {void}
  18. */
  19. apply(compiler) {
  20. compiler.hooks.compilation.tap(
  21. "ChunkPrefetchPreloadPlugin",
  22. compilation => {
  23. compilation.hooks.additionalChunkRuntimeRequirements.tap(
  24. "ChunkPrefetchPreloadPlugin",
  25. (chunk, set, { chunkGraph }) => {
  26. if (chunkGraph.getNumberOfEntryModules(chunk) === 0) return;
  27. const startupChildChunks = chunk.getChildrenOfTypeInOrder(
  28. chunkGraph,
  29. "prefetchOrder"
  30. );
  31. if (startupChildChunks) {
  32. set.add(RuntimeGlobals.prefetchChunk);
  33. set.add(RuntimeGlobals.onChunksLoaded);
  34. set.add(RuntimeGlobals.exports);
  35. compilation.addRuntimeModule(
  36. chunk,
  37. new ChunkPrefetchStartupRuntimeModule(startupChildChunks)
  38. );
  39. }
  40. }
  41. );
  42. compilation.hooks.additionalTreeRuntimeRequirements.tap(
  43. "ChunkPrefetchPreloadPlugin",
  44. (chunk, set, { chunkGraph }) => {
  45. const chunkMap = chunk.getChildIdsByOrdersMap(chunkGraph);
  46. if (chunkMap.prefetch) {
  47. set.add(RuntimeGlobals.prefetchChunk);
  48. compilation.addRuntimeModule(
  49. chunk,
  50. new ChunkPrefetchTriggerRuntimeModule(chunkMap.prefetch)
  51. );
  52. }
  53. if (chunkMap.preload) {
  54. set.add(RuntimeGlobals.preloadChunk);
  55. compilation.addRuntimeModule(
  56. chunk,
  57. new ChunkPreloadTriggerRuntimeModule(chunkMap.preload)
  58. );
  59. }
  60. }
  61. );
  62. compilation.hooks.runtimeRequirementInTree
  63. .for(RuntimeGlobals.prefetchChunk)
  64. .tap("ChunkPrefetchPreloadPlugin", (chunk, set) => {
  65. compilation.addRuntimeModule(
  66. chunk,
  67. new ChunkPrefetchFunctionRuntimeModule(
  68. "prefetch",
  69. RuntimeGlobals.prefetchChunk,
  70. RuntimeGlobals.prefetchChunkHandlers
  71. )
  72. );
  73. set.add(RuntimeGlobals.prefetchChunkHandlers);
  74. });
  75. compilation.hooks.runtimeRequirementInTree
  76. .for(RuntimeGlobals.preloadChunk)
  77. .tap("ChunkPrefetchPreloadPlugin", (chunk, set) => {
  78. compilation.addRuntimeModule(
  79. chunk,
  80. new ChunkPrefetchFunctionRuntimeModule(
  81. "preload",
  82. RuntimeGlobals.preloadChunk,
  83. RuntimeGlobals.preloadChunkHandlers
  84. )
  85. );
  86. set.add(RuntimeGlobals.preloadChunkHandlers);
  87. });
  88. }
  89. );
  90. }
  91. }
  92. module.exports = ChunkPrefetchPreloadPlugin;