| 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 | const resolve = require('resolve');
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Get additional module paths based on the baseUrl of a compilerOptions object.
|
|---|
| 19 | *
|
|---|
| 20 | * @param {Object} options
|
|---|
| 21 | */
|
|---|
| 22 | function getAdditionalModulePaths(options = {}) {
|
|---|
| 23 | const baseUrl = options.baseUrl;
|
|---|
| 24 |
|
|---|
| 25 | if (!baseUrl) {
|
|---|
| 26 | return '';
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
|
|---|
| 30 |
|
|---|
| 31 | // We don't need to do anything if `baseUrl` is set to `node_modules`. This is
|
|---|
| 32 | // the default behavior.
|
|---|
| 33 | if (path.relative(paths.appNodeModules, baseUrlResolved) === '') {
|
|---|
| 34 | return null;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | // Allow the user set the `baseUrl` to `appSrc`.
|
|---|
| 38 | if (path.relative(paths.appSrc, baseUrlResolved) === '') {
|
|---|
| 39 | return [paths.appSrc];
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | // If the path is equal to the root directory we ignore it here.
|
|---|
| 43 | // We don't want to allow importing from the root directly as source files are
|
|---|
| 44 | // not transpiled outside of `src`. We do allow importing them with the
|
|---|
| 45 | // absolute path (e.g. `src/Components/Button.js`) but we set that up with
|
|---|
| 46 | // an alias.
|
|---|
| 47 | if (path.relative(paths.appPath, baseUrlResolved) === '') {
|
|---|
| 48 | return null;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | // Otherwise, throw an error.
|
|---|
| 52 | throw new Error(
|
|---|
| 53 | chalk.red.bold(
|
|---|
| 54 | "Your project's `baseUrl` can only be set to `src` or `node_modules`." +
|
|---|
| 55 | ' Create React App does not support other values at this time.'
|
|---|
| 56 | )
|
|---|
| 57 | );
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Get webpack aliases based on the baseUrl of a compilerOptions object.
|
|---|
| 62 | *
|
|---|
| 63 | * @param {*} options
|
|---|
| 64 | */
|
|---|
| 65 | function getWebpackAliases(options = {}) {
|
|---|
| 66 | const baseUrl = options.baseUrl;
|
|---|
| 67 |
|
|---|
| 68 | if (!baseUrl) {
|
|---|
| 69 | return {};
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
|
|---|
| 73 |
|
|---|
| 74 | if (path.relative(paths.appPath, baseUrlResolved) === '') {
|
|---|
| 75 | return {
|
|---|
| 76 | src: paths.appSrc,
|
|---|
| 77 | };
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Get jest aliases based on the baseUrl of a compilerOptions object.
|
|---|
| 83 | *
|
|---|
| 84 | * @param {*} options
|
|---|
| 85 | */
|
|---|
| 86 | function getJestAliases(options = {}) {
|
|---|
| 87 | const baseUrl = options.baseUrl;
|
|---|
| 88 |
|
|---|
| 89 | if (!baseUrl) {
|
|---|
| 90 | return {};
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
|
|---|
| 94 |
|
|---|
| 95 | if (path.relative(paths.appPath, baseUrlResolved) === '') {
|
|---|
| 96 | return {
|
|---|
| 97 | '^src/(.*)$': '<rootDir>/src/$1',
|
|---|
| 98 | };
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | function getModules() {
|
|---|
| 103 | // Check if TypeScript is setup
|
|---|
| 104 | const hasTsConfig = fs.existsSync(paths.appTsConfig);
|
|---|
| 105 | const hasJsConfig = fs.existsSync(paths.appJsConfig);
|
|---|
| 106 |
|
|---|
| 107 | if (hasTsConfig && hasJsConfig) {
|
|---|
| 108 | throw new Error(
|
|---|
| 109 | 'You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file.'
|
|---|
| 110 | );
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | let config;
|
|---|
| 114 |
|
|---|
| 115 | // If there's a tsconfig.json we assume it's a
|
|---|
| 116 | // TypeScript project and set up the config
|
|---|
| 117 | // based on tsconfig.json
|
|---|
| 118 | if (hasTsConfig) {
|
|---|
| 119 | const ts = require(resolve.sync('typescript', {
|
|---|
| 120 | basedir: paths.appNodeModules,
|
|---|
| 121 | }));
|
|---|
| 122 | config = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile).config;
|
|---|
| 123 | // Otherwise we'll check if there is jsconfig.json
|
|---|
| 124 | // for non TS projects.
|
|---|
| 125 | } else if (hasJsConfig) {
|
|---|
| 126 | config = require(paths.appJsConfig);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | config = config || {};
|
|---|
| 130 | const options = config.compilerOptions || {};
|
|---|
| 131 |
|
|---|
| 132 | const additionalModulePaths = getAdditionalModulePaths(options);
|
|---|
| 133 |
|
|---|
| 134 | return {
|
|---|
| 135 | additionalModulePaths: additionalModulePaths,
|
|---|
| 136 | webpackAliases: getWebpackAliases(options),
|
|---|
| 137 | jestAliases: getJestAliases(options),
|
|---|
| 138 | hasTsConfig,
|
|---|
| 139 | };
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | module.exports = getModules();
|
|---|