Crate oxc_resolver

source ·
Expand description

§Oxc Resolver

Node.js CommonJS and ECMAScript Module Resolution.

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

  • package_json_raw_json_api — Enables the PackageJson::raw_json API, which returns the package.json with serde_json::Value.

§Example

///! See documentation at <https://docs.rs/oxc_resolver>
use std::{env, path::PathBuf};

use oxc_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")])],
        ..ResolveOptions::default()
    };

    match Resolver::new(options).resolve(path, &specifier) {
        Err(error) => println!("Error: {error}"),
        Ok(resolution) => println!("Resolved: {:?}", resolution.full_path()),
    }
}

Structs§

Enums§

Constants§

Traits§

  • File System abstraction used for ResolverGeneric

Type Aliases§