JsonpChunkLoadingPlugin.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 JsonpChunkLoadingRuntimeModule = require("./JsonpChunkLoadingRuntimeModule");
  8. /** @typedef {import("../Chunk")} Chunk */
  9. /** @typedef {import("../Compiler")} Compiler */
  10. /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
  11. const PLUGIN_NAME = "JsonpChunkLoadingPlugin";
  12. /**
  13. * Enables browser-side JavaScript chunk loading through the JSONP runtime and
  14. * adds the supporting runtime requirements for matching chunks.
  15. */
  16. class JsonpChunkLoadingPlugin {
  17. /**
  18. * Registers compilation hooks that attach the JSONP chunk-loading runtime
  19. * module and its dependent runtime globals to chunks using `chunkLoading:
  20. * "jsonp"`.
  21. * @param {Compiler} compiler the compiler instance
  22. * @returns {void}
  23. */
  24. apply(compiler) {
  25. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
  26. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  27. /**
  28. * Determines whether the chunk resolves JavaScript chunks through the
  29. * JSONP loading backend.
  30. * @param {Chunk} chunk chunk
  31. * @returns {boolean} true, if wasm loading is enabled for the chunk
  32. */
  33. const isEnabledForChunk = (chunk) => {
  34. const options = chunk.getEntryOptions();
  35. const chunkLoading =
  36. options && options.chunkLoading !== undefined
  37. ? options.chunkLoading
  38. : globalChunkLoading;
  39. return chunkLoading === "jsonp";
  40. };
  41. /** @type {WeakSet<Chunk>} */
  42. const onceForChunkSet = new WeakSet();
  43. /**
  44. * Adds the JSONP runtime module to a chunk once, along with the core
  45. * runtime globals it relies on.
  46. * @param {Chunk} chunk chunk
  47. * @param {RuntimeRequirements} set runtime requirements
  48. */
  49. const handler = (chunk, set) => {
  50. if (onceForChunkSet.has(chunk)) return;
  51. onceForChunkSet.add(chunk);
  52. if (!isEnabledForChunk(chunk)) return;
  53. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  54. set.add(RuntimeGlobals.hasOwnProperty);
  55. compilation.addRuntimeModule(
  56. chunk,
  57. new JsonpChunkLoadingRuntimeModule(set)
  58. );
  59. };
  60. compilation.hooks.runtimeRequirementInTree
  61. .for(RuntimeGlobals.ensureChunkHandlers)
  62. .tap(PLUGIN_NAME, handler);
  63. compilation.hooks.runtimeRequirementInTree
  64. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  65. .tap(PLUGIN_NAME, handler);
  66. compilation.hooks.runtimeRequirementInTree
  67. .for(RuntimeGlobals.hmrDownloadManifest)
  68. .tap(PLUGIN_NAME, handler);
  69. compilation.hooks.runtimeRequirementInTree
  70. .for(RuntimeGlobals.baseURI)
  71. .tap(PLUGIN_NAME, handler);
  72. compilation.hooks.runtimeRequirementInTree
  73. .for(RuntimeGlobals.onChunksLoaded)
  74. .tap(PLUGIN_NAME, handler);
  75. compilation.hooks.runtimeRequirementInTree
  76. .for(RuntimeGlobals.ensureChunkHandlers)
  77. .tap(PLUGIN_NAME, (chunk, set) => {
  78. if (!isEnabledForChunk(chunk)) return;
  79. set.add(RuntimeGlobals.publicPath);
  80. set.add(RuntimeGlobals.loadScript);
  81. set.add(RuntimeGlobals.getChunkScriptFilename);
  82. });
  83. compilation.hooks.runtimeRequirementInTree
  84. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  85. .tap(PLUGIN_NAME, (chunk, set) => {
  86. if (!isEnabledForChunk(chunk)) return;
  87. set.add(RuntimeGlobals.publicPath);
  88. set.add(RuntimeGlobals.loadScript);
  89. set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
  90. set.add(RuntimeGlobals.moduleCache);
  91. set.add(RuntimeGlobals.hmrModuleData);
  92. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  93. });
  94. compilation.hooks.runtimeRequirementInTree
  95. .for(RuntimeGlobals.hmrDownloadManifest)
  96. .tap(PLUGIN_NAME, (chunk, set) => {
  97. if (!isEnabledForChunk(chunk)) return;
  98. set.add(RuntimeGlobals.publicPath);
  99. set.add(RuntimeGlobals.getUpdateManifestFilename);
  100. });
  101. });
  102. }
  103. }
  104. module.exports = JsonpChunkLoadingPlugin;