@@ -50,9 +50,9 @@ const nodePaths = process.env.NODE_PATH
5050class Resolver {
5151 private readonly _options : ResolverConfig ;
5252 private readonly _moduleMap : ModuleMap ;
53- private readonly _moduleIDCache : { [ key : string ] : string } ;
54- private readonly _moduleNameCache : { [ name : string ] : Config . Path } ;
55- private readonly _modulePathCache : { [ path : string ] : Array < Config . Path > } ;
53+ private readonly _moduleIDCache : Map < string , string > ;
54+ private readonly _moduleNameCache : Map < string , Config . Path > ;
55+ private readonly _modulePathCache : Map < string , Array < Config . Path > > ;
5656 private readonly _supportsNativePlatform : boolean ;
5757
5858 constructor ( moduleMap : ModuleMap , options : ResolverConfig ) {
@@ -73,9 +73,9 @@ class Resolver {
7373 ? options . platforms . includes ( NATIVE_PLATFORM )
7474 : false ;
7575 this . _moduleMap = moduleMap ;
76- this . _moduleIDCache = Object . create ( null ) ;
77- this . _moduleNameCache = Object . create ( null ) ;
78- this . _modulePathCache = Object . create ( null ) ;
76+ this . _moduleIDCache = new Map ( ) ;
77+ this . _moduleNameCache = new Map ( ) ;
78+ this . _modulePathCache = new Map ( ) ;
7979 }
8080
8181 static findNodeModule (
@@ -127,14 +127,16 @@ class Resolver {
127127
128128 // 1. If we have already resolved this module for this directory name,
129129 // return a value from the cache.
130- if ( this . _moduleNameCache [ key ] ) {
131- return this . _moduleNameCache [ key ] ;
130+ const cacheResult = this . _moduleNameCache . get ( key ) ;
131+ if ( cacheResult ) {
132+ return cacheResult ;
132133 }
133134
134135 // 2. Check if the module is a haste module.
135136 module = this . getModule ( moduleName ) ;
136137 if ( module ) {
137- return ( this . _moduleNameCache [ key ] = module ) ;
138+ this . _moduleNameCache . set ( key , module ) ;
139+ return module ;
138140 }
139141
140142 // 3. Check if the module is a node module and resolve it based on
@@ -161,7 +163,8 @@ class Resolver {
161163 module = resolveNodeModule ( moduleName ) ;
162164
163165 if ( module ) {
164- return ( this . _moduleNameCache [ key ] = module ) ;
166+ this . _moduleNameCache . set ( key , module ) ;
167+ return module ;
165168 }
166169 }
167170
@@ -177,8 +180,10 @@ class Resolver {
177180 ) ;
178181 // try resolving with custom resolver first to support extensions,
179182 // then fallback to require.resolve
180- return ( this . _moduleNameCache [ key ] =
181- resolveNodeModule ( module ) || require . resolve ( module ) ) ;
183+ const resolvedModule =
184+ resolveNodeModule ( module ) || require . resolve ( module ) ;
185+ this . _moduleNameCache . set ( key , resolvedModule ) ;
186+ return resolvedModule ;
182187 } catch ( ignoredError ) { }
183188 }
184189
@@ -250,16 +255,19 @@ class Resolver {
250255 }
251256
252257 getModulePaths ( from : Config . Path ) : Array < Config . Path > {
253- if ( ! this . _modulePathCache [ from ] ) {
254- const moduleDirectory = this . _options . moduleDirectories ;
255- const paths = nodeModulesPaths ( from , { moduleDirectory} ) ;
256- if ( paths [ paths . length - 1 ] === undefined ) {
257- // circumvent node-resolve bug that adds `undefined` as last item.
258- paths . pop ( ) ;
259- }
260- this . _modulePathCache [ from ] = paths ;
258+ const cachedModule = this . _modulePathCache . get ( from ) ;
259+ if ( cachedModule ) {
260+ return cachedModule ;
261+ }
262+
263+ const moduleDirectory = this . _options . moduleDirectories ;
264+ const paths = nodeModulesPaths ( from , { moduleDirectory} ) ;
265+ if ( paths [ paths . length - 1 ] === undefined ) {
266+ // circumvent node-resolve bug that adds `undefined` as last item.
267+ paths . pop ( ) ;
261268 }
262- return this . _modulePathCache [ from ] ;
269+ this . _modulePathCache . set ( from , paths ) ;
270+ return paths ;
263271 }
264272
265273 getModuleID (
@@ -270,8 +278,9 @@ class Resolver {
270278 const moduleName = _moduleName || '' ;
271279
272280 const key = from + path . delimiter + moduleName ;
273- if ( this . _moduleIDCache [ key ] ) {
274- return this . _moduleIDCache [ key ] ;
281+ const cachedModuleID = this . _moduleIDCache . get ( key ) ;
282+ if ( cachedModuleID ) {
283+ return cachedModuleID ;
275284 }
276285
277286 const moduleType = this . _getModuleType ( moduleName ) ;
@@ -285,7 +294,8 @@ class Resolver {
285294 ( absolutePath ? absolutePath + sep : '' ) +
286295 ( mockPath ? mockPath + sep : '' ) ;
287296
288- return ( this . _moduleIDCache [ key ] = id ) ;
297+ this . _moduleIDCache . set ( key , id ) ;
298+ return id ;
289299 }
290300
291301 private _getModuleType ( moduleName : string ) : 'node' | 'user' {
0 commit comments