|
| 1 | +// @remove-on-eject-begin |
| 2 | +/** |
| 3 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +// @remove-on-eject-end |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +const fs = require('fs'); |
| 12 | +const path = require('path'); |
| 13 | +const paths = require('./paths'); |
| 14 | +const chalk = require('react-dev-utils/chalk'); |
| 15 | + |
| 16 | +/** |
| 17 | + * Get the baseUrl of a compilerOptions object. |
| 18 | + * |
| 19 | + * @param {Object} options |
| 20 | + */ |
| 21 | +function getAdditionalModulePaths(options = {}) { |
| 22 | + const baseUrl = options.baseUrl; |
| 23 | + |
| 24 | + // We need to explicitly check for null and undefined (and not a falsy value) because |
| 25 | + // TypeScript treats an empty string as `.`. |
| 26 | + if (baseUrl == null) { |
| 27 | + // If there's no baseUrl set we respect NODE_PATH |
| 28 | + // Note that NODE_PATH is deprecated and will be removed |
| 29 | + // in the next major release of create-react-app. |
| 30 | + |
| 31 | + const nodePath = process.env.NODE_PATH || ''; |
| 32 | + return nodePath.split(path.delimiter).filter(Boolean); |
| 33 | + } |
| 34 | + |
| 35 | + const baseUrlResolved = path.resolve(paths.appPath, baseUrl); |
| 36 | + |
| 37 | + // We don't need to do anything if `baseUrl` is set to `node_modules`. This is |
| 38 | + // the default behavior. |
| 39 | + if (path.relative(paths.appNodeModules, baseUrlResolved) === '') { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + // Allow the user set the `baseUrl` to `appSrc`. |
| 44 | + if (path.relative(paths.appSrc, baseUrlResolved) === '') { |
| 45 | + return [paths.appSrc]; |
| 46 | + } |
| 47 | + |
| 48 | + // Otherwise, throw an error. |
| 49 | + throw new Error( |
| 50 | + chalk.red.bold( |
| 51 | + "Your project's `baseUrl` can only be set to `src` or `node_modules`." + |
| 52 | + ' Create React App does not support other values at this time.' |
| 53 | + ) |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +function getModules() { |
| 58 | + // Check if TypeScript is setup |
| 59 | + const hasTsConfig = fs.existsSync(paths.appTsConfig); |
| 60 | + const hasJsConfig = fs.existsSync(paths.appJsConfig); |
| 61 | + |
| 62 | + if (hasTsConfig && hasJsConfig) { |
| 63 | + throw new Error( |
| 64 | + 'You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file.' |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + let config; |
| 69 | + |
| 70 | + // If there's a tsconfig.json we assume it's a |
| 71 | + // TypeScript project and set up the config |
| 72 | + // based on tsconfig.json |
| 73 | + if (hasTsConfig) { |
| 74 | + config = require(paths.appTsConfig); |
| 75 | + // Otherwise we'll check if there is jsconfig.json |
| 76 | + // for non TS projects. |
| 77 | + } else if (hasJsConfig) { |
| 78 | + config = require(paths.appJsConfig); |
| 79 | + } |
| 80 | + |
| 81 | + config = config || {}; |
| 82 | + const options = config.compilerOptions || {}; |
| 83 | + |
| 84 | + const additionalModulePaths = getAdditionalModulePaths(options); |
| 85 | + |
| 86 | + return { |
| 87 | + additionalModulePaths: additionalModulePaths, |
| 88 | + hasTsConfig, |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +module.exports = getModules(); |
0 commit comments