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