Skip to content

Commit 5ede59d

Browse files
authored
fix: try browsers field and alias before resolving directory in node_modules (#349)
1 parent e1b0118 commit 5ede59d

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/lib.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,11 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
657657
Ok(None)
658658
}
659659

660-
fn load_alias_or_file(&self, cached_path: &CachedPath, ctx: &mut Ctx) -> ResolveResult {
660+
fn load_browser_field_or_alias(
661+
&self,
662+
cached_path: &CachedPath,
663+
ctx: &mut Ctx,
664+
) -> ResolveResult {
661665
if !self.options.alias_fields.is_empty() {
662666
if let Some((package_url, package_json)) =
663667
cached_path.find_package_json(&self.options, &self.cache, ctx)?
@@ -679,6 +683,13 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
679683
return Ok(Some(path));
680684
}
681685
}
686+
Ok(None)
687+
}
688+
689+
fn load_alias_or_file(&self, cached_path: &CachedPath, ctx: &mut Ctx) -> ResolveResult {
690+
if let Some(path) = self.load_browser_field_or_alias(cached_path, ctx)? {
691+
return Ok(Some(path));
692+
}
682693
if cached_path.is_file(&self.cache.fs, ctx) {
683694
return Ok(Some(cached_path.clone()));
684695
}
@@ -750,19 +761,25 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
750761
let cached_path = cached_path.normalize_with(specifier, &self.cache);
751762

752763
// Perf: try the directory first for package specifiers.
753-
if cached_path.is_dir(&self.cache.fs, ctx) {
754-
if let Some(path) = self.load_as_directory(&cached_path, ctx)? {
755-
return Ok(Some(path));
756-
}
757-
}
758764
if self.options.resolve_to_context {
759765
return Ok(cached_path
760766
.is_dir(&self.cache.fs, ctx)
761767
.then(|| cached_path.clone()));
762768
}
769+
if cached_path.is_dir(&self.cache.fs, ctx) {
770+
if let Some(path) = self.load_browser_field_or_alias(&cached_path, ctx)? {
771+
return Ok(Some(path));
772+
}
773+
if let Some(path) = self.load_as_directory(&cached_path, ctx)? {
774+
return Ok(Some(path));
775+
}
776+
}
763777
if let Some(path) = self.load_as_file(&cached_path, ctx)? {
764778
return Ok(Some(path));
765779
}
780+
if let Some(path) = self.load_as_directory(&cached_path, ctx)? {
781+
return Ok(Some(path));
782+
}
766783
}
767784
}
768785
Ok(None)

0 commit comments

Comments
 (0)