ImportScriptsChunkLoadingPlugin.js 3.7 KB

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