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
use std::{
fmt,
path::{Path, PathBuf},
sync::Arc,
};
use crate::PackageJson;
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ModuleType {
Module,
CommonJs,
Json,
Wasm,
Addon,
}
/// The final path resolution with optional `?query` and `#fragment`
#[derive(Clone)]
pub struct Resolution {
pub(crate) path: PathBuf,
/// Path query `?query`, contains `?`.
pub(crate) query: Option<String>,
/// Path fragment `#query`, contains `#`.
pub(crate) fragment: Option<String>,
/// `package.json` for the given module.
pub(crate) package_json: Option<Arc<PackageJson>>,
/// Module type for this path.
///
/// Enable with [crate::ResolveOptions::module_type].
///
/// The module type is computed `ESM_FILE_FORMAT` from the [ESM resolution algorithm specification](https://nodejs.org/docs/latest/api/esm.html#resolution-algorithm-specification).
///
/// The algorithm uses the file extension or finds the closest `package.json` with the `type` field.
pub(crate) module_type: Option<ModuleType>,
}
impl fmt::Debug for Resolution {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Resolution")
.field("path", &self.path)
.field("query", &self.query)
.field("fragment", &self.fragment)
.field("module_type", &self.module_type)
.field("package_json", &self.package_json.as_ref().map(|p| p.path()))
.finish()
}
}
impl PartialEq for Resolution {
fn eq(&self, other: &Self) -> bool {
self.path == other.path && self.query == other.query && self.fragment == other.fragment
}
}
impl Eq for Resolution {}
impl Resolution {
/// Returns the path without query and fragment
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
/// Returns the path without query and fragment
#[must_use]
pub fn into_path_buf(self) -> PathBuf {
self.path
}
/// Returns the path query `?query`, contains the leading `?`
#[must_use]
pub fn query(&self) -> Option<&str> {
self.query.as_deref()
}
/// Returns the path fragment `#fragment`, contains the leading `#`
#[must_use]
pub fn fragment(&self) -> Option<&str> {
self.fragment.as_deref()
}
/// Returns serialized package_json
#[must_use]
pub fn package_json(&self) -> Option<&Arc<PackageJson>> {
self.package_json.as_ref()
}
/// Returns the full path with query and fragment
#[must_use]
pub fn full_path(&self) -> PathBuf {
let mut path = self.path.clone().into_os_string();
if let Some(query) = &self.query {
path.push(query);
}
if let Some(fragment) = &self.fragment {
path.push(fragment);
}
PathBuf::from(path)
}
/// Returns the module type of this path.
#[must_use]
pub fn module_type(&self) -> Option<ModuleType> {
self.module_type
}
}