Skip to content

Commit 6e88c7a

Browse files
authored
Merge pull request #177 from inokawa/esbuild
Replace metro with esbuild and support latest React Native/Expo
2 parents 24dc7e0 + 7f88140 commit 6e88c7a

File tree

154 files changed

+16598
-20604
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+16598
-20604
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ Steps to reproduce the behavior.
1717
A clear and concise description of what you expected to happen.
1818

1919
**Platform:**
20-
- Version of react-native: [x.x.x]
20+
- Version of [react-native, expo]: [x.x.x]
2121
- Version of this package: [x.x.x]
22-
- Version of expo (only if you use it): [x.x.x]
2322
- OS: [iOS, Android]
2423
- Running on: [Device, Simulator]
2524
- JavaScript engine: [Hermes, JavaScriptCore]

README.md

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![npm](https://img.shields.io/npm/v/react-native-react-bridge) ![npm](https://img.shields.io/npm/dw/react-native-react-bridge) ![check](https://github.com/inokawa/react-native-react-bridge/workflows/check/badge.svg)
44

5-
An easy way to integrate your [React](https://github.com/facebook/react) (or [Preact](https://github.com/preactjs/preact)) app into [React Native](https://github.com/facebook/react-native) app with WebView.
5+
An easy way to integrate your [React](https://github.com/facebook/react) (or [Preact](https://github.com/preactjs/preact)/[React Native Web](https://github.com/necolas/react-native-web)) app into [React Native](https://github.com/facebook/react-native) app with WebView.
66

77
## Why?
88

@@ -21,8 +21,8 @@ The communication between React app and React Native app will be also simplified
2121

2222
## Features
2323

24-
- Create React (or Preact) app bundle for WebView automatically in build process of React Native
25-
- `.js`, `.ts`, `.jsx`, `.tsx`, `.mjs` and `.cjs` will be packed into one source.
24+
- Create React (or Preact/React Native Web) app bundle for WebView automatically in build process of React Native
25+
- All JS modules (with or without JSX/TypeScript) will be bundled with [esbuild](https://github.com/evanw/esbuild).
2626
- **NOTE: Only the edits in the entry file of web will invoke rebuild because of the limitation of [metro](https://github.com/facebook/metro)'s build process.**
2727
- Handle communication between React Native and WebView with React hook style
2828
- With `useWebViewMessage` hook, you can subscribe messages from WebView.
@@ -49,21 +49,16 @@ npm install react-dom
4949
# Necessary only if you render Preact app in WebView
5050
# preact >= 10.0
5151
npm install preact
52+
53+
# Necessary only if you render React Native Web app in WebView
54+
npm install react-native-web
5255
```
5356

5457
### Requirements
5558

56-
- react >= 16.8
59+
- react >= 16.14
5760
- react-native >= 0.60
5861

59-
### Supported react-native versions
60-
61-
| react-native-react-bridge | react-native |
62-
| ------------------------- | ---------------- |
63-
| >=0.11.2 | >=0.70.0 |
64-
| >=0.9.0 | >=0.65.0 <0.70.0 |
65-
| 0.0.0 - 0.8.1 | <=0.64.2 |
66-
6762
## Usage
6863

6964
### 1. Fix `metro.config.js` to use babelTransformer from this library.
@@ -83,21 +78,38 @@ module.exports = {
8378
rnrb: {
8479
// Set `true` if you use Preact in web side.
8580
// This will alias imports from `react` and `react-dom` to `preact/compat` automatically.
86-
preact: true
81+
preact: true,
82+
// Set `true` if you use react-native-web in web side.
83+
// This will alias imports from `react-native` to `react-native-web` automatically.
84+
web: true
8785
},
8886
*/
8987
...
9088
};
9189
```
9290

93-
##### Projects with Multiple Transformers
91+
#### Expo
92+
93+
```javascript
94+
const { getDefaultConfig } = require("expo/metro-config");
95+
96+
const config = getDefaultConfig(__dirname);
97+
98+
config.transformer.babelTransformerPath = require.resolve(
99+
"react-native-react-bridge/lib/plugin"
100+
);
101+
102+
module.exports = config;
103+
```
104+
105+
#### Projects with Multiple Transformers
94106

95107
If your project at some point requires a metro configuration with additional transformers, consider making a separate `customTransformer.js` file in the project root with logic for delegating files types to the appropriate transformer, and modifying `metro.config.js` file to reference the customer transformer file. For example, if you are using `react-native-svg-transformer`, this would be your custom transformer file:
96108

97109
```js
98110
// root/customTransformer.js
99-
var reactNativeReactBridgeTransformer = require("react-native-react-bridge/lib/plugin");
100-
var svgTransformer = require("react-native-svg-transformer");
111+
const reactNativeReactBridgeTransformer = require("react-native-react-bridge/lib/plugin");
112+
const svgTransformer = require("react-native-svg-transformer");
101113

102114
module.exports.transform = function ({ src, filename, options }) {
103115
if (filename.endsWith(".svg")) {
@@ -134,29 +146,10 @@ module.exports = (async () => {
134146
})();
135147
```
136148

137-
#### Expo
138-
139-
```javascript
140-
const { getDefaultConfig } = require("expo/metro-config");
141-
142-
const config = getDefaultConfig(__dirname);
143-
144-
module.exports = {
145-
...config,
146-
transformer: {
147-
...config.transformer,
148-
babelTransformerPath: require.resolve(
149-
"react-native-react-bridge/lib/plugin"
150-
),
151-
},
152-
};
153-
```
154-
155149
### 2. Make entry file for web app.
156150

157-
- If you use React in web, import modules from `react` and `react-native-react-bridge/lib/web`.
158-
- If you use Preact in web, import modules from `preact` and `react-native-react-bridge/lib/web/preact`.
159-
- If you use Preact in web but with React aliases, import modules from `react` and `react-native-react-bridge/lib/web`.
151+
- If you use React, React Native Web or Preact with React alias, import modules `react-native-react-bridge/lib/web`.
152+
- If you use Preact, import modules from `react-native-react-bridge/lib/web/preact`.
160153

161154
```jsx
162155
// WebApp.js
@@ -253,24 +246,11 @@ https://github.com/react-native-webview/react-native-webview/blob/master/docs/Re
253246

254247
This repository includes demo app.
255248

256-
### React Native
257-
258-
Before running this app, please prepare environment for React Native (https://reactnative.dev/docs/environment-setup).
259-
260249
```sh
261250
git clone git@github.com:inokawa/react-native-react-bridge.git
262251
cd examples/DemoApp
263-
yarn
264-
yarn ios # or yarn android
265-
```
266-
267-
### Expo
268-
269-
```sh
270-
git clone git@github.com:inokawa/react-native-react-bridge.git
271-
cd examples/DemoAppExpo
272-
yarn
273-
expo start
252+
npm install
253+
npm run ios # or npm run android
274254
```
275255

276256
## Contribute

babel.config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

e2e/index.spec.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { readdirSync } from "node:fs";
33
import { writeFile } from "node:fs/promises";
44
import path from "node:path";
55
import { bundle } from "../src/plugin/bundler";
6-
import { wrapWithWebViewHTML } from "../src/plugin/html";
76
import { WEB_ROOT_ID } from "../src/constants";
87

98
test.beforeEach(async ({}, testInfo) => {
@@ -18,11 +17,12 @@ test.describe("smoke webview", () => {
1817
readdirSync(fixturePath).forEach((filename) => {
1918
if (filename.endsWith(".jsx") || filename.endsWith(".tsx")) {
2019
test(filename, async ({ page }) => {
21-
const code = await bundle(
22-
path.join(fixturePath, filename),
23-
wrapWithWebViewHTML
24-
);
25-
const html = eval(code);
20+
const isPreact = filename.includes("preact");
21+
const isWeb = filename.includes("web");
22+
const html = await bundle(path.join(fixturePath, filename), {
23+
preact: isPreact,
24+
web: isWeb,
25+
});
2626
expect(html).toMatchSnapshot();
2727

2828
await writeFile(path.join(tempPath, filename) + ".html", html, "utf-8");

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-commonjs-jsx-1-chromium.txt

Lines changed: 55 additions & 18 deletions
Large diffs are not rendered by default.

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-default-jsx-1-chromium.txt

Lines changed: 55 additions & 18 deletions
Large diffs are not rendered by default.

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-default-preact-jsx-1-chromium.txt

Lines changed: 13 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div style="background: red; width: 100vw; height: 100vh;">hoge</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
<meta charset="utf-8">
3+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-default-react-jsx-1-chromium.txt

Lines changed: 55 additions & 18 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)