@@ -246,8 +246,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
246246 let cached_path = self . cache . value ( path) ;
247247 let cached_path = self . require ( & cached_path, specifier, ctx) ?;
248248 let path = self . load_realpath ( & cached_path) ?;
249- // enhanced-resolve: restrictions
250- self . check_restrictions ( & path) ?;
249+
251250 let package_json = cached_path. find_package_json ( & self . cache . fs , & self . options , ctx) ?;
252251 if let Some ( package_json) = & package_json {
253252 // path must be inside the package.
@@ -623,7 +622,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
623622 }
624623 }
625624
626- fn check_restrictions ( & self , path : & Path ) -> Result < ( ) , ResolveError > {
625+ fn check_restrictions ( & self , path : & Path ) -> bool {
627626 // https://github.com/webpack/enhanced-resolve/blob/a998c7d218b7a9ec2461fc4fddd1ad5dd7687485/lib/RestrictionsPlugin.js#L19-L24
628627 fn is_inside ( path : & Path , parent : & Path ) -> bool {
629628 if !path. starts_with ( parent) {
@@ -638,18 +637,17 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
638637 match restriction {
639638 Restriction :: Path ( restricted_path) => {
640639 if !is_inside ( path, restricted_path) {
641- return Err ( ResolveError :: Restriction (
642- path. to_path_buf ( ) ,
643- restricted_path. clone ( ) ,
644- ) ) ;
640+ return false ;
645641 }
646642 }
647- Restriction :: RegExp ( _) => {
648- return Err ( ResolveError :: Unimplemented ( "Restriction with regex" ) )
643+ Restriction :: Fn ( f) => {
644+ if !f ( path) {
645+ return false ;
646+ }
649647 }
650648 }
651649 }
652- Ok ( ( ) )
650+ true
653651 }
654652
655653 fn load_index ( & self , cached_path : & CachedPath , ctx : & mut Ctx ) -> ResolveResult {
@@ -658,7 +656,9 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
658656 let cached_path = self . cache . value ( & main_path) ;
659657 if self . options . enforce_extension . is_disabled ( ) {
660658 if let Some ( path) = self . load_alias_or_file ( & cached_path, ctx) ? {
661- return Ok ( Some ( path) ) ;
659+ if self . check_restrictions ( path. path ( ) ) {
660+ return Ok ( Some ( path) ) ;
661+ }
662662 }
663663 }
664664 // 1. If X/index.js is a file, load X/index.js as JavaScript text. STOP
@@ -690,7 +690,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
690690 {
691691 return Ok ( Some ( path) ) ;
692692 }
693- if cached_path. is_file ( & self . cache . fs , ctx) {
693+ if cached_path. is_file ( & self . cache . fs , ctx) && self . check_restrictions ( cached_path . path ( ) ) {
694694 return Ok ( Some ( cached_path. clone ( ) ) ) ;
695695 }
696696 Ok ( None )
@@ -988,7 +988,11 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
988988 // Complete when resolving to self `{"./a.js": "./a.js"}`
989989 if new_specifier. strip_prefix ( "./" ) . filter ( |s| path. ends_with ( Path :: new ( s) ) ) . is_some ( ) {
990990 return if cached_path. is_file ( & self . cache . fs , ctx) {
991- Ok ( Some ( cached_path. clone ( ) ) )
991+ if self . check_restrictions ( cached_path. path ( ) ) {
992+ Ok ( Some ( cached_path. clone ( ) ) )
993+ } else {
994+ Ok ( None )
995+ }
992996 } else {
993997 Err ( ResolveError :: NotFound ( new_specifier. to_string ( ) ) )
994998 } ;
@@ -1145,6 +1149,8 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
11451149 if !cached_path. is_file ( & self . cache . fs , ctx) {
11461150 ctx. with_fully_specified ( false ) ;
11471151 return Ok ( None ) ;
1152+ } else if !self . check_restrictions ( cached_path. path ( ) ) {
1153+ return Ok ( None ) ;
11481154 }
11491155 // Create a meaningful error message.
11501156 let dir = path. parent ( ) . unwrap ( ) . to_path_buf ( ) ;
@@ -1348,8 +1354,10 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
13481354 // 1. Return the URL resolution of main in packageURL.
13491355 let path = cached_path. path ( ) . normalize_with ( main_field) ;
13501356 let cached_path = self . cache . value ( & path) ;
1351- if cached_path. is_file ( & self . cache . fs , ctx) {
1352- return Ok ( Some ( cached_path) ) ;
1357+ if cached_path. is_file ( & self . cache . fs , ctx)
1358+ && self . check_restrictions ( cached_path. path ( ) )
1359+ {
1360+ return Ok ( Some ( cached_path. clone ( ) ) ) ;
13531361 }
13541362 }
13551363 }
0 commit comments