@@ -14,6 +14,7 @@ const resolve = require('resolve');
14
14
const path = require ( 'path' ) ;
15
15
const paths = require ( '../../config/paths' ) ;
16
16
const os = require ( 'os' ) ;
17
+ const immer = require ( 'react-dev-utils/immer' ) . produce ;
17
18
18
19
function writeJson ( fileName , object ) {
19
20
fs . writeFileSync ( fileName , JSON . stringify ( object , null , 2 ) + os . EOL ) ;
@@ -103,10 +104,11 @@ function verifyTypeScriptSetup() {
103
104
} ;
104
105
105
106
const messages = [ ] ;
106
- let tsconfig ;
107
- let parsedOptions ;
107
+ let appTsConfig ;
108
+ let parsedTsConfig ;
109
+ let parsedCompilerOptions ;
108
110
try {
109
- const { config, error } = ts . readConfigFile (
111
+ const { config : readTsConfig , error } = ts . readConfigFile (
110
112
paths . appTsConfig ,
111
113
ts . sys . readFile
112
114
) ;
@@ -115,22 +117,25 @@ function verifyTypeScriptSetup() {
115
117
throw error ;
116
118
}
117
119
118
- tsconfig = config ;
120
+ appTsConfig = readTsConfig ;
119
121
120
122
// Get TS to parse and resolve any "extends"
121
123
// Calling this function also mutates the tsconfig above,
122
124
// 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
+ } ) ;
128
133
129
134
if ( result . errors && result . errors . length ) {
130
135
throw result . errors [ 0 ] ;
131
136
}
132
137
133
- parsedOptions = result . options ;
138
+ parsedCompilerOptions = result . options ;
134
139
} catch ( _ ) {
135
140
console . error (
136
141
chalk . red . bold (
@@ -142,8 +147,8 @@ function verifyTypeScriptSetup() {
142
147
process . exit ( 1 ) ;
143
148
}
144
149
145
- if ( tsconfig . compilerOptions == null ) {
146
- tsconfig . compilerOptions = { } ;
150
+ if ( appTsConfig . compilerOptions == null ) {
151
+ appTsConfig . compilerOptions = { } ;
147
152
firstTimeSetup = true ;
148
153
}
149
154
@@ -153,16 +158,16 @@ function verifyTypeScriptSetup() {
153
158
const valueToCheck = parsedValue === undefined ? value : parsedValue ;
154
159
155
160
if ( suggested != null ) {
156
- if ( parsedOptions [ option ] === undefined ) {
157
- tsconfig . compilerOptions [ option ] = suggested ;
161
+ if ( parsedCompilerOptions [ option ] === undefined ) {
162
+ appTsConfig . compilerOptions [ option ] = suggested ;
158
163
messages . push (
159
164
`${ chalk . cyan ( 'compilerOptions.' + option ) } to be ${ chalk . bold (
160
165
'suggested'
161
166
) } value: ${ chalk . cyan . bold ( suggested ) } (this can be changed)`
162
167
) ;
163
168
}
164
- } else if ( parsedOptions [ option ] !== valueToCheck ) {
165
- tsconfig . compilerOptions [ option ] = value ;
169
+ } else if ( parsedCompilerOptions [ option ] !== valueToCheck ) {
170
+ appTsConfig . compilerOptions [ option ] = value ;
166
171
messages . push (
167
172
`${ chalk . cyan ( 'compilerOptions.' + option ) } ${ chalk . bold (
168
173
'must'
@@ -173,14 +178,14 @@ function verifyTypeScriptSetup() {
173
178
}
174
179
175
180
// 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' ] ;
178
183
messages . push (
179
184
`${ chalk . cyan ( 'include' ) } should be ${ chalk . cyan . bold ( 'src' ) } `
180
185
) ;
181
186
}
182
- if ( tsconfig . exclude == null ) {
183
- tsconfig . exclude = [ '**/__tests__/**' , '**/?*test.*' , '**/?*spec.*' ] ;
187
+ if ( parsedTsConfig . exclude == null ) {
188
+ appTsConfig . exclude = [ '**/__tests__/**' , '**/?*test.*' , '**/?*spec.*' ] ;
184
189
messages . push ( `${ chalk . cyan ( 'exclude' ) } should exclude test files` ) ;
185
190
}
186
191
@@ -207,7 +212,7 @@ function verifyTypeScriptSetup() {
207
212
} ) ;
208
213
console . warn ( ) ;
209
214
}
210
- writeJson ( paths . appTsConfig , tsconfig ) ;
215
+ writeJson ( paths . appTsConfig , appTsConfig ) ;
211
216
}
212
217
213
218
// Copy type declarations associated with this version of `react-scripts`
0 commit comments