Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto link dependencies of a dependency in React Native?

I want to share components of one app with the other and therefore moved them to a separate NPM package. The structure of this package looks like this:

- src
--- components
----- component A
----- component B
- package.json

Some components use other 3rd party libraries and they are listed in the package.json.

Now when installing that shared package to the second project these 3rd party dependencies are not auto linked. For example, when calling pod install they're not installed.

Is it possible somehow install these 3rd party dependencies?

like image 535
Shyngys Kassymov Avatar asked Nov 21 '19 17:11

Shyngys Kassymov


Video Answer


1 Answers

We can patch function findDependencies in @react-native-community/cli as follow to enable auto link dependencies of dependency

function findDependencies(root) {
  let pjson;

  try {
    pjson = JSON.parse(_fs().default.readFileSync(_path().default.join(root, 'package.json'), 'UTF-8'));
  } catch (e) {
    return [];
  }

  const dependencies = Object.keys(pjson.dependencies || []);
  const subDeps = dependencies.reduce(function(accumulator, currentValue) {
    if (!currentValue.includes('your_dependency')) {
      return accumulator;
    }
    const subRoot = `${root}/node_modules/${currentValue}`;
    return accumulator.concat(findDependencies(subRoot));
  }, []);

  const deps = [...Object.keys(pjson.dependencies || {}), ...Object.keys(pjson.devDependencies || {}), ...subDeps];
  return deps;
}
like image 148
phamducgiam Avatar answered Sep 24 '22 00:09

phamducgiam