From 17a6f63c3109ad82209dbc09c4d2632615f85c29 Mon Sep 17 00:00:00 2001 From: Boshen Date: Thu, 10 Aug 2023 21:05:27 +0800 Subject: [PATCH] refactor(resolver): remove the leading dot trim on extensions --- crates/oxc_resolver/src/lib.rs | 5 ++--- crates/oxc_resolver/src/options.rs | 17 ----------------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/crates/oxc_resolver/src/lib.rs b/crates/oxc_resolver/src/lib.rs index 89baa6816..fa51a5eb9 100644 --- a/crates/oxc_resolver/src/lib.rs +++ b/crates/oxc_resolver/src/lib.rs @@ -348,8 +348,7 @@ impl ResolverGeneric { } for extension in extensions { let mut path_with_extension = path.to_path_buf().into_os_string(); - path_with_extension.reserve_exact(1 + extension.len()); - path_with_extension.push("."); + path_with_extension.reserve_exact(extension.len()); path_with_extension.push(extension); let path_with_extension = PathBuf::from(path_with_extension); let cached_path = self.cache.value(&path_with_extension); @@ -679,7 +678,7 @@ impl ResolverGeneric { fn load_extension_alias(&self, cached_path: &CachedPath, ctx: &ResolveContext) -> ResolveState { let Some(path_extension) = cached_path.path().extension() else { return Ok(None) }; let Some((_, extensions)) = - self.options.extension_alias.iter().find(|(ext, _)| OsStr::new(ext) == path_extension) + self.options.extension_alias.iter().find(|(ext, _)| OsStr::new(ext.trim_start_matches('.')) == path_extension) else { return Ok(None); }; diff --git a/crates/oxc_resolver/src/options.rs b/crates/oxc_resolver/src/options.rs index 7b38f8f87..62daa14f1 100644 --- a/crates/oxc_resolver/src/options.rs +++ b/crates/oxc_resolver/src/options.rs @@ -196,25 +196,8 @@ impl ResolveOptions { self.extensions.retain(String::is_empty); } } - self.extensions = Self::remove_leading_dots(self.extensions); - self.extension_alias = self - .extension_alias - .into_iter() - .map(|(extension, extensions)| { - (Self::remove_leading_dot(&extension), Self::remove_leading_dots(extensions)) - }) - .collect(); self } - - // Remove the leading `.` because `Path::with_extension` does not accept the dot. - fn remove_leading_dot(s: &str) -> String { - s.trim_start_matches('.').to_string() - } - - fn remove_leading_dots(v: Vec) -> Vec { - v.into_iter().map(|s| Self::remove_leading_dot(&s)).collect() - } } // For tracing