Crate unrspack_resolver

Source
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:

§Feature flags

  • fs_cache (enabled by default) — Provides the FsCache implementation.
  • package_json_raw_json_api — Enables the PackageJsonSerde::raw_json API, which returns the package.json with serde_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§

context

Structs§

CompilerOptionsSerdefs_cache
Compiler Options
FileMetadatafs_cache
Metadata information about a file
FileSystemOsfs_cache
Operating System
FsCachefs_cache
Cache implementation used for caching filesystem access.
FsCachedPathfs_cache
JSONError
JSON error from serde_json::Error
PackageJsonSerdefs_cache
Serde implementation for the deserialized package.json.
ProjectReferenceSerdefs_cache
Project Reference
Resolution
The final path resolution with optional ?query and #fragment
ResolveContext
Context returned from the Resolver::resolve_with_context API
ResolveOptions
Module Resolution Options
ResolverGeneric
Generic implementation of the resolver, can be configured by the Cache trait
TsConfigSerdefs_cache
TsconfigOptions
Tsconfig Options for ResolveOptions::tsconfig

Enums§

AliasValue
Alias Value for ResolveOptions::alias and ResolveOptions::fallback
EnforceExtension
Value for ResolveOptions::enforce_extension
ExtendsFieldfs_cache
Value for the “extends” field.
ImportsExportsKind
PackageType
ResolveError
All resolution errors
Restriction
Value for ResolveOptions::restrictions
SpecifierError
Error for ResolveError::Specifier
TsconfigReferences
Configuration for TsconfigOptions::references

Constants§

NODEJS_BUILTINS
Node.js built-in modules

Traits§

Cache
CachedPath
CompilerOptions
Compiler Options.
FileSystemfs_cache
File System abstraction used for ResolverGeneric
ImportsExportsArray
Trait used for representing array values in the imports and exports fields without allocation.
ImportsExportsEntry
Trait used for representing entries in the imports and exports fields without allocation.
ImportsExportsMap
Trait used for representing map (object) values in the imports and exports fields without allocation.
PackageJson
Abstract representation for the contents of a package.json file, as well as the location where it was found.
PathUtil
Extension trait to add path normalization to std’s Path.
ProjectReference
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
CompilerOptionsPathsMap
FsResolutionfs_cache
Resolverfs_cache
Resolver with the current operating system as the file system