|
| 1 | +# @rnx-kit/esbuild-service |
| 2 | + |
| 3 | +[](https://github.com/microsoft/rnx-kit/actions/workflows/build.yml) |
| 4 | + |
| 5 | +🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧 |
| 6 | + |
| 7 | +### This tool is EXPERIMENTAL - USE WITH CAUTION |
| 8 | + |
| 9 | +🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧 |
| 10 | + |
| 11 | +A Metro-independent, esbuild-based bundler for React Native. |
| 12 | + |
| 13 | +## Motivation: Metro vs. esbuild |
| 14 | + |
| 15 | +[Metro](https://facebook.github.io/metro/) is the standard bundler for React |
| 16 | +Native. It is reliable, battle-tested, and deeply integrated into the React |
| 17 | +Native toolchain. However, Metro was designed around CommonJS semantics and |
| 18 | +Babel transformations. This makes it slower at scale and harder to integrate |
| 19 | +with modern tooling. |
| 20 | + |
| 21 | +[esbuild](https://esbuild.github.io/) is an extremely fast JavaScript bundler |
| 22 | +written in Go. It handles TypeScript and JSX natively, provides excellent tree- |
| 23 | +shaking, and produces source maps with minimal overhead. |
| 24 | + |
| 25 | +This package explores using esbuild as a **complete replacement for Metro** |
| 26 | +rather than just its serialization step (which is what |
| 27 | +[`@rnx-kit/metro-serializer-esbuild`](../packages/metro-serializer-esbuild) |
| 28 | +does). |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Metro component analysis |
| 33 | + |
| 34 | +The table below maps each Metro component to its esbuild equivalent and |
| 35 | +explains how much code must be reimplemented. |
| 36 | + |
| 37 | +| Metro component | Can esbuild replace it? | Notes | |
| 38 | +|---|---|---| |
| 39 | +| **Transformer** (Babel / Flow) | ✅ Yes — natively | esbuild supports TypeScript and JSX out of the box. Flow types can be stripped with a simple plugin. Babel is no longer needed for the common case. | |
| 40 | +| **Dependency graph** | ✅ Yes — natively | esbuild builds its own dependency graph as part of bundling. | |
| 41 | +| **Tree-shaking** | ✅ Yes — natively | esbuild performs dead code elimination (DCE) automatically for ESM code. | |
| 42 | +| **Minifier** | ✅ Yes — natively | esbuild has a built-in, high-performance minifier. | |
| 43 | +| **Source maps** | ✅ Yes — natively | esbuild generates linked or inline source maps. | |
| 44 | +| **Serializer** | ✅ Yes — natively | esbuild produces the final bundle; this is the role of `metro-serializer-esbuild`. | |
| 45 | +| **Resolver** (platform extensions, `react-native` field) | ⚠️ Plugin required | The `reactNativeResolver` plugin in this package reimplements Metro's platform-extension resolution (`.ios.js`, `.android.js`, `.native.js`) and the `react-native` → `module` → `browser` → `main` field priority from `package.json`. ~250 lines of code. | |
| 46 | +| **Pre-modules / polyfills** | ⚠️ Plugin required | The `reactNativePolyfills` plugin reimplements Metro's `preModules` mechanism by injecting a virtual entry-point that sets up `global`, `__DEV__`, and any user-provided polyfills. ~110 lines of code. | |
| 47 | +| **Asset handling** | ⚠️ Plugin required | Metro's asset system resolves image/font imports to an asset registry lookup. An esbuild plugin can replicate this, but it is not yet included in this package. | |
| 48 | +| **Dev server + HMR** | ❌ Cannot replace | Metro's development server implements React Native's fast-refresh / HMR protocol. esbuild has a basic HTTP server mode but no HMR support. | |
| 49 | +| **RAM bundles** | ❌ Cannot replace | Metro's indexed RAM bundle format has no esbuild equivalent. | |
| 50 | +| **Lazy module loading** | ❌ Cannot replace | Metro's async require / lazy-loading mechanism requires a custom module loader runtime that esbuild does not provide. | |
| 51 | + |
| 52 | +### Code reuse from `@rnx-kit/metro-serializer-esbuild` |
| 53 | + |
| 54 | +| Component | Reuse? | Notes | |
| 55 | +|---|---|---| |
| 56 | +| `targets.ts` — Hermes target inference | ✅ Copied | Identical logic; infers the right `hermesX.Y` esbuild target from the installed `react-native` version. | |
| 57 | +| `getSideEffects` from `module.ts` | ✅ Concept reused | The `sideEffects` package.json field logic applies equally to a standalone esbuild bundler; esbuild respects it natively through its own side-effects handling. | |
| 58 | +| `esbuildTransformerConfig` | ❌ Not applicable | That export configures Metro's Babel transformer to be esbuild-friendly. It is not relevant when Metro is removed entirely. | |
| 59 | +| `index.ts` — the custom serializer | ❌ Not applicable | The serializer depends on Metro's dependency graph API and cannot be reused. | |
| 60 | +| `sourceMap.ts` — Metro source map helpers | ❌ Not applicable | These helpers wrap Metro's source-map utilities; not needed without Metro. | |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## Installation |
| 65 | + |
| 66 | +```sh |
| 67 | +yarn add --dev @rnx-kit/esbuild-service |
| 68 | +``` |
| 69 | + |
| 70 | +## Usage |
| 71 | + |
| 72 | +```typescript |
| 73 | +import { bundle } from "@rnx-kit/esbuild-service"; |
| 74 | + |
| 75 | +await bundle({ |
| 76 | + entryFile: "index.js", |
| 77 | + platform: "ios", |
| 78 | + dev: false, |
| 79 | + bundleOutput: "dist/main.ios.jsbundle", |
| 80 | + sourcemapOutput: "dist/main.ios.jsbundle.map", |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | +## API |
| 85 | + |
| 86 | +### `bundle(options)` |
| 87 | + |
| 88 | +Bundles a React Native application using esbuild, without Metro. |
| 89 | + |
| 90 | +#### Options |
| 91 | + |
| 92 | +| Option | Type | Default | Description | |
| 93 | +|---|---|---|---| |
| 94 | +| `entryFile` | `string` | required | Path to the entry file. | |
| 95 | +| `platform` | `AllPlatforms` | required | Target platform (`android`, `ios`, `macos`, `windows`, …). | |
| 96 | +| `dev` | `boolean` | `false` | Bundle in development mode. | |
| 97 | +| `minify` | `boolean` | `!dev` | Minify the output. | |
| 98 | +| `bundleOutput` | `string` | required | Path to write the bundle to. | |
| 99 | +| `sourcemapOutput` | `string` | — | Path to write the source map to. | |
| 100 | +| `target` | `string \| string[]` | Auto-detected | esbuild target (e.g. `"hermes0.12"`). | |
| 101 | +| `plugins` | `Plugin[]` | `[]` | Extra esbuild plugins. | |
| 102 | +| `projectRoot` | `string` | `process.cwd()` | Project root directory. | |
| 103 | +| `logLevel` | esbuild log level | `"warning"` | esbuild log level. | |
| 104 | +| `drop` | esbuild drop | — | Drop `debugger` or `console` calls. | |
| 105 | +| `pure` | `string[]` | — | Mark calls as side-effect free. | |
| 106 | + |
| 107 | +### `reactNativeResolver(platform, mainFields?)` |
| 108 | + |
| 109 | +An esbuild plugin that adds React Native–specific module resolution: |
| 110 | + |
| 111 | +- Platform-specific file extensions (`.ios.ts`, `.android.ts`, `.native.ts`, …) |
| 112 | +- `react-native` → `module` → `browser` → `main` field priority in `package.json` |
| 113 | + |
| 114 | +```typescript |
| 115 | +import { reactNativeResolver } from "@rnx-kit/esbuild-service"; |
| 116 | +import * as esbuild from "esbuild"; |
| 117 | + |
| 118 | +await esbuild.build({ |
| 119 | + entryPoints: ["index.ts"], |
| 120 | + bundle: true, |
| 121 | + plugins: [reactNativeResolver("ios")], |
| 122 | + outfile: "dist/bundle.js", |
| 123 | +}); |
| 124 | +``` |
| 125 | + |
| 126 | +### `reactNativePolyfills(options)` |
| 127 | + |
| 128 | +An esbuild plugin that injects React Native globals (`global`, `__DEV__`) and |
| 129 | +optional polyfills as a virtual entry-point before your application code. |
| 130 | + |
| 131 | +```typescript |
| 132 | +import { reactNativePolyfills } from "@rnx-kit/esbuild-service"; |
| 133 | +import * as esbuild from "esbuild"; |
| 134 | + |
| 135 | +await esbuild.build({ |
| 136 | + entryPoints: ["index.ts"], |
| 137 | + bundle: true, |
| 138 | + plugins: [ |
| 139 | + reactNativePolyfills({ |
| 140 | + entryFile: "index.ts", |
| 141 | + dev: false, |
| 142 | + polyfills: ["./polyfills/myPolyfill.js"], |
| 143 | + }), |
| 144 | + ], |
| 145 | + outfile: "dist/bundle.js", |
| 146 | +}); |
| 147 | +``` |
| 148 | + |
| 149 | +### `inferBuildTarget(projectRoot?)` |
| 150 | + |
| 151 | +Infers the appropriate esbuild target string for the installed version of |
| 152 | +`react-native` / Hermes. |
| 153 | + |
| 154 | +```typescript |
| 155 | +import { inferBuildTarget } from "@rnx-kit/esbuild-service"; |
| 156 | + |
| 157 | +const target = inferBuildTarget(); // e.g. "hermes0.12" |
| 158 | +``` |
| 159 | + |
| 160 | +## Known Limitations |
| 161 | + |
| 162 | +- **Dev server / HMR** — use Metro for development; this package targets |
| 163 | + production bundling only. |
| 164 | +- **RAM bundles** — not supported. Use Metro if you need indexed RAM bundles. |
| 165 | +- **Asset handling** — image and font imports are not yet handled. Contributions |
| 166 | + welcome. |
| 167 | +- **Flow types** — esbuild cannot strip Flow types natively. You'll need a Flow- |
| 168 | + stripping Babel transform or a third-party esbuild plugin if your code uses |
| 169 | + Flow. |
0 commit comments