use std::collections::BTreeMap;
use std::sync::Arc;
use crate::vfs::{within_root, OsFs, SharedFs, SourceFs};
use crate::{
build_potentials, ext_kind, resolve_path_spec, source_dir, MultiSourceOptions, Resolution,
Resolver, ResolverInput, NONE,
};
const NODE_MODULES_LEVELS: usize = 7;
fn base_for(
input: &ResolverInput<'_>,
is_file: &dyn Fn(&str) -> bool,
dir: &dyn Fn(&str) -> String,
) -> String {
let raw = match input.source_path() {
Some(path) => path,
None => input.options.path.clone(),
};
if raw.is_empty() {
return String::new();
}
if is_file(&raw) {
dir(&raw)
} else {
raw
}
}
#[derive(Clone, Debug, Default)]
pub struct MapResolver {
files: Arc<BTreeMap<String, String>>,
}
impl MapResolver {
pub fn new() -> Self {
Self::default()
}
pub fn from_map(files: BTreeMap<String, String>) -> Self {
MapResolver {
files: Arc::new(files),
}
}
pub fn paths(&self) -> Vec<&str> {
self.files.keys().map(String::as_str).collect()
}
}
impl<K, V> FromIterator<(K, V)> for MapResolver
where
K: Into<String>,
V: Into<String>,
{
fn from_iter<T: IntoIterator<Item = (K, V)>>(entries: T) -> Self {
MapResolver::from_map(
entries
.into_iter()
.map(|(path, content)| (path.into(), content.into()))
.collect(),
)
}
}
impl<K, V, const N: usize> From<[(K, V); N]> for MapResolver
where
K: Into<String>,
V: Into<String>,
{
fn from(entries: [(K, V); N]) -> Self {
entries.into_iter().collect()
}
}
impl Resolver for MapResolver {
fn resolve(&self, reference: Option<&str>, input: &ResolverInput<'_>) -> Resolution {
let files = Arc::clone(&self.files);
let is_file = move |path: &str| files.contains_key(path);
let base = base_for(input, &is_file, &|path| source_dir(path));
let spec = resolve_path_spec(reference, &base);
let Some(full) = spec.full.clone() else {
return Resolution::not_found(spec);
};
let potentials = if spec.kind == NONE {
build_potentials(&full, &input.options.implicit_ext)
} else {
vec![full]
};
for candidate in &potentials {
if let Some(src) = self.files.get(candidate) {
return Resolution::found(spec, candidate, src.clone())
.with_search(potentials.clone());
}
}
Resolution::not_found(spec).with_search(potentials)
}
}
pub type PathFinder = Arc<dyn Fn(&str) -> String + Send + Sync>;
#[derive(Clone, Default)]
pub struct FileResolver {
pathfinder: Option<PathFinder>,
preload: Arc<BTreeMap<String, String>>,
fs: Option<SharedFs>,
root: Option<String>,
}
impl std::fmt::Debug for FileResolver {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("FileResolver")
.field(
"pathfinder",
&self.pathfinder.as_ref().map(|_| "<function>"),
)
.field("preload", &self.preload.len())
.field("fs", &self.fs.as_ref().map(|_| "<SourceFs>"))
.field("root", &self.root)
.finish()
}
}
impl FileResolver {
pub fn new() -> Self {
Self::default()
}
pub fn with_pathfinder(
mut self,
pathfinder: impl Fn(&str) -> String + Send + Sync + 'static,
) -> Self {
self.pathfinder = Some(Arc::new(pathfinder));
self
}
pub fn with_preload(mut self, preload: BTreeMap<String, String>) -> Self {
self.preload = Arc::new(preload);
self
}
pub fn with_fs(mut self, filesystem: impl SourceFs + 'static) -> Self {
self.fs = Some(Arc::new(filesystem));
self
}
pub fn with_shared_fs(mut self, filesystem: SharedFs) -> Self {
self.fs = Some(filesystem);
self
}
pub fn with_root(mut self, root: impl Into<String>) -> Self {
self.root = Some(root.into());
self
}
fn filesystem(&self, options: &MultiSourceOptions) -> SharedFs {
self.fs
.clone()
.or_else(|| options.fs.clone())
.unwrap_or_else(|| Arc::new(OsFs))
}
fn load(&self, filesystem: &dyn SourceFs, path: &str, root: &Confinement) -> Option<String> {
if !root.allows(filesystem, path) {
return None;
}
if let Some(src) = self.preload.get(path) {
return Some(src.clone());
}
filesystem.read_file(path)
}
}
struct Confinement {
root: Option<String>,
os_paths: bool,
}
impl Confinement {
fn new(filesystem: &dyn SourceFs, root: Option<&String>) -> Self {
Confinement {
root: root.map(|root| {
filesystem
.real_path(root)
.unwrap_or_else(|| filesystem.canon(root))
}),
os_paths: filesystem.native_paths(),
}
}
fn allows(&self, filesystem: &dyn SourceFs, candidate: &str) -> bool {
let Some(root) = &self.root else {
return true;
};
match filesystem.real_path(candidate) {
Some(real) => within_root(&real, root, self.os_paths),
None => false,
}
}
}
impl Resolver for FileResolver {
fn resolve(&self, reference: Option<&str>, input: &ResolverInput<'_>) -> Resolution {
let filesystem = self.filesystem(input.options);
let root = Confinement::new(filesystem.as_ref(), self.root.as_ref());
let found = reference.map(|reference| match &self.pathfinder {
Some(pathfinder) => pathfinder(reference),
None => reference.to_string(),
});
let is_file =
|path: &str| self.preload.contains_key(path) || filesystem.read_file(path).is_some();
let dir = |path: &str| filesystem.dir(path);
let base = base_for(input, &is_file, &dir);
let mut spec = resolve_path_spec(found.as_deref(), &base);
let Some(full) = spec.full.clone() else {
return Resolution::not_found(spec);
};
let full = filesystem.canon(&full);
spec.full = Some(full.clone());
spec.kind = ext_kind(Some(&full));
let mut search = vec![full.clone()];
if let Some(src) = self.load(filesystem.as_ref(), &full, &root) {
return Resolution::found(spec, full, src).with_search(search);
}
let mut potentials: Vec<String> = Vec::new();
if let Some(path) = &spec.path {
let mut level = base.clone();
let mut last: Option<String> = None;
for _ in 0..NODE_MODULES_LEVELS {
potentials.push(filesystem.canon(&filesystem.join(&[
&level,
"node_modules",
path,
])));
let parent = filesystem.dir(&level);
if last.as_deref() == Some(parent.as_str()) {
break;
}
last = Some(parent.clone());
level = parent;
}
}
if spec.kind == NONE {
potentials.extend(
build_potentials(&full, &input.options.implicit_ext)
.into_iter()
.skip(1),
);
}
search.extend(potentials.iter().cloned());
for candidate in &potentials {
if let Some(src) = self.load(filesystem.as_ref(), candidate, &root) {
return Resolution::found(spec, candidate, src).with_search(search);
}
}
Resolution::not_found(spec).with_search(search)
}
}
#[derive(Clone, Default)]
pub struct PkgResolver {
paths: Vec<String>,
fs: Option<SharedFs>,
root: Option<String>,
}
impl std::fmt::Debug for PkgResolver {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("PkgResolver")
.field("paths", &self.paths)
.field("fs", &self.fs.as_ref().map(|_| "<SourceFs>"))
.field("root", &self.root)
.finish()
}
}
impl PkgResolver {
pub fn new() -> Self {
Self::default()
}
pub fn with_paths<I, S>(mut self, paths: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.paths = paths.into_iter().map(Into::into).collect();
self
}
pub fn with_fs(mut self, filesystem: impl SourceFs + 'static) -> Self {
self.fs = Some(Arc::new(filesystem));
self
}
pub fn with_shared_fs(mut self, filesystem: SharedFs) -> Self {
self.fs = Some(filesystem);
self
}
pub fn with_root(mut self, root: impl Into<String>) -> Self {
self.root = Some(root.into());
self
}
fn filesystem(&self, options: &MultiSourceOptions) -> SharedFs {
self.fs
.clone()
.or_else(|| options.fs.clone())
.unwrap_or_else(|| Arc::new(OsFs))
}
}
fn is_relative_ref(reference: &str) -> bool {
reference == "."
|| reference == ".."
|| reference.starts_with("./")
|| reference.starts_with("../")
|| reference.starts_with(".\\")
|| reference.starts_with("..\\")
}
fn ancestors(filesystem: &dyn SourceFs, dir: &str) -> Vec<String> {
if dir.is_empty() {
return Vec::new();
}
let mut dirs = Vec::new();
let mut current = dir.to_string();
loop {
dirs.push(current.clone());
let parent = filesystem.dir(¤t);
if parent == current {
break;
}
current = parent;
if dirs.len() > 64 {
break;
}
}
dirs
}
impl Resolver for PkgResolver {
fn resolve(&self, reference: Option<&str>, input: &ResolverInput<'_>) -> Resolution {
let filesystem = self.filesystem(input.options);
let root = Confinement::new(filesystem.as_ref(), self.root.as_ref());
let os_paths = filesystem.native_paths();
let is_file = |path: &str| filesystem.read_file(path).is_some();
let dir = |path: &str| filesystem.dir(path);
let base = base_for(input, &is_file, &dir);
let spec = resolve_path_spec(reference, &base);
let Some(path) = spec.path.clone() else {
return Resolution::not_found(spec);
};
if path.is_empty() {
return Resolution::not_found(spec);
}
let load = |candidate: &str| -> Option<String> {
if !root.allows(filesystem.as_ref(), candidate) {
return None;
}
filesystem.read_file(candidate)
};
if is_relative_ref(&path) {
let Some(full) = spec.full.clone() else {
return Resolution::not_found(spec);
};
let full = filesystem.canon(&full);
let potentials = if ext_kind(Some(&full)) == NONE {
build_potentials(&full, &input.options.implicit_ext)
} else {
vec![full]
};
for candidate in &potentials {
if let Some(src) = load(candidate) {
return Resolution::found(spec, candidate, src).with_search(potentials.clone());
}
}
return Resolution::not_found(spec).with_search(potentials);
}
let roots: Vec<String> = if !self.paths.is_empty() {
self.paths.clone()
} else if os_paths {
std::env::current_dir()
.map(|cwd| vec![cwd.to_string_lossy().into_owned()])
.unwrap_or_default()
} else {
vec![".".to_string()]
};
let mut seen: Vec<String> = Vec::new();
let mut search: Vec<String> = Vec::new();
for root in &roots {
for folder in ancestors(filesystem.as_ref(), &filesystem.canon(root)) {
let node_modules = filesystem.join(&[&folder, "node_modules"]);
if seen.contains(&node_modules) {
continue;
}
seen.push(node_modules.clone());
if let Some((full, src)) = resolve_in_pkg_dir(
filesystem.as_ref(),
&load,
&node_modules,
&path,
&input.options.implicit_ext,
&mut search,
) {
return Resolution::found(spec, full, src).with_search(search);
}
}
}
Resolution::not_found(spec).with_search(search)
}
}
fn resolve_in_pkg_dir(
filesystem: &dyn SourceFs,
load: &dyn Fn(&str) -> Option<String>,
node_modules: &str,
reference: &str,
implicit_ext: &[String],
search: &mut Vec<String>,
) -> Option<(String, String)> {
let target = filesystem.join(&[node_modules, reference]);
let (files, indexes): (Vec<String>, Vec<String>) = build_potentials(&target, implicit_ext)
.into_iter()
.partition(|candidate| !is_index_of(candidate, &target));
for candidate in &files {
search.push(candidate.clone());
if let Some(src) = load(candidate) {
return Some((candidate.clone(), src));
}
}
if let Some(main) = manifest_main(load, filesystem, &target, search) {
let main_path = filesystem.join(&[&target, &main]);
for candidate in build_potentials(&main_path, implicit_ext) {
search.push(candidate.clone());
if let Some(src) = load(&candidate) {
return Some((candidate, src));
}
}
}
for candidate in &indexes {
search.push(candidate.clone());
if let Some(src) = load(candidate) {
return Some((candidate.clone(), src));
}
}
None
}
fn is_index_of(candidate: &str, target: &str) -> bool {
candidate
.strip_prefix(target)
.and_then(|rest| rest.strip_prefix(['/', '\\']))
.is_some_and(|name| name.starts_with("index."))
}
fn manifest_main(
load: &dyn Fn(&str) -> Option<String>,
filesystem: &dyn SourceFs,
target: &str,
search: &mut Vec<String>,
) -> Option<String> {
let manifest = filesystem.join(&[target, "package.json"]);
search.push(manifest.clone());
let text = load(&manifest)?;
let main = serde_json::from_str::<serde_json::Value>(&text)
.ok()
.and_then(|value| {
value
.get("main")
.and_then(serde_json::Value::as_str)
.map(str::to_string)
})?;
if main.is_empty() {
return None;
}
Some(main)
}