UniversalCompileAsyncWasmPlugin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const { WEBASSEMBLY_MODULE_TYPE_ASYNC } = require("../ModuleTypeConstants");
  7. const RuntimeGlobals = require("../RuntimeGlobals");
  8. const Template = require("../Template");
  9. const AsyncWasmLoadingRuntimeModule = require("../wasm-async/AsyncWasmLoadingRuntimeModule");
  10. /** @typedef {import("../Chunk")} Chunk */
  11. /** @typedef {import("../Compiler")} Compiler */
  12. const PLUGIN_NAME = "UniversalCompileAsyncWasmPlugin";
  13. class UniversalCompileAsyncWasmPlugin {
  14. /**
  15. * Apply the plugin
  16. * @param {Compiler} compiler the compiler instance
  17. * @returns {void}
  18. */
  19. apply(compiler) {
  20. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
  21. const globalWasmLoading = compilation.outputOptions.wasmLoading;
  22. /**
  23. * @param {Chunk} chunk chunk
  24. * @returns {boolean} true, if wasm loading is enabled for the chunk
  25. */
  26. const isEnabledForChunk = chunk => {
  27. const options = chunk.getEntryOptions();
  28. const wasmLoading =
  29. options && options.wasmLoading !== undefined
  30. ? options.wasmLoading
  31. : globalWasmLoading;
  32. return wasmLoading === "universal";
  33. };
  34. const generateBeforeInstantiateStreaming = () =>
  35. Template.asString([
  36. "if (!useFetch) {",
  37. Template.indent(["return fallback();"]),
  38. "}"
  39. ]);
  40. const generateBeforeLoadBinaryCode = path =>
  41. Template.asString([
  42. "var useFetch = typeof document !== 'undefined' || typeof self !== 'undefined';",
  43. `var wasmUrl = ${path};`
  44. ]);
  45. /**
  46. * @type {(path: string) => string}
  47. */
  48. const generateLoadBinaryCode = () =>
  49. Template.asString([
  50. "(useFetch",
  51. Template.indent([
  52. `? fetch(new URL(wasmUrl, ${compilation.outputOptions.importMetaName}.url))`
  53. ]),
  54. Template.indent([
  55. ": Promise.all([import('fs'), import('url')]).then(([{ readFile }, { URL }]) => new Promise((resolve, reject) => {",
  56. Template.indent([
  57. `readFile(new URL(wasmUrl, ${compilation.outputOptions.importMetaName}.url), (err, buffer) => {`,
  58. Template.indent([
  59. "if (err) return reject(err);",
  60. "",
  61. "// Fake fetch response",
  62. "resolve({",
  63. Template.indent(["arrayBuffer() { return buffer; }"]),
  64. "});"
  65. ]),
  66. "});"
  67. ]),
  68. "})))"
  69. ])
  70. ]);
  71. compilation.hooks.runtimeRequirementInTree
  72. .for(RuntimeGlobals.instantiateWasm)
  73. .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
  74. if (!isEnabledForChunk(chunk)) return;
  75. if (
  76. !chunkGraph.hasModuleInGraph(
  77. chunk,
  78. m => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC
  79. )
  80. ) {
  81. return;
  82. }
  83. compilation.addRuntimeModule(
  84. chunk,
  85. new AsyncWasmLoadingRuntimeModule({
  86. generateBeforeLoadBinaryCode,
  87. generateLoadBinaryCode,
  88. generateBeforeInstantiateStreaming,
  89. supportsStreaming: true
  90. })
  91. );
  92. });
  93. });
  94. }
  95. }
  96. module.exports = UniversalCompileAsyncWasmPlugin;