JsonpChunkLoadingPlugin.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. class JsonpChunkLoadingPlugin {
  11. /**
  12. * Apply the plugin
  13. * @param {Compiler} compiler the compiler instance
  14. * @returns {void}
  15. */
  16. apply(compiler) {
  17. compiler.hooks.thisCompilation.tap(
  18. "JsonpChunkLoadingPlugin",
  19. compilation => {
  20. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  21. /**
  22. * @param {Chunk} chunk chunk
  23. * @returns {boolean} true, if wasm loading 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 === "jsonp";
  32. };
  33. const onceForChunkSet = new WeakSet();
  34. /**
  35. * @param {Chunk} chunk chunk
  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 JsonpChunkLoadingRuntimeModule(set)
  47. );
  48. };
  49. compilation.hooks.runtimeRequirementInTree
  50. .for(RuntimeGlobals.ensureChunkHandlers)
  51. .tap("JsonpChunkLoadingPlugin", handler);
  52. compilation.hooks.runtimeRequirementInTree
  53. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  54. .tap("JsonpChunkLoadingPlugin", handler);
  55. compilation.hooks.runtimeRequirementInTree
  56. .for(RuntimeGlobals.hmrDownloadManifest)
  57. .tap("JsonpChunkLoadingPlugin", handler);
  58. compilation.hooks.runtimeRequirementInTree
  59. .for(RuntimeGlobals.baseURI)
  60. .tap("JsonpChunkLoadingPlugin", handler);
  61. compilation.hooks.runtimeRequirementInTree
  62. .for(RuntimeGlobals.onChunksLoaded)
  63. .tap("JsonpChunkLoadingPlugin", handler);
  64. compilation.hooks.runtimeRequirementInTree
  65. .for(RuntimeGlobals.ensureChunkHandlers)
  66. .tap("JsonpChunkLoadingPlugin", (chunk, set) => {
  67. if (!isEnabledForChunk(chunk)) return;
  68. set.add(RuntimeGlobals.publicPath);
  69. set.add(RuntimeGlobals.loadScript);
  70. set.add(RuntimeGlobals.getChunkScriptFilename);
  71. });
  72. compilation.hooks.runtimeRequirementInTree
  73. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  74. .tap("JsonpChunkLoadingPlugin", (chunk, set) => {
  75. if (!isEnabledForChunk(chunk)) return;
  76. set.add(RuntimeGlobals.publicPath);
  77. set.add(RuntimeGlobals.loadScript);
  78. set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
  79. set.add(RuntimeGlobals.moduleCache);
  80. set.add(RuntimeGlobals.hmrModuleData);
  81. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  82. });
  83. compilation.hooks.runtimeRequirementInTree
  84. .for(RuntimeGlobals.hmrDownloadManifest)
  85. .tap("JsonpChunkLoadingPlugin", (chunk, set) => {
  86. if (!isEnabledForChunk(chunk)) return;
  87. set.add(RuntimeGlobals.publicPath);
  88. set.add(RuntimeGlobals.getUpdateManifestFilename);
  89. });
  90. }
  91. );
  92. }
  93. }
  94. module.exports = JsonpChunkLoadingPlugin;