ImportScriptsChunkLoadingPlugin.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 StartupChunkDependenciesPlugin = require("../runtime/StartupChunkDependenciesPlugin");
  8. const ImportScriptsChunkLoadingRuntimeModule = require("./ImportScriptsChunkLoadingRuntimeModule");
  9. /** @typedef {import("../Chunk")} Chunk */
  10. /** @typedef {import("../Compiler")} Compiler */
  11. const PLUGIN_NAME = "ImportScriptsChunkLoadingPlugin";
  12. class ImportScriptsChunkLoadingPlugin {
  13. /**
  14. * Apply the plugin
  15. * @param {Compiler} compiler the compiler instance
  16. * @returns {void}
  17. */
  18. apply(compiler) {
  19. new StartupChunkDependenciesPlugin({
  20. chunkLoading: "import-scripts",
  21. asyncChunkLoading: true
  22. }).apply(compiler);
  23. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
  24. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  25. /**
  26. * @param {Chunk} chunk chunk
  27. * @returns {boolean} true, if wasm loading is enabled for the chunk
  28. */
  29. const isEnabledForChunk = chunk => {
  30. const options = chunk.getEntryOptions();
  31. const chunkLoading =
  32. options && options.chunkLoading !== undefined
  33. ? options.chunkLoading
  34. : globalChunkLoading;
  35. return chunkLoading === "import-scripts";
  36. };
  37. const onceForChunkSet = new WeakSet();
  38. /**
  39. * @param {Chunk} chunk chunk
  40. * @param {Set<string>} set runtime requirements
  41. */
  42. const handler = (chunk, set) => {
  43. if (onceForChunkSet.has(chunk)) return;
  44. onceForChunkSet.add(chunk);
  45. if (!isEnabledForChunk(chunk)) return;
  46. const withCreateScriptUrl = Boolean(
  47. compilation.outputOptions.trustedTypes
  48. );
  49. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  50. set.add(RuntimeGlobals.hasOwnProperty);
  51. if (withCreateScriptUrl) {
  52. set.add(RuntimeGlobals.createScriptUrl);
  53. }
  54. compilation.addRuntimeModule(
  55. chunk,
  56. new ImportScriptsChunkLoadingRuntimeModule(set, withCreateScriptUrl)
  57. );
  58. };
  59. compilation.hooks.runtimeRequirementInTree
  60. .for(RuntimeGlobals.ensureChunkHandlers)
  61. .tap(PLUGIN_NAME, handler);
  62. compilation.hooks.runtimeRequirementInTree
  63. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  64. .tap(PLUGIN_NAME, handler);
  65. compilation.hooks.runtimeRequirementInTree
  66. .for(RuntimeGlobals.hmrDownloadManifest)
  67. .tap(PLUGIN_NAME, handler);
  68. compilation.hooks.runtimeRequirementInTree
  69. .for(RuntimeGlobals.baseURI)
  70. .tap(PLUGIN_NAME, handler);
  71. compilation.hooks.runtimeRequirementInTree
  72. .for(RuntimeGlobals.onChunksLoaded)
  73. .tap(PLUGIN_NAME, handler);
  74. compilation.hooks.runtimeRequirementInTree
  75. .for(RuntimeGlobals.ensureChunkHandlers)
  76. .tap(PLUGIN_NAME, (chunk, set) => {
  77. if (!isEnabledForChunk(chunk)) return;
  78. set.add(RuntimeGlobals.publicPath);
  79. set.add(RuntimeGlobals.getChunkScriptFilename);
  80. });
  81. compilation.hooks.runtimeRequirementInTree
  82. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  83. .tap(PLUGIN_NAME, (chunk, set) => {
  84. if (!isEnabledForChunk(chunk)) return;
  85. set.add(RuntimeGlobals.publicPath);
  86. set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
  87. set.add(RuntimeGlobals.moduleCache);
  88. set.add(RuntimeGlobals.hmrModuleData);
  89. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  90. });
  91. compilation.hooks.runtimeRequirementInTree
  92. .for(RuntimeGlobals.hmrDownloadManifest)
  93. .tap(PLUGIN_NAME, (chunk, set) => {
  94. if (!isEnabledForChunk(chunk)) return;
  95. set.add(RuntimeGlobals.publicPath);
  96. set.add(RuntimeGlobals.getUpdateManifestFilename);
  97. });
  98. });
  99. }
  100. }
  101. module.exports = ImportScriptsChunkLoadingPlugin;