react-native-community / react-native-community/cli
RFC: Simplify workflow for native dependencies for libraries
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 2.9k
- Forks
- 949
- PR merge metrics
- No merged PRs in 30d
Description
The problem
Currently the CLI looks for packages in user's dependencies and links them if they contain native code that needs to be linked. Which works great for apps. But there's no way for libraries to specify any libraries they depend on that contain native code.
The current approach is to use peer dependencies to specify them. However, due to React Native core not including things like gesture handler, reanimated, and due to lean core extracting built-in modules, it becomes cumbersome for the user to install tons of dependencies to use a single library.
The workflow for peer dependencies in npm/yarn isn't really great and the workflow is confusing.
Example scenario: Let's consider @react-navigation/stack as an example. It uses the following libraries which contain native code:
@react-native-community/masked-viewreact-native-gesture-handlerreact-native-reanimatedreact-native-screensreact-native-safe-area-context
So, to use a single navigator, the user needs to install all of these packages manually into their project. It's discouraging and complicated for the user to have to install all these dependencies for a single package. Now instead of the library being charge of these dependencies, users have to make sure to install the correct version of these, and keep them updated themselves even if they don't use them.
What would be ideal is that the user should only need to install @react-navigation/stack and everything else should be taken care of automatically. This RFC is for discussing possible approaches and finding a good solution.
Possible solution
To reduce this overhead, in the library's package, we could define which native modules we depend on, let's say, in a new key dependencies (or the name you prefer) in react-native.config.js:
module.exports = {
dependencies: [
'@react-native-community/masked-view',
'react-native-gesture-handler',
'react-native-reanimated',
'react-native-screens',
'react-native-safe-area-context',
],
};
This way, the CLI knows that the library depends on these other libraries that need to be linked. Now, we could approach the solution in multiple ways:
1. Prompt the user to install these deps
The CLI could detect if the user already has these dependencies, and if not, show a selection list for installing them, then upon selection, install the correct versions according to the peer dependencies inside the library's package.json.
Optionally, CLI could also detect if in future the peer deps specify a different version and ask the user to upgrade to those versions as well.
2. Use regular deps and rely on flattening
Instead of peer dependencies, libraries could use regular dependencies for these packages instead. Then npm/yarn would flatten to single version if they are semver compatible.
For regular dependencies, the CLI would now need to look for libraries to link in nested node_modules as well (only for modules that have this new dependencies option).
If the CLI finds multiple versions installed, it would prompt the user to select which version to use. Then we'd need to force this version:
- Use
resolutionsoptions inyarn,overridesinnpm - Configure metro to always resolve to the selected version (how?)
Do you have any other ideas?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
This RFC does not name an implementation file or test. Start by reviewing the CLI's existing dependency discovery and native-linking workflow, then compare it with the proposed react-native.config.js and package.json approaches. The issue needs a decided design, implementation scope, and tests before a newcomer can tell what done looks like.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react-native, typescript
- Domain
- cli, developer-experience
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100