UNPKG

2.04 kBJavaScriptView Raw
1import { readFile } from 'fs';
2import { resolve } from 'path';
3
4function wasm(options) {
5 if (options === void 0) { options = {}; }
6 var syncFiles = (options.sync || []).map(function (x) { return resolve(x); });
7 return {
8 name: 'wasm',
9 load: function (id) {
10 if (/\.wasm$/.test(id)) {
11 return new Promise(function (res, reject) {
12 readFile(id, function (error, buffer) {
13 if (error != null) {
14 reject(error);
15 }
16 res(buffer.toString('binary'));
17 });
18 });
19 }
20 return null;
21 },
22 banner: "\n function _loadWasmModule (sync, src, imports) {\n var buf = null\n var isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null\n if (isNode) {\n buf = Buffer.from(src, 'base64')\n } else {\n var raw = globalThis.atob(src)\n var rawLength = raw.length\n buf = new Uint8Array(new ArrayBuffer(rawLength))\n for(var i = 0; i < rawLength; i++) {\n buf[i] = raw.charCodeAt(i)\n }\n }\n\n if (imports && !sync) {\n return WebAssembly.instantiate(buf, imports)\n } else if (!imports && !sync) {\n return WebAssembly.compile(buf)\n } else {\n var mod = new WebAssembly.Module(buf)\n return imports ? new WebAssembly.Instance(mod, imports) : mod\n }\n }\n ".trim(),
23 transform: function (code, id) {
24 if (code && /\.wasm$/.test(id)) {
25 var src = Buffer.from(code, 'binary').toString('base64');
26 var sync = syncFiles.indexOf(id) !== -1;
27 return "export default function(imports){return _loadWasmModule(" + +sync + ", '" + src + "', imports)}";
28 }
29 return null;
30 }
31 };
32}
33
34export default wasm;
35export { wasm };