Skip to content

Commit 7e2b6b1

Browse files
robertvansteenmrmckeb
authored andcommitted
Support setting baseUrl to root directory (#7755)
1 parent 85aac9b commit 7e2b6b1

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

packages/react-scripts/config/modules.js

+54-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const chalk = require('react-dev-utils/chalk');
1515
const resolve = require('resolve');
1616

1717
/**
18-
* Get the baseUrl of a compilerOptions object.
18+
* Get additional module paths based on the baseUrl of a compilerOptions object.
1919
*
2020
* @param {Object} options
2121
*/
@@ -46,6 +46,15 @@ function getAdditionalModulePaths(options = {}) {
4646
return [paths.appSrc];
4747
}
4848

49+
// If the path is equal to the root directory we ignore it here.
50+
// We don't want to allow importing from the root directly as source files are
51+
// not transpiled outside of `src`. We do allow importing them with the
52+
// absolute path (e.g. `src/Components/Button.js`) but we set that up with
53+
// an alias.
54+
if (path.relative(paths.appPath, baseUrlResolved) === '') {
55+
return null;
56+
}
57+
4958
// Otherwise, throw an error.
5059
throw new Error(
5160
chalk.red.bold(
@@ -55,6 +64,48 @@ function getAdditionalModulePaths(options = {}) {
5564
);
5665
}
5766

67+
/**
68+
* Get webpack aliases based on the baseUrl of a compilerOptions object.
69+
*
70+
* @param {*} options
71+
*/
72+
function getWebpackAliases(options = {}) {
73+
const baseUrl = options.baseUrl;
74+
75+
if (!baseUrl) {
76+
return {};
77+
}
78+
79+
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
80+
81+
if (path.relative(paths.appPath, baseUrlResolved) === '') {
82+
return {
83+
src: paths.appSrc,
84+
};
85+
}
86+
}
87+
88+
/**
89+
* Get jest aliases based on the baseUrl of a compilerOptions object.
90+
*
91+
* @param {*} options
92+
*/
93+
function getJestAliases(options = {}) {
94+
const baseUrl = options.baseUrl;
95+
96+
if (!baseUrl) {
97+
return {};
98+
}
99+
100+
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
101+
102+
if (path.relative(paths.appPath, baseUrlResolved) === '') {
103+
return {
104+
'src/(.*)$': '<rootDir>/src/$1',
105+
};
106+
}
107+
}
108+
58109
function getModules() {
59110
// Check if TypeScript is setup
60111
const hasTsConfig = fs.existsSync(paths.appTsConfig);
@@ -89,6 +140,8 @@ function getModules() {
89140

90141
return {
91142
additionalModulePaths: additionalModulePaths,
143+
webpackAliases: getWebpackAliases(options),
144+
jestAliases: getJestAliases(options),
92145
hasTsConfig,
93146
};
94147
}

packages/react-scripts/config/webpack.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ module.exports = function(webpackEnv) {
306306
// Support React Native Web
307307
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
308308
'react-native': 'react-native-web',
309+
...(modules.webpackAliases || {}),
309310
},
310311
plugins: [
311312
// Adds support for installing with Plug'n'Play, leading to faster installs and adding
@@ -352,7 +353,9 @@ module.exports = function(webpackEnv) {
352353
const eslintCli = new eslint.CLIEngine();
353354
let eslintConfig;
354355
try {
355-
eslintConfig = eslintCli.getConfigForFile(paths.appIndexJs);
356+
eslintConfig = eslintCli.getConfigForFile(
357+
paths.appIndexJs
358+
);
356359
} catch (e) {
357360
console.error(e);
358361
process.exit(1);

packages/react-scripts/scripts/utils/createJestConfig.js

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ module.exports = (resolve, rootDir, isEjecting) => {
5656
moduleNameMapper: {
5757
'^react-native$': 'react-native-web',
5858
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
59+
...(modules.jestAliases || {}),
5960
},
6061
moduleFileExtensions: [...paths.moduleFileExtensions, 'node'].filter(
6162
ext => !ext.includes('mjs')

0 commit comments

Comments
 (0)