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.jsonwithserde_json::Value.yarn_pnp— Yarn Plug’n’Play
§Example
ⓘ
///! See documentation at <https://docs.rs/rspack_resolver>
use std::{env, path::PathBuf};
use rspack_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()),
}
}Structs§
- Metadata information about a file
- JSON error from serde_json::Error
- Deserialized package.json
- The final path resolution with optional
?queryand#fragment - Context returned from the Resolver::resolve_with_context API
- Module Resolution Options
- Generic implementation of the resolver, can be configured by the FileSystem trait
- Tsconfig Options for ResolveOptions::tsconfig
Enums§
- Alias Value for ResolveOptions::alias and ResolveOptions::fallback
- Value for ResolveOptions::enforce_extension
- All resolution errors
- Value for ResolveOptions::restrictions
- Error for ResolveError::Specifier
- Configuration for TsconfigOptions::references
Constants§
- Node.js built-in modules
Traits§
- File System abstraction used for
ResolverGeneric
Type Aliases§
- Alias for ResolveOptions::alias and ResolveOptions::fallback
- Resolver with the current operating system as the file system