ReadFileCompileWasmPlugin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_SYNC } = require("../ModuleTypeConstants");
  7. const RuntimeGlobals = require("../RuntimeGlobals");
  8. const Template = require("../Template");
  9. const WasmChunkLoadingRuntimeModule = require("../wasm-sync/WasmChunkLoadingRuntimeModule");
  10. /** @typedef {import("../Chunk")} Chunk */
  11. /** @typedef {import("../Compiler")} Compiler */
  12. /**
  13. * @typedef {object} ReadFileCompileWasmPluginOptions
  14. * @property {boolean} [mangleImports] mangle imports
  15. */
  16. // TODO webpack 6 remove
  17. class ReadFileCompileWasmPlugin {
  18. /**
  19. * @param {ReadFileCompileWasmPluginOptions} [options] options object
  20. */
  21. constructor(options = {}) {
  22. this.options = options;
  23. }
  24. /**
  25. * Apply the plugin
  26. * @param {Compiler} compiler the compiler instance
  27. * @returns {void}
  28. */
  29. apply(compiler) {
  30. compiler.hooks.thisCompilation.tap(
  31. "ReadFileCompileWasmPlugin",
  32. compilation => {
  33. const globalWasmLoading = compilation.outputOptions.wasmLoading;
  34. /**
  35. * @param {Chunk} chunk chunk
  36. * @returns {boolean} true, when wasm loading is enabled for the chunk
  37. */
  38. const isEnabledForChunk = chunk => {
  39. const options = chunk.getEntryOptions();
  40. const wasmLoading =
  41. options && options.wasmLoading !== undefined
  42. ? options.wasmLoading
  43. : globalWasmLoading;
  44. return wasmLoading === "async-node";
  45. };
  46. /**
  47. * @param {string} path path to wasm file
  48. * @returns {string} generated code to load the wasm file
  49. */
  50. const generateLoadBinaryCode = path =>
  51. Template.asString([
  52. "new Promise(function (resolve, reject) {",
  53. Template.indent([
  54. "var { readFile } = require('fs');",
  55. "var { join } = require('path');",
  56. "",
  57. "try {",
  58. Template.indent([
  59. `readFile(join(__dirname, ${path}), function(err, buffer){`,
  60. Template.indent([
  61. "if (err) return reject(err);",
  62. "",
  63. "// Fake fetch response",
  64. "resolve({",
  65. Template.indent(["arrayBuffer() { return buffer; }"]),
  66. "});"
  67. ]),
  68. "});"
  69. ]),
  70. "} catch (err) { reject(err); }"
  71. ]),
  72. "})"
  73. ]);
  74. compilation.hooks.runtimeRequirementInTree
  75. .for(RuntimeGlobals.ensureChunkHandlers)
  76. .tap("ReadFileCompileWasmPlugin", (chunk, set) => {
  77. if (!isEnabledForChunk(chunk)) return;
  78. const chunkGraph = compilation.chunkGraph;
  79. if (
  80. !chunkGraph.hasModuleInGraph(
  81. chunk,
  82. m => m.type === WEBASSEMBLY_MODULE_TYPE_SYNC
  83. )
  84. ) {
  85. return;
  86. }
  87. set.add(RuntimeGlobals.moduleCache);
  88. compilation.addRuntimeModule(
  89. chunk,
  90. new WasmChunkLoadingRuntimeModule({
  91. generateLoadBinaryCode,
  92. supportsStreaming: false,
  93. mangleImports: this.options.mangleImports,
  94. runtimeRequirements: set
  95. })
  96. );
  97. });
  98. }
  99. );
  100. }
  101. }
  102. module.exports = ReadFileCompileWasmPlugin;