ModuleChunkLoadingPlugin.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. class ModuleChunkLoadingPlugin {
  12. /**
  13. * Apply the plugin
  14. * @param {Compiler} compiler the compiler instance
  15. * @returns {void}
  16. */
  17. apply(compiler) {
  18. compiler.hooks.thisCompilation.tap(
  19. "ModuleChunkLoadingPlugin",
  20. compilation => {
  21. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  22. /**
  23. * @param {Chunk} chunk chunk to check
  24. * @returns {boolean} true, when the plugin is enabled for the chunk
  25. */
  26. const isEnabledForChunk = chunk => {
  27. const options = chunk.getEntryOptions();
  28. const chunkLoading =
  29. options && options.chunkLoading !== undefined
  30. ? options.chunkLoading
  31. : globalChunkLoading;
  32. return chunkLoading === "import";
  33. };
  34. const onceForChunkSet = new WeakSet();
  35. /**
  36. * @param {Chunk} chunk chunk to check
  37. * @param {Set<string>} set runtime requirements
  38. */
  39. const handler = (chunk, set) => {
  40. if (onceForChunkSet.has(chunk)) return;
  41. onceForChunkSet.add(chunk);
  42. if (!isEnabledForChunk(chunk)) return;
  43. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  44. set.add(RuntimeGlobals.hasOwnProperty);
  45. compilation.addRuntimeModule(
  46. chunk,
  47. new ModuleChunkLoadingRuntimeModule(set)
  48. );
  49. };
  50. compilation.hooks.runtimeRequirementInTree
  51. .for(RuntimeGlobals.ensureChunkHandlers)
  52. .tap("ModuleChunkLoadingPlugin", handler);
  53. compilation.hooks.runtimeRequirementInTree
  54. .for(RuntimeGlobals.baseURI)
  55. .tap("ModuleChunkLoadingPlugin", handler);
  56. compilation.hooks.runtimeRequirementInTree
  57. .for(RuntimeGlobals.externalInstallChunk)
  58. .tap("ModuleChunkLoadingPlugin", handler);
  59. compilation.hooks.runtimeRequirementInTree
  60. .for(RuntimeGlobals.onChunksLoaded)
  61. .tap("ModuleChunkLoadingPlugin", handler);
  62. compilation.hooks.runtimeRequirementInTree
  63. .for(RuntimeGlobals.externalInstallChunk)
  64. .tap("ModuleChunkLoadingPlugin", (chunk, set) => {
  65. if (!isEnabledForChunk(chunk)) return;
  66. compilation.addRuntimeModule(
  67. chunk,
  68. new ExportWebpackRequireRuntimeModule()
  69. );
  70. });
  71. compilation.hooks.runtimeRequirementInTree
  72. .for(RuntimeGlobals.ensureChunkHandlers)
  73. .tap("ModuleChunkLoadingPlugin", (chunk, set) => {
  74. if (!isEnabledForChunk(chunk)) return;
  75. set.add(RuntimeGlobals.getChunkScriptFilename);
  76. });
  77. }
  78. );
  79. }
  80. }
  81. module.exports = ModuleChunkLoadingPlugin;