Skip to content

RFC: Simplify workflow for native dependencies for libraries #870

Open
@satya164

Description

@satya164

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-view
  • react-native-gesture-handler
  • react-native-reanimated
  • react-native-screens
  • react-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:

  1. Use resolutions options in yarn, overrides in npm
  2. Configure metro to always resolve to the selected version (how?)

Do you have any other ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions