@@ -33,9 +33,22 @@ import {
33
33
} from './utils' ;
34
34
import { makeWatchRun } from './watch-run' ;
35
35
36
- const instances = new Map < string , TSInstance > ( ) ;
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 > > ( ) ;
37
40
const instancesBySolutionBuilderConfigs = new Map < FilePathKey , TSInstance > ( ) ;
38
41
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
+
39
52
/**
40
53
* The loader is executed once for each file seen by webpack. However, we need to keep
41
54
* a persistent instance of TypeScript that contains all of the files in the program
@@ -47,6 +60,12 @@ export function getTypeScriptInstance(
47
60
loaderOptions : LoaderOptions ,
48
61
loader : webpack . loader . LoaderContext
49
62
) : { 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
+
50
69
const existing = instances . get ( loaderOptions . instance ) ;
51
70
if ( existing ) {
52
71
if ( ! existing . initialSetupPending ) {
@@ -141,7 +160,7 @@ function successfulTypeScriptInstance(
141
160
const existing = getExistingSolutionBuilderHost ( configFileKey ) ;
142
161
if ( existing ) {
143
162
// Reuse the instance if config file for project references is shared.
144
- instances . set ( loaderOptions . instance , existing ) ;
163
+ addTSInstanceToCache ( loader . _compiler , loaderOptions . instance , existing ) ;
145
164
return { instance : existing } ;
146
165
}
147
166
}
@@ -226,7 +245,12 @@ function successfulTypeScriptInstance(
226
245
log,
227
246
filePathKeyMapper,
228
247
} ;
229
- instances . set ( loaderOptions . instance , transpileInstance ) ;
248
+
249
+ addTSInstanceToCache (
250
+ loader . _compiler ,
251
+ loaderOptions . instance ,
252
+ transpileInstance
253
+ ) ;
230
254
return { instance : transpileInstance } ;
231
255
}
232
256
@@ -278,7 +302,8 @@ function successfulTypeScriptInstance(
278
302
log,
279
303
filePathKeyMapper,
280
304
} ;
281
- instances . set ( loaderOptions . instance , instance ) ;
305
+
306
+ addTSInstanceToCache ( loader . _compiler , loaderOptions . instance , instance ) ;
282
307
return { instance } ;
283
308
}
284
309
0 commit comments