From 9a7076c5d5d29035452c4967170bf51337fb24ae Mon Sep 17 00:00:00 2001 From: Boshen Date: Sat, 14 Dec 2024 14:17:14 +0800 Subject: [PATCH] fix: try browsers field and alias before resolving directory in node_modules --- src/lib.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 31a334c5..3e7f7432 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -657,7 +657,11 @@ impl ResolverGeneric { Ok(None) } - fn load_alias_or_file(&self, cached_path: &CachedPath, ctx: &mut Ctx) -> ResolveResult { + fn load_browser_field_or_alias( + &self, + cached_path: &CachedPath, + ctx: &mut Ctx, + ) -> ResolveResult { if !self.options.alias_fields.is_empty() { if let Some((package_url, package_json)) = cached_path.find_package_json(&self.options, &self.cache, ctx)? @@ -679,6 +683,13 @@ impl ResolverGeneric { return Ok(Some(path)); } } + Ok(None) + } + + fn load_alias_or_file(&self, cached_path: &CachedPath, ctx: &mut Ctx) -> ResolveResult { + if let Some(path) = self.load_browser_field_or_alias(cached_path, ctx)? { + return Ok(Some(path)); + } if cached_path.is_file(&self.cache.fs, ctx) { return Ok(Some(cached_path.clone())); } @@ -750,19 +761,25 @@ impl ResolverGeneric { let cached_path = cached_path.normalize_with(specifier, &self.cache); // Perf: try the directory first for package specifiers. - if cached_path.is_dir(&self.cache.fs, ctx) { - if let Some(path) = self.load_as_directory(&cached_path, ctx)? { - return Ok(Some(path)); - } - } if self.options.resolve_to_context { return Ok(cached_path .is_dir(&self.cache.fs, ctx) .then(|| cached_path.clone())); } + if cached_path.is_dir(&self.cache.fs, ctx) { + if let Some(path) = self.load_browser_field_or_alias(&cached_path, ctx)? { + return Ok(Some(path)); + } + if let Some(path) = self.load_as_directory(&cached_path, ctx)? { + return Ok(Some(path)); + } + } if let Some(path) = self.load_as_file(&cached_path, ctx)? { return Ok(Some(path)); } + if let Some(path) = self.load_as_directory(&cached_path, ctx)? { + return Ok(Some(path)); + } } } Ok(None)