From 15eb40a16ad22295f3a15fba2deec6633bab2ff7 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Wed, 27 Dec 2023 19:05:24 +0800 Subject: [PATCH] refactor: stats still follow webpack specs --- crates/mako/src/stats.rs | 54 ++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/crates/mako/src/stats.rs b/crates/mako/src/stats.rs index c736ea899..7e65f9f54 100644 --- a/crates/mako/src/stats.rs +++ b/crates/mako/src/stats.rs @@ -1,5 +1,6 @@ use std::cell::RefCell; use std::cmp::Ordering; +use std::collections::HashMap; use std::fs; use std::path::PathBuf; use std::rc::Rc; @@ -67,25 +68,20 @@ pub struct StatsJsonModuleItem { pub chunk_id: String, } #[derive(Serialize, Debug)] -#[serde(untagged)] -pub enum StatsJsonChunkEntry { - /** - * only can be `false`, internal logic so omit deserialize fn - */ - False(bool), - Name(String), -} -#[derive(Serialize, Debug)] pub struct StatsJsonChunkItem { #[serde(flatten)] pub chunk_type: StatsJsonType, pub chunk_id: String, - pub dependencies: Vec, pub files: Vec, - pub entry: StatsJsonChunkEntry, + pub entry: bool, pub modules: Vec, } #[derive(Serialize, Debug)] +pub struct StatsJsonEntryItem { + pub name: String, + pub chunks: Vec, +} +#[derive(Serialize, Debug)] pub struct StatsJsonMap { hash: u64, time: u128, @@ -95,6 +91,7 @@ pub struct StatsJsonMap { assets: Vec, modules: Vec, chunks: Vec, + entrypoints: HashMap, } impl StatsJsonMap { @@ -108,6 +105,7 @@ impl StatsJsonMap { assets: vec![], modules: vec![], chunks: vec![], + entrypoints: HashMap::new(), } } } @@ -206,10 +204,7 @@ pub fn create_stats_info(compile_time: u128, compiler: &Compiler) -> StatsJsonMa .iter() .map(|chunk| { let modules = chunk.get_modules(); - let entry = match &chunk.chunk_type { - ChunkType::Entry(_, name, _) => StatsJsonChunkEntry::Name(name.clone()), - _ => StatsJsonChunkEntry::False(false), - }; + let entry = matches!(chunk.chunk_type, ChunkType::Entry(_, _, _)); let id = chunk.id.id.clone(); let chunk_modules: Vec = modules .iter() @@ -245,22 +240,39 @@ pub fn create_stats_info(compile_time: u128, compiler: &Compiler) -> StatsJsonMa .filter(|asset| asset.chunk_id == id) .map(|asset| asset.name.clone()) .collect(); - let dependencies = chunk_graph - .entry_dependencies_chunk(&chunk.id) - .into_iter() - .map(|id| id.id) - .collect::>(); StatsJsonChunkItem { chunk_type: StatsJsonType::Chunk("chunk".to_string()), chunk_id: id, - dependencies, files, entry, modules: chunk_modules, } }) .collect(); + stats_map.entrypoints = chunks + .iter() + .filter_map(|chunk| match &chunk.chunk_type { + ChunkType::Entry(_, name, _) => { + let mut chunks = chunk_graph + .entry_dependencies_chunk(&chunk.id) + .into_iter() + .map(|id| id.id) + .collect::>(); + + chunks.push(chunk.id.id.clone()); + + Some(( + name.clone(), + StatsJsonEntryItem { + name: name.clone(), + chunks, + }, + )) + } + _ => None, + }) + .collect::>(); // 获取 modules let modules: Vec = modules_vec.borrow().iter().cloned().collect();