1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var fs = require('fs');
|
6 | var path = require('path');
|
7 |
|
8 | function wasm(options = {}) {
|
9 | options = Object.assign({}, options);
|
10 |
|
11 | const syncFiles = (options.sync || []).map((x) => path.resolve(x));
|
12 |
|
13 | return {
|
14 | name: 'wasm',
|
15 |
|
16 | load(id) {
|
17 | if (/\.wasm$/.test(id)) {
|
18 | return new Promise((res, reject) => {
|
19 | fs.readFile(id, (error, buffer) => {
|
20 | if (error != null) {
|
21 | reject(error);
|
22 | }
|
23 | res(buffer.toString('binary'));
|
24 | });
|
25 | });
|
26 | }
|
27 | return null;
|
28 | },
|
29 |
|
30 | banner: `
|
31 | function _loadWasmModule (sync, src, imports) {
|
32 | var buf = null
|
33 | var isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null
|
34 | if (isNode) {
|
35 | buf = Buffer.from(src, 'base64')
|
36 | } else {
|
37 | var raw = window.atob(src)
|
38 | var rawLength = raw.length
|
39 | buf = new Uint8Array(new ArrayBuffer(rawLength))
|
40 | for(var i = 0; i < rawLength; i++) {
|
41 | buf[i] = raw.charCodeAt(i)
|
42 | }
|
43 | }
|
44 |
|
45 | if (imports && !sync) {
|
46 | return WebAssembly.instantiate(buf, imports)
|
47 | } else if (!imports && !sync) {
|
48 | return WebAssembly.compile(buf)
|
49 | } else {
|
50 | var mod = new WebAssembly.Module(buf)
|
51 | return imports ? new WebAssembly.Instance(mod, imports) : mod
|
52 | }
|
53 | }
|
54 | `.trim(),
|
55 |
|
56 |
|
57 | transform(code, id) {
|
58 | if (code && /\.wasm$/.test(id)) {
|
59 | const src = Buffer.from(code, 'binary').toString('base64');
|
60 | const sync = syncFiles.indexOf(id) !== -1;
|
61 | return `export default function(imports){return _loadWasmModule(${+sync}, '${src}', imports)}`;
|
62 | }
|
63 | }
|
64 | };
|
65 | }
|
66 |
|
67 | exports.default = wasm;
|