StartupChunkDependenciesPlugin.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. const PLUGIN_NAME = "StartupChunkDependenciesPlugin";
  17. class StartupChunkDependenciesPlugin {
  18. /**
  19. * @param {Options} options options
  20. */
  21. constructor(options) {
  22. this.chunkLoading = options.chunkLoading;
  23. this.asyncChunkLoading =
  24. typeof options.asyncChunkLoading === "boolean"
  25. ? options.asyncChunkLoading
  26. : true;
  27. }
  28. /**
  29. * Apply the plugin
  30. * @param {Compiler} compiler the compiler instance
  31. * @returns {void}
  32. */
  33. apply(compiler) {
  34. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
  35. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  36. /**
  37. * @param {Chunk} chunk chunk to check
  38. * @returns {boolean} true, when the plugin is enabled for the chunk
  39. */
  40. const isEnabledForChunk = chunk => {
  41. const options = chunk.getEntryOptions();
  42. const chunkLoading =
  43. options && options.chunkLoading !== undefined
  44. ? options.chunkLoading
  45. : globalChunkLoading;
  46. return chunkLoading === this.chunkLoading;
  47. };
  48. compilation.hooks.additionalTreeRuntimeRequirements.tap(
  49. PLUGIN_NAME,
  50. (chunk, set, { chunkGraph }) => {
  51. if (!isEnabledForChunk(chunk)) return;
  52. if (chunkGraph.hasChunkEntryDependentChunks(chunk)) {
  53. set.add(RuntimeGlobals.startup);
  54. set.add(RuntimeGlobals.ensureChunk);
  55. set.add(RuntimeGlobals.ensureChunkIncludeEntries);
  56. compilation.addRuntimeModule(
  57. chunk,
  58. new StartupChunkDependenciesRuntimeModule(this.asyncChunkLoading)
  59. );
  60. }
  61. }
  62. );
  63. compilation.hooks.runtimeRequirementInTree
  64. .for(RuntimeGlobals.startupEntrypoint)
  65. .tap(PLUGIN_NAME, (chunk, set) => {
  66. if (!isEnabledForChunk(chunk)) return;
  67. set.add(RuntimeGlobals.require);
  68. set.add(RuntimeGlobals.ensureChunk);
  69. set.add(RuntimeGlobals.ensureChunkIncludeEntries);
  70. compilation.addRuntimeModule(
  71. chunk,
  72. new StartupEntrypointRuntimeModule(this.asyncChunkLoading)
  73. );
  74. });
  75. });
  76. }
  77. }
  78. module.exports = StartupChunkDependenciesPlugin;