@@ -45,6 +45,7 @@ import {
45
45
addSingletonFilterWarning ,
46
46
testRequestFilters ,
47
47
createLookupKeyForSharing ,
48
+ extractPathAfterNodeModules ,
48
49
} from './utils' ;
49
50
50
51
const ModuleNotFoundError = require (
@@ -105,6 +106,7 @@ class ConsumeSharedPlugin {
105
106
request : key ,
106
107
include : undefined ,
107
108
exclude : undefined ,
109
+ nodeModulesReconstructedLookup : undefined ,
108
110
}
109
111
: // key is a request/key
110
112
// item is a version
@@ -123,6 +125,7 @@ class ConsumeSharedPlugin {
123
125
request : key ,
124
126
include : undefined ,
125
127
exclude : undefined ,
128
+ nodeModulesReconstructedLookup : undefined ,
126
129
} ;
127
130
return result ;
128
131
} ,
@@ -149,6 +152,7 @@ class ConsumeSharedPlugin {
149
152
issuerLayer : item . issuerLayer ? item . issuerLayer : undefined ,
150
153
layer : item . layer ? item . layer : undefined ,
151
154
request,
155
+ nodeModulesReconstructedLookup : item . nodeModulesReconstructedLookup ,
152
156
} as ConsumeOptions ;
153
157
} ,
154
158
) ;
@@ -496,6 +500,69 @@ class ConsumeSharedPlugin {
496
500
) ;
497
501
}
498
502
503
+ // Then try relative path handling and node_modules paths
504
+ let reconstructed : string | null = null ;
505
+ let modulePathAfterNodeModules : string | null = null ;
506
+
507
+ if (
508
+ request &&
509
+ ! path . isAbsolute ( request ) &&
510
+ RELATIVE_OR_ABSOLUTE_PATH_REGEX . test ( request )
511
+ ) {
512
+ reconstructed = path . join ( context , request ) ;
513
+ modulePathAfterNodeModules =
514
+ extractPathAfterNodeModules ( reconstructed ) ;
515
+
516
+ // Try to match with module path after node_modules
517
+ if ( modulePathAfterNodeModules ) {
518
+ const moduleMatch =
519
+ unresolvedConsumes . get (
520
+ createLookupKeyForSharing (
521
+ modulePathAfterNodeModules ,
522
+ contextInfo . issuerLayer ,
523
+ ) ,
524
+ ) ||
525
+ unresolvedConsumes . get (
526
+ createLookupKeyForSharing (
527
+ modulePathAfterNodeModules ,
528
+ undefined ,
529
+ ) ,
530
+ ) ;
531
+
532
+ if (
533
+ moduleMatch !== undefined &&
534
+ moduleMatch . nodeModulesReconstructedLookup
535
+ ) {
536
+ return boundCreateConsumeSharedModule (
537
+ compilation ,
538
+ context ,
539
+ modulePathAfterNodeModules ,
540
+ moduleMatch ,
541
+ ) ;
542
+ }
543
+ }
544
+
545
+ // Try to match with the full reconstructed path
546
+ const reconstructedMatch =
547
+ unresolvedConsumes . get (
548
+ createLookupKeyForSharing (
549
+ reconstructed ,
550
+ contextInfo . issuerLayer ,
551
+ ) ,
552
+ ) ||
553
+ unresolvedConsumes . get (
554
+ createLookupKeyForSharing ( reconstructed , undefined ) ,
555
+ ) ;
556
+
557
+ if ( reconstructedMatch !== undefined ) {
558
+ return boundCreateConsumeSharedModule (
559
+ compilation ,
560
+ context ,
561
+ reconstructed ,
562
+ reconstructedMatch ,
563
+ ) ;
564
+ }
565
+ }
499
566
// Check for prefixed consumes with original request
500
567
for ( const [ prefix , options ] of prefixedConsumes ) {
501
568
const lookup = options . request || prefix ;
@@ -538,6 +605,55 @@ class ConsumeSharedPlugin {
538
605
}
539
606
}
540
607
608
+ // Also check prefixed consumes with modulePathAfterNodeModules
609
+ if ( modulePathAfterNodeModules ) {
610
+ for ( const [ prefix , options ] of prefixedConsumes ) {
611
+ if ( ! options . nodeModulesReconstructedLookup ) {
612
+ continue ;
613
+ }
614
+ // Refined issuerLayer matching logic for reconstructed path
615
+ if ( options . issuerLayer ) {
616
+ if ( ! contextInfo . issuerLayer ) {
617
+ continue ; // Option is layered, request is not: skip
618
+ }
619
+ if ( contextInfo . issuerLayer !== options . issuerLayer ) {
620
+ continue ; // Both are layered but do not match: skip
621
+ }
622
+ }
623
+ // If contextInfo.issuerLayer exists but options.issuerLayer does not, allow (non-layered option matches layered request)
624
+ const lookup = options . request || prefix ;
625
+ if ( modulePathAfterNodeModules . startsWith ( lookup ) ) {
626
+ const remainder = modulePathAfterNodeModules . slice (
627
+ lookup . length ,
628
+ ) ;
629
+
630
+ if (
631
+ ! testRequestFilters (
632
+ remainder ,
633
+ options . include ?. request ,
634
+ options . exclude ?. request ,
635
+ )
636
+ ) {
637
+ continue ;
638
+ }
639
+
640
+ return boundCreateConsumeSharedModule (
641
+ compilation ,
642
+ context ,
643
+ modulePathAfterNodeModules ,
644
+ {
645
+ ...options ,
646
+ import : options . import
647
+ ? options . import + remainder
648
+ : undefined ,
649
+ shareKey : options . shareKey + remainder ,
650
+ layer : options . layer ,
651
+ } ,
652
+ ) ;
653
+ }
654
+ }
655
+ }
656
+
541
657
return ;
542
658
} ) ;
543
659
} ,
0 commit comments