Repository: oxc-project/oxc-resolver
Pull request #194 (2024-06-27 15:55:04)

perf: skip searching for package.json when `alias_fields` is not provided

codspeed-hq[bot]: ## [CodSpeed Performance Report](https://codspeed.io/oxc-project/oxc-resolver/branches/perf-alias)

### Merging #194 will **not alter performance**

<sub>Comparing <code>perf-alias</code> (99fb397) with <code>main</code> (e18bd5e)</sub>



### Summary

`✅ 2` untouched benchmarks





codecov[bot]: ## [Codecov](https://app.codecov.io/gh/oxc-project/oxc-resolver/pull/194?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oxc-project) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 96.88%. Comparing base [(`e18bd5e`)](https://app.codecov.io/gh/oxc-project/oxc-resolver/commit/e18bd5e4dda5ba59b7470328aee8ff9f1c7a837c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oxc-project) to head [(`99fb397`)](https://app.codecov.io/gh/oxc-project/oxc-resolver/commit/99fb39734e8acb45c862f3254d05b3f931cdfd3f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oxc-project).

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main     #194   +/-   ##
=======================================
  Coverage   96.88%   96.88%           
=======================================
  Files          11       11           
  Lines        2278     2281    +3     
=======================================
+ Hits         2207     2210    +3     
  Misses         71       71           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/oxc-project/oxc-resolver/pull/194?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oxc-project).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oxc-project).


Patch:
diff --git a/src/lib.rs b/src/lib.rs
index 40c5135..f1fe36c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -647,11 +647,15 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
     }
 
     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


Test patch:
diff --git a/src/tests/incorrect_description_file.rs b/src/tests/incorrect_description_file.rs
index a5c8179..e2caecb 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