StartupChunkDependenciesPlugin.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const RuntimeGlobals = require("../RuntimeGlobals");
  6. const StartupChunkDependenciesRuntimeModule = require("./StartupChunkDependenciesRuntimeModule");
  7. const StartupEntrypointRuntimeModule = require("./StartupEntrypointRuntimeModule");
  8. /** @typedef {import("../../declarations/WebpackOptions").ChunkLoadingType} ChunkLoadingType */
  9. /** @typedef {import("../Chunk")} Chunk */
  10. /** @typedef {import("../Compiler")} Compiler */
  11. /**
  12. * @typedef {object} Options
  13. * @property {ChunkLoadingType} chunkLoading
  14. * @property {boolean=} asyncChunkLoading
  15. */
  16. class StartupChunkDependenciesPlugin {
  17. /**
  18. * @param {Options} options options
  19. */
  20. constructor(options) {
  21. this.chunkLoading = options.chunkLoading;
  22. this.asyncChunkLoading =
  23. typeof options.asyncChunkLoading === "boolean"
  24. ? options.asyncChunkLoading
  25. : true;
  26. }
  27. /**
  28. * Apply the plugin
  29. * @param {Compiler} compiler the compiler instance
  30. * @returns {void}
  31. */
  32. apply(compiler) {
  33. compiler.hooks.thisCompilation.tap(
  34. "StartupChunkDependenciesPlugin",
  35. compilation => {
  36. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  37. /**
  38. * @param {Chunk} chunk chunk to check
  39. * @returns {boolean} true, when the plugin is enabled for the chunk
  40. */
  41. const isEnabledForChunk = chunk => {
  42. const options = chunk.getEntryOptions();
  43. const chunkLoading =
  44. options && options.chunkLoading !== undefined
  45. ? options.chunkLoading
  46. : globalChunkLoading;
  47. return chunkLoading === this.chunkLoading;
  48. };
  49. compilation.hooks.additionalTreeRuntimeRequirements.tap(
  50. "StartupChunkDependenciesPlugin",
  51. (chunk, set, { chunkGraph }) => {
  52. if (!isEnabledForChunk(chunk)) return;
  53. if (chunkGraph.hasChunkEntryDependentChunks(chunk)) {
  54. set.add(RuntimeGlobals.startup);
  55. set.add(RuntimeGlobals.ensureChunk);
  56. set.add(RuntimeGlobals.ensureChunkIncludeEntries);
  57. compilation.addRuntimeModule(
  58. chunk,
  59. new StartupChunkDependenciesRuntimeModule(
  60. this.asyncChunkLoading
  61. )
  62. );
  63. }
  64. }
  65. );
  66. compilation.hooks.runtimeRequirementInTree
  67. .for(RuntimeGlobals.startupEntrypoint)
  68. .tap("StartupChunkDependenciesPlugin", (chunk, set) => {
  69. if (!isEnabledForChunk(chunk)) return;
  70. set.add(RuntimeGlobals.require);
  71. set.add(RuntimeGlobals.ensureChunk);
  72. set.add(RuntimeGlobals.ensureChunkIncludeEntries);
  73. compilation.addRuntimeModule(
  74. chunk,
  75. new StartupEntrypointRuntimeModule(this.asyncChunkLoading)
  76. );
  77. });
  78. }
  79. );
  80. }
  81. }
  82. module.exports = StartupChunkDependenciesPlugin;