@@ -386,6 +386,20 @@ function hash(...valueList : any[]) : string {
386
386
}
387
387
388
388
389
+ /**
390
+ * @internal
391
+ */
392
+ class Loading {
393
+
394
+ promise : Promise < Module > ;
395
+
396
+ constructor ( promise : Promise < Module > ) {
397
+
398
+ this . promise = promise ;
399
+ }
400
+ }
401
+
402
+
389
403
/**
390
404
* Simple cache helper
391
405
* preventCache usage: non-fatal error
@@ -481,9 +495,7 @@ function parseDeps(fileAst : t.File) : string[] {
481
495
async function loadDeps ( filename : string , deps : string [ ] , options : Options ) {
482
496
483
497
const { pathHandlers : { resolve } } = options ;
484
-
485
- for ( const dep of deps )
486
- await loadModule ( resolve ( filename , dep ) , options ) ;
498
+ await Promise . all ( deps . map ( dep => loadModule ( resolve ( filename , dep ) , options ) ) )
487
499
}
488
500
489
501
@@ -834,28 +846,46 @@ export async function loadModule(path : string, options_ : Options = throwNotDef
834
846
...options_
835
847
} ;
836
848
837
- if ( path in moduleCache )
838
- return moduleCache [ path ] ;
839
-
840
- if ( loadModule ) {
849
+ if ( path in moduleCache ) {
841
850
842
- const module = await loadModule ( path , options ) ;
843
- if ( module !== undefined )
844
- return moduleCache [ path ] = module ;
851
+ if ( moduleCache [ path ] instanceof Loading )
852
+ return await moduleCache [ path ] . promise ;
853
+ else
854
+ return moduleCache [ path ] ;
845
855
}
846
856
847
857
848
- const moduleHandlers = { ...defaultModuleHandlers , ...additionalModuleHandlers } ;
858
+ moduleCache [ path ] = new Loading (
859
+
860
+ ( async ( ) => {
861
+
862
+ if ( loadModule ) {
863
+
864
+ const module = await loadModule ( path , options ) ;
865
+ if ( module !== undefined )
866
+ return moduleCache [ path ] = module ;
867
+ }
868
+
869
+ const moduleHandlers = { ...defaultModuleHandlers , ...additionalModuleHandlers } ;
870
+
871
+ const res = await getFile ( path ) ;
872
+
873
+ const file = typeof res === 'object' ? res : { content : res , extname : pathHandlers . extname ( path ) } ;
874
+
875
+ if ( ! ( file . extname in moduleHandlers ) )
876
+ throw new TypeError ( `Unable to handle ${ file . extname } files (${ path } ), see additionalModuleHandlers` ) ;
849
877
850
- const res = await getFile ( path ) ;
878
+ if ( typeof file . content !== 'string' )
879
+ throw new TypeError ( `Invalid module content (${ path } ): ${ file . content } ` ) ;
851
880
852
- const file = typeof res === 'object' ? res : { content : res , extname : pathHandlers . extname ( path ) } ;
853
881
854
- if ( ! ( file . extname in moduleHandlers ) )
855
- throw new TypeError ( `Unable to handle ${ file . extname } files (${ path } ), see additionalModuleHandlers` ) ;
882
+ const module = await moduleHandlers [ file . extname ] ( file . content , path , options ) ;
883
+
884
+ return moduleCache [ path ] = module ;
885
+
886
+ } ) ( )
856
887
857
- if ( typeof file . content !== 'string' )
858
- throw new TypeError ( `Invalid module content (${ path } ): ${ file . content } ` ) ;
888
+ ) ;
859
889
860
- return moduleCache [ path ] = await moduleHandlers [ file . extname ] ( file . content , path , options ) ;
890
+ return await moduleCache [ path ] . promise ;
861
891
}
0 commit comments