@@ -487,6 +487,7 @@ class ConsumeSharedPlugin {
487
487
) => this . createConsumeSharedModule ( compilation , ctx , req , cfg ) ;
488
488
489
489
return promise . then ( async ( ) => {
490
+ const debugAlias = process . env [ 'MF_DEBUG_ALIAS' ] === '1' ;
490
491
if (
491
492
dependencies [ 0 ] instanceof ConsumeSharedFallbackDependency ||
492
493
dependencies [ 0 ] instanceof ProvideForSharedDependency
@@ -510,6 +511,7 @@ class ConsumeSharedPlugin {
510
511
let aliasAfterNodeModules : string | undefined ;
511
512
const aliasShareKeyCandidates : string [ ] = [ ] ;
512
513
if ( request && ! RELATIVE_OR_ABSOLUTE_PATH_REGEX . test ( request ) ) {
514
+ if ( debugAlias ) console . log ( '[alias][consume] bare:' , request ) ;
513
515
try {
514
516
const resolveContext = {
515
517
fileDependencies : new LazySet < string > ( ) ,
@@ -542,13 +544,26 @@ class ConsumeSharedPlugin {
542
544
) ;
543
545
if ( err || ! resPath ) return res ( undefined ) ;
544
546
const resolvedPath = resPath as string ;
547
+ if ( debugAlias )
548
+ console . log (
549
+ '[alias][consume] resolved ->' ,
550
+ resolvedPath ,
551
+ ) ;
545
552
const nm = extractPathAfterNodeModules ( resolvedPath ) ;
546
553
if ( nm ) {
547
554
aliasAfterNodeModules = nm ;
548
555
const nmDir = nm . replace ( / \/ ( i n d e x \. [ ^ / ] + ) $ / , '' ) ;
549
556
if ( nmDir && nmDir !== nm )
550
557
aliasShareKeyCandidates . push ( nmDir ) ;
558
+ const nmNoExt = nm . replace ( / \. [ ^ / ] + $ / , '' ) ;
559
+ if ( nmNoExt && nmNoExt !== nm )
560
+ aliasShareKeyCandidates . push ( nmNoExt ) ;
551
561
aliasShareKeyCandidates . push ( nm ) ;
562
+ if ( debugAlias )
563
+ console . log (
564
+ '[alias][consume] nm candidates:' ,
565
+ [ nmDir , nmNoExt , nm ] . filter ( Boolean ) ,
566
+ ) ;
552
567
}
553
568
try {
554
569
if (
@@ -572,7 +587,20 @@ class ConsumeSharedPlugin {
572
587
) ;
573
588
if ( pkgKeyDir && pkgKeyDir !== pkgKey )
574
589
aliasShareKeyCandidates . push ( pkgKeyDir ) ;
590
+ const pkgKeyNoExt = pkgKey . replace (
591
+ / \. [ ^ / ] + $ / ,
592
+ '' ,
593
+ ) ;
594
+ if ( pkgKeyNoExt && pkgKeyNoExt !== pkgKey )
595
+ aliasShareKeyCandidates . push ( pkgKeyNoExt ) ;
575
596
aliasShareKeyCandidates . push ( pkgKey ) ;
597
+ if ( debugAlias )
598
+ console . log (
599
+ '[alias][consume] pkg candidates:' ,
600
+ [ pkgKeyDir , pkgKeyNoExt , pkgKey ] . filter (
601
+ Boolean ,
602
+ ) ,
603
+ ) ;
576
604
}
577
605
} catch { }
578
606
res ( resolvedPath ) ;
@@ -613,9 +641,40 @@ class ConsumeSharedPlugin {
613
641
createLookupKeyForSharing ( cand , undefined ) ,
614
642
) ;
615
643
if ( aliasMatch ) {
644
+ if ( debugAlias )
645
+ console . log (
646
+ '[alias][consume] direct candidate match:' ,
647
+ cand ,
648
+ ) ;
616
649
return createConsume ( context , request , aliasMatch ) ;
617
650
}
618
651
}
652
+ // Fallback: scan unresolved keys for prefix matches when allowed
653
+ for ( const [ lookupKey , opts ] of unresolvedConsumes ) {
654
+ const keyNoLayer = lookupKey . replace ( / ^ \( [ ^ ) ] * \) / , '' ) ;
655
+ if ( ! opts . allowNodeModulesSuffixMatch ) continue ;
656
+ for ( const cand of aliasShareKeyCandidates ) {
657
+ const candTrim = cand
658
+ . replace ( / \/ ( i n d e x \. [ ^ / ] + ) $ / , '' )
659
+ . replace ( / \. [ ^ / ] + $ / , '' ) ;
660
+ const keyTrim = keyNoLayer
661
+ . replace ( / \/ ( i n d e x \. [ ^ / ] + ) $ / , '' )
662
+ . replace ( / \. [ ^ / ] + $ / , '' ) ;
663
+ if (
664
+ candTrim . startsWith ( keyTrim ) ||
665
+ keyTrim . startsWith ( candTrim )
666
+ ) {
667
+ if ( debugAlias )
668
+ console . log (
669
+ '[alias][consume] fallback prefix match:' ,
670
+ keyNoLayer ,
671
+ '<->' ,
672
+ candTrim ,
673
+ ) ;
674
+ return createConsume ( context , request , opts ) ;
675
+ }
676
+ }
677
+ }
619
678
}
620
679
621
680
// 2b) Try unresolved match with path after node_modules (if allowed) from reconstructed relative
@@ -712,6 +771,13 @@ class ConsumeSharedPlugin {
712
771
) {
713
772
continue ;
714
773
}
774
+ if ( debugAlias )
775
+ console . log (
776
+ '[alias][consume] prefix nm match:' ,
777
+ lookup ,
778
+ '+' ,
779
+ remainder ,
780
+ ) ;
715
781
return createConsume ( context , afterNodeModules , {
716
782
...options ,
717
783
import : options . import
@@ -745,6 +811,13 @@ class ConsumeSharedPlugin {
745
811
) {
746
812
continue ;
747
813
}
814
+ if ( debugAlias )
815
+ console . log (
816
+ '[alias][consume] prefix alias match:' ,
817
+ lookup ,
818
+ '+' ,
819
+ remainder ,
820
+ ) ;
748
821
return createConsume ( context , request , {
749
822
...options ,
750
823
import : options . import
0 commit comments