From b21b3b8528dec1d0a661fb7dcf3bf57ce7bef0f2 Mon Sep 17 00:00:00 2001 From: Boshen Date: Mon, 24 Jul 2023 15:35:10 +0800 Subject: [PATCH] chore(resolver): add a alias test and check resolution is the same in benchmark (#600) --- crates/oxc_resolver/benches/resolver.rs | 23 ++++++++++++++++++----- crates/oxc_resolver/examples/resolver.rs | 7 +++++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/crates/oxc_resolver/benches/resolver.rs b/crates/oxc_resolver/benches/resolver.rs index 815aa4f3a..5521b5e71 100644 --- a/crates/oxc_resolver/benches/resolver.rs +++ b/crates/oxc_resolver/benches/resolver.rs @@ -40,13 +40,16 @@ fn data() -> Vec<(PathBuf, &'static str)> { // scoped (cwd.join("test/fixtures/scoped"), "@scope/pack1"), (cwd.join("test/fixtures/scoped"), "@scope/pack2/lib"), + // alias + (cwd.clone(), "/absolute/path"), ] } fn nodejs_resolver() -> nodejs_resolver::Resolver { - use nodejs_resolver::{Options, Resolver}; + use nodejs_resolver::{AliasMap, Options, Resolver}; Resolver::new(Options { browser_field: true, + alias: vec![("/absolute/path".into(), vec![AliasMap::Target("./".into())])], extension_alias: vec![ (".js".into(), vec![".ts".into(), ".js".into()]), (".mjs".into(), vec![".mts".into()]), @@ -56,8 +59,9 @@ fn nodejs_resolver() -> nodejs_resolver::Resolver { } fn oxc_resolver() -> oxc_resolver::Resolver { - use oxc_resolver::{ResolveOptions, Resolver}; + use oxc_resolver::{AliasValue, ResolveOptions, Resolver}; Resolver::new(ResolveOptions { + alias: vec![("/absolute/path".into(), vec![AliasValue::Path("./".into())])], alias_fields: vec!["browser".into()], extension_alias: vec![ (".js".into(), vec![".ts".into(), ".js".into()]), @@ -70,10 +74,19 @@ fn oxc_resolver() -> oxc_resolver::Resolver { fn resolver_benchmark(c: &mut Criterion) { let data = data(); - // Check path is valid, re-initialize so they don't use cache + // Check path is valid by comparing the outputs, re-initialize so they don't use cache for (path, request) in &data { - assert!(nodejs_resolver().resolve(path, request).is_ok(), "{path:?} {request}"); - assert!(oxc_resolver().resolve(path, request).is_ok(), "{path:?} {request}"); + let nodejs_resolver_path = if let Ok(nodejs_resolver::ResolveResult::Resource(resource)) = + nodejs_resolver().resolve(path, request) + { + Some(resource.path) + } else { + None + }; + let oxc_path = oxc_resolver() + .resolve(path, request) + .map_or(None, |oxc_path| Some(oxc_path.into_path_buf())); + assert_eq!(nodejs_resolver_path, oxc_path, "{path:?} {request}"); } // Bench nodejs_resolver with cache diff --git a/crates/oxc_resolver/examples/resolver.rs b/crates/oxc_resolver/examples/resolver.rs index be935dcc5..7d811c6bc 100644 --- a/crates/oxc_resolver/examples/resolver.rs +++ b/crates/oxc_resolver/examples/resolver.rs @@ -4,7 +4,7 @@ use std::{env, path::PathBuf}; -use oxc_resolver::{ResolveOptions, Resolver}; +use oxc_resolver::{AliasValue, ResolveOptions, Resolver}; fn main() { let path = env::args().nth(1).expect("require path"); @@ -14,7 +14,10 @@ fn main() { println!("path: {path:?}"); println!("request: {request}"); - let options = ResolveOptions::default(); + let options = ResolveOptions { + alias: vec![("/asdf".into(), vec![AliasValue::Path("./test.js".into())])], + ..ResolveOptions::default() + }; let resolved_path = Resolver::new(options).resolve(path, &request); println!("Result: {resolved_path:?}");