xref: /expo/packages/expo/react-native.config.js (revision ba52f93f)
1const findProjectRoot = require('@react-native-community/cli-tools').findProjectRoot;
2const fs = require('fs');
3const path = require('path');
4
5const projectRoot = findProjectRoot();
6
7function isMatchedInFile(filePath, regexp) {
8  const contents = fs.readFileSync(filePath, 'utf8');
9  return !!contents.match(regexp);
10}
11
12/**
13 * Checks if expo-modules-autolinking is setup on iOS
14 */
15function isExpoModulesInstalledIos(projectRoot) {
16  const podfilePath = path.join(projectRoot, 'ios', 'Podfile');
17  if (!fs.existsSync(podfilePath)) {
18    // Assumes true for managed apps
19    return true;
20  }
21  return isMatchedInFile(
22    podfilePath,
23    /^\s*require File.join\(File\.dirname\(`node --print "require\.resolve\('expo\/package\.json'\)"`\), "scripts\/autolinking"\)\s*$/m
24  );
25}
26
27/**
28 * Checks if expo-modules-autolinking is setup on Android
29 */
30function isExpoModulesInstalledAndroid(projectRoot) {
31  const gradlePath = path.join(projectRoot, 'android', 'settings.gradle');
32  if (!fs.existsSync(gradlePath)) {
33    // Assumes true for managed apps
34    return true;
35  }
36  return isMatchedInFile(
37    gradlePath,
38    /^\s*apply from: (new File|file)\(\["node", "--print", "require\.resolve\('expo\/package.json'\)"\]\.execute\(null, rootDir\)\.text\.trim\(\), "\.\.\/scripts\/autolinking\.gradle"\);?\s*$/m
39  );
40}
41
42module.exports = {
43  dependency: {
44    platforms: {
45      // To make Expo CLI works on bare react-native projects without installing Expo Modules, we disable autolinking in this case.
46      ios: !isExpoModulesInstalledIos(projectRoot) ? null : {},
47      android: !isExpoModulesInstalledAndroid(projectRoot)
48        ? null
49        : {
50            packageImportPath: 'import expo.modules.ExpoModulesPackage;',
51          },
52      macos: null,
53      windows: null,
54    },
55  },
56};
57