ModuleChunkLoadingPlugin.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 ExportWebpackRequireRuntimeModule = require("./ExportWebpackRequireRuntimeModule");
  8. const ModuleChunkLoadingRuntimeModule = require("./ModuleChunkLoadingRuntimeModule");
  9. /** @typedef {import("../Chunk")} Chunk */
  10. /** @typedef {import("../Compiler")} Compiler */
  11. const PLUGIN_NAME = "ModuleChunkLoadingPlugin";
  12. class ModuleChunkLoadingPlugin {
  13. /**
  14. * Apply the plugin
  15. * @param {Compiler} compiler the compiler instance
  16. * @returns {void}
  17. */
  18. apply(compiler) {
  19. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
  20. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  21. /**
  22. * @param {Chunk} chunk chunk to check
  23. * @returns {boolean} true, when the plugin is enabled for the chunk
  24. */
  25. const isEnabledForChunk = (chunk) => {
  26. const options = chunk.getEntryOptions();
  27. const chunkLoading =
  28. options && options.chunkLoading !== undefined
  29. ? options.chunkLoading
  30. : globalChunkLoading;
  31. return chunkLoading === "import";
  32. };
  33. const onceForChunkSet = new WeakSet();
  34. /**
  35. * @param {Chunk} chunk chunk to check
  36. * @param {Set<string>} set runtime requirements
  37. */
  38. const handler = (chunk, set) => {
  39. if (onceForChunkSet.has(chunk)) return;
  40. onceForChunkSet.add(chunk);
  41. if (!isEnabledForChunk(chunk)) return;
  42. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  43. set.add(RuntimeGlobals.hasOwnProperty);
  44. compilation.addRuntimeModule(
  45. chunk,
  46. new ModuleChunkLoadingRuntimeModule(set)
  47. );
  48. };
  49. compilation.hooks.runtimeRequirementInTree
  50. .for(RuntimeGlobals.ensureChunkHandlers)
  51. .tap(PLUGIN_NAME, handler);
  52. compilation.hooks.runtimeRequirementInTree
  53. .for(RuntimeGlobals.baseURI)
  54. .tap(PLUGIN_NAME, handler);
  55. compilation.hooks.runtimeRequirementInTree
  56. .for(RuntimeGlobals.externalInstallChunk)
  57. .tap(PLUGIN_NAME, handler);
  58. compilation.hooks.runtimeRequirementInTree
  59. .for(RuntimeGlobals.onChunksLoaded)
  60. .tap(PLUGIN_NAME, handler);
  61. compilation.hooks.runtimeRequirementInTree
  62. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  63. .tap(PLUGIN_NAME, handler);
  64. compilation.hooks.runtimeRequirementInTree
  65. .for(RuntimeGlobals.hmrDownloadManifest)
  66. .tap(PLUGIN_NAME, handler);
  67. compilation.hooks.runtimeRequirementInTree
  68. .for(RuntimeGlobals.externalInstallChunk)
  69. .tap(PLUGIN_NAME, (chunk) => {
  70. if (!isEnabledForChunk(chunk)) return;
  71. compilation.addRuntimeModule(
  72. chunk,
  73. new ExportWebpackRequireRuntimeModule()
  74. );
  75. });
  76. // We need public path only when we prefetch/preload chunk or public path is not `auto`
  77. compilation.hooks.runtimeRequirementInTree
  78. .for(RuntimeGlobals.prefetchChunkHandlers)
  79. .tap(PLUGIN_NAME, (chunk, set) => {
  80. if (!isEnabledForChunk(chunk)) return;
  81. set.add(RuntimeGlobals.publicPath);
  82. });
  83. compilation.hooks.runtimeRequirementInTree
  84. .for(RuntimeGlobals.preloadChunkHandlers)
  85. .tap(PLUGIN_NAME, (chunk, set) => {
  86. if (!isEnabledForChunk(chunk)) return;
  87. set.add(RuntimeGlobals.publicPath);
  88. });
  89. compilation.hooks.runtimeRequirementInTree
  90. .for(RuntimeGlobals.ensureChunkHandlers)
  91. .tap(PLUGIN_NAME, (chunk, set) => {
  92. if (!isEnabledForChunk(chunk)) return;
  93. if (compilation.outputOptions.publicPath !== "auto") {
  94. set.add(RuntimeGlobals.publicPath);
  95. }
  96. set.add(RuntimeGlobals.getChunkScriptFilename);
  97. });
  98. compilation.hooks.runtimeRequirementInTree
  99. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  100. .tap(PLUGIN_NAME, (chunk, set) => {
  101. if (!isEnabledForChunk(chunk)) return;
  102. set.add(RuntimeGlobals.publicPath);
  103. set.add(RuntimeGlobals.loadScript);
  104. set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
  105. set.add(RuntimeGlobals.moduleCache);
  106. set.add(RuntimeGlobals.hmrModuleData);
  107. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  108. });
  109. compilation.hooks.runtimeRequirementInTree
  110. .for(RuntimeGlobals.hmrDownloadManifest)
  111. .tap(PLUGIN_NAME, (chunk, set) => {
  112. if (!isEnabledForChunk(chunk)) return;
  113. set.add(RuntimeGlobals.publicPath);
  114. set.add(RuntimeGlobals.getUpdateManifestFilename);
  115. });
  116. compilation.hooks.additionalTreeRuntimeRequirements.tap(
  117. PLUGIN_NAME,
  118. (chunk, set, { chunkGraph }) => {
  119. if (chunkGraph.hasChunkEntryDependentChunks(chunk)) {
  120. set.add(RuntimeGlobals.externalInstallChunk);
  121. }
  122. }
  123. );
  124. });
  125. }
  126. }
  127. module.exports = ModuleChunkLoadingPlugin;