Skip to content

Commit e8df6ed

Browse files
committed
Refactor
1 parent a86d533 commit e8df6ed

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,21 @@ module.exports = (async () => {
147147
```
148148

149149
#### Custom Esbuild options
150+
150151
To support custom Esbuild options, we can use Multiple Transformers method and replace the customTransformer.js file with the following code:
151152

152-
```
153+
```tsx
153154
// root/customTransformer.js
154155
const reactNativeReactBridgeTransformer = require("react-native-react-bridge/lib/plugin");
155156

157+
const esbuildOptions = {
158+
pluglins: [],
159+
};
160+
const transform =
161+
reactNativeReactBridgeTransformer.createTransformer(esbuildOptions);
162+
156163
module.exports.transform = function ({ src, filename, options }) {
157-
const esbuildOptions = {
158-
pluglins: [
159-
],
160-
};
161-
const transform = reactNativeReactBridgeTransformer.createTransformer(esbuildOptions);
162-
return transform({ src, filename, options });
164+
return transform({ src, filename, options });
163165
};
164166
```
165167

src/plugin/index.ts

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

7-
import { BuildOptions } from "esbuild";
7+
import type { BuildOptions } from "esbuild";
88
import { isEntryFile } from "./babel";
99
import { RNRBConfig, bundle, escape } from "./bundler";
1010
import { join } from "path";
@@ -32,25 +32,10 @@ try {
3232
// NOP
3333
}
3434

35-
export const transform = async (args: any /* TODO */) => {
36-
const { filename, src } = args;
37-
const isEntry = isEntryFile(src, filename);
38-
if (isEntry) {
39-
const res = await bundle(filename, metroOptions);
40-
return metroTransformer.transform({
41-
...args,
42-
src:
43-
"export default String.raw`" +
44-
escape(res).replace(/\$\{(.*?)\}/g, '\\$\\{$1\\}') +
45-
"`.replace(/\\\\([`${}])/g, '\\$1')",
46-
});
47-
}
48-
49-
return metroTransformer.transform(args);
50-
};
51-
52-
export const createTransformer = (esbuildOptions: Omit<BuildOptions , "write" | "entryPoints" | "alias"> = {} ) => {
53-
const transform = async (args: any /* TODO */) => {
35+
export const createTransformer = (
36+
esbuildOptions: Omit<BuildOptions, "write" | "entryPoints" | "alias"> = {}
37+
) => {
38+
return async (args: any /* TODO */) => {
5439
const { filename, src } = args;
5540
const isEntry = isEntryFile(src, filename);
5641
if (isEntry) {
@@ -59,12 +44,13 @@ export const createTransformer = (esbuildOptions: Omit<BuildOptions , "write" |
5944
...args,
6045
src:
6146
"export default String.raw`" +
62-
escape(res).replace(/\$/g, '\\$') +
47+
escape(res).replace(/\$\{(.*?)\}/g, "\\$\\{$1\\}") +
6348
"`.replace(/\\\\([`${}])/g, '\\$1')",
6449
});
6550
}
66-
51+
6752
return metroTransformer.transform(args);
68-
};
69-
return transform;
70-
}
53+
};
54+
};
55+
56+
export const transform = createTransformer();

0 commit comments

Comments
 (0)