You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.4 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Template = require("../Template");
  7. const WasmMainTemplatePlugin = require("../wasm/WasmMainTemplatePlugin");
  8. class ReadFileCompileWasmTemplatePlugin {
  9. constructor(options) {
  10. this.options = options || {};
  11. }
  12. apply(compiler) {
  13. compiler.hooks.thisCompilation.tap(
  14. "ReadFileCompileWasmTemplatePlugin",
  15. compilation => {
  16. const generateLoadBinaryCode = path =>
  17. Template.asString([
  18. "new Promise(function (resolve, reject) {",
  19. Template.indent([
  20. "var { readFile } = require('fs');",
  21. "var { join } = require('path');",
  22. "",
  23. "try {",
  24. Template.indent([
  25. `readFile(join(__dirname, ${path}), function(err, buffer){`,
  26. Template.indent([
  27. "if (err) return reject(err);",
  28. "",
  29. "// Fake fetch response",
  30. "resolve({",
  31. Template.indent([
  32. "arrayBuffer() { return Promise.resolve(buffer); }"
  33. ]),
  34. "});"
  35. ]),
  36. "});"
  37. ]),
  38. "} catch (err) { reject(err); }"
  39. ]),
  40. "})"
  41. ]);
  42. const plugin = new WasmMainTemplatePlugin(
  43. Object.assign(
  44. {
  45. generateLoadBinaryCode,
  46. supportsStreaming: false
  47. },
  48. this.options
  49. )
  50. );
  51. plugin.apply(compilation.mainTemplate);
  52. }
  53. );
  54. }
  55. }
  56. module.exports = ReadFileCompileWasmTemplatePlugin;