ReadFileCompileWasmPlugin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * @property {boolean=} import use import?
  16. */
  17. // TODO webpack 6 remove
  18. const PLUGIN_NAME = "ReadFileCompileWasmPlugin";
  19. class ReadFileCompileWasmPlugin {
  20. /**
  21. * @param {ReadFileCompileWasmPluginOptions=} options options object
  22. */
  23. constructor(options = {}) {
  24. this.options = options;
  25. }
  26. /**
  27. * Apply the plugin
  28. * @param {Compiler} compiler the compiler instance
  29. * @returns {void}
  30. */
  31. apply(compiler) {
  32. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, 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. * @type {(path: string) => string} callback to generate code to load the wasm file
  48. */
  49. const generateLoadBinaryCode = this.options.import
  50. ? path =>
  51. Template.asString([
  52. "Promise.all([import('fs'), import('url')]).then(([{ readFile }, { URL }]) => new Promise((resolve, reject) => {",
  53. Template.indent([
  54. `readFile(new URL(${path}, ${compilation.outputOptions.importMetaName}.url), (err, buffer) => {`,
  55. Template.indent([
  56. "if (err) return reject(err);",
  57. "",
  58. "// Fake fetch response",
  59. "resolve({",
  60. Template.indent(["arrayBuffer() { return buffer; }"]),
  61. "});"
  62. ]),
  63. "});"
  64. ]),
  65. "}))"
  66. ])
  67. : path =>
  68. Template.asString([
  69. "new Promise(function (resolve, reject) {",
  70. Template.indent([
  71. "var { readFile } = require('fs');",
  72. "var { join } = require('path');",
  73. "",
  74. "try {",
  75. Template.indent([
  76. `readFile(join(__dirname, ${path}), function(err, buffer){`,
  77. Template.indent([
  78. "if (err) return reject(err);",
  79. "",
  80. "// Fake fetch response",
  81. "resolve({",
  82. Template.indent(["arrayBuffer() { return buffer; }"]),
  83. "});"
  84. ]),
  85. "});"
  86. ]),
  87. "} catch (err) { reject(err); }"
  88. ]),
  89. "})"
  90. ]);
  91. compilation.hooks.runtimeRequirementInTree
  92. .for(RuntimeGlobals.ensureChunkHandlers)
  93. .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
  94. if (!isEnabledForChunk(chunk)) return;
  95. if (
  96. !chunkGraph.hasModuleInGraph(
  97. chunk,
  98. m => m.type === WEBASSEMBLY_MODULE_TYPE_SYNC
  99. )
  100. ) {
  101. return;
  102. }
  103. set.add(RuntimeGlobals.moduleCache);
  104. compilation.addRuntimeModule(
  105. chunk,
  106. new WasmChunkLoadingRuntimeModule({
  107. generateLoadBinaryCode,
  108. supportsStreaming: false,
  109. mangleImports: this.options.mangleImports,
  110. runtimeRequirements: set
  111. })
  112. );
  113. });
  114. });
  115. }
  116. }
  117. module.exports = ReadFileCompileWasmPlugin;