@@ -130,11 +130,21 @@ export function mockGlobalTSConfigSchema(globals: any) {
130
130
{ __TS_CONFIG__ : config } ;
131
131
}
132
132
133
+ const tsConfigCache : { [ key : string ] : any } = { } ;
133
134
export function getTSConfig ( globals , collectCoverage : boolean = false ) {
134
135
let config = getTSConfigOptionFromConfig ( globals ) ;
135
136
const skipBabel = getTSJestConfig ( globals ) . skipBabel ;
136
137
const isReferencedExternalFile = typeof config === 'string' ;
137
138
139
+ // check cache before resolving configuration
140
+ // NB: config is a string unless taken from __TS_CONFIG__, which should be immutable (and is deprecated anyways)
141
+ // NB: We use JSON.stringify() to create a consistent, unique signature. Although it lacks a uniform
142
+ // shape, this is simpler and faster than using the crypto package to generate a hash signature.
143
+ const tsConfigCacheKey = JSON . stringify ( [ skipBabel , collectCoverage , isReferencedExternalFile ? config : undefined ] ) ;
144
+ if ( tsConfigCacheKey in tsConfigCache ) {
145
+ return tsConfigCache [ tsConfigCacheKey ] ;
146
+ }
147
+
138
148
if ( isReferencedExternalFile ) {
139
149
const configFileName = config ;
140
150
const configPath = path . resolve ( config ) ;
@@ -170,14 +180,15 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
170
180
// to their string properties, and convert the result accordingly afterwards.
171
181
// In case of an external file, reading the config file already converted it as well, and
172
182
// an additional attempt would lead to errors.
183
+ let result ;
173
184
if ( isReferencedExternalFile ) {
174
185
config . jsx = config . jsx || tsc . JsxEmit . React ;
175
186
config . module = config . module || tsc . ModuleKind . CommonJS ;
176
187
if ( config . allowSyntheticDefaultImports && ! skipBabel ) {
177
188
// compile ts to es2015 and transform with babel afterwards
178
189
config . module = tsc . ModuleKind . ES2015 ;
179
190
}
180
- return config ;
191
+ result = config ;
181
192
} else {
182
193
config . jsx = config . jsx || 'react' ;
183
194
config . module = config . module || 'commmonjs' ;
@@ -190,6 +201,10 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
190
201
const formattedErrors = formatTscParserErrors ( converted . errors ) ;
191
202
throw new Error ( `Some errors occurred while attempting to convert ${ JSON . stringify ( config ) } : ${ formattedErrors } ` ) ;
192
203
}
193
- return converted . options ;
204
+ result = converted . options ;
194
205
}
206
+
207
+ // cache result for future requests
208
+ tsConfigCache [ tsConfigCacheKey ] = result ;
209
+ return result ;
195
210
}
0 commit comments