ImportScriptsChunkLoadingPlugin.js 4.2 KB

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