StartupChunkDependenciesPlugin.js 2.2 KB

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