Skip to content

Commit 276b794

Browse files
committed
Support bundling of HTML
1 parent 3ae80ba commit 276b794

File tree

7 files changed

+63
-6
lines changed

7 files changed

+63
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The communication between React app and React Native app will be also simplified
2525
- `.json` is imported as an object, like require in Node.js.
2626
- `.css` is injected to the HTML head of WebView, like [css-loader](https://github.com/webpack-contrib/css-loader).
2727
- `.bmp`, `.gif`, `.png`, `.jpg`, `.jpeg`, `.webp` and `.svg` are loaded as base64 encoded url, like [url-loader](https://github.com/webpack-contrib/url-loader).
28+
- `.htm` and `.html` are loaded as string, which can be rendered with React's dangerouslySetInnerHTML.
2829

2930
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).
3031

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

fixtures/example.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>My test page</title>
6+
</head>
7+
<body>
8+
<div>test</div>
9+
</body>
10+
</html>

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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import Metro from "metro";
22

33
const babelTransformerPath = require.resolve("./transformer");
4-
const sourceExts = ["js", "ts", "jsx", "tsx"];
4+
const codeExts = ["js", "ts", "jsx", "tsx"];
5+
const htmlExts = ["htm", "html", "css"];
56
const imageExts = ["bmp", "gif", "png", "jpg", "jpeg", "webp", "svg"];
6-
const assetExts = [...imageExts, "css"];
7+
const sourceExts = [...codeExts, ...htmlExts, ...imageExts];
78

89
export const bundle = async (filename) => {
910
const config = await Metro.loadConfig();
10-
config.resolver.sourceExts = [...sourceExts, ...assetExts];
11+
config.resolver.sourceExts = sourceExts;
1112
config.resolver.assetExts = config.resolver.assetExts.filter(
12-
(ext) => !assetExts.includes(ext)
13+
(ext) => !sourceExts.includes(ext)
1314
);
1415
config.transformer.babelTransformerPath = babelTransformerPath;
1516

src/plugin/metro.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ describe("bundle", () => {
3030
const res = await bundle(filePath);
3131
expect(res).toMatchSnapshot();
3232
});
33+
34+
it("with html", async () => {
35+
const filename = "app-export-default-with-html.jsx";
36+
const filePath = resolvePath(filename);
37+
const res = await bundle(filePath);
38+
expect(res).toMatchSnapshot();
39+
});
3340
});

src/plugin/transformer.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ module.exports.transform = async (args) => {
77

88
const ext = path.extname(filename);
99
switch (ext) {
10+
case ".htm":
11+
case ".html":
12+
return metroTransformer.transform({
13+
...args,
14+
src: injectHtml(src),
15+
});
1016
case ".css":
1117
return metroTransformer.transform({
1218
...args,
@@ -50,9 +56,13 @@ module.exports.transform = async (args) => {
5056
return metroTransformer.transform(args);
5157
};
5258

53-
const injectCss = (css) => `
59+
const injectHtml = (src) => {
60+
return `export default String.raw\`${src}\``;
61+
};
62+
63+
const injectCss = (src) => `
5464
(function () {
55-
var css = String.raw\`${css}\`;
65+
var css = String.raw\`${src}\`;
5666
var head = document.head || document.getElementsByTagName("head")[0];
5767
var style = document.createElement("style");
5868
style.type = "text/css";

0 commit comments

Comments
 (0)