Skip to content

Commit

Permalink
added short circuit in resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
alshdavid committed Apr 11, 2024
1 parent 43c5216 commit 78edde8
Showing 1 changed file with 11 additions and 29 deletions.
40 changes: 11 additions & 29 deletions mach/src/platform/plugins/resolver_javascript/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
// TODO not a plugin yet

use std::path::Component;
use std::path::Path;
use std::path::PathBuf;
use std::path::MAIN_SEPARATOR_STR;

use oxc_resolver::ResolveOptions;
use oxc_resolver::Resolver;
use normalize_path::NormalizePath;

pub fn resolve(
from_raw: &PathBuf,
specifier: &str,
) -> Result<PathBuf, String> {
if specifier.starts_with(MAIN_SEPARATOR_STR) {
return Ok(PathBuf::from(specifier).normalize());
}

let from = {
if from_raw.is_file() {
from_raw.parent().unwrap()
Expand All @@ -19,6 +24,10 @@ pub fn resolve(
}
};

if specifier.starts_with(".") {
return Ok(from.join(specifier).normalize());
}

let result = resolve_oxc(&specifier, &from);

if let Ok(result) = result {
Expand Down Expand Up @@ -69,33 +78,6 @@ fn resolve_oxc(

match resolver.resolve(from, specifier) {
Err(error) => return Err(format!("{error}")),
Ok(resolution) => return Ok(normalize_path(&resolution.full_path())),
};
}

pub fn normalize_path(path: &Path) -> PathBuf {
let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next();
PathBuf::from(c.as_os_str())
} else {
PathBuf::new()
Ok(resolution) => return Ok(resolution.full_path().normalize()),
};

for component in components {
match component {
Component::Prefix(..) => unreachable!(),
Component::RootDir => {
ret.push(component.as_os_str());
}
Component::CurDir => {}
Component::ParentDir => {
ret.pop();
}
Component::Normal(c) => {
ret.push(c);
}
}
}
return ret;
}

0 comments on commit 78edde8

Please sign in to comment.