@@ -8,6 +8,7 @@ import { makeAfterCompile } from './after-compile';
88import { getCompiler , getCompilerOptions } from './compilerSetup' ;
99import { getConfigFile , getConfigParseResult } from './config' ;
1010import { dtsDtsxOrDtsDtsxMapRegex , EOL , tsTsxRegex } from './constants' ;
11+ import { getTSInstanceFromCache , setTSInstanceInCache } from './instance-cache' ;
1112import {
1213 FilePathKey ,
1314 LoaderOptions ,
@@ -33,22 +34,8 @@ import {
3334} from './utils' ;
3435import { makeWatchRun } from './watch-run' ;
3536
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 > > ( ) ;
4037const instancesBySolutionBuilderConfigs = new Map < FilePathKey , TSInstance > ( ) ;
4138
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-
5239/**
5340 * The loader is executed once for each file seen by webpack. However, we need to keep
5441 * a persistent instance of TypeScript that contains all of the files in the program
@@ -60,13 +47,10 @@ export function getTypeScriptInstance(
6047 loaderOptions : LoaderOptions ,
6148 loader : webpack . loader . LoaderContext
6249) : { 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+ ) ;
7054 if ( existing ) {
7155 if ( ! existing . initialSetupPending ) {
7256 ensureProgram ( existing ) ;
@@ -160,7 +144,7 @@ function successfulTypeScriptInstance(
160144 const existing = getExistingSolutionBuilderHost ( configFileKey ) ;
161145 if ( existing ) {
162146 // 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 ) ;
164148 return { instance : existing } ;
165149 }
166150 }
@@ -246,7 +230,7 @@ function successfulTypeScriptInstance(
246230 filePathKeyMapper,
247231 } ;
248232
249- addTSInstanceToCache (
233+ setTSInstanceInCache (
250234 loader . _compiler ,
251235 loaderOptions . instance ,
252236 transpileInstance
@@ -303,7 +287,7 @@ function successfulTypeScriptInstance(
303287 filePathKeyMapper,
304288 } ;
305289
306- addTSInstanceToCache ( loader . _compiler , loaderOptions . instance , instance ) ;
290+ setTSInstanceInCache ( loader . _compiler , loaderOptions . instance , instance ) ;
307291 return { instance } ;
308292}
309293
0 commit comments