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
fs_cache
(enabled by default) — Provides theFsCache
implementation.package_json_raw_json_api
— Enables the PackageJsonSerde::raw_json API, which returns thepackage.json
withserde_json::Value
.yarn_pnp
(enabled by default) — Yarn Plug’n’Play
§Example
ⓘ
// See documentation at <https://docs.rs/unrspack_resolver>
use std::{env, path::PathBuf};
use unrspack_resolver::{AliasValue, ResolveOptions, Resolver};
fn main() {
let path = PathBuf::from(env::args().nth(1).expect("path"));
assert!(path.is_dir(), "{path:?} must be a directory that will be resolved against.");
assert!(path.is_absolute(), "{path:?} must be an absolute path.",);
let specifier = env::args().nth(2).expect("specifier");
println!("path: {path:?}");
println!("specifier: {specifier}");
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()],
..ResolveOptions::default()
};
match Resolver::new(options).resolve(path, &specifier) {
Err(error) => println!("Error: {error}"),
Ok(resolution) => println!("Resolved: {:?}", resolution.full_path()),
}
}
Modules§
Structs§
- Compiler
Options Serde fs_cache
- Compiler Options
- File
Metadata fs_cache
- Metadata information about a file
- File
System Os fs_cache
- Operating System
- FsCache
fs_cache
- Cache implementation used for caching filesystem access.
- FsCached
Path fs_cache
- JSON
Error - JSON error from serde_json::Error
- Package
Json Serde fs_cache
- Serde implementation for the deserialized
package.json
. - Project
Reference Serde fs_cache
- 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
Serde fs_cache
- 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
- Extends
Field fs_cache
- Value for the “extends” field.
- Imports
Exports Kind - 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§
- Cache
- Cached
Path - Compiler
Options - Compiler Options.
- File
System fs_cache
- File System abstraction used for
ResolverGeneric
- Imports
Exports Array - Trait used for representing array values in the
imports
andexports
fields without allocation. - Imports
Exports Entry - Trait used for representing entries in the
imports
andexports
fields without allocation. - Imports
Exports Map - Trait used for representing map (object) values in the
imports
andexports
fields without allocation. - Package
Json - Abstract representation for the contents of a
package.json
file, as well as the location where it was found. - Path
Util - Extension trait to add path normalization to std’s
Path
. - Project
Reference - Project Reference.
- TsConfig
- Abstract representation for the contents of a
tsconfig.json
file, as well as the location where it was found.
Type Aliases§
- Alias
- Alias for ResolveOptions::alias and ResolveOptions::fallback
- Compiler
Options Paths Map - FsResolution
fs_cache
- Resolver
fs_cache
- Resolver with the current operating system as the file system