您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ReadFileCompileWasmTemplatePlugin.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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;