StartupChunkDependenciesPlugin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const RuntimeGlobals = require("../RuntimeGlobals");
  6. const lazyModule = require("../util/lazyModule");
  7. /** @import { ChunkLoadingType } from "../../declarations/WebpackOptions" */
  8. /** @import Chunk from "../Chunk" */
  9. /** @import Compiler from "../Compiler" */
  10. // only a chunk with startup dependencies emits these
  11. const getStartupChunkDependenciesRuntimeModule = lazyModule(() =>
  12. require("./StartupChunkDependenciesRuntimeModule")
  13. );
  14. const getStartupEntrypointRuntimeModule = lazyModule(() =>
  15. require("./StartupEntrypointRuntimeModule")
  16. );
  17. /**
  18. * Options that describe which chunk loading backend should receive startup
  19. * dependency handling and whether the runtime should wait for those chunks
  20. * asynchronously.
  21. * @typedef {object} Options
  22. * @property {ChunkLoadingType} chunkLoading
  23. * @property {boolean=} asyncChunkLoading
  24. */
  25. const PLUGIN_NAME = "StartupChunkDependenciesPlugin";
  26. /**
  27. * Adds runtime modules that delay entry startup until entry-dependent chunks
  28. * required by the selected chunk loading strategy have been loaded.
  29. */
  30. class StartupChunkDependenciesPlugin {
  31. /**
  32. * Configures which chunk loading implementation this plugin should enhance
  33. * and whether startup waits should use promises or synchronous ensures.
  34. * @param {Options} options options
  35. */
  36. constructor(options) {
  37. /** @type {ChunkLoadingType} */
  38. this.chunkLoading = options.chunkLoading;
  39. /** @type {boolean} */
  40. this.asyncChunkLoading =
  41. typeof options.asyncChunkLoading === "boolean"
  42. ? options.asyncChunkLoading
  43. : true;
  44. }
  45. /**
  46. * Registers compilation hooks that attach the startup dependency runtime
  47. * modules to entry chunks using the configured chunk loading mechanism.
  48. * @param {Compiler} compiler the compiler instance
  49. * @returns {void}
  50. */
  51. apply(compiler) {
  52. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
  53. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  54. /**
  55. * Determines whether a chunk uses the chunk loading backend that this
  56. * plugin is responsible for augmenting.
  57. * @param {Chunk} chunk chunk to check
  58. * @returns {boolean} true, when the plugin is enabled for the chunk
  59. */
  60. const isEnabledForChunk = (chunk) => {
  61. const options = chunk.getEntryOptions();
  62. const chunkLoading =
  63. options && options.chunkLoading !== undefined
  64. ? options.chunkLoading
  65. : globalChunkLoading;
  66. return chunkLoading === this.chunkLoading;
  67. };
  68. compilation.hooks.additionalTreeRuntimeRequirements.tap(
  69. PLUGIN_NAME,
  70. (chunk, set, { chunkGraph }) => {
  71. if (!isEnabledForChunk(chunk)) return;
  72. if (chunkGraph.hasChunkEntryDependentChunks(chunk)) {
  73. set.add(RuntimeGlobals.startup);
  74. set.add(RuntimeGlobals.ensureChunk);
  75. set.add(RuntimeGlobals.ensureChunkIncludeEntries);
  76. compilation.addLazyRuntimeModule(
  77. chunk,
  78. getStartupChunkDependenciesRuntimeModule,
  79. (Ctor) => new Ctor(this.asyncChunkLoading)
  80. );
  81. }
  82. }
  83. );
  84. compilation.hooks.runtimeRequirementInTree
  85. .for(RuntimeGlobals.startupEntrypoint)
  86. .tap(PLUGIN_NAME, (chunk, set) => {
  87. if (!isEnabledForChunk(chunk)) return;
  88. set.add(RuntimeGlobals.require);
  89. set.add(RuntimeGlobals.ensureChunk);
  90. set.add(RuntimeGlobals.ensureChunkIncludeEntries);
  91. compilation.addLazyRuntimeModule(
  92. chunk,
  93. getStartupEntrypointRuntimeModule,
  94. (Ctor) => new Ctor(this.asyncChunkLoading)
  95. );
  96. });
  97. });
  98. }
  99. }
  100. module.exports = StartupChunkDependenciesPlugin;