Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting baseUrl to root directory #7755

Merged
merged 1 commit into from Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 54 additions & 1 deletion packages/react-scripts/config/modules.js
Expand Up @@ -15,7 +15,7 @@ const chalk = require('react-dev-utils/chalk');
const resolve = require('resolve');

/**
* Get the baseUrl of a compilerOptions object.
* Get additional module paths based on the baseUrl of a compilerOptions object.
*
* @param {Object} options
*/
Expand Down Expand Up @@ -46,6 +46,15 @@ function getAdditionalModulePaths(options = {}) {
return [paths.appSrc];
}

// If the path is equal to the root directory we ignore it here.
// We don't want to allow importing from the root directly as source files are
// not transpiled outside of `src`. We do allow importing them with the
// absolute path (e.g. `src/Components/Button.js`) but we set that up with
// an alias.
if (path.relative(paths.appPath, baseUrlResolved) === '') {
return null;
}

// Otherwise, throw an error.
throw new Error(
chalk.red.bold(
Expand All @@ -55,6 +64,48 @@ function getAdditionalModulePaths(options = {}) {
);
}

/**
* Get webpack aliases based on the baseUrl of a compilerOptions object.
*
* @param {*} options
*/
function getWebpackAliases(options = {}) {
const baseUrl = options.baseUrl;

if (!baseUrl) {
return {};
}

const baseUrlResolved = path.resolve(paths.appPath, baseUrl);

if (path.relative(paths.appPath, baseUrlResolved) === '') {
return {
src: paths.appSrc,
};
}
}

/**
* Get jest aliases based on the baseUrl of a compilerOptions object.
*
* @param {*} options
*/
function getJestAliases(options = {}) {
const baseUrl = options.baseUrl;

if (!baseUrl) {
return {};
}

const baseUrlResolved = path.resolve(paths.appPath, baseUrl);

if (path.relative(paths.appPath, baseUrlResolved) === '') {
return {
'src/(.*)$': '<rootDir>/src/$1',
};
}
}

function getModules() {
// Check if TypeScript is setup
const hasTsConfig = fs.existsSync(paths.appTsConfig);
Expand Down Expand Up @@ -89,6 +140,8 @@ function getModules() {

return {
additionalModulePaths: additionalModulePaths,
webpackAliases: getWebpackAliases(options),
jestAliases: getJestAliases(options),
hasTsConfig,
};
}
Expand Down
5 changes: 4 additions & 1 deletion packages/react-scripts/config/webpack.config.js
Expand Up @@ -306,6 +306,7 @@ module.exports = function(webpackEnv) {
// Support React Native Web
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
'react-native': 'react-native-web',
...(modules.webpackAliases || {}),
},
plugins: [
// Adds support for installing with Plug'n'Play, leading to faster installs and adding
Expand Down Expand Up @@ -352,7 +353,9 @@ module.exports = function(webpackEnv) {
const eslintCli = new eslint.CLIEngine();
let eslintConfig;
try {
eslintConfig = eslintCli.getConfigForFile(paths.appIndexJs);
eslintConfig = eslintCli.getConfigForFile(
paths.appIndexJs
);
} catch (e) {
console.error(e);
process.exit(1);
Expand Down
1 change: 1 addition & 0 deletions packages/react-scripts/scripts/utils/createJestConfig.js
Expand Up @@ -56,6 +56,7 @@ module.exports = (resolve, rootDir, isEjecting) => {
moduleNameMapper: {
'^react-native$': 'react-native-web',
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
...(modules.jestAliases || {}),
},
moduleFileExtensions: [...paths.moduleFileExtensions, 'node'].filter(
ext => !ext.includes('mjs')
Expand Down