@@ -8,6 +8,7 @@ import { makeAfterCompile } from './after-compile';
8
8
import { getCompiler , getCompilerOptions } from './compilerSetup' ;
9
9
import { getConfigFile , getConfigParseResult } from './config' ;
10
10
import { dtsDtsxOrDtsDtsxMapRegex , EOL , tsTsxRegex } from './constants' ;
11
+ import { getTSInstanceFromCache , setTSInstanceInCache } from './instance-cache' ;
11
12
import {
12
13
FilePathKey ,
13
14
LoaderOptions ,
@@ -33,22 +34,8 @@ import {
33
34
} from './utils' ;
34
35
import { makeWatchRun } from './watch-run' ;
35
36
36
- // Each TypeScript instance is based on the webpack instance (key of the WeakMap)
37
- // and also the name that was generated or passed via the options (string key of the
38
- // internal Map)
39
- const instanceCache = new WeakMap < webpack . Compiler , Map < string , TSInstance > > ( ) ;
40
37
const instancesBySolutionBuilderConfigs = new Map < FilePathKey , TSInstance > ( ) ;
41
38
42
- function addTSInstanceToCache (
43
- key : webpack . Compiler ,
44
- instanceName : string ,
45
- instance : TSInstance
46
- ) {
47
- const instances = instanceCache . get ( key ) ?? new Map < string , TSInstance > ( ) ;
48
- instances . set ( instanceName , instance ) ;
49
- instanceCache . set ( key , instances ) ;
50
- }
51
-
52
39
/**
53
40
* The loader is executed once for each file seen by webpack. However, we need to keep
54
41
* a persistent instance of TypeScript that contains all of the files in the program
@@ -60,13 +47,10 @@ export function getTypeScriptInstance(
60
47
loaderOptions : LoaderOptions ,
61
48
loader : webpack . loader . LoaderContext
62
49
) : { instance ?: TSInstance ; error ?: WebpackError } {
63
- let instances = instanceCache . get ( loader . _compiler ) ;
64
- if ( ! instances ) {
65
- instances = new Map ( ) ;
66
- instanceCache . set ( loader . _compiler , instances ) ;
67
- }
68
-
69
- const existing = instances . get ( loaderOptions . instance ) ;
50
+ const existing = getTSInstanceFromCache (
51
+ loader . _compiler ,
52
+ loaderOptions . instance
53
+ ) ;
70
54
if ( existing ) {
71
55
if ( ! existing . initialSetupPending ) {
72
56
ensureProgram ( existing ) ;
@@ -160,7 +144,7 @@ function successfulTypeScriptInstance(
160
144
const existing = getExistingSolutionBuilderHost ( configFileKey ) ;
161
145
if ( existing ) {
162
146
// Reuse the instance if config file for project references is shared.
163
- addTSInstanceToCache ( loader . _compiler , loaderOptions . instance , existing ) ;
147
+ setTSInstanceInCache ( loader . _compiler , loaderOptions . instance , existing ) ;
164
148
return { instance : existing } ;
165
149
}
166
150
}
@@ -246,7 +230,7 @@ function successfulTypeScriptInstance(
246
230
filePathKeyMapper,
247
231
} ;
248
232
249
- addTSInstanceToCache (
233
+ setTSInstanceInCache (
250
234
loader . _compiler ,
251
235
loaderOptions . instance ,
252
236
transpileInstance
@@ -303,7 +287,7 @@ function successfulTypeScriptInstance(
303
287
filePathKeyMapper,
304
288
} ;
305
289
306
- addTSInstanceToCache ( loader . _compiler , loaderOptions . instance , instance ) ;
290
+ setTSInstanceInCache ( loader . _compiler , loaderOptions . instance , instance ) ;
307
291
return { instance } ;
308
292
}
309
293
0 commit comments