Skip to content

Commit c1ef7bc

Browse files
authored
refactor(rspack_loader_runner): remove nodejs_resolver dependency (#3982)
1 parent 02062a8 commit c1ef7bc

File tree

8 files changed

+60
-42
lines changed

8 files changed

+60
-42
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ json = { version = "0.12.4" }
2626
linked_hash_set = { version = "0.1.4" }
2727
mimalloc-rust = { version = "0.2" }
2828
mime_guess = { version = "2.0.4" }
29-
nodejs-resolver = { version = "0.0.88" }
29+
nodejs-resolver = { version = "0.1.0" }
3030
once_cell = { version = "1.18.0" }
3131
paste = { version = "1.0" }
3232
pathdiff = { version = "0.2.1" }

crates/rspack_core/src/normal_module_factory.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rspack_error::{
66
internal_error, Diagnostic, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray,
77
};
88
use rspack_identifier::Identifiable;
9-
use rspack_loader_runner::{get_scheme, Scheme};
9+
use rspack_loader_runner::{get_scheme, DescriptionData, Scheme};
1010
use sugar_path::{AsPath, SugarPath};
1111
use swc_core::common::Span;
1212

@@ -318,10 +318,13 @@ impl NormalModuleFactory {
318318
match resource_data {
319319
Ok(ResolveResult::Resource(resource)) => {
320320
let uri = resource.join().display().to_string();
321+
let description_data = resource.description.map(|d| {
322+
DescriptionData::new(d.dir().as_ref().to_path_buf(), Arc::clone(d.data().raw()))
323+
});
321324
ResourceData::new(uri, resource.path)
322325
.query_optional(resource.query)
323326
.fragment_optional(resource.fragment)
324-
.description_optional(resource.description)
327+
.description_optional(description_data)
325328
}
326329
Ok(ResolveResult::Ignored) => {
327330
let ident = format!("{}/{}", &data.context, request_without_match_resource);
@@ -620,8 +623,8 @@ impl NormalModuleFactory {
620623
}
621624
let resource_path = &resource_data.resource_path;
622625
let description = resource_data.resource_description.as_ref()?;
623-
let package_path = description.dir().as_ref();
624-
let side_effects = SideEffects::from_description(description)?;
626+
let package_path = description.path();
627+
let side_effects = SideEffects::from_description(description.json())?;
625628

626629
let relative_path = resource_path.relative(package_path);
627630
side_effect_res = Some(get_side_effects_from_package_json(

crates/rspack_core/src/tree_shaking/visitor.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,29 +1815,25 @@ pub enum SideEffects {
18151815
}
18161816

18171817
impl SideEffects {
1818-
pub fn from_description(description: &nodejs_resolver::DescriptionData) -> Option<Self> {
1819-
description
1820-
.data()
1821-
.raw()
1822-
.get("sideEffects")
1823-
.and_then(|value| {
1824-
if let Some(b) = value.as_bool() {
1825-
Some(SideEffects::Bool(b))
1826-
} else if let Some(s) = value.as_str() {
1827-
Some(SideEffects::String(s.to_owned()))
1828-
} else if let Some(vec) = value.as_array() {
1829-
let mut ans = vec![];
1830-
for value in vec {
1831-
if let Some(str) = value.as_str() {
1832-
ans.push(str.to_string());
1833-
} else {
1834-
return None;
1835-
}
1818+
pub fn from_description(description: &serde_json::Value) -> Option<Self> {
1819+
description.get("sideEffects").and_then(|value| {
1820+
if let Some(b) = value.as_bool() {
1821+
Some(SideEffects::Bool(b))
1822+
} else if let Some(s) = value.as_str() {
1823+
Some(SideEffects::String(s.to_owned()))
1824+
} else if let Some(vec) = value.as_array() {
1825+
let mut ans = vec![];
1826+
for value in vec {
1827+
if let Some(str) = value.as_str() {
1828+
ans.push(str.to_string());
1829+
} else {
1830+
return None;
18361831
}
1837-
Some(SideEffects::Array(ans))
1838-
} else {
1839-
None
18401832
}
1841-
})
1833+
Some(SideEffects::Array(ans))
1834+
} else {
1835+
None
1836+
}
1837+
})
18421838
}
18431839
}

crates/rspack_core/src/utils/module_rules.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,7 @@ pub async fn module_rule_matcher<'a>(
103103
if let Some(description_data) = &module_rule.description_data {
104104
if let Some(resource_description) = &resource_data.resource_description {
105105
for (k, matcher) in description_data {
106-
if let Some(v) = resource_description
107-
.data()
108-
.raw()
109-
.get(k)
110-
.and_then(|v| v.as_str())
111-
{
106+
if let Some(v) = resource_description.json().get(k).and_then(|v| v.as_str()) {
112107
if !matcher.try_match(v).await? {
113108
return Ok(false);
114109
}

crates/rspack_loader_runner/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ derivative = { workspace = true }
1818
rustc-hash = { workspace = true }
1919
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "macros", "test-util", "parking_lot", "fs"] }
2020

21-
nodejs-resolver = { workspace = true }
2221
once_cell = { workspace = true }
2322
regex = { workspace = true }
2423
rspack_error = { path = "../rspack_error" }
2524
rspack_identifier = { path = "../rspack_identifier" }
2625
rspack_sources = { workspace = true }
26+
serde_json = { workspace = true }

crates/rspack_loader_runner/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub use content::Content;
1010
pub use loader::{DisplayWithSuffix, Loader};
1111
pub use plugin::LoaderRunnerPlugin;
1212
pub use rspack_identifier::{Identifiable, Identifier};
13-
pub use runner::{run_loaders, LoaderContext, ResourceData};
13+
pub use runner::{run_loaders, DescriptionData, LoaderContext, ResourceData};
1414
pub use scheme::{get_scheme, Scheme};
1515

1616
pub const BUILTIN_LOADER_PREFIX: &str = "builtin:";

crates/rspack_loader_runner/src/runner.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::{
55
};
66

77
use derivative::Derivative;
8-
use nodejs_resolver::DescriptionData;
98
use once_cell::sync::OnceCell;
109
use rspack_error::{
1110
internal_error, Diagnostic, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray,
@@ -31,7 +30,7 @@ pub struct ResourceData {
3130
pub resource_query: Option<String>,
3231
/// Resource fragment with `#` prefix
3332
pub resource_fragment: Option<String>,
34-
pub resource_description: Option<Arc<DescriptionData>>,
33+
pub resource_description: Option<DescriptionData>,
3534
pub mimetype: Option<String>,
3635
pub parameters: Option<String>,
3736
pub encoding: Option<String>,
@@ -79,12 +78,12 @@ impl ResourceData {
7978
self
8079
}
8180

82-
pub fn description(mut self, v: Arc<DescriptionData>) -> Self {
81+
pub fn description(mut self, v: DescriptionData) -> Self {
8382
self.resource_description = Some(v);
8483
self
8584
}
8685

87-
pub fn description_optional(mut self, v: Option<Arc<DescriptionData>>) -> Self {
86+
pub fn description_optional(mut self, v: Option<DescriptionData>) -> Self {
8887
self.resource_description = v;
8988
self
9089
}
@@ -110,6 +109,31 @@ impl ResourceData {
110109
}
111110
}
112111

112+
/// Used for [Rule.descriptionData](https://www.rspack.dev/config/module.html#ruledescriptiondata) and
113+
/// package.json.sideEffects in tree shaking.
114+
#[derive(Debug, Clone)]
115+
pub struct DescriptionData {
116+
/// Path to package.json
117+
path: PathBuf,
118+
119+
/// Raw package.json
120+
json: Arc<serde_json::Value>,
121+
}
122+
123+
impl DescriptionData {
124+
pub fn new(path: PathBuf, json: Arc<serde_json::Value>) -> Self {
125+
Self { path, json }
126+
}
127+
128+
pub fn path(&self) -> &Path {
129+
&self.path
130+
}
131+
132+
pub fn json(&self) -> &serde_json::Value {
133+
self.json.as_ref()
134+
}
135+
}
136+
113137
#[derive(Derivative)]
114138
#[derivative(Debug)]
115139
pub struct LoaderContext<'c, C> {

0 commit comments

Comments
 (0)