UniversalCompileAsyncWasmPlugin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /**
  41. * @param {string} path path
  42. * @returns {string} code
  43. */
  44. const generateBeforeLoadBinaryCode = path =>
  45. Template.asString([
  46. "var useFetch = typeof document !== 'undefined' || typeof self !== 'undefined';",
  47. `var wasmUrl = ${path};`
  48. ]);
  49. /**
  50. * @type {(path: string) => string}
  51. */
  52. const generateLoadBinaryCode = () =>
  53. Template.asString([
  54. "(useFetch",
  55. Template.indent([
  56. `? fetch(new URL(wasmUrl, ${compilation.outputOptions.importMetaName}.url))`
  57. ]),
  58. Template.indent([
  59. ": Promise.all([import('fs'), import('url')]).then(([{ readFile }, { URL }]) => new Promise((resolve, reject) => {",
  60. Template.indent([
  61. `readFile(new URL(wasmUrl, ${compilation.outputOptions.importMetaName}.url), (err, buffer) => {`,
  62. Template.indent([
  63. "if (err) return reject(err);",
  64. "",
  65. "// Fake fetch response",
  66. "resolve({",
  67. Template.indent(["arrayBuffer() { return buffer; }"]),
  68. "});"
  69. ]),
  70. "});"
  71. ]),
  72. "})))"
  73. ])
  74. ]);
  75. compilation.hooks.runtimeRequirementInTree
  76. .for(RuntimeGlobals.instantiateWasm)
  77. .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
  78. if (!isEnabledForChunk(chunk)) return;
  79. if (
  80. !chunkGraph.hasModuleInGraph(
  81. chunk,
  82. m => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC
  83. )
  84. ) {
  85. return;
  86. }
  87. compilation.addRuntimeModule(
  88. chunk,
  89. new AsyncWasmLoadingRuntimeModule({
  90. generateBeforeLoadBinaryCode,
  91. generateLoadBinaryCode,
  92. generateBeforeInstantiateStreaming,
  93. supportsStreaming: true
  94. })
  95. );
  96. });
  97. });
  98. }
  99. }
  100. module.exports = UniversalCompileAsyncWasmPlugin;