Skip to content

Commit b0a8ef6

Browse files
authored
Merge pull request #196 from ieow/feat/esbuildOptions
Feat/esbuild options
2 parents 1b63119 + e8df6ed commit b0a8ef6

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,25 @@ module.exports = (async () => {
146146
})();
147147
```
148148

149+
#### Custom Esbuild options
150+
151+
To support custom Esbuild options, we can use Multiple Transformers method and replace the customTransformer.js file with the following code:
152+
153+
```tsx
154+
// root/customTransformer.js
155+
const reactNativeReactBridgeTransformer = require("react-native-react-bridge/lib/plugin");
156+
157+
const esbuildOptions = {
158+
pluglins: [],
159+
};
160+
const transform =
161+
reactNativeReactBridgeTransformer.createTransformer(esbuildOptions);
162+
163+
module.exports.transform = function ({ src, filename, options }) {
164+
return transform({ src, filename, options });
165+
};
166+
```
167+
149168
### 2. Make entry file for web app.
150169

151170
- If you use React, React Native Web or Preact with React alias, import modules `react-native-react-bridge/lib/web`.

src/plugin/bundler.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ module.exports = (function () {
5454
*/
5555
export const bundle = async (
5656
filename: string,
57-
options: RNRBConfig = {}
57+
options: RNRBConfig = {},
58+
esbuildOptions: Omit<esbuild.BuildOptions , "write" | "entryPoints" | "alias"> = {},
5859
): Promise<string> => {
5960
const alias: Record<string, string> = {};
6061

@@ -71,6 +72,8 @@ export const bundle = async (
7172
alias["react-native"] = "react-native-web";
7273
}
7374

75+
const {plugins, ...restOptions} = esbuildOptions;
76+
7477
const bundled = await esbuild.build({
7578
entryPoints: [filename],
7679
bundle: true,
@@ -139,7 +142,9 @@ export const bundle = async (
139142
});
140143
},
141144
},
145+
...(plugins || [])
142146
],
147+
...restOptions,
143148
});
144149

145150
const code = bundled.outputFiles[0]!.text;

src/plugin/index.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* @module
55
*/
66

7+
import type { BuildOptions } from "esbuild";
78
import { isEntryFile } from "./babel";
89
import { RNRBConfig, bundle, escape } from "./bundler";
910
import { join } from "path";
@@ -31,19 +32,25 @@ try {
3132
// NOP
3233
}
3334

34-
export const transform = async (args: any /* TODO */) => {
35-
const { filename, src } = args;
36-
const isEntry = isEntryFile(src, filename);
37-
if (isEntry) {
38-
const res = await bundle(filename, metroOptions);
39-
return metroTransformer.transform({
40-
...args,
41-
src:
42-
"export default String.raw`" +
43-
escape(res).replace(/\$\{(.*?)\}/g, '\\$\\{$1\\}') +
44-
"`.replace(/\\\\([`${}])/g, '\\$1')",
45-
});
46-
}
47-
48-
return metroTransformer.transform(args);
35+
export const createTransformer = (
36+
esbuildOptions: Omit<BuildOptions, "write" | "entryPoints" | "alias"> = {}
37+
) => {
38+
return async (args: any /* TODO */) => {
39+
const { filename, src } = args;
40+
const isEntry = isEntryFile(src, filename);
41+
if (isEntry) {
42+
const res = await bundle(filename, metroOptions, esbuildOptions);
43+
return metroTransformer.transform({
44+
...args,
45+
src:
46+
"export default String.raw`" +
47+
escape(res).replace(/\$\{(.*?)\}/g, "\\$\\{$1\\}") +
48+
"`.replace(/\\\\([`${}])/g, '\\$1')",
49+
});
50+
}
51+
52+
return metroTransformer.transform(args);
53+
};
4954
};
55+
56+
export const transform = createTransformer();

0 commit comments

Comments
 (0)