@@ -341,11 +341,11 @@ export default class Resolver {
341
341
resolveModule (
342
342
from : string ,
343
343
moduleName : string ,
344
- options ? : ResolveModuleConfig ,
344
+ options : ResolveModuleConfig ,
345
345
) : string {
346
346
const dirname = path . dirname ( from ) ;
347
347
const module =
348
- this . resolveStubModuleName ( from , moduleName ) ||
348
+ this . resolveStubModuleName ( from , moduleName , options ) ||
349
349
this . resolveModuleFromDirIfExists ( dirname , moduleName , options ) ;
350
350
if ( module ) return module ;
351
351
@@ -362,7 +362,7 @@ export default class Resolver {
362
362
) : Promise < string > {
363
363
const dirname = path . dirname ( from ) ;
364
364
const module =
365
- ( await this . resolveStubModuleNameAsync ( from , moduleName ) ) ||
365
+ ( await this . resolveStubModuleNameAsync ( from , moduleName , options ) ) ||
366
366
( await this . resolveModuleFromDirIfExistsAsync (
367
367
dirname ,
368
368
moduleName ,
@@ -482,25 +482,37 @@ export default class Resolver {
482
482
) ;
483
483
}
484
484
485
- getMockModule ( from : string , name : string ) : string | null {
485
+ getMockModule (
486
+ from : string ,
487
+ name : string ,
488
+ options : Pick < ResolveModuleConfig , 'conditions' > ,
489
+ ) : string | null {
486
490
const mock = this . _moduleMap . getMockModule ( name ) ;
487
491
if ( mock ) {
488
492
return mock ;
489
493
} else {
490
- const moduleName = this . resolveStubModuleName ( from , name ) ;
494
+ const moduleName = this . resolveStubModuleName ( from , name , options ) ;
491
495
if ( moduleName ) {
492
496
return this . getModule ( moduleName ) || moduleName ;
493
497
}
494
498
}
495
499
return null ;
496
500
}
497
501
498
- async getMockModuleAsync ( from : string , name : string ) : Promise < string | null > {
502
+ async getMockModuleAsync (
503
+ from : string ,
504
+ name : string ,
505
+ options : Pick < ResolveModuleConfig , 'conditions' > ,
506
+ ) : Promise < string | null > {
499
507
const mock = this . _moduleMap . getMockModule ( name ) ;
500
508
if ( mock ) {
501
509
return mock ;
502
510
} else {
503
- const moduleName = await this . resolveStubModuleNameAsync ( from , name ) ;
511
+ const moduleName = await this . resolveStubModuleNameAsync (
512
+ from ,
513
+ name ,
514
+ options ,
515
+ ) ;
504
516
if ( moduleName ) {
505
517
return this . getModule ( moduleName ) || moduleName ;
506
518
}
@@ -536,7 +548,7 @@ export default class Resolver {
536
548
virtualMocks : Map < string , boolean > ,
537
549
from : string ,
538
550
moduleName = '' ,
539
- options ? : ResolveModuleConfig ,
551
+ options : ResolveModuleConfig ,
540
552
) : string {
541
553
const stringifiedOptions = options ? JSON . stringify ( options ) : '' ;
542
554
const key = from + path . delimiter + moduleName + stringifiedOptions ;
@@ -552,7 +564,7 @@ export default class Resolver {
552
564
moduleName ,
553
565
options ,
554
566
) ;
555
- const mockPath = this . _getMockPath ( from , moduleName ) ;
567
+ const mockPath = this . _getMockPath ( from , moduleName , options ) ;
556
568
557
569
const sep = path . delimiter ;
558
570
const id =
@@ -570,7 +582,7 @@ export default class Resolver {
570
582
virtualMocks : Map < string , boolean > ,
571
583
from : string ,
572
584
moduleName = '' ,
573
- options ? : ResolveModuleConfig ,
585
+ options : ResolveModuleConfig ,
574
586
) : Promise < string > {
575
587
const stringifiedOptions = options ? JSON . stringify ( options ) : '' ;
576
588
const key = from + path . delimiter + moduleName + stringifiedOptions ;
@@ -589,7 +601,7 @@ export default class Resolver {
589
601
moduleName ,
590
602
options ,
591
603
) ;
592
- const mockPath = await this . _getMockPathAsync ( from , moduleName ) ;
604
+ const mockPath = await this . _getMockPathAsync ( from , moduleName , options ) ;
593
605
594
606
const sep = path . delimiter ;
595
607
const id =
@@ -611,15 +623,15 @@ export default class Resolver {
611
623
virtualMocks : Map < string , boolean > ,
612
624
from : string ,
613
625
moduleName : string ,
614
- options ? : ResolveModuleConfig ,
626
+ options : ResolveModuleConfig ,
615
627
) : string | null {
616
628
if ( this . isCoreModule ( moduleName ) ) {
617
629
return moduleName ;
618
630
}
619
631
if ( moduleName . startsWith ( 'data:' ) ) {
620
632
return moduleName ;
621
633
}
622
- return this . _isModuleResolved ( from , moduleName )
634
+ return this . _isModuleResolved ( from , moduleName , options )
623
635
? this . getModule ( moduleName )
624
636
: this . _getVirtualMockPath ( virtualMocks , from , moduleName , options ) ;
625
637
}
@@ -628,7 +640,7 @@ export default class Resolver {
628
640
virtualMocks : Map < string , boolean > ,
629
641
from : string ,
630
642
moduleName : string ,
631
- options ? : ResolveModuleConfig ,
643
+ options : ResolveModuleConfig ,
632
644
) : Promise < string | null > {
633
645
if ( this . isCoreModule ( moduleName ) ) {
634
646
return moduleName ;
@@ -639,32 +651,38 @@ export default class Resolver {
639
651
const isModuleResolved = await this . _isModuleResolvedAsync (
640
652
from ,
641
653
moduleName ,
654
+ options ,
642
655
) ;
643
656
return isModuleResolved
644
657
? this . getModule ( moduleName )
645
658
: this . _getVirtualMockPathAsync ( virtualMocks , from , moduleName , options ) ;
646
659
}
647
660
648
- private _getMockPath ( from : string , moduleName : string ) : string | null {
661
+ private _getMockPath (
662
+ from : string ,
663
+ moduleName : string ,
664
+ options : Pick < ResolveModuleConfig , 'conditions' > ,
665
+ ) : string | null {
649
666
return this . isCoreModule ( moduleName )
650
667
? null
651
- : this . getMockModule ( from , moduleName ) ;
668
+ : this . getMockModule ( from , moduleName , options ) ;
652
669
}
653
670
654
671
private async _getMockPathAsync (
655
672
from : string ,
656
673
moduleName : string ,
674
+ options : Pick < ResolveModuleConfig , 'conditions' > ,
657
675
) : Promise < string | null > {
658
676
return this . isCoreModule ( moduleName )
659
677
? null
660
- : this . getMockModuleAsync ( from , moduleName ) ;
678
+ : this . getMockModuleAsync ( from , moduleName , options ) ;
661
679
}
662
680
663
681
private _getVirtualMockPath (
664
682
virtualMocks : Map < string , boolean > ,
665
683
from : string ,
666
684
moduleName : string ,
667
- options ? : ResolveModuleConfig ,
685
+ options : ResolveModuleConfig ,
668
686
) : string {
669
687
const virtualMockPath = this . getModulePath ( from , moduleName ) ;
670
688
return virtualMocks . get ( virtualMockPath )
@@ -688,23 +706,33 @@ export default class Resolver {
688
706
: from ;
689
707
}
690
708
691
- private _isModuleResolved ( from : string , moduleName : string ) : boolean {
709
+ private _isModuleResolved (
710
+ from : string ,
711
+ moduleName : string ,
712
+ options : Pick < ResolveModuleConfig , 'conditions' > ,
713
+ ) : boolean {
692
714
return ! ! (
693
- this . getModule ( moduleName ) || this . getMockModule ( from , moduleName )
715
+ this . getModule ( moduleName ) ||
716
+ this . getMockModule ( from , moduleName , options )
694
717
) ;
695
718
}
696
719
697
720
private async _isModuleResolvedAsync (
698
721
from : string ,
699
722
moduleName : string ,
723
+ options : Pick < ResolveModuleConfig , 'conditions' > ,
700
724
) : Promise < boolean > {
701
725
return ! ! (
702
726
this . getModule ( moduleName ) ||
703
- ( await this . getMockModuleAsync ( from , moduleName ) )
727
+ ( await this . getMockModuleAsync ( from , moduleName , options ) )
704
728
) ;
705
729
}
706
730
707
- resolveStubModuleName ( from : string , moduleName : string ) : string | null {
731
+ resolveStubModuleName (
732
+ from : string ,
733
+ moduleName : string ,
734
+ options : Pick < ResolveModuleConfig , 'conditions' > ,
735
+ ) : string | null {
708
736
const dirname = path . dirname ( from ) ;
709
737
710
738
const { extensions, moduleDirectory, paths} = this . _prepareForResolution (
@@ -727,11 +755,11 @@ export default class Resolver {
727
755
let module : string | null = null ;
728
756
for ( const possibleModuleName of possibleModuleNames ) {
729
757
const updatedName = mapModuleName ( possibleModuleName ) ;
730
-
731
758
module =
732
759
this . getModule ( updatedName ) ||
733
760
Resolver . findNodeModule ( updatedName , {
734
761
basedir : dirname ,
762
+ conditions : options ?. conditions ,
735
763
extensions,
736
764
moduleDirectory,
737
765
paths,
@@ -763,6 +791,7 @@ export default class Resolver {
763
791
async resolveStubModuleNameAsync (
764
792
from : string ,
765
793
moduleName : string ,
794
+ options ?: Pick < ResolveModuleConfig , 'conditions' > ,
766
795
) : Promise < string | null > {
767
796
const dirname = path . dirname ( from ) ;
768
797
@@ -791,6 +820,7 @@ export default class Resolver {
791
820
this . getModule ( updatedName ) ||
792
821
( await Resolver . findNodeModuleAsync ( updatedName , {
793
822
basedir : dirname ,
823
+ conditions : options ?. conditions ,
794
824
extensions,
795
825
moduleDirectory,
796
826
paths,
0 commit comments