1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
7 const fs = require('fs');
8 const path = require('path');
9 const paths = require('./paths');
10 const chalk = require('react-dev-utils/chalk');
11 const resolve = require('resolve');
14 * Get additional module paths based on the baseUrl of a compilerOptions object.
16 * @param {Object} options
18 function getAdditionalModulePaths(options = {}) {
19 const baseUrl = options.baseUrl;
25 const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
27 // We don't need to do anything if `baseUrl` is set to `node_modules`. This is
28 // the default behavior.
29 if (path.relative(paths.appNodeModules, baseUrlResolved) === '') {
33 // Allow the user set the `baseUrl` to `appSrc`.
34 if (path.relative(paths.appSrc, baseUrlResolved) === '') {
35 return [paths.appSrc];
38 // If the path is equal to the root directory we ignore it here.
39 // We don't want to allow importing from the root directly as source files are
40 // not transpiled outside of `src`. We do allow importing them with the
41 // absolute path (e.g. `src/Components/Button.js`) but we set that up with
43 if (path.relative(paths.appPath, baseUrlResolved) === '') {
47 // Otherwise, throw an error.
50 "Your project's `baseUrl` can only be set to `src` or `node_modules`." +
51 ' Create React App does not support other values at this time.'
57 * Get webpack aliases based on the baseUrl of a compilerOptions object.
61 function getWebpackAliases(options = {}) {
62 const baseUrl = options.baseUrl;
68 const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
70 if (path.relative(paths.appPath, baseUrlResolved) === '') {
78 * Get jest aliases based on the baseUrl of a compilerOptions object.
82 function getJestAliases(options = {}) {
83 const baseUrl = options.baseUrl;
89 const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
91 if (path.relative(paths.appPath, baseUrlResolved) === '') {
93 '^src/(.*)$': '<rootDir>/src/$1',
98 function getModules() {
99 // Check if TypeScript is setup
100 const hasTsConfig = fs.existsSync(paths.appTsConfig);
101 const hasJsConfig = fs.existsSync(paths.appJsConfig);
103 if (hasTsConfig && hasJsConfig) {
105 'You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file.'
111 // If there's a tsconfig.json we assume it's a
112 // TypeScript project and set up the config
113 // based on tsconfig.json
115 const ts = require(resolve.sync('typescript', {
116 basedir: paths.appNodeModules,
118 config = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile).config;
119 // Otherwise we'll check if there is jsconfig.json
120 // for non TS projects.
121 } else if (hasJsConfig) {
122 config = require(paths.appJsConfig);
125 config = config || {};
126 const options = config.compilerOptions || {};
128 const additionalModulePaths = getAdditionalModulePaths(options);
131 additionalModulePaths: additionalModulePaths,
132 webpackAliases: getWebpackAliases(options),
133 jestAliases: getJestAliases(options),
138 module.exports = getModules();