Skip to content

Commit bde9ad6

Browse files
committed
chore: add resolve_file example (#881)
1 parent f917412 commit bde9ad6

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ literal_string_with_formatting_args = "allow"
7575
missing_const_for_fn = "allow"
7676

7777
[[example]]
78-
name = "resolver"
78+
name = "dir"
7979

8080
[dependencies]
8181
cfg-if = "1"
File renamed without changes.

examples/file.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// See documentation at <https://docs.rs/oxc_resolver>
2+
3+
use std::path::PathBuf;
4+
5+
use oxc_resolver::{AliasValue, ResolveOptions, Resolver, TsconfigDiscovery};
6+
use pico_args::Arguments;
7+
8+
fn main() {
9+
let mut args = Arguments::from_env();
10+
11+
let path = args.free_from_str::<PathBuf>().expect("path");
12+
let specifier = args.free_from_str::<String>().expect("specifier");
13+
14+
assert!(path.is_file(), "{} must be a file that will be resolved against.", path.display());
15+
assert!(path.is_absolute(), "{} must be an absolute path.", path.display());
16+
17+
println!("path: {}", path.to_string_lossy());
18+
println!("specifier: {specifier}");
19+
20+
let options = ResolveOptions {
21+
alias_fields: vec![vec!["browser".into()]],
22+
alias: vec![("asdf".into(), vec![AliasValue::from("./test.js")])],
23+
extensions: vec![".js".into(), ".ts".into()],
24+
extension_alias: vec![(".js".into(), vec![".ts".into(), ".js".into()])],
25+
// ESM
26+
condition_names: vec!["node".into(), "import".into()],
27+
// CJS
28+
// condition_names: vec!["node".into(), "require".into()],
29+
tsconfig: Some(TsconfigDiscovery::Auto),
30+
..ResolveOptions::default()
31+
};
32+
33+
println!();
34+
35+
match Resolver::new(options).resolve_file(path, &specifier) {
36+
Err(error) => println!("Error: {error}"),
37+
Ok(resolution) => {
38+
println!("Resolution: {}", resolution.full_path().to_string_lossy());
39+
println!("Module Type: {:?}", resolution.module_type());
40+
println!(
41+
"package.json: {:?}",
42+
resolution.package_json().map(|p| p.path.to_string_lossy())
43+
);
44+
}
45+
}
46+
}

justfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ watch *args='':
4040
watch-check:
4141
just watch "'cargo check; cargo clippy'"
4242

43-
watch-example *args='':
44-
just watch "cargo run --example resolver -- {{args}}"
43+
watch-example target *args='':
44+
just watch "cargo run --example {{target}} -- {{args}}"
4545

4646
# Run the example
47-
example *args='':
48-
cargo run --example resolver -- {{args}}
47+
example target *args='':
48+
cargo run --example {{target}} -- {{args}}
4949

5050
# Format all files
5151
fmt:

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
//! ## Example
4444
//!
4545
//! ```rust,ignore
46-
#![doc = include_str!("../examples/resolver.rs")]
46+
#![doc = include_str!("../examples/dir.rs")]
4747
//! ```
4848
4949
mod builtins;

0 commit comments

Comments
 (0)