Expand description
§Rspack 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(enabled by default) — Yarn Plug’n’Play
§Example
ⓘ
///! See documentation at <https://docs.rs/rspack_resolver>
use std::{env, path::PathBuf};
use rspack_resolver::{AliasValue, ResolveOptions, Resolver};
#[tokio::main]
async 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()
};
let mut ctx = Default::default();
match Resolver::new(options)
.resolve_with_context(path, &specifier, &mut ctx)
.await
{
Err(error) => println!("Error: {error}"),
Ok(resolution) => println!("Resolved: {:?}", resolution.full_path()),
};
let mut sorted_file_deps = ctx.file_dependencies.iter().collect::<Vec<_>>();
sorted_file_deps.sort();
println!("file_deps: {:#?}", sorted_file_deps);
let mut sorted_missing = ctx.missing_dependencies.iter().collect::<Vec<_>>();
sorted_missing.sort();
println!("missing_deps: {:#?}", sorted_missing);
}Structs§
- File
Metadata - Metadata information about a file
- File
System Os - Operating System
- JSON
Error - JSON error from serde_json::Error
- Package
Json - Deserialized package.json
- Resolution
- The final path resolution with optional
?queryand#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 FileSystem trait
- 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
- JSON
Value - Borrowed JSON-DOM Value, consider using the
ValueTraitto access its content - Module
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
Type Aliases§
- Alias
- Alias for ResolveOptions::alias and ResolveOptions::fallback
- Resolver
- Resolver with the current operating system as the file system