Skip to main content

resolve_module_specifier

Function resolve_module_specifier 

Source
pub fn resolve_module_specifier(
    base: Option<&Path>,
    specifier: &JsString,
    referrer: Option<&Path>,
    _context: &mut Context,
) -> JsResult<PathBuf>
Expand description

Resolves paths from the referrer and the specifier, normalize the paths and ensure the path is within a base. If the base is empty, that last verification will be skipped.

The returned specifier is a resolved absolute path that is guaranteed to be a descendant of base. All path component that are either empty or . and .. have been resolved.

§Errors

This predicate will return an error if the specifier is relative but the referrer does not have a path, or if the resolved path is outside base.

§Examples

#[cfg(target_family = "unix")]
assert_eq!(
    resolve_module_specifier(
        Some(Path::new("/base")),
        &js_string!("../a.js"),
        Some(Path::new("/base/hello/ref.js")),
        &mut Context::default()
    ),
    Ok("/base/a.js".into())
);