FetchCompileAsyncWasmPlugin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { WEBASSEMBLY_MODULE_TYPE_ASYNC } = require("../ModuleTypeConstants");
  7. const RuntimeGlobals = require("../RuntimeGlobals");
  8. const AsyncWasmCompileRuntimeModule = require("../wasm-async/AsyncWasmCompileRuntimeModule");
  9. const AsyncWasmLoadingRuntimeModule = require("../wasm-async/AsyncWasmLoadingRuntimeModule");
  10. /** @typedef {import("../Chunk")} Chunk */
  11. /** @typedef {import("../Compiler")} Compiler */
  12. const PLUGIN_NAME = "FetchCompileAsyncWasmPlugin";
  13. /**
  14. * Enables asynchronous WebAssembly loading through `fetch` for environments
  15. * that can instantiate fetched binaries at runtime.
  16. */
  17. class FetchCompileAsyncWasmPlugin {
  18. /**
  19. * Registers compilation hooks that attach the async fetch-based wasm runtime
  20. * to chunks containing async WebAssembly modules.
  21. * @param {Compiler} compiler the compiler instance
  22. * @returns {void}
  23. */
  24. apply(compiler) {
  25. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
  26. const globalWasmLoading = compilation.outputOptions.wasmLoading;
  27. /**
  28. * Determines whether the chunk should load async WebAssembly binaries
  29. * through the `fetch` backend.
  30. * @param {Chunk} chunk chunk
  31. * @returns {boolean} true, if wasm loading is enabled for the chunk
  32. */
  33. const isEnabledForChunk = (chunk) => {
  34. const options = chunk.getEntryOptions();
  35. const wasmLoading =
  36. options && options.wasmLoading !== undefined
  37. ? options.wasmLoading
  38. : globalWasmLoading;
  39. return wasmLoading === "fetch";
  40. };
  41. /**
  42. * Generates the runtime expression that downloads the emitted wasm
  43. * binary for an async WebAssembly module.
  44. * @param {string} path path to the wasm file
  45. * @returns {string} code to load the wasm file
  46. */
  47. const generateLoadBinaryCode = (path) =>
  48. `fetch(${RuntimeGlobals.publicPath} + ${path})`;
  49. compilation.hooks.runtimeRequirementInTree
  50. .for(RuntimeGlobals.instantiateWasm)
  51. .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
  52. if (!isEnabledForChunk(chunk)) return;
  53. if (
  54. !chunkGraph.hasModuleInGraph(
  55. chunk,
  56. (m) => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC
  57. )
  58. ) {
  59. return;
  60. }
  61. set.add(RuntimeGlobals.publicPath);
  62. compilation.addRuntimeModule(
  63. chunk,
  64. new AsyncWasmLoadingRuntimeModule({
  65. generateLoadBinaryCode,
  66. supportsStreaming: true
  67. })
  68. );
  69. });
  70. compilation.hooks.runtimeRequirementInTree
  71. .for(RuntimeGlobals.compileWasm)
  72. .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
  73. if (!isEnabledForChunk(chunk)) return;
  74. if (
  75. !chunkGraph.hasModuleInGraph(
  76. chunk,
  77. (m) => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC
  78. )
  79. ) {
  80. return;
  81. }
  82. set.add(RuntimeGlobals.publicPath);
  83. compilation.addRuntimeModule(
  84. chunk,
  85. new AsyncWasmCompileRuntimeModule({
  86. generateLoadBinaryCode,
  87. supportsStreaming: true
  88. })
  89. );
  90. });
  91. });
  92. }
  93. }
  94. module.exports = FetchCompileAsyncWasmPlugin;