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
use crate::map::{ExportsField, Field, ImportsField, PathTreeNode};
use crate::{AliasMap, Error, RResult, Resolver};
use std::path::{Path, PathBuf};
use std::sync::Arc;
#[derive(Clone, Debug)]
pub enum SideEffects {
Bool(bool),
Array(Vec<String>),
}
#[derive(Debug)]
pub struct PkgJSON {
pub name: Option<String>,
pub version: Option<String>,
pub alias_fields: Vec<(String, AliasMap)>,
pub exports_field_tree: Option<PathTreeNode>,
pub imports_field_tree: Option<PathTreeNode>,
pub side_effects: Option<SideEffects>,
pub raw: serde_json::Value,
}
#[derive(Debug)]
pub struct PkgInfo {
pub json: Arc<PkgJSON>,
pub dir_path: PathBuf,
}
impl PkgJSON {
pub(crate) fn parse(content: &str, file_path: &Path) -> RResult<Self> {
let json: serde_json::Value =
tracing::debug_span!("serde_json_from_str").in_scope(|| {
serde_json::from_str(content)
.map_err(|error| Error::UnexpectedJson((file_path.to_path_buf(), error)))
})?;
let mut alias_fields = Vec::new();
if let Some(value) = json.get("browser") {
if let Some(map) = value.as_object() {
for (key, value) in map {
if let Some(false) = value.as_bool() {
alias_fields.push((key.to_string(), AliasMap::Ignored));
} else if let Some(s) = value.as_str() {
alias_fields.push((key.to_string(), AliasMap::Target(s.to_string())));
}
}
} else if let Some(false) = value.as_bool() {
alias_fields.push((String::from("."), AliasMap::Ignored));
} else if let Some(s) = value.as_str() {
alias_fields.push((String::from("."), AliasMap::Target(s.to_string())));
} else {
let msg = format!(
"The browser is {} which meet unhandled value, error in {}/package.json",
value,
file_path.display()
);
println!("{}", msg);
}
}
let exports_field_tree = if let Some(value) = json.get("exports") {
let tree = ExportsField::build_field_path_tree(value)?;
Some(tree)
} else {
None
};
let imports_field_tree = if let Some(value) = json.get("imports") {
let tree = ImportsField::build_field_path_tree(value)?;
Some(tree)
} else {
None
};
let name = json
.get("name")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let side_effects: Option<SideEffects> =
json.get("sideEffects").map_or(Ok(None), |value| {
if let Some(b) = value.as_bool() {
Ok(Some(SideEffects::Bool(b)))
} else if let Some(vec) = value.as_array() {
let mut ans = vec![];
for value in vec {
if let Some(str) = value.as_str() {
ans.push(str.to_string());
} else {
println!(
"warning: sideEffects in {} had unexpected value {}",
file_path.display(),
value
);
return Ok(None);
}
}
Ok(Some(SideEffects::Array(ans)))
} else {
println!(
"warning: sideEffects in {} had unexpected value {}",
file_path.display(),
value
);
Ok(None)
}
})?;
let version = json
.get("version")
.and_then(|value| value.as_str())
.map(|str| str.to_string());
Ok(Self {
name,
version,
alias_fields,
exports_field_tree,
imports_field_tree,
side_effects,
raw: json,
})
}
}
impl Resolver {
pub fn load_side_effects(
&self,
path: &Path,
) -> RResult<Option<(PathBuf, Option<SideEffects>)>> {
let entry = self.load_entry(path)?;
let ans = entry.pkg_info.as_ref().map(|pkg_info| {
(
pkg_info.dir_path.join(&self.options.description_file),
pkg_info.json.side_effects.clone(),
)
});
Ok(ans)
}
}