Skip to content

Commit

Permalink
Merge pull request #196 from ieow/feat/esbuildOptions
Browse files Browse the repository at this point in the history
Feat/esbuild options
  • Loading branch information
inokawa authored Oct 2, 2024
2 parents 1b63119 + e8df6ed commit b0a8ef6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,25 @@ module.exports = (async () => {
})();
```

#### Custom Esbuild options

To support custom Esbuild options, we can use Multiple Transformers method and replace the customTransformer.js file with the following code:

```tsx
// root/customTransformer.js
const reactNativeReactBridgeTransformer = require("react-native-react-bridge/lib/plugin");

const esbuildOptions = {
pluglins: [],
};
const transform =
reactNativeReactBridgeTransformer.createTransformer(esbuildOptions);

module.exports.transform = function ({ src, filename, options }) {
return transform({ src, filename, options });
};
```

### 2. Make entry file for web app.

- If you use React, React Native Web or Preact with React alias, import modules `react-native-react-bridge/lib/web`.
Expand Down
7 changes: 6 additions & 1 deletion src/plugin/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ module.exports = (function () {
*/
export const bundle = async (
filename: string,
options: RNRBConfig = {}
options: RNRBConfig = {},
esbuildOptions: Omit<esbuild.BuildOptions , "write" | "entryPoints" | "alias"> = {},
): Promise<string> => {
const alias: Record<string, string> = {};

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

const {plugins, ...restOptions} = esbuildOptions;

const bundled = await esbuild.build({
entryPoints: [filename],
bundle: true,
Expand Down Expand Up @@ -139,7 +142,9 @@ export const bundle = async (
});
},
},
...(plugins || [])
],
...restOptions,
});

const code = bundled.outputFiles[0]!.text;
Expand Down
37 changes: 22 additions & 15 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @module
*/

import type { BuildOptions } from "esbuild";
import { isEntryFile } from "./babel";
import { RNRBConfig, bundle, escape } from "./bundler";
import { join } from "path";
Expand Down Expand Up @@ -31,19 +32,25 @@ try {
// NOP
}

export const transform = async (args: any /* TODO */) => {
const { filename, src } = args;
const isEntry = isEntryFile(src, filename);
if (isEntry) {
const res = await bundle(filename, metroOptions);
return metroTransformer.transform({
...args,
src:
"export default String.raw`" +
escape(res).replace(/\$\{(.*?)\}/g, '\\$\\{$1\\}') +
"`.replace(/\\\\([`${}])/g, '\\$1')",
});
}

return metroTransformer.transform(args);
export const createTransformer = (
esbuildOptions: Omit<BuildOptions, "write" | "entryPoints" | "alias"> = {}
) => {
return async (args: any /* TODO */) => {
const { filename, src } = args;
const isEntry = isEntryFile(src, filename);
if (isEntry) {
const res = await bundle(filename, metroOptions, esbuildOptions);
return metroTransformer.transform({
...args,
src:
"export default String.raw`" +
escape(res).replace(/\$\{(.*?)\}/g, "\\$\\{$1\\}") +
"`.replace(/\\\\([`${}])/g, '\\$1')",
});
}

return metroTransformer.transform(args);
};
};

export const transform = createTransformer();

0 comments on commit b0a8ef6

Please sign in to comment.