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 all 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
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();