|
| 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 | +} |
0 commit comments