Skip to content

Support JSONC format for jsconfig.json #8140

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

Closed
wants to merge 2 commits into from
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
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
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'
) + '.'
)
);
process.exit(1);
}
}
}

config = config || {};
Expand Down