Skip to content

Commit e03b08a

Browse files
authored
refactor: remove a redundant path clone from PackageJson::parse (#725)
1 parent d9ad492 commit e03b08a

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

src/cache/cache_impl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<Fs: FileSystem> Cache<Fs> {
102102
path: &CachedPath,
103103
options: &ResolveOptions,
104104
ctx: &mut Ctx,
105-
) -> Result<Option<(CachedPath, Arc<PackageJson>)>, ResolveError> {
105+
) -> Result<Option<Arc<PackageJson>>, ResolveError> {
106106
// Change to `std::sync::OnceLock::get_or_try_init` when it is stable.
107107
let result = path
108108
.package_json
@@ -120,13 +120,13 @@ impl<Fs: FileSystem> Cache<Fs> {
120120
package_json_path.clone()
121121
};
122122
PackageJson::parse(package_json_path.clone(), real_path, &package_json_string)
123-
.map(|package_json| Some((path.clone(), (Arc::new(package_json)))))
123+
.map(|package_json| Some(Arc::new(package_json)))
124124
.map_err(|error| ResolveError::from_serde_json_error(package_json_path, &error))
125125
})
126126
.cloned();
127127
// https://github.com/webpack/enhanced-resolve/blob/58464fc7cb56673c9aa849e68e6300239601e615/lib/DescriptionFileUtils.js#L68-L82
128128
match &result {
129-
Ok(Some((_, package_json))) => {
129+
Ok(Some(package_json)) => {
130130
ctx.add_file_dependency(&package_json.path);
131131
}
132132
Ok(None) => {

src/cache/cached_path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct CachedPathImpl {
3030
pub canonicalized: OnceLock<Result<CachedPath, ResolveError>>,
3131
pub canonicalizing: AtomicU64,
3232
pub node_modules: OnceLock<Option<CachedPath>>,
33-
pub package_json: OnceLock<Option<(CachedPath, Arc<PackageJson>)>>,
33+
pub package_json: OnceLock<Option<Arc<PackageJson>>>,
3434
}
3535

3636
impl CachedPathImpl {
@@ -113,7 +113,7 @@ impl CachedPath {
113113
options: &ResolveOptions,
114114
cache: &Cache<Fs>,
115115
ctx: &mut Ctx,
116-
) -> Result<Option<(Self, Arc<PackageJson>)>, ResolveError> {
116+
) -> Result<Option<Arc<PackageJson>>, ResolveError> {
117117
let mut cache_value = self;
118118
// Go up directories when the querying path is not a directory
119119
while !cache.is_dir(cache_value, ctx) {

src/lib.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
309309
break;
310310
}
311311
if self.cache.is_dir(cp, ctx) {
312-
if let Some((_, package_json)) =
312+
if let Some(package_json) =
313313
self.cache.get_package_json(cp, &self.options, ctx)?
314314
{
315315
last = Some(package_json);
@@ -318,9 +318,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
318318
}
319319
Ok(last)
320320
} else {
321-
cached_path
322-
.find_package_json(&self.options, self.cache.as_ref(), ctx)
323-
.map(|result| result.map(|(_, p)| p))
321+
cached_path.find_package_json(&self.options, self.cache.as_ref(), ctx)
324322
}
325323
}
326324

@@ -619,7 +617,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
619617
) -> ResolveResult {
620618
// 1. Find the closest package scope SCOPE to DIR.
621619
// 2. If no scope was found, return.
622-
let Some((_, package_json)) =
620+
let Some(package_json) =
623621
cached_path.find_package_json(&self.options, self.cache.as_ref(), ctx)?
624622
else {
625623
return Ok(None);
@@ -656,9 +654,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
656654
fn load_as_directory(&self, cached_path: &CachedPath, ctx: &mut Ctx) -> ResolveResult {
657655
// 1. If X/package.json is a file,
658656
// a. Parse X/package.json, and look for "main" field.
659-
if let Some((_, package_json)) =
660-
self.cache.get_package_json(cached_path, &self.options, ctx)?
661-
{
657+
if let Some(package_json) = self.cache.get_package_json(cached_path, &self.options, ctx)? {
662658
// b. If "main" is a falsy value, GOTO 2.
663659
for main_field in package_json.main_fields(&self.options.main_fields) {
664660
// ref https://github.com/webpack/enhanced-resolve/blob/main/lib/MainFieldPlugin.js#L66-L67
@@ -805,11 +801,11 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
805801
ctx: &mut Ctx,
806802
) -> ResolveResult {
807803
if !self.options.alias_fields.is_empty() {
808-
if let Some((package_url, package_json)) =
804+
if let Some(package_json) =
809805
cached_path.find_package_json(&self.options, self.cache.as_ref(), ctx)?
810806
{
811807
if let Some(path) =
812-
self.load_browser_field(cached_path, None, &package_url, &package_json, ctx)?
808+
self.load_browser_field(cached_path, None, &package_json, ctx)?
813809
{
814810
return Ok(Some(path));
815811
}
@@ -1044,8 +1040,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
10441040
) -> ResolveResult {
10451041
// 2. If X does not match this pattern or DIR/NAME/package.json is not a file,
10461042
// return.
1047-
let Some((_, package_json)) =
1048-
self.cache.get_package_json(cached_path, &self.options, ctx)?
1043+
let Some(package_json) = self.cache.get_package_json(cached_path, &self.options, ctx)?
10491044
else {
10501045
return Ok(None);
10511046
};
@@ -1073,7 +1068,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
10731068
) -> ResolveResult {
10741069
// 1. Find the closest package scope SCOPE to DIR.
10751070
// 2. If no scope was found, return.
1076-
let Some((package_url, package_json)) =
1071+
let Some(package_json) =
10771072
cached_path.find_package_json(&self.options, self.cache.as_ref(), ctx)?
10781073
else {
10791074
return Ok(None);
@@ -1089,6 +1084,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
10891084
// defined in the ESM resolver.
10901085
// Note: The subpath is not prepended with a dot on purpose
10911086
// because `package_exports_resolve` matches subpath without the leading dot.
1087+
let package_url = self.cache.value(package_json.path.parent().unwrap());
10921088
for exports in package_json.exports_fields(&self.options.exports_fields) {
10931089
if let Some(cached_path) = self.package_exports_resolve(
10941090
&package_url,
@@ -1101,7 +1097,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
11011097
}
11021098
}
11031099
}
1104-
self.load_browser_field(cached_path, Some(specifier), &package_url, &package_json, ctx)
1100+
self.load_browser_field(cached_path, Some(specifier), &package_json, ctx)
11051101
}
11061102

11071103
/// RESOLVE_ESM_MATCH(MATCH)
@@ -1128,7 +1124,6 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
11281124
&self,
11291125
cached_path: &CachedPath,
11301126
module_specifier: Option<&str>,
1131-
package_url: &CachedPath,
11321127
package_json: &PackageJson,
11331128
ctx: &mut Ctx,
11341129
) -> ResolveResult {
@@ -1162,7 +1157,8 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
11621157
}
11631158
ctx.with_resolving_alias(new_specifier.to_string());
11641159
ctx.with_fully_specified(false);
1165-
self.require(package_url, new_specifier, ctx).map(Some)
1160+
let package_url = self.cache.value(package_json.path().parent().unwrap());
1161+
self.require(&package_url, new_specifier, ctx).map(Some)
11661162
}
11671163

11681164
/// enhanced-resolve: AliasPlugin for [ResolveOptions::alias] and [ResolveOptions::fallback].
@@ -1546,7 +1542,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
15461542
// 1. Continue the next loop iteration.
15471543
if self.cache.is_dir(&cached_path, ctx) {
15481544
// 4. Let pjson be the result of READ_PACKAGE_JSON(packageURL).
1549-
if let Some((_, package_json)) =
1545+
if let Some(package_json) =
15501546
self.cache.get_package_json(&cached_path, &self.options, ctx)?
15511547
{
15521548
// 5. If pjson is not null and pjson.exports is not null or undefined, then
@@ -2088,7 +2084,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
20882084
let package_json =
20892085
cached_path.find_package_json(&self.options, self.cache.as_ref(), ctx)?;
20902086
// 9. Let packageType be null.
2091-
if let Some((_, package_json)) = package_json {
2087+
if let Some(package_json) = package_json {
20922088
// 10. If pjson?.type is "module" or "commonjs", then
20932089
// 1. Set packageType to pjson.type.
20942090
if let Some(ty) = package_json.r#type() {

0 commit comments

Comments
 (0)