From a2fef9cbfb2fb3d7dbbe5fde932babeb0eb81791 Mon Sep 17 00:00:00 2001 From: esvyridov Date: Wed, 11 Dec 2019 16:24:36 +0300 Subject: [PATCH 1/2] Support JSONC format for jsconfig.json --- packages/react-scripts/config/modules.js | 38 +++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/react-scripts/config/modules.js b/packages/react-scripts/config/modules.js index 38c95b91e44..a4112015e32 100644 --- a/packages/react-scripts/config/modules.js +++ b/packages/react-scripts/config/modules.js @@ -110,6 +110,10 @@ function getModules() { // Check if TypeScript is setup const hasTsConfig = fs.existsSync(paths.appTsConfig); const hasJsConfig = fs.existsSync(paths.appJsConfig); + const hasTsSetup = fs.existsSync( + path.join(paths.appNodeModules, 'typescript') + ); + const isYarn = fs.existsSync(paths.yarnLockFile); if (hasTsConfig && hasJsConfig) { throw new Error( @@ -130,7 +134,39 @@ function getModules() { // Otherwise we'll check if there is jsconfig.json // for non TS projects. } else if (hasJsConfig) { - config = require(paths.appJsConfig); + if (hasTsSetup) { + const ts = require(resolve.sync('typescript', { + basedir: paths.appNodeModules, + })); + + config = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile).config; + } else { + try { + config = require(paths.appJsConfig); + } 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 typescript' : 'npm install typescript' + ) + '.' + ) + ); + process.exit(1); + } + } } config = config || {}; From 2676656cf711ce7e0c8a63b5f56fa33e96866dcc Mon Sep 17 00:00:00 2001 From: esvyridov Date: Mon, 16 Dec 2019 22:05:05 +0300 Subject: [PATCH 2/2] Remove hasTsSetup, optimize error message --- packages/react-scripts/config/modules.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/react-scripts/config/modules.js b/packages/react-scripts/config/modules.js index a4112015e32..6a2e69a1de4 100644 --- a/packages/react-scripts/config/modules.js +++ b/packages/react-scripts/config/modules.js @@ -110,9 +110,6 @@ function getModules() { // Check if TypeScript is setup const hasTsConfig = fs.existsSync(paths.appTsConfig); const hasJsConfig = fs.existsSync(paths.appJsConfig); - const hasTsSetup = fs.existsSync( - path.join(paths.appNodeModules, 'typescript') - ); const isYarn = fs.existsSync(paths.yarnLockFile); if (hasTsConfig && hasJsConfig) { @@ -134,15 +131,19 @@ function getModules() { // Otherwise we'll check if there is jsconfig.json // for non TS projects. } else if (hasJsConfig) { - if (hasTsSetup) { + // Trying to parse jsconfig.json + // using TypeScript + try { const ts = require(resolve.sync('typescript', { basedir: paths.appNodeModules, })); - config = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile).config; - } else { + 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( @@ -160,7 +161,7 @@ function getModules() { chalk.cyan.bold('typescript'), 'by running', chalk.cyan.bold( - isYarn ? 'yarn add typescript' : 'npm install typescript' + (isYarn ? 'yarn add' : 'npm install') + ' typescript' ) + '.' ) );