Skip to content

Commit bd1c860

Browse files
committed
feat: esbuild options
1 parent 1b63119 commit bd1c860

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* @module
55
*/
66

7+
import { BuildOptions } from "esbuild";
78
import { isEntryFile } from "./babel";
89
import { RNRBConfig, bundle, escape } from "./bundler";
910
import { join } from "path";
@@ -47,3 +48,23 @@ export const transform = async (args: any /* TODO */) => {
4748

4849
return metroTransformer.transform(args);
4950
};
51+
52+
export const createTransformer = (esbuildOptions: Omit<BuildOptions , "write" | "entryPoints" | "alias"> = {} ) => {
53+
const transform = async (args: any /* TODO */) => {
54+
const { filename, src } = args;
55+
const isEntry = isEntryFile(src, filename);
56+
if (isEntry) {
57+
const res = await bundle(filename, metroOptions, esbuildOptions);
58+
return metroTransformer.transform({
59+
...args,
60+
src:
61+
"export default String.raw`" +
62+
escape(res).replace(/\$\{(.*?)\}/g, '\\$\\{$1\\}') +
63+
"`.replace(/\\\\([`${}])/g, '\\$1')",
64+
});
65+
}
66+
67+
return metroTransformer.transform(args);
68+
};
69+
return transform;
70+
}

0 commit comments

Comments
 (0)