Skip to content

Commit 1feaa5c

Browse files
committed
Prevent modifications to original tsconfig
1 parent f6a1235 commit 1feaa5c

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

packages/react-dev-utils/immer.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
var immer = require('immer');
11+
12+
module.exports = immer;

packages/react-dev-utils/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"getCSSModuleLocalIdent.js",
2525
"getProcessForPort.js",
2626
"ignoredFiles.js",
27+
"immer.js",
2728
"InlineChunkHtmlPlugin.js",
2829
"inquirer.js",
2930
"InterpolateHtmlPlugin.js",
@@ -53,6 +54,7 @@
5354
"find-up": "3.0.0",
5455
"global-modules": "1.0.0",
5556
"gzip-size": "5.0.0",
57+
"immer": "1.7.2",
5658
"inquirer": "6.2.0",
5759
"is-root": "2.0.0",
5860
"loader-utils": "1.1.0",

packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js

+26-21
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const resolve = require('resolve');
1414
const path = require('path');
1515
const paths = require('../../config/paths');
1616
const os = require('os');
17+
const immer = require('react-dev-utils/immer').produce;
1718

1819
function writeJson(fileName, object) {
1920
fs.writeFileSync(fileName, JSON.stringify(object, null, 2) + os.EOL);
@@ -103,10 +104,11 @@ function verifyTypeScriptSetup() {
103104
};
104105

105106
const messages = [];
106-
let tsconfig;
107-
let parsedOptions;
107+
let appTsConfig;
108+
let parsedTsConfig;
109+
let parsedCompilerOptions;
108110
try {
109-
const { config, error } = ts.readConfigFile(
111+
const { config: readTsConfig, error } = ts.readConfigFile(
110112
paths.appTsConfig,
111113
ts.sys.readFile
112114
);
@@ -115,22 +117,25 @@ function verifyTypeScriptSetup() {
115117
throw error;
116118
}
117119

118-
tsconfig = config;
120+
appTsConfig = readTsConfig;
119121

120122
// Get TS to parse and resolve any "extends"
121123
// Calling this function also mutates the tsconfig above,
122124
// adding in "include" and "exclude", but the compilerOptions remain untouched
123-
const result = ts.parseJsonConfigFileContent(
124-
config,
125-
ts.sys,
126-
path.dirname(paths.appTsConfig)
127-
);
125+
let result;
126+
parsedTsConfig = immer(readTsConfig, config => {
127+
result = ts.parseJsonConfigFileContent(
128+
config,
129+
ts.sys,
130+
path.dirname(paths.appTsConfig)
131+
);
132+
});
128133

129134
if (result.errors && result.errors.length) {
130135
throw result.errors[0];
131136
}
132137

133-
parsedOptions = result.options;
138+
parsedCompilerOptions = result.options;
134139
} catch (_) {
135140
console.error(
136141
chalk.red.bold(
@@ -142,8 +147,8 @@ function verifyTypeScriptSetup() {
142147
process.exit(1);
143148
}
144149

145-
if (tsconfig.compilerOptions == null) {
146-
tsconfig.compilerOptions = {};
150+
if (appTsConfig.compilerOptions == null) {
151+
appTsConfig.compilerOptions = {};
147152
firstTimeSetup = true;
148153
}
149154

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

155160
if (suggested != null) {
156-
if (parsedOptions[option] === undefined) {
157-
tsconfig.compilerOptions[option] = suggested;
161+
if (parsedCompilerOptions[option] === undefined) {
162+
appTsConfig.compilerOptions[option] = suggested;
158163
messages.push(
159164
`${chalk.cyan('compilerOptions.' + option)} to be ${chalk.bold(
160165
'suggested'
161166
)} value: ${chalk.cyan.bold(suggested)} (this can be changed)`
162167
);
163168
}
164-
} else if (parsedOptions[option] !== valueToCheck) {
165-
tsconfig.compilerOptions[option] = value;
169+
} else if (parsedCompilerOptions[option] !== valueToCheck) {
170+
appTsConfig.compilerOptions[option] = value;
166171
messages.push(
167172
`${chalk.cyan('compilerOptions.' + option)} ${chalk.bold(
168173
'must'
@@ -173,14 +178,14 @@ function verifyTypeScriptSetup() {
173178
}
174179

175180
// tsconfig will have the merged "include" and "exclude" by this point
176-
if (tsconfig.include == null) {
177-
tsconfig.include = ['src'];
181+
if (parsedTsConfig.include == null) {
182+
appTsConfig.include = ['src'];
178183
messages.push(
179184
`${chalk.cyan('include')} should be ${chalk.cyan.bold('src')}`
180185
);
181186
}
182-
if (tsconfig.exclude == null) {
183-
tsconfig.exclude = ['**/__tests__/**', '**/?*test.*', '**/?*spec.*'];
187+
if (parsedTsConfig.exclude == null) {
188+
appTsConfig.exclude = ['**/__tests__/**', '**/?*test.*', '**/?*spec.*'];
184189
messages.push(`${chalk.cyan('exclude')} should exclude test files`);
185190
}
186191

@@ -207,7 +212,7 @@ function verifyTypeScriptSetup() {
207212
});
208213
console.warn();
209214
}
210-
writeJson(paths.appTsConfig, tsconfig);
215+
writeJson(paths.appTsConfig, appTsConfig);
211216
}
212217

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

0 commit comments

Comments
 (0)