From 99fb39734e8acb45c862f3254d05b3f931cdfd3f Mon Sep 17 00:00:00 2001 From: Boshen Date: Thu, 27 Jun 2024 23:53:55 +0800 Subject: [PATCH] perf: omit searching for package.json when `alias_fields` is not provided --- src/lib.rs | 14 +++++++++----- src/tests/incorrect_description_file.rs | 7 +++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 40c51359..f1fe36ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -647,11 +647,15 @@ impl ResolverGeneric { } fn load_alias_or_file(&self, cached_path: &CachedPath, ctx: &mut Ctx) -> ResolveResult { - if let Some(package_json) = - cached_path.find_package_json(&self.cache.fs, &self.options, ctx)? - { - if let Some(path) = self.load_browser_field(cached_path, None, &package_json, ctx)? { - return Ok(Some(path)); + if !self.options.alias_fields.is_empty() { + if let Some(package_json) = + cached_path.find_package_json(&self.cache.fs, &self.options, ctx)? + { + if let Some(path) = + self.load_browser_field(cached_path, None, &package_json, ctx)? + { + return Ok(Some(path)); + } } } // enhanced-resolve: try file as alias diff --git a/src/tests/incorrect_description_file.rs b/src/tests/incorrect_description_file.rs index a5c81796..e2caecbb 100644 --- a/src/tests/incorrect_description_file.rs +++ b/src/tests/incorrect_description_file.rs @@ -17,8 +17,11 @@ fn incorrect_description_file_1() { column: 0, }); assert_eq!(resolution, Err(error)); - assert_eq!(ctx.file_dependencies, FxHashSet::from_iter([f.join("pack1/package.json")])); - assert!(ctx.missing_dependencies.is_empty()); + assert_eq!( + ctx.file_dependencies, + FxHashSet::from_iter([f.join("pack1"), f.join("pack1/package.json")]) + ); + assert!(!ctx.missing_dependencies.is_empty()); } // should not resolve main in incorrect description file #2