ChunkPrefetchPreloadPlugin.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. compilation.addRuntimeModule(
  35. chunk,
  36. new ChunkPrefetchStartupRuntimeModule(startupChildChunks)
  37. );
  38. }
  39. }
  40. );
  41. compilation.hooks.additionalTreeRuntimeRequirements.tap(
  42. "ChunkPrefetchPreloadPlugin",
  43. (chunk, set, { chunkGraph }) => {
  44. const chunkMap = chunk.getChildIdsByOrdersMap(chunkGraph);
  45. if (chunkMap.prefetch) {
  46. set.add(RuntimeGlobals.prefetchChunk);
  47. compilation.addRuntimeModule(
  48. chunk,
  49. new ChunkPrefetchTriggerRuntimeModule(chunkMap.prefetch)
  50. );
  51. }
  52. if (chunkMap.preload) {
  53. set.add(RuntimeGlobals.preloadChunk);
  54. compilation.addRuntimeModule(
  55. chunk,
  56. new ChunkPreloadTriggerRuntimeModule(chunkMap.preload)
  57. );
  58. }
  59. }
  60. );
  61. compilation.hooks.runtimeRequirementInTree
  62. .for(RuntimeGlobals.prefetchChunk)
  63. .tap("ChunkPrefetchPreloadPlugin", (chunk, set) => {
  64. compilation.addRuntimeModule(
  65. chunk,
  66. new ChunkPrefetchFunctionRuntimeModule(
  67. "prefetch",
  68. RuntimeGlobals.prefetchChunk,
  69. RuntimeGlobals.prefetchChunkHandlers
  70. )
  71. );
  72. set.add(RuntimeGlobals.prefetchChunkHandlers);
  73. });
  74. compilation.hooks.runtimeRequirementInTree
  75. .for(RuntimeGlobals.preloadChunk)
  76. .tap("ChunkPrefetchPreloadPlugin", (chunk, set) => {
  77. compilation.addRuntimeModule(
  78. chunk,
  79. new ChunkPrefetchFunctionRuntimeModule(
  80. "preload",
  81. RuntimeGlobals.preloadChunk,
  82. RuntimeGlobals.preloadChunkHandlers
  83. )
  84. );
  85. set.add(RuntimeGlobals.preloadChunkHandlers);
  86. });
  87. }
  88. );
  89. }
  90. }
  91. module.exports = ChunkPrefetchPreloadPlugin;