* Get additional module paths based on the baseUrl of a compilerOptions object. * * @param {Object} options
(options = {})
| 12 | * @param {Object} options |
| 13 | */ |
| 14 | function getAdditionalModulePaths(options = {}) { |
| 15 | const baseUrl = options.baseUrl; |
| 16 | |
| 17 | if (!baseUrl) { |
| 18 | return ''; |
| 19 | } |
| 20 | |
| 21 | const baseUrlResolved = path.resolve(paths.appPath, baseUrl); |
| 22 | |
| 23 | // We don't need to do anything if `baseUrl` is set to `node_modules`. This is |
| 24 | // the default behavior. |
| 25 | if (path.relative(paths.appNodeModules, baseUrlResolved) === '') { |
| 26 | return null; |
| 27 | } |
| 28 | |
| 29 | // Allow the user set the `baseUrl` to `appSrc`. |
| 30 | if (path.relative(paths.appSrc, baseUrlResolved) === '') { |
| 31 | return [paths.appSrc]; |
| 32 | } |
| 33 | |
| 34 | // If the path is equal to the root directory we ignore it here. |
| 35 | // We don't want to allow importing from the root directly as source files are |
| 36 | // not transpiled outside of `src`. We do allow importing them with the |
| 37 | // absolute path (e.g. `src/Components/Button.js`) but we set that up with |
| 38 | // an alias. |
| 39 | if (path.relative(paths.appPath, baseUrlResolved) === '') { |
| 40 | return null; |
| 41 | } |
| 42 | |
| 43 | // Otherwise, throw an error. |
| 44 | throw new Error( |
| 45 | chalk.red.bold( |
| 46 | "Your project's `baseUrl` can only be set to `src` or `node_modules`." + |
| 47 | ' Create React App does not support other values at this time.' |
| 48 | ) |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get webpack aliases based on the baseUrl of a compilerOptions object. |