chore(resolver): add a alias test and check resolution is the same in benchmark (#600)

This commit is contained in:
Boshen 2023-07-24 15:35:10 +08:00 committed by GitHub
parent 70d34f74e0
commit b21b3b8528
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 7 deletions

View file

@ -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

View file

@ -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:?}");