Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion packages/react-scripts/config/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function getModules() {
// Check if TypeScript is setup
const hasTsConfig = fs.existsSync(paths.appTsConfig);
const hasJsConfig = fs.existsSync(paths.appJsConfig);
const isYarn = fs.existsSync(paths.yarnLockFile);

if (hasTsConfig && hasJsConfig) {
throw new Error(
Expand All @@ -130,7 +131,43 @@ function getModules() {
// Otherwise we'll check if there is jsconfig.json
// for non TS projects.
} else if (hasJsConfig) {
config = require(paths.appJsConfig);
// Trying to parse jsconfig.json
// using TypeScript
try {
const ts = require(resolve.sync('typescript', {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using hasTsSetup, I think you could just do a try/catch here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

basedir: paths.appNodeModules,
}));

config = ts.readConfigFile(paths.appJsConfig, ts.sys.readFile).config;
} catch (err) {
// Trying to parse jsconfig.json
try {
config = require(paths.appJsConfig);
// If error we assume jsconfig.json has comments
} catch (err) {
console.error(
chalk.bold.red(
`It looks like your jsconfig.json file has comments but ${chalk.bold(
'typescript'
)} is not installed.`,
`If you want to support JSONC you have to install ${chalk.bold(
'typescript'
)} as a dependency.`
)
);
console.log(
chalk.bold(
'Please install',
chalk.cyan.bold('typescript'),
'by running',
chalk.cyan.bold(
(isYarn ? 'yarn add' : 'npm install') + ' typescript'
) + '.'
Comment thread
mrmckeb marked this conversation as resolved.
)
);
process.exit(1);
}
}
}

config = config || {};
Expand Down