JsonpChunkLoadingPlugin.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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("../Compiler")} Compiler */
  9. class JsonpChunkLoadingPlugin {
  10. /**
  11. * Apply the plugin
  12. * @param {Compiler} compiler the compiler instance
  13. * @returns {void}
  14. */
  15. apply(compiler) {
  16. compiler.hooks.thisCompilation.tap(
  17. "JsonpChunkLoadingPlugin",
  18. compilation => {
  19. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  20. const isEnabledForChunk = chunk => {
  21. const options = chunk.getEntryOptions();
  22. const chunkLoading =
  23. options && options.chunkLoading !== undefined
  24. ? options.chunkLoading
  25. : globalChunkLoading;
  26. return chunkLoading === "jsonp";
  27. };
  28. const onceForChunkSet = new WeakSet();
  29. const handler = (chunk, set) => {
  30. if (onceForChunkSet.has(chunk)) return;
  31. onceForChunkSet.add(chunk);
  32. if (!isEnabledForChunk(chunk)) return;
  33. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  34. set.add(RuntimeGlobals.hasOwnProperty);
  35. compilation.addRuntimeModule(
  36. chunk,
  37. new JsonpChunkLoadingRuntimeModule(set)
  38. );
  39. };
  40. compilation.hooks.runtimeRequirementInTree
  41. .for(RuntimeGlobals.ensureChunkHandlers)
  42. .tap("JsonpChunkLoadingPlugin", handler);
  43. compilation.hooks.runtimeRequirementInTree
  44. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  45. .tap("JsonpChunkLoadingPlugin", handler);
  46. compilation.hooks.runtimeRequirementInTree
  47. .for(RuntimeGlobals.hmrDownloadManifest)
  48. .tap("JsonpChunkLoadingPlugin", handler);
  49. compilation.hooks.runtimeRequirementInTree
  50. .for(RuntimeGlobals.baseURI)
  51. .tap("JsonpChunkLoadingPlugin", handler);
  52. compilation.hooks.runtimeRequirementInTree
  53. .for(RuntimeGlobals.onChunksLoaded)
  54. .tap("JsonpChunkLoadingPlugin", handler);
  55. compilation.hooks.runtimeRequirementInTree
  56. .for(RuntimeGlobals.ensureChunkHandlers)
  57. .tap("JsonpChunkLoadingPlugin", (chunk, set) => {
  58. if (!isEnabledForChunk(chunk)) return;
  59. set.add(RuntimeGlobals.publicPath);
  60. set.add(RuntimeGlobals.loadScript);
  61. set.add(RuntimeGlobals.getChunkScriptFilename);
  62. });
  63. compilation.hooks.runtimeRequirementInTree
  64. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  65. .tap("JsonpChunkLoadingPlugin", (chunk, set) => {
  66. if (!isEnabledForChunk(chunk)) return;
  67. set.add(RuntimeGlobals.publicPath);
  68. set.add(RuntimeGlobals.loadScript);
  69. set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
  70. set.add(RuntimeGlobals.moduleCache);
  71. set.add(RuntimeGlobals.hmrModuleData);
  72. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  73. });
  74. compilation.hooks.runtimeRequirementInTree
  75. .for(RuntimeGlobals.hmrDownloadManifest)
  76. .tap("JsonpChunkLoadingPlugin", (chunk, set) => {
  77. if (!isEnabledForChunk(chunk)) return;
  78. set.add(RuntimeGlobals.publicPath);
  79. set.add(RuntimeGlobals.getUpdateManifestFilename);
  80. });
  81. }
  82. );
  83. }
  84. }
  85. module.exports = JsonpChunkLoadingPlugin;