1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::{Ctx, Error, Resolver, Result};
use relative_path::{RelativePath, RelativePathBuf};
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "loader")))]
#[derive(Debug)]
pub struct FileResolver {
paths: Vec<RelativePathBuf>,
patterns: Vec<String>,
}
impl FileResolver {
pub fn add_path<P: Into<RelativePathBuf>>(&mut self, path: P) -> &mut Self {
self.paths.push(path.into());
self
}
#[must_use]
pub fn with_path<P: Into<RelativePathBuf>>(mut self, path: P) -> Self {
self.add_path(path);
self
}
pub fn add_paths<I: IntoIterator<Item = P>, P: Into<RelativePathBuf>>(
&mut self,
paths: I,
) -> &mut Self {
self.paths.extend(paths.into_iter().map(|path| path.into()));
self
}
#[must_use]
pub fn with_paths<I: IntoIterator<Item = P>, P: Into<RelativePathBuf>>(
mut self,
paths: I,
) -> Self {
self.add_paths(paths);
self
}
pub fn add_pattern<P: Into<String>>(&mut self, pattern: P) -> &mut Self {
self.patterns.push(pattern.into());
self
}
#[must_use]
pub fn with_pattern<P: Into<String>>(mut self, pattern: P) -> Self {
self.add_pattern(pattern);
self
}
pub fn add_native(&mut self) -> &mut Self {
#[cfg(target_family = "windows")]
self.add_pattern("{}.dll");
#[cfg(target_vendor = "apple")]
self.add_pattern("{}.dylib").add_pattern("lib{}.dylib");
#[cfg(all(target_family = "unix"))]
self.add_pattern("{}.so").add_pattern("lib{}.so");
self
}
#[must_use]
pub fn with_native(mut self) -> Self {
self.add_native();
self
}
fn try_patterns(&self, path: &RelativePath) -> Option<RelativePathBuf> {
if let Some(extension) = &path.extension() {
if !is_file(path) {
return None;
}
self.patterns
.iter()
.find(|pattern| {
let path = RelativePath::new(pattern);
if let Some(known_extension) = &path.extension() {
known_extension == extension
} else {
false
}
})
.map(|_| path.to_relative_path_buf())
} else {
self.patterns
.iter()
.filter_map(|pattern| {
let name = pattern.replace("{}", path.file_name()?);
let file = path.with_file_name(&name);
if is_file(&file) {
Some(file)
} else {
None
}
})
.next()
}
}
}
impl Default for FileResolver {
fn default() -> Self {
Self {
paths: vec![],
patterns: vec!["{}.js".into()],
}
}
}
impl Resolver for FileResolver {
fn resolve<'js>(&mut self, _ctx: Ctx<'js>, base: &str, name: &str) -> Result<String> {
let path = if !name.starts_with('.') {
self.paths
.iter()
.filter_map(|path| {
let path = path.join_normalized(name);
self.try_patterns(&path)
})
.next()
} else {
let path = RelativePath::new(base);
let path = if let Some(dir) = path.parent() {
dir.join_normalized(name)
} else {
name.into()
};
self.try_patterns(&path)
}
.ok_or_else(|| Error::new_resolving(base, name))?;
Ok(path.to_string())
}
}
fn is_file<P: AsRef<RelativePath>>(path: P) -> bool {
path.as_ref().to_path(".").is_file()
}