xref: /expo/packages/@expo/config/build/paths/paths.js (revision b422da63)
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.ensureSlash = ensureSlash;
7exports.getEntryPoint = getEntryPoint;
8exports.getEntryPointWithExtensions = getEntryPointWithExtensions;
9exports.getFileWithExtensions = getFileWithExtensions;
10exports.getPossibleProjectRoot = getPossibleProjectRoot;
11exports.resolveEntryPoint = resolveEntryPoint;
12exports.resolveFromSilentWithExtensions = resolveFromSilentWithExtensions;
13function _fs() {
14  const data = _interopRequireDefault(require("fs"));
15  _fs = function () {
16    return data;
17  };
18  return data;
19}
20function _path() {
21  const data = _interopRequireDefault(require("path"));
22  _path = function () {
23    return data;
24  };
25  return data;
26}
27function _resolveFrom() {
28  const data = _interopRequireDefault(require("resolve-from"));
29  _resolveFrom = function () {
30    return data;
31  };
32  return data;
33}
34function _Config() {
35  const data = require("../Config");
36  _Config = function () {
37    return data;
38  };
39  return data;
40}
41function _extensions() {
42  const data = require("./extensions");
43  _extensions = function () {
44    return data;
45  };
46  return data;
47}
48function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
49// https://github.com/facebook/create-react-app/blob/9750738cce89a967cc71f28390daf5d4311b193c/packages/react-scripts/config/paths.js#L22
50function ensureSlash(inputPath, needsSlash) {
51  const hasSlash = inputPath.endsWith('/');
52  if (hasSlash && !needsSlash) {
53    return inputPath.substr(0, inputPath.length - 1);
54  } else if (!hasSlash && needsSlash) {
55    return `${inputPath}/`;
56  } else {
57    return inputPath;
58  }
59}
60function getPossibleProjectRoot() {
61  return _fs().default.realpathSync(process.cwd());
62}
63const nativePlatforms = ['ios', 'android'];
64function resolveEntryPoint(projectRoot, {
65  platform,
66  projectConfig
67}) {
68  const platforms = nativePlatforms.includes(platform) ? [platform, 'native'] : [platform];
69  return getEntryPoint(projectRoot, ['./index'], platforms, projectConfig);
70}
71function getEntryPoint(projectRoot, entryFiles, platforms, projectConfig) {
72  const extensions = (0, _extensions().getBareExtensions)(platforms);
73  return getEntryPointWithExtensions(projectRoot, entryFiles, extensions, projectConfig);
74}
75
76// Used to resolve the main entry file for a project.
77function getEntryPointWithExtensions(projectRoot, entryFiles, extensions, projectConfig) {
78  const {
79    exp,
80    pkg
81  } = projectConfig !== null && projectConfig !== void 0 ? projectConfig : (0, _Config().getConfig)(projectRoot, {
82    skipSDKVersionRequirement: true
83  });
84
85  // This will first look in the `app.json`s `expo.entryPoint` field for a potential main file.
86  // We check the Expo config first in case you want your project to start differently with Expo then in a standalone environment.
87  if (exp && exp.entryPoint && typeof exp.entryPoint === 'string') {
88    // If the field exists then we want to test it against every one of the supplied extensions
89    // to ensure the bundler resolves the same way.
90    let entry = getFileWithExtensions(projectRoot, exp.entryPoint, extensions);
91    if (!entry) {
92      // Allow for paths like: `{ "main": "expo/AppEntry" }`
93      entry = resolveFromSilentWithExtensions(projectRoot, exp.entryPoint, extensions);
94
95      // If it doesn't resolve then just return the entryPoint as-is. This makes
96      // it possible for people who have an unconventional setup (eg: multiple
97      // apps in monorepo with metro at root) to customize entry point without
98      // us imposing our assumptions.
99      if (!entry) {
100        return exp.entryPoint;
101      }
102    }
103    return entry;
104  } else if (pkg) {
105    // If the config doesn't define a custom entry then we want to look at the `package.json`s `main` field, and try again.
106    const {
107      main
108    } = pkg;
109    if (main && typeof main === 'string') {
110      // Testing the main field against all of the provided extensions - for legacy reasons we can't use node module resolution as the package.json allows you to pass in a file without a relative path and expect it as a relative path.
111      let entry = getFileWithExtensions(projectRoot, main, extensions);
112      if (!entry) {
113        // Allow for paths like: `{ "main": "expo/AppEntry" }`
114        entry = resolveFromSilentWithExtensions(projectRoot, main, extensions);
115        if (!entry) throw new Error(`Cannot resolve entry file: The \`main\` field defined in your \`package.json\` points to a non-existent path.`);
116      }
117      return entry;
118    }
119  }
120
121  // Now we will start looking for a default entry point using the provided `entryFiles` argument.
122  // This will add support for create-react-app (src/index.js) and react-native-cli (index.js) which don't define a main.
123  for (const fileName of entryFiles) {
124    const entry = resolveFromSilentWithExtensions(projectRoot, fileName, extensions);
125    if (entry) return entry;
126  }
127  try {
128    // If none of the default files exist then we will attempt to use the main Expo entry point.
129    // This requires `expo` to be installed in the project to work as it will use `node_module/expo/AppEntry.js`
130    // Doing this enables us to create a bare minimum Expo project.
131
132    // TODO(Bacon): We may want to do a check against `./App` and `expo` in the `package.json` `dependencies` as we can more accurately ensure that the project is expo-min without needing the modules installed.
133    return (0, _resolveFrom().default)(projectRoot, 'expo/AppEntry');
134  } catch {
135    throw new Error(`The project entry file could not be resolved. Please either define it in the \`package.json\` (main), \`app.json\` (expo.entryPoint), create an \`index.js\`, or install the \`expo\` package.`);
136  }
137}
138
139// Resolve from but with the ability to resolve like a bundler
140function resolveFromSilentWithExtensions(fromDirectory, moduleId, extensions) {
141  for (const extension of extensions) {
142    const modulePath = _resolveFrom().default.silent(fromDirectory, `${moduleId}.${extension}`);
143    if (modulePath && modulePath.endsWith(extension)) {
144      return modulePath;
145    }
146  }
147  return _resolveFrom().default.silent(fromDirectory, moduleId) || null;
148}
149
150// Statically attempt to resolve a module but with the ability to resolve like a bundler.
151// This won't use node module resolution.
152function getFileWithExtensions(fromDirectory, moduleId, extensions) {
153  const modulePath = _path().default.join(fromDirectory, moduleId);
154  if (_fs().default.existsSync(modulePath)) {
155    return modulePath;
156  }
157  for (const extension of extensions) {
158    const modulePath = _path().default.join(fromDirectory, `${moduleId}.${extension}`);
159    if (_fs().default.existsSync(modulePath)) {
160      return modulePath;
161    }
162  }
163  return null;
164}
165//# sourceMappingURL=paths.js.map