Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/esbuild options #196

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ 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:

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

module.exports.transform = function ({ src, filename, options }) {
const esbuildOptions = {
pluglins: [
],
};
const transform = reactNativeReactBridgeTransformer.createTransformer(esbuildOptions);
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
21 changes: 21 additions & 0 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @module
*/

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

return metroTransformer.transform(args);
};

export const createTransformer = (esbuildOptions: Omit<BuildOptions , "write" | "entryPoints" | "alias"> = {} ) => {
const transform = 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, '\\$') +
"`.replace(/\\\\([`${}])/g, '\\$1')",
});
}

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