Skip to content

Commit 9a9d74d

Browse files
authored
Merge pull request #22 from inokawa/support-wasm
Support WebAssembly
2 parents 678bfc8 + 454b99b commit 9a9d74d

File tree

7 files changed

+62
-1
lines changed

7 files changed

+62
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The communication between React app and React Native app will be also simplified
3131
- `.css` is injected to the HTML head of WebView, like [css-loader](https://github.com/webpack-contrib/css-loader).
3232
- `.bmp`, `.gif`, `.png`, `.jpg`, `.jpeg`, `.webp` and `.svg` are loaded as base64 encoded url, like [url-loader](https://github.com/webpack-contrib/url-loader).
3333
- `.htm` and `.html` are loaded as string, which can be rendered with React's dangerouslySetInnerHTML.
34+
- `.wasm` is imported as [WebAssembly instance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance), so you can call its methods by `foo.exports.bar();`.
3435

3536
If you have some feature requests or improvements, please create a [issue](https://github.com/inokawa/react-native-react-bridge/issues) or [PR](https://github.com/inokawa/react-native-react-bridge/pulls).
3637

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)