Skip to content
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
41 changes: 35 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,56 @@
# React Native CLI

Command Line Interface for React Native.
Command line tools to interact with React Native projects.

[![Build Status][build-badge]][build] [![Version][version-badge]][package] [![MIT License][license-badge]][license] [![PRs Welcome][prs-welcome-badge]][prs-welcome]

_Note: CLI has been extracted from core `react-native` as a part of "[Lean Core](https://github.com/facebook/react-native/issues/23313)" effort. Please read [this blog post](https://blog.callstack.io/the-react-native-cli-has-a-new-home-79b63838f0e6) for more details._

## Compatibility

This CLI is intended to be used with a certain version of React Native. You'll find the support table with compatible versions below.

| CLI | React Native |
| ------ | ------------ |
| ^2.0.0 | ^0.60.0 |
| ^1.0.0 | ^0.59.0 |

## Documentation

- [commands](./docs/commands.md)
- [autolinking](./docs/autolinking.md)
- [plugins](./docs/plugins.md)

## About

This repository contains tools and helpers for React Native projects in form of a CLI. We want to make a couple of things clear for you first:

- this is a monorepo;
- there are currently two CLIs: the actual one called [`@react-native-community/cli`](./packages/cli) that does all the job and global `react-native-cli` which is used as its proxy;
- there are currently two CLIs: the actual one called [`@react-native-community/cli`](./packages/cli) that does all the job and global [`react-native-cli`](./packages/global-cli) which is used as its proxy and installation helper;

We know it's confusing, but we're actively working to make this indirection gone.

## Creating a new React Native project

To start a new React Native project, you'll need to install a global module [`react-native-cli`](./packages/global-cli) and follow instructions there.
There are two ways to start a React Native project.

### Using `npx`

> Available since `react-native@0.60`

This method is preferred if you don't want to install global packages.

```sh
npx react-native init MyApp
```

### Using global CLI

You'll need to install a global module [`react-native-cli`](./packages/global-cli) and follow instructions there.

We strongly encourage you to **only use global `react-native-cli` for bootstrapping new projects**. Use local version for everything else.

## Usage in existing React Native project
## Usage in an existing React Native project

Once you're inside an existing project, a local `react-native` binary will be available for you to use. Feel free to use Yarn to call it directly.

Expand Down Expand Up @@ -49,8 +78,8 @@ Everything inside this repository is [MIT licensed](./LICENSE).

<!-- badges -->

[build-badge]: https://img.shields.io/circleci/project/github/react-native-community/react-native-cli/master.svg?style=flat-square
[build]: https://circleci.com/gh/react-native-community/react-native-cli/tree/master
[build-badge]: https://img.shields.io/circleci/project/github/react-native-community/cli/master.svg?style=flat-square
[build]: https://circleci.com/gh/react-native-community/cli/tree/master
[version-badge]: https://img.shields.io/npm/v/@react-native-community/cli.svg?style=flat-square
[package]: https://www.npmjs.com/package/@react-native-community/cli.svg
[license-badge]: https://img.shields.io/npm/l/@react-native-community/cli.svg?style=flat-square
Expand Down
67 changes: 67 additions & 0 deletions docs/autolinking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Autolinking

Autolinking is a mechanism built into CLI that allows adding a dependency with native components for React Native to be as simple as:

```sh
yarn add react-native-firebase
```

## How does it work

React Native CLI provides a [`config`](./commands.md#config) command which grabs all of the configuration for React Native packages installed in the project (by scanning dependenecies in `package.json`) and outputs it in JSON format.

This information is then used by the projects advertised as platforms (with `react-native` being a default project supporting both iOS and Android platforms) that implement their autolinking behavior.

This design ensures consistent settings for any platform and allows `react-native config` to update the implementation of how to source this information (be it specified filenames, entries in the module’s package.json, etc).

## Platform iOS

The `Podfile` gets the package metadata from `react-native config` and:

1. Adds dependencies via CocoaPods dev pods (using files from a local path).
1. Adds build phase scripts to the App project’s build phase. (see examples below)

This means that all libraries need to ship a Podspec in the root of their folder to work seamlessly. This references the native code that your library depends on, and notes any of its other native dependencies.

The implementation ensures that a library is imported only once, so if you need to have a custom `pod` directive then including it above the function `use_native_modules!`.

Script Phases
A project which wants to add an Xcode script phase to a user's app can use the custom support in iOS auto-linking via custom settings in either the project's `package.json` under `"react-native"` or via a `react-native.config.js` file in the root.

The options for the build phase are passed more-or-less directly to the `Podfile` via `build_phase`. Here's an example of adding the settings in your `package.json`:

```json
{
"react-native":
"ios":
"scriptPhases": {
"name": "My Dep pre-parser",
"path": "relative/path/to/script.sh",
"execution_position": "after_compile"
}
}
}
}
```

`"path"` is an extra option provided to simplify storing the scripts in your repo, it sets `"script"` for you. You can see all available options [here](https://www.rubydoc.info/gems/cocoapods-core/Pod/Podfile/DSL#script_phase-instance_method).

See implementation of [native_modules.rb](https://github.com/react-native-community/cli/blob/master/packages/platform-ios/native_modules.rb).

## Platform Android

1. At build time, before the build script is run, a first gradle plugin (`settings.gradle`) is ran that takes the package metadata from `react-native config` to dynamically include Android library projects into the build.
1. A second plugin then adds those dependencies to the app project and generates a manifest of React Native packages to include in the fully generated file `/android/build/generated/rn/src/main/java/com/facebook/react/PackageList.java`.
1. Finally, at runtime (on startup) the list of React Native packages, generated in step 2, is used to instruct the React Native runtime of what native modules are available.

See implementation of [native_modules.gradle](https://github.com/react-native-community/cli/blob/master/packages/platform-android/native_modules.gradle).

## What do I need to have in my package to make it work?

You’re already using Gradle, so Android support will work by default.

On the iOS side, you will need to ensure you have a Podspec to the root of your repo. The `react-native-webview` Podspec is a good example of a [`package.json`](https://github.com/react-native-community/react-native-webview/blob/master/react-native-webview.podspec)-driven Podspec.

## How can I customize how autolinking works for my package?

A library can add a `react-native.config.js` configuration file, which will customize the defaults.
Loading