Expand description
§Oxc Resolver
Node.js CommonJS and ECMAScript Module Resolution.
Released on crates.io and npm.
A module resolution is the process of finding the file referenced by a module specifier in
import "specifier"
or require("specifier")
.
All configuration options are aligned with webpack’s enhanced-resolve.
§Terminology
§Specifier
For CommonJS modules,
the specifier is the string passed to the require
function. e.g. "id"
in require("id")
.
For ECMAScript modules,
the specifier of an import
statement is the string after the from
keyword,
e.g. 'specifier'
in import 'specifier'
or import { sep } from 'specifier'
.
Specifiers are also used in export from statements, and as the argument to an import()
expression.
This is also named “request” in some places.
§References:
- Algorithm adapted from Node.js CommonJS Module Resolution Algorithm and ECMAScript Module Resolution Algorithm.
- Tests are ported from enhanced-resolve.
- Some code is adapted from parcel-resolver.
- The documentation is copied from webpack’s resolve configuration.
§Feature flags
package_json_raw_json_api
— Enables the PackageJson::raw_json API, which returns thepackage.json
withserde_json::Value
.yarn_pnp
— Yarn Plug’n’Play
§Example
ⓘ
// See documentation at <https://docs.rs/oxc_resolver>
use std::path::PathBuf;
use oxc_resolver::{AliasValue, ResolveOptions, Resolver, TsconfigOptions, TsconfigReferences};
use pico_args::Arguments;
fn main() {
let mut args = Arguments::from_env();
let tsconfig_path = args.value_from_str::<&'static str, PathBuf>("--tsconfig").ok();
let path = args.free_from_str::<PathBuf>().expect("path");
let specifier = args.free_from_str::<String>().expect("specifier");
assert!(path.is_dir(), "{} must be a directory that will be resolved against.", path.display());
assert!(path.is_absolute(), "{} must be an absolute path.", path.display());
println!("path: {}", path.to_string_lossy());
println!("specifier: {specifier}");
if let Some(path) = &tsconfig_path {
println!("tsconfig: {}", path.to_string_lossy());
}
let options = ResolveOptions {
alias_fields: vec![vec!["browser".into()]],
alias: vec![("asdf".into(), vec![AliasValue::from("./test.js")])],
extensions: vec![".js".into(), ".ts".into()],
extension_alias: vec![(".js".into(), vec![".ts".into(), ".js".into()])],
// ESM
condition_names: vec!["node".into(), "import".into()],
// CJS
// condition_names: vec!["node".into(), "require".into()],
tsconfig: tsconfig_path.map(|config_file| TsconfigOptions {
config_file,
references: TsconfigReferences::Auto,
}),
..ResolveOptions::default()
};
println!();
match Resolver::new(options).resolve(path, &specifier) {
Err(error) => println!("Error: {error}"),
Ok(resolution) => {
println!("Resolution: {}", resolution.full_path().to_string_lossy());
println!("Module Type: {:?}", resolution.module_type());
println!(
"package json: {:?}",
resolution.package_json().map(|p| p.path.to_string_lossy())
);
}
}
}
Structs§
- Cache
- Cache implementation used for caching filesystem access.
- Cached
Path - Compiler
Options - Compiler Options
- File
Metadata - Metadata information about a file
- File
System Os - Imports
Exports Array - Imports
Exports Entry - Imports
Exports Map - JSON
Error - JSON error from serde_json::Error
- Package
Json - Serde implementation for the deserialized
package.json
. - Project
Reference - Project Reference
- Resolution
- The final path resolution with optional
?query
and#fragment
- Resolve
Context - Context returned from the Resolver::resolve_with_context API
- Resolve
Options - Module Resolution Options
- Resolver
Generic - Generic implementation of the resolver, can be configured by the Cache trait
- TsConfig
- Tsconfig
Options - Tsconfig Options for ResolveOptions::tsconfig
Enums§
- Alias
Value - Alias Value for ResolveOptions::alias and ResolveOptions::fallback
- Enforce
Extension - Value for ResolveOptions::enforce_extension
- Imports
Exports Kind - Module
Type - Package
Type - Resolve
Error - All resolution errors
- Restriction
- Value for ResolveOptions::restrictions
- Specifier
Error - Error for ResolveError::Specifier
- Tsconfig
References - Configuration for TsconfigOptions::references
Constants§
- NODEJS_
BUILTINS - Node.js built-in modules
Traits§
- File
System - File System abstraction used for
ResolverGeneric
- Path
Util - Extension trait to add path normalization to std’s
Path
.
Type Aliases§
- Alias
- Alias for ResolveOptions::alias and ResolveOptions::fallback
- Compiler
Options Paths Map - Resolver
- Resolver with the current operating system as the file system