Skip to content

Commit d91eb0b

Browse files
committed
Automatically bundle base64decode function
1 parent e1b6e03 commit d91eb0b

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

main.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
const fs = require("fs");
22

3-
// TODO: Add the base64 decode function automagically
3+
const decodeFunc = `
4+
function _base64ToArrayBuffer(base64) {
5+
var binary_string = window.atob(base64);
6+
var len = binary_string.length;
7+
var bytes = new Uint8Array(len);
8+
for (var i = 0; i < len; i++) {
9+
bytes[i] = binary_string.charCodeAt(i);
10+
}
11+
return bytes.buffer;
12+
}`;
413

514
module.exports = function base64wasm() {
615
return {
716
name: 'base64-wasm-bundler', // this name will show up in warnings and errors
817
async transform(code, id) {
9-
if(id.endsWith(".wasm")) {
18+
if (id.endsWith(".wasm")) {
1019
const base64 = fs.readFileSync(id).toString("base64");
1120
return {
12-
code: `export default "${base64}"`,
21+
code: `export default () => {
22+
${decodeFunc}
23+
return _base64ToArrayBuffer("${base64}");
24+
}`,
1325
map: { mappings: "" },
1426
};
1527
}
1628
return null;
17-
},
29+
}
1830
};
1931
}

0 commit comments

Comments
 (0)