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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
mod cache;
mod context;
mod description;
mod entry;
mod error;
mod fs;
mod info;
mod kind;
mod map;
mod normalize;
mod options;
mod parse;
mod plugin;
mod resolve;
mod state;
mod tsconfig;
mod tsconfig_path;
pub use cache::Cache;
use context::Context;
pub use description::SideEffects;
use entry::Entry;
pub use error::Error;
pub use info::Info;
use kind::PathKind;
pub use options::{AliasMap, EnforceExtension, Options};
use plugin::{AliasFieldPlugin, AliasPlugin, ImportsFieldPlugin, Plugin, PreferRelativePlugin};
use state::State;
use std::{
path::{Path, PathBuf},
sync::Arc,
};
#[derive(Debug)]
pub struct Resolver {
pub options: Options,
pub(crate) cache: Arc<Cache>,
pub(crate) entries: dashmap::DashMap<(PathBuf, bool), Arc<Entry>>,
}
#[derive(Debug)]
pub enum ResolveResult {
Info(Info),
Ignored,
}
pub(crate) static MODULE: &str = "node_modules";
pub type RResult<T> = Result<T, Error>;
impl Resolver {
pub fn new(options: Options) -> Self {
let cache = if let Some(external_cache) = options.external_cache.as_ref() {
external_cache.clone()
} else {
Arc::new(Cache::default())
};
use options::EnforceExtension::*;
let enforce_extension = match options.enforce_extension {
Auto => {
if options.extensions.iter().any(|ext| ext.is_empty()) {
Enabled
} else {
Disabled
}
}
_ => options.enforce_extension,
};
let options = Options {
enforce_extension,
..options
};
let entries = Default::default();
Self {
cache,
options,
entries,
}
}
#[tracing::instrument]
pub fn resolve(&self, path: &Path, request: &str) -> RResult<ResolveResult> {
let info = Info::from(path.to_path_buf(), self.parse(request));
let mut context = Context::new();
let result = if let Some(tsconfig_location) = self.options.tsconfig.as_ref() {
self._resolve_with_tsconfig(info, tsconfig_location, &mut context)
} else {
self._resolve(info, &mut context)
};
match result {
State::Success(result) => self.normalize_result(result),
State::Error(err) => Err(err),
State::Resolving(_) | State::Failed(_) => Err(Error::ResolveFailedTag),
}
}
#[tracing::instrument]
fn _resolve(&self, info: Info, context: &mut Context) -> State {
context.depth.increase();
if context.depth.cmp(127).is_ge() {
return State::Error(Error::Overflow);
}
let state = AliasPlugin::default()
.apply(self, info, context)
.and_then(|info| PreferRelativePlugin::default().apply(self, info, context))
.and_then(|info| {
let request = if info.request.kind.eq(&PathKind::Normal) {
info.path.join(MODULE).join(&*info.request.target)
} else {
info.get_path()
};
let pkg_info = match self.load_entry(&request) {
Ok(entry) => entry.pkg_info.clone(),
Err(error) => return State::Error(error),
};
if let Some(pkg_info) = pkg_info {
ImportsFieldPlugin::new(&pkg_info)
.apply(self, info, context)
.and_then(|info| {
AliasFieldPlugin::new(&pkg_info).apply(self, info, context)
})
} else {
State::Resolving(info)
}
})
.and_then(|info| {
if matches!(
info.request.kind,
PathKind::AbsolutePosix | PathKind::AbsoluteWin | PathKind::Relative
) {
self.resolve_as_file(info)
.and_then(|info| self.resolve_as_dir(info, context))
} else {
self.resolve_as_modules(info, context)
}
});
context.depth.decrease();
state
}
}
#[cfg(debug_assertions)]
pub mod test_helper {
pub fn p(paths: Vec<&str>) -> std::path::PathBuf {
paths.iter().fold(
std::env::current_dir()
.unwrap()
.join("tests")
.join("fixtures"),
|acc, path| acc.join(path),
)
}
pub fn vec_to_set(vec: Vec<&str>) -> std::collections::HashSet<String> {
std::collections::HashSet::from_iter(vec.into_iter().map(|s| s.to_string()))
}
}