Skip to content

Commit c105789

Browse files
committed
feat: rsdoctor native plugin get json size
1 parent 91e6be8 commit c105789

File tree

4 files changed

+176
-145
lines changed

4 files changed

+176
-145
lines changed

crates/rspack_plugin_json/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rspack_core::{
1717
PrefetchExportsInfoMode, PrefetchedExportsInfoWrapper, RuntimeGlobals, RuntimeSpec, SourceType,
1818
UsageState, UsedNameItem,
1919
diagnostics::ModuleParseError,
20-
rspack_sources::{BoxSource, RawStringSource, Source, SourceExt},
20+
rspack_sources::{BoxSource, OriginalSource, RawStringSource, Source, SourceExt},
2121
};
2222
use rspack_error::{Error, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray, error};
2323
use rspack_util::itoa;
@@ -42,6 +42,7 @@ impl ParserAndGenerator for JsonParserAndGenerator {
4242
}
4343

4444
fn size(&self, module: &dyn Module, _source_type: Option<&SourceType>) -> f64 {
45+
// dbg!(&module.build_info().json_data.as_ref());
4546
module
4647
.build_info()
4748
.json_data
@@ -229,7 +230,11 @@ impl ParserAndGenerator for JsonParserAndGenerator {
229230
.insert(RuntimeGlobals::MODULE);
230231
format!(r#"module.exports = {json_expr}"#)
231232
};
232-
Ok(RawStringSource::from(content).boxed())
233+
if module.get_source_map_kind().enabled() {
234+
Ok(OriginalSource::new(content, module.identifier().as_str()).boxed())
235+
} else {
236+
Ok(RawStringSource::from(content).boxed())
237+
}
233238
}
234239
_ => panic!(
235240
"Unsupported source type: {:?}",

crates/rspack_plugin_rsdoctor/src/data.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct RsdoctorModule {
4848
pub belong_modules: HashSet<ModuleUkey>,
4949
pub issuer_path: Option<Vec<RsdoctorStatsModuleIssuer>>,
5050
pub bailout_reason: HashSet<String>,
51+
pub size: i32,
5152
}
5253

5354
#[derive(Debug, Default)]

crates/rspack_plugin_rsdoctor/src/module_graph.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn collect_modules(
2121
module_graph: &ModuleGraph,
2222
chunk_graph: &ChunkGraph,
2323
context: &Context,
24+
compilation: &Compilation,
2425
) -> HashMap<Identifier, RsdoctorModule> {
2526
let module_ukey_counter: Arc<AtomicI32> = Arc::new(AtomicI32::new(0));
2627

@@ -54,6 +55,20 @@ pub fn collect_modules(
5455
.collect::<HashSet<_>>()
5556
})
5657
.unwrap_or_default();
58+
59+
let mut size = 0;
60+
let (map, result_map) = compilation.code_generation_results.inner();
61+
if let Some(entry) = map.get(module_id)
62+
&& let Some(id) = entry.values().next()
63+
&& let Some(res) = result_map.get(id)
64+
{
65+
size = res.inner().values().map(|s| s.size() as i32).sum();
66+
}
67+
68+
if true {
69+
println!("Module: {}, Size: {}", module.identifier(), size);
70+
}
71+
5772
(
5873
module_id.to_owned(),
5974
RsdoctorModule {
@@ -74,6 +89,7 @@ pub fn collect_modules(
7489
chunks,
7590
issuer_path: None,
7691
bailout_reason: HashSet::default(),
92+
size: 0,
7793
},
7894
)
7995
})
@@ -142,7 +158,8 @@ pub fn collect_module_original_sources(
142158
let module_ukey = module_ukeys.get(module_id)?;
143159
let resource = module.resource_resolved_data().resource().to_owned();
144160
let object_pool = tls.get_or(ObjectPool::default);
145-
let source = module
161+
// let source = module
162+
let mut source = module
146163
.source()
147164
.and_then(|s| s.map(object_pool, &MapOptions::default()))
148165
.and_then(|s| {
@@ -164,6 +181,15 @@ pub fn collect_module_original_sources(
164181
source: content,
165182
})
166183
})?;
184+
185+
let (map, result_map) = compilation.code_generation_results.inner();
186+
if let Some(entry) = map.get(module_id)
187+
&& let Some(id) = entry.values().next()
188+
&& let Some(res) = result_map.get(id)
189+
{
190+
source.size = res.inner().values().map(|s| s.size() as i32).sum();
191+
}
192+
167193
Some(source)
168194
})
169195
.collect::<Vec<_>>()

0 commit comments

Comments
 (0)