ReadFileCompileAsyncWasmPlugin.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 Template = require("../Template");
  9. const AsyncWasmLoadingRuntimeModule = require("../wasm-async/AsyncWasmLoadingRuntimeModule");
  10. /** @typedef {import("../Compiler")} Compiler */
  11. class ReadFileCompileAsyncWasmPlugin {
  12. constructor({ type = "async-node", import: useImport = false } = {}) {
  13. this._type = type;
  14. this._import = useImport;
  15. }
  16. /**
  17. * Apply the plugin
  18. * @param {Compiler} compiler the compiler instance
  19. * @returns {void}
  20. */
  21. apply(compiler) {
  22. compiler.hooks.thisCompilation.tap(
  23. "ReadFileCompileAsyncWasmPlugin",
  24. compilation => {
  25. const globalWasmLoading = compilation.outputOptions.wasmLoading;
  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 === this._type;
  33. };
  34. const generateLoadBinaryCode = this._import
  35. ? path =>
  36. Template.asString([
  37. "Promise.all([import('fs'), import('url')]).then(([{ readFile }, { URL }]) => new Promise((resolve, reject) => {",
  38. Template.indent([
  39. `readFile(new URL(${path}, import.meta.url), (err, buffer) => {`,
  40. Template.indent([
  41. "if (err) return reject(err);",
  42. "",
  43. "// Fake fetch response",
  44. "resolve({",
  45. Template.indent(["arrayBuffer() { return buffer; }"]),
  46. "});"
  47. ]),
  48. "});"
  49. ]),
  50. "}))"
  51. ])
  52. : path =>
  53. Template.asString([
  54. "new Promise(function (resolve, reject) {",
  55. Template.indent([
  56. "try {",
  57. Template.indent([
  58. "var { readFile } = require('fs');",
  59. "var { join } = require('path');",
  60. "",
  61. `readFile(join(__dirname, ${path}), function(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. "} catch (err) { reject(err); }"
  73. ]),
  74. "})"
  75. ]);
  76. compilation.hooks.runtimeRequirementInTree
  77. .for(RuntimeGlobals.instantiateWasm)
  78. .tap("ReadFileCompileAsyncWasmPlugin", (chunk, set) => {
  79. if (!isEnabledForChunk(chunk)) return;
  80. const chunkGraph = compilation.chunkGraph;
  81. if (
  82. !chunkGraph.hasModuleInGraph(
  83. chunk,
  84. m => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC
  85. )
  86. ) {
  87. return;
  88. }
  89. set.add(RuntimeGlobals.publicPath);
  90. compilation.addRuntimeModule(
  91. chunk,
  92. new AsyncWasmLoadingRuntimeModule({
  93. generateLoadBinaryCode,
  94. supportsStreaming: false
  95. })
  96. );
  97. });
  98. }
  99. );
  100. }
  101. }
  102. module.exports = ReadFileCompileAsyncWasmPlugin;