Skip to content

Commit 66da6e3

Browse files
committed
Support bundling wasm
1 parent 678bfc8 commit 66da6e3

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from "react";
2+
import { webViewRender } from "react-native-react-bridge/lib/web";
3+
import Comp from "./Component";
4+
import wasm from "./example.wasm";
5+
alert(wasm);
6+
7+
const App = () => {
8+
return <Comp />;
9+
};
10+
11+
export default webViewRender(App);

fixtures/example.wasm

13.8 KB
Binary file not shown.

src/plugin/__snapshots__/metro.spec.js.snap

Lines changed: 19 additions & 0 deletions
Large diffs are not rendered by default.

src/plugin/metro.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ const codeExts = ["js", "ts", "jsx", "tsx"];
55
const htmlExts = ["htm", "html", "css"];
66
const imageExts = ["bmp", "gif", "png", "jpg", "jpeg", "webp", "svg"];
77
const textExts = ["txt", "md"];
8-
const sourceExts = [...codeExts, ...htmlExts, ...imageExts, ...textExts];
8+
const sourceExts = [
9+
...codeExts,
10+
...htmlExts,
11+
...imageExts,
12+
...textExts,
13+
"wasm",
14+
];
915

1016
export const bundle = async (filename) => {
1117
const config = await Metro.loadConfig();

src/plugin/metro.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,11 @@ describe("bundle", () => {
5858
const res = await bundle(filePath);
5959
expect(res).toMatchSnapshot();
6060
});
61+
62+
it("with wasm", async () => {
63+
const filename = "app-export-default-with-wasm.jsx";
64+
const filePath = resolvePath(filename);
65+
const res = await bundle(filePath);
66+
expect(res).toMatchSnapshot();
67+
});
6168
});

src/plugin/transformer.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require("path");
22
const fs = require("fs");
3+
const { TextEncoder } = require("util");
34
const metroTransformer = require("metro-react-native-babel-transformer");
45

56
module.exports.transform = async (args) => {
@@ -55,6 +56,11 @@ module.exports.transform = async (args) => {
5556
...args,
5657
src: injectImage(filename, "svg+xml"),
5758
});
59+
case ".wasm":
60+
return metroTransformer.transform({
61+
...args,
62+
src: injectWasm(src),
63+
});
5864
default:
5965
break;
6066
}
@@ -81,3 +87,14 @@ const injectImage = (filename, ext) => {
8187
const src = fs.readFileSync(filename, "base64");
8288
return `export default "data:image/${ext};base64,${src}";`;
8389
};
90+
91+
const injectWasm = (src) => {
92+
const buf = new TextEncoder().encode(src);
93+
return `
94+
export default (function () {
95+
const wasmModule = new WebAssembly.Module(Uint8Array.from([${buf.toString()}]));
96+
const instance = new WebAssembly.Instance(wasmModule);
97+
return instance;
98+
})();
99+
`;
100+
};

0 commit comments

Comments
 (0)