Skip to content

Commit

Permalink
Prevent modifications to original tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer committed Oct 23, 2018
1 parent f6a1235 commit 1feaa5c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
12 changes: 12 additions & 0 deletions packages/react-dev-utils/immer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var immer = require('immer');

module.exports = immer;
2 changes: 2 additions & 0 deletions packages/react-dev-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"getCSSModuleLocalIdent.js",
"getProcessForPort.js",
"ignoredFiles.js",
"immer.js",
"InlineChunkHtmlPlugin.js",
"inquirer.js",
"InterpolateHtmlPlugin.js",
Expand Down Expand Up @@ -53,6 +54,7 @@
"find-up": "3.0.0",
"global-modules": "1.0.0",
"gzip-size": "5.0.0",
"immer": "1.7.2",
"inquirer": "6.2.0",
"is-root": "2.0.0",
"loader-utils": "1.1.0",
Expand Down
47 changes: 26 additions & 21 deletions packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const resolve = require('resolve');
const path = require('path');
const paths = require('../../config/paths');
const os = require('os');
const immer = require('react-dev-utils/immer').produce;

function writeJson(fileName, object) {
fs.writeFileSync(fileName, JSON.stringify(object, null, 2) + os.EOL);
Expand Down Expand Up @@ -103,10 +104,11 @@ function verifyTypeScriptSetup() {
};

const messages = [];
let tsconfig;
let parsedOptions;
let appTsConfig;
let parsedTsConfig;
let parsedCompilerOptions;
try {
const { config, error } = ts.readConfigFile(
const { config: readTsConfig, error } = ts.readConfigFile(
paths.appTsConfig,
ts.sys.readFile
);
Expand All @@ -115,22 +117,25 @@ function verifyTypeScriptSetup() {
throw error;
}

tsconfig = config;
appTsConfig = readTsConfig;

// Get TS to parse and resolve any "extends"
// Calling this function also mutates the tsconfig above,
// adding in "include" and "exclude", but the compilerOptions remain untouched
const result = ts.parseJsonConfigFileContent(
config,
ts.sys,
path.dirname(paths.appTsConfig)
);
let result;
parsedTsConfig = immer(readTsConfig, config => {
result = ts.parseJsonConfigFileContent(
config,
ts.sys,
path.dirname(paths.appTsConfig)
);
});

if (result.errors && result.errors.length) {
throw result.errors[0];
}

parsedOptions = result.options;
parsedCompilerOptions = result.options;
} catch (_) {
console.error(
chalk.red.bold(
Expand All @@ -142,8 +147,8 @@ function verifyTypeScriptSetup() {
process.exit(1);
}

if (tsconfig.compilerOptions == null) {
tsconfig.compilerOptions = {};
if (appTsConfig.compilerOptions == null) {
appTsConfig.compilerOptions = {};
firstTimeSetup = true;
}

Expand All @@ -153,16 +158,16 @@ function verifyTypeScriptSetup() {
const valueToCheck = parsedValue === undefined ? value : parsedValue;

if (suggested != null) {
if (parsedOptions[option] === undefined) {
tsconfig.compilerOptions[option] = suggested;
if (parsedCompilerOptions[option] === undefined) {
appTsConfig.compilerOptions[option] = suggested;
messages.push(
`${chalk.cyan('compilerOptions.' + option)} to be ${chalk.bold(
'suggested'
)} value: ${chalk.cyan.bold(suggested)} (this can be changed)`
);
}
} else if (parsedOptions[option] !== valueToCheck) {
tsconfig.compilerOptions[option] = value;
} else if (parsedCompilerOptions[option] !== valueToCheck) {
appTsConfig.compilerOptions[option] = value;
messages.push(
`${chalk.cyan('compilerOptions.' + option)} ${chalk.bold(
'must'
Expand All @@ -173,14 +178,14 @@ function verifyTypeScriptSetup() {
}

// tsconfig will have the merged "include" and "exclude" by this point
if (tsconfig.include == null) {
tsconfig.include = ['src'];
if (parsedTsConfig.include == null) {
appTsConfig.include = ['src'];
messages.push(
`${chalk.cyan('include')} should be ${chalk.cyan.bold('src')}`
);
}
if (tsconfig.exclude == null) {
tsconfig.exclude = ['**/__tests__/**', '**/?*test.*', '**/?*spec.*'];
if (parsedTsConfig.exclude == null) {
appTsConfig.exclude = ['**/__tests__/**', '**/?*test.*', '**/?*spec.*'];
messages.push(`${chalk.cyan('exclude')} should exclude test files`);
}

Expand All @@ -207,7 +212,7 @@ function verifyTypeScriptSetup() {
});
console.warn();
}
writeJson(paths.appTsConfig, tsconfig);
writeJson(paths.appTsConfig, appTsConfig);
}

// Copy type declarations associated with this version of `react-scripts`
Expand Down

0 comments on commit 1feaa5c

Please sign in to comment.