JsonpChunkLoadingPlugin.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 lazyModule = require("../util/lazyModule");
  8. /** @import Chunk from "../Chunk" */
  9. /** @import Compiler from "../Compiler" */
  10. /** @import { RuntimeRequirements } from "../Module" */
  11. // only a chunk that loads other chunks at runtime emits this
  12. const getJsonpChunkLoadingRuntimeModule = lazyModule(() =>
  13. require("./JsonpChunkLoadingRuntimeModule")
  14. );
  15. const PLUGIN_NAME = "JsonpChunkLoadingPlugin";
  16. /**
  17. * Enables browser-side JavaScript chunk loading through the JSONP runtime and
  18. * adds the supporting runtime requirements for matching chunks.
  19. */
  20. class JsonpChunkLoadingPlugin {
  21. /**
  22. * Registers compilation hooks that attach the JSONP chunk-loading runtime
  23. * module and its dependent runtime globals to chunks using `chunkLoading:
  24. * "jsonp"`.
  25. * @param {Compiler} compiler the compiler instance
  26. * @returns {void}
  27. */
  28. apply(compiler) {
  29. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
  30. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  31. /**
  32. * Determines whether the chunk resolves JavaScript chunks through the
  33. * JSONP loading backend.
  34. * @param {Chunk} chunk chunk
  35. * @returns {boolean} true, if wasm loading is enabled for the chunk
  36. */
  37. const isEnabledForChunk = (chunk) => {
  38. const options = chunk.getEntryOptions();
  39. const chunkLoading =
  40. options && options.chunkLoading !== undefined
  41. ? options.chunkLoading
  42. : globalChunkLoading;
  43. return chunkLoading === "jsonp";
  44. };
  45. /** @type {WeakSet<Chunk>} */
  46. const onceForChunkSet = new WeakSet();
  47. /**
  48. * Adds the JSONP runtime module to a chunk once, along with the core
  49. * runtime globals it relies on.
  50. * @param {Chunk} chunk chunk
  51. * @param {RuntimeRequirements} set runtime requirements
  52. */
  53. const handler = (chunk, set) => {
  54. if (onceForChunkSet.has(chunk)) return;
  55. onceForChunkSet.add(chunk);
  56. if (!isEnabledForChunk(chunk)) return;
  57. compilation.addLazyRuntimeModule(
  58. chunk,
  59. getJsonpChunkLoadingRuntimeModule,
  60. (Ctor) => new Ctor(set)
  61. );
  62. };
  63. /**
  64. * Installing a chunk, on load or on a hot update, is the only part that writes
  65. * the module factories and looks a chunk id up; a base uri or `onChunksLoaded`
  66. * comes without either. Kept off `handler`, which runs once per chunk and so
  67. * cannot see which of the requirements below the chunk ends up with.
  68. * @param {Chunk} chunk chunk to check
  69. * @param {RuntimeRequirements} set runtime requirements
  70. */
  71. const installHandler = (chunk, set) => {
  72. if (!isEnabledForChunk(chunk)) return;
  73. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  74. set.add(RuntimeGlobals.hasOwnProperty);
  75. };
  76. for (const requirement of [
  77. RuntimeGlobals.ensureChunkHandlers,
  78. RuntimeGlobals.hmrDownloadUpdateHandlers,
  79. RuntimeGlobals.hmrDownloadManifest,
  80. RuntimeGlobals.baseURI,
  81. RuntimeGlobals.onChunksLoaded
  82. ]) {
  83. compilation.hooks.runtimeRequirementInTree
  84. .for(requirement)
  85. .tap(PLUGIN_NAME, handler);
  86. }
  87. for (const requirement of [
  88. RuntimeGlobals.ensureChunkHandlers,
  89. RuntimeGlobals.chunkCallback,
  90. RuntimeGlobals.hmrDownloadUpdateHandlers,
  91. RuntimeGlobals.prefetchChunkHandlers,
  92. RuntimeGlobals.preloadChunkHandlers
  93. ]) {
  94. compilation.hooks.runtimeRequirementInTree
  95. .for(requirement)
  96. .tap(PLUGIN_NAME, installHandler);
  97. }
  98. compilation.hooks.runtimeRequirementInTree
  99. .for(RuntimeGlobals.ensureChunkHandlers)
  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.getChunkScriptFilename);
  105. });
  106. compilation.hooks.runtimeRequirementInTree
  107. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  108. .tap(PLUGIN_NAME, (chunk, set) => {
  109. if (!isEnabledForChunk(chunk)) return;
  110. set.add(RuntimeGlobals.publicPath);
  111. set.add(RuntimeGlobals.loadScript);
  112. set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
  113. set.add(RuntimeGlobals.moduleCache);
  114. set.add(RuntimeGlobals.hmrModuleData);
  115. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  116. });
  117. compilation.hooks.runtimeRequirementInTree
  118. .for(RuntimeGlobals.hmrDownloadManifest)
  119. .tap(PLUGIN_NAME, (chunk, set) => {
  120. if (!isEnabledForChunk(chunk)) return;
  121. set.add(RuntimeGlobals.publicPath);
  122. set.add(RuntimeGlobals.getUpdateManifestFilename);
  123. });
  124. });
  125. }
  126. }
  127. module.exports = JsonpChunkLoadingPlugin;