• Stars
    star
    595
  • Rank 75,217 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fast bundler and dev server for react-native using esbuild

react-native-esbuild

The fastest bundler for React Native.

Features

  • Fast – ~10-50x faster depending on project
  • Tree shaking – Smaller bundles means faster apps (21% smaller for init project)
  • Compatible – Drop-in replacement for metro
  • Configurable – Support for custom transformers and env variables

Sponsoring

If this library helped you, please consider sponsoring.

Installation

yarn add react-native-esbuild esbuild

Configuration

react-native CLI plugin

Make sure react-native.config.js exists in the root of your project, and create one if not. Add this library to the commands section like this:

// react-native.config.js
const { commands } = require('react-native-esbuild');

module.exports = {
  commands,
};

Optional: Esbuild settings

If you want to customize the esbuild configuration, for example by adding your own plugins you may do so with the createEsbuildCommands function:

// react-native.config.js
const { createEsbuildCommands, babelPlugin } = require('react-native-esbuild');

// See https://esbuild.github.io/api/#simple-options
const commands = createEsbuildCommands((config) => ({
  ...config,
  plugins: config.plugins.concat(
    babelPlugin({
      filter: /src\/my-babel-components\/.+\.[tj]sx?$/,
    })
  ),
}));

module.exports = {
  commands,
};

Optional: Use esbuild for development

  1. Open package.json in your editor and locate scripts section.
  2. Edit start script to be react-native esbuild-start.
  3. Prevent metro from starting automatically by appending --no-packager to the ios/android scripts.
{
  "scripts": {
    "android": "react-native run-android --no-packager",
    "ios": "react-native run-ios --no-packager",
    "start": "react-native esbuild-start"
  }
}

Optional: Build production app with esbuild

Android

Set project.ext.react.bundleCommand to esbuild-bundle in android/app/build.gradle:

// android/app/build.gradle
project.ext.react = [
    enableHermes: false,
    bundleCommand: "esbuild-bundle",
]

iOS

  1. Open your iOS project in Xcode manually or with xed ios
  2. Select the Build Phases tab in your project settings.
  3. Expand the Bundle React Native code and images section and add export BUNDLE_COMMAND=esbuild-bundle so it looks like this:
set -e

export BUNDLE_COMMAND=esbuild-bundle
export NODE_BINARY=node
../node_modules/react-native/scripts/react-native-xcode.sh

Usage

This library aims to be a plug-in replacement for the metro equivalent commands with the esbuild- prefix.

react-native esbuild-start

Argument Description Default
--port Port to listen for http requests 8081
--host Host to listen for http requests 127.0.0.1
--projectRoot Path to a custom project root. None
--reset-cache Removes cached files. N/A
--no-interactive Disables interactive mode. false

react-native esbuild-bundle

Argument Description Default
--entry-file Path to the root JS file, either absolute or relative to JS root index.js
--platform Either ios or android ios
--dev If false, warnings are disabled and the bundle is minified true
--minify Allows overriding whether bundle is minified otherwise determined by dev value. Opposite of dev
--bundle-output File name where to store the resulting bundle. None
--sourcemap-output File name where to store the sourcemap file for resulting bundle. None
--assets-dest Directory name where to store assets referenced in the bundle. None
--reset-cache Removes cached files. N/A

Troubleshooting

Flow syntax errors such as Expected "from" but found "{"

Esbuild doesn't natively support flow so such syntax needs to be stripped with a plugin. By default any file with @flow or @noflow pragmas will be stripped from flow, but you may also opt-in to flow stripping for more files by passing a custom flow syntax checker:

// react-native.config.js
const {
  createEsbuildCommands,
  defaultHasFlowSyntax,
  syntaxAwareLoaderPlugin,
} = require('react-native-esbuild');

const FLOW_MODULES_WITHOUT_PRAGMA = ['react-native-video', 'rn-fetch-blob'];

const commands = createEsbuildCommands((config, args) => ({
  ...config,
  plugins: config.plugins
    .filter((plugin) => plugin.name !== 'syntax-aware-loader')
    .concat(
      syntaxAwareLoaderPlugin({
        filter: /\.([mc]js|[tj]sx?)$/,
        cache: args.dev,
        hasFlowSyntax: (contents, filePath) =>
          defaultHasFlowSyntax(contents, filePath) ||
          FLOW_MODULES_WITHOUT_PRAGMA.find((m) =>
            filePath.includes(`node_modules/${m}/`)
          ),
      })
    ),
}));

module.exports = {
  commands,
};

Limitations

Hermes engine

Hermes doesn't support crucial ES6 features like block level scoping (let/const) and the team doesn't seem to want to merge this feature mentioning it being a too big of a change without having good enough reasons to add it.

HMR/Fast Refresh

Esbuild doesn't support Fast Refresh or Hot Module Replacement, but this library supports live reload instead.

License

MIT © Joel Arvidsson 2022-

More Repositories

1

react-native-vector-icons

Customizable Icons for React Native with support for image source and full styling.
JavaScript
17,427
star
2

react-native-animatable

Standard set of easy to use animations and declarative transitions for React Native
JavaScript
9,838
star
3

react-native-progress

Progress indicators and spinners for React Native
JavaScript
3,628
star
4

hush

🤫 Noiseless Browsing – Content Blocker for Safari
JavaScript
3,264
star
5

react-native-keychain

🔑 Keychain Access for React Native
Kotlin
3,194
star
6

react-native-lightbox

Images etc in Full Screen Lightbox Popovers for React Native
JavaScript
2,818
star
7

react-native-collapsible

Animated collapsible component for React Native, good for accordions, toggles etc
JavaScript
2,450
star
8

loki

👁 Visual Regression Testing for Storybook
JavaScript
1,778
star
9

react-native-image-progress

Progress indicator for networked images in React Native
JavaScript
1,723
star
10

angular-scroll

Scrollspy, animated scrollTo and scroll events for angular.js
JavaScript
1,482
star
11

react-native-performance

📐 Monitor and measure React Native performance
TypeScript
929
star
12

react-native-store-review

Rate on App/Play Store directly in your React Native app
Java
751
star
13

react-native-shimmer

Simple shimmering effect for any view in React Native
Java
700
star
14

react-native-parallax

Parallax effects for React Native using Animated API
JavaScript
585
star
15

react-native-vector-image

iOS/Android native vector assets generated from SVG
JavaScript
310
star
16

react-native-pinchable

Instagram like pinch to zoom for React Native
Java
225
star
17

angular-parallax

Lightweight & performant parallax scrolling for angular.js.
JavaScript
203
star
18

angular-lazytube

Lightweight, responsive, lazy loaded YouTube videos that degrades gracefully.
JavaScript
45
star
19

diglett

Keep your JS project lean by detecting duplicate dependencies
JavaScript
44
star
20

esbuild-server

Fast, lightweight and powerful development server for esbuild
TypeScript
38
star
21

react-native-oxc

10x your react-native builds with OXC
TypeScript
28
star
22

oblador.github.io

Landing page for my GitHub projects
HTML
10
star
23

oblador

5
star
24

node-cloud-imager

Powerful yet simple way to apply filters/crop/resize images and upload to cloud providers (Amazon S3, Rackspace, Azure).
JavaScript
3
star
25

node-ups-sdk

JavaScript
3
star
26

sovra

🤏 Rust-based Test Decider for JavaScript using Oxc
Rust
2
star
27

expo-requestidlecallback-issue

TypeScript
1
star
28

react-native-android-autolinking-bug-repro

TypeScript
1
star
29

.github

1
star
30

react-native-fabric-image-unicode-issue

TypeScript
1
star