From 42c89e377706481b5a775d935b7f2ebdd23df211 Mon Sep 17 00:00:00 2001 From: Boshen Date: Sun, 17 Dec 2023 20:09:04 +0800 Subject: [PATCH] refactor: tweak the load_browser_field API --- src/lib.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3eab20a7..4cf48c30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -586,8 +586,7 @@ impl ResolverGeneric { ctx: &mut ResolveContext, ) -> ResolveState { if let Some(package_json) = cached_path.find_package_json(&self.cache.fs, &self.options)? { - let path = cached_path.path(); - if let Some(path) = self.load_browser_field(path, None, &package_json, ctx)? { + if let Some(path) = self.load_browser_field(cached_path, None, &package_json, ctx)? { return Ok(Some(path)); } } @@ -723,12 +722,7 @@ impl ResolverGeneric { }; // 3. If the SCOPE/package.json "exports" is null or undefined, return. if package_json.exports.is_empty() { - return self.load_browser_field( - cached_path.path(), - Some(specifier), - &package_json, - ctx, - ); + return self.load_browser_field(cached_path, Some(specifier), &package_json, ctx); } // 4. If the SCOPE/package.json "name" is not the first segment of X, return. let Some(subpath) = package_json @@ -766,7 +760,7 @@ impl ResolverGeneric { package_json: &PackageJson, ctx: &mut ResolveContext, ) -> ResolveState { - if let Some(path) = self.load_browser_field(cached_path.path(), None, package_json, ctx)? { + if let Some(path) = self.load_browser_field(cached_path, None, package_json, ctx)? { return Ok(Some(path)); } @@ -785,11 +779,12 @@ impl ResolverGeneric { /// enhanced-resolve: AliasFieldPlugin for [ResolveOptions::alias_fields] fn load_browser_field( &self, - path: &Path, + cached_path: &CachedPath, specifier: Option<&str>, package_json: &PackageJson, ctx: &mut ResolveContext, ) -> ResolveState { + let path = cached_path.path(); let Some(new_specifier) = package_json.resolve_browser_field(path, specifier)? else { return Ok(None); };