Skip to content

Commit

Permalink
feat: add stats.modules config to generate modules with deps and depe…
Browse files Browse the repository at this point in the history
…ndents (#1202)
  • Loading branch information
sorrycc authored May 23, 2024
1 parent 6190de2 commit 56306fc
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 34 deletions.
9 changes: 7 additions & 2 deletions crates/mako/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ create_deserialize_fn!(deserialize_minifish, MinifishConfig);
create_deserialize_fn!(deserialize_inline_css, InlineCssConfig);
create_deserialize_fn!(deserialize_rsc_client, RscClientConfig);
create_deserialize_fn!(deserialize_rsc_server, RscServerConfig);
create_deserialize_fn!(deserialize_stats, StatsConfig);

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -186,6 +187,11 @@ pub enum ModuleIdStrategy {
Named,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct StatsConfig {
pub modules: bool,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub enum CodeSplittingStrategy {
#[serde(rename = "auto")]
Expand Down Expand Up @@ -430,7 +436,7 @@ pub struct Config {
pub platform: Platform,
pub module_id_strategy: ModuleIdStrategy,
pub define: HashMap<String, Value>,
pub stats: bool,
pub stats: Option<StatsConfig>,
pub mdx: bool,
#[serde(deserialize_with = "deserialize_hmr")]
pub hmr: Option<HmrConfig>,
Expand Down Expand Up @@ -612,7 +618,6 @@ const DEFAULT_CONFIG: &str = r#"
"targets": { "chrome": 80 },
"less": { "theme": {}, "lesscPath": "", javascriptEnabled: true },
"define": {},
"stats": false,
"mdx": false,
"platform": "browser",
"hmr": { "host": "127.0.0.1", "port": 3000 },
Expand Down
18 changes: 10 additions & 8 deletions crates/mako/src/features/rsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::ast::file::File;
use crate::ast::js_ast::JsAst;
use crate::build::parse::ParseError;
use crate::compiler::Context;
use crate::config::{Config, LogServerComponent, ModuleIdStrategy};
use crate::config::{Config, LogServerComponent};
use crate::module::{ModuleAst, ModuleId};

#[derive(Serialize, Debug, Clone)]
Expand All @@ -18,8 +18,10 @@ pub struct RscClientInfo {
}

#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RscCssModules {
pub path: String,
pub module_id: String,
pub modules: bool,
}

Expand Down Expand Up @@ -57,13 +59,11 @@ impl Rsc {
}

fn generate_client(file: &File, tpl: &str, context: Arc<Context>) -> ModuleAst {
let path = if matches!(context.config.module_id_strategy, ModuleIdStrategy::Hashed) {
let id = ModuleId::new(file.path.to_string_lossy().to_string());
id.generate(&context)
} else {
file.relative_path.to_string_lossy().to_string()
};
let content = tpl.replace("{{path}}", path.as_str());
let id = ModuleId::new(file.path.to_string_lossy().to_string()).generate(&context);
let path = file.relative_path.to_string_lossy().to_string();
let content = tpl
.replace("{{path}}", path.as_str())
.replace("{{id}}", id.as_str());
ModuleAst::Script(
JsAst::build(file.path.to_str().unwrap(), &content, context.clone()).unwrap(),
)
Expand Down Expand Up @@ -91,8 +91,10 @@ impl Rsc {

fn emit_css(file: &File, context: Arc<Context>) {
let stats_info = &context.stats_info;
let module_id = ModuleId::from_path(file.path.clone()).generate(&context);
stats_info.add_rsc_css_module(RscCssModules {
path: file.relative_path.to_string_lossy().to_string(),
module_id,
modules: file.is_css() && file.has_param("modules"),
});
}
Expand Down
24 changes: 22 additions & 2 deletions crates/mako/src/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ impl Compiler {
debug!("generate");
let t_generate = Instant::now();

if self
.context
.config
.stats
.as_ref()
.is_some_and(|s| s.modules)
{
self.context.stats_info.parse_modules(self.context.clone());
}

debug!("tree_shaking");
let t_tree_shaking = Instant::now();

Expand Down Expand Up @@ -164,7 +174,7 @@ impl Compiler {

// generate stats
let stats = create_stats_info(0, self);
if self.context.config.stats {
if self.context.config.stats.is_some() {
write_stats(&stats, self);
}

Expand Down Expand Up @@ -248,6 +258,16 @@ impl Compiler {

let t_generate = Instant::now();

if self
.context
.config
.stats
.as_ref()
.is_some_and(|s| s.modules)
{
self.context.stats_info.parse_modules(self.context.clone());
}

// ensure output dir exists
let config = &self.context.config;
if !config.output.path.exists() {
Expand Down Expand Up @@ -283,7 +303,7 @@ impl Compiler {
// TODO: do not write to fs, using jsapi hooks to pass stats
// why generate stats?
// ref: https://github.com/umijs/mako/issues/1107
if self.context.config.stats {
if self.context.config.stats.is_some() {
let stats = create_stats_info(0, self);
write_stats(&stats, self);
}
Expand Down
76 changes: 63 additions & 13 deletions crates/mako/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Mutex;
use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};

use mako_core::anyhow::Result;
Expand All @@ -14,7 +14,7 @@ use mako_core::pathdiff::diff_paths;
use mako_core::serde::Serialize;
use swc_core::common::source_map::Pos;

use crate::compiler::Compiler;
use crate::compiler::{Compiler, Context};
use crate::features::rsc::{RscClientInfo, RscCssModules};
use crate::generate::chunk::ChunkType;

Expand All @@ -40,11 +40,20 @@ impl PartialOrd for AssetsInfo {
Some(self.cmp(other))
}
}

#[derive(Serialize, Debug, Clone)]
pub struct ModuleInfo {
pub id: String,
pub dependencies: Vec<String>,
pub dependents: Vec<String>,
}

#[derive(Debug)]
pub struct StatsInfo {
pub assets: Mutex<Vec<AssetsInfo>>,
pub rsc_client_components: Mutex<Vec<RscClientInfo>>,
pub rsc_css_modules: Mutex<Vec<RscCssModules>>,
pub modules: Mutex<HashMap<String, ModuleInfo>>,
}

impl StatsInfo {
Expand All @@ -53,6 +62,7 @@ impl StatsInfo {
assets: Mutex::new(vec![]),
rsc_client_components: Mutex::new(vec![]),
rsc_css_modules: Mutex::new(vec![]),
modules: Mutex::new(HashMap::new()),
}
}

Expand All @@ -64,7 +74,8 @@ impl StatsInfo {
path: PathBuf,
hashname: String,
) {
self.assets.lock().unwrap().push(AssetsInfo {
let mut assets = self.assets.lock().unwrap();
assets.push(AssetsInfo {
assets_type: "asset".to_string(),
size,
name,
Expand All @@ -82,6 +93,36 @@ impl StatsInfo {
self.assets.lock().unwrap().iter().cloned().collect()
}

pub fn parse_modules(&self, context: Arc<Context>) {
let module_graph = context.module_graph.read().unwrap();
let mut modules = self.modules.lock().unwrap();
module_graph.modules().iter().for_each(|module| {
let dependencies = module_graph
.get_dependencies(&module.id)
.iter()
.map(|(id, _dep)| id.generate(&context))
.collect::<Vec<_>>();
let dependents = module_graph
.get_dependents(&module.id)
.iter()
.map(|(id, _dep)| id.generate(&context))
.collect::<Vec<_>>();
let id = module.id.generate(&context);
modules.insert(
id.clone(),
ModuleInfo {
id,
dependencies,
dependents,
},
);
});
}

pub fn get_modules(&self) -> HashMap<String, ModuleInfo> {
self.modules.lock().unwrap().clone()
}

pub fn get_rsc_client_components(&self) -> Vec<RscClientInfo> {
self.rsc_client_components.lock().unwrap().clone()
}
Expand Down Expand Up @@ -129,12 +170,19 @@ pub struct StatsJsonAssetsItem {

#[derive(Serialize, Clone, Debug)]
pub struct StatsJsonModuleItem {
pub id: String,
pub deps: Vec<String>,
}

#[derive(Serialize, Clone, Debug)]
pub struct StatsJsonChunkModuleItem {
#[serde(flatten)]
pub module_type: StatsJsonType,
pub size: u64,
pub id: String,
pub chunks: Vec<String>,
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct StatsJsonChunkOriginItem {
Expand All @@ -151,7 +199,7 @@ pub struct StatsJsonChunkItem {
pub id: String,
pub files: Vec<String>,
pub entry: bool,
pub modules: Vec<StatsJsonModuleItem>,
pub modules: Vec<StatsJsonChunkModuleItem>,
pub siblings: Vec<String>,
pub origins: Vec<StatsJsonChunkOriginItem>,
}
Expand All @@ -169,7 +217,8 @@ pub struct StatsJsonMap {
root_path: PathBuf,
output_path: PathBuf,
assets: Vec<StatsJsonAssetsItem>,
modules: Vec<StatsJsonModuleItem>,
chunk_modules: Vec<StatsJsonChunkModuleItem>,
modules: HashMap<String, ModuleInfo>,
chunks: Vec<StatsJsonChunkItem>,
entrypoints: HashMap<String, StatsJsonEntryItem>,
rsc_client_components: Vec<RscClientInfo>,
Expand All @@ -186,7 +235,8 @@ impl StatsJsonMap {
root_path: PathBuf::new(),
output_path: PathBuf::new(),
assets: vec![],
modules: vec![],
modules: HashMap::new(),
chunk_modules: vec![],
chunks: vec![],
entrypoints: HashMap::new(),
rsc_client_components: vec![],
Expand Down Expand Up @@ -253,7 +303,7 @@ pub fn create_stats_info(compile_time: u128, compiler: &Compiler) -> StatsJsonMa
let chunks = chunk_graph.get_chunks();

// 在 chunks 中获取 modules
let modules_vec: Rc<RefCell<Vec<StatsJsonModuleItem>>> = Rc::new(RefCell::new(Vec::new()));
let modules_vec: Rc<RefCell<Vec<StatsJsonChunkModuleItem>>> = Rc::new(RefCell::new(Vec::new()));

// 获取 chunks
stats_map.chunks = chunks
Expand All @@ -262,7 +312,7 @@ pub fn create_stats_info(compile_time: u128, compiler: &Compiler) -> StatsJsonMa
let modules = chunk.get_modules();
let entry = matches!(chunk.chunk_type, ChunkType::Entry(_, _, _));
let id = chunk.id.id.clone();
let chunk_modules: Vec<StatsJsonModuleItem> = modules
let chunk_modules: Vec<StatsJsonChunkModuleItem> = modules
.iter()
.filter(|module| {
// ?modules 是虚拟模块,暂不记录
Expand All @@ -277,7 +327,7 @@ pub fn create_stats_info(compile_time: u128, compiler: &Compiler) -> StatsJsonMa
Ok(size) => size,
Err(..) => 0,
};
let module = StatsJsonModuleItem {
let module = StatsJsonChunkModuleItem {
module_type: StatsJsonType::Module("module".to_string()),
size,
id,
Expand Down Expand Up @@ -378,11 +428,11 @@ pub fn create_stats_info(compile_time: u128, compiler: &Compiler) -> StatsJsonMa
_ => None,
})
.collect::<HashMap<_, _>>();
let chunk_modules: Vec<StatsJsonChunkModuleItem> =
modules_vec.borrow().iter().cloned().collect();
stats_map.chunk_modules = chunk_modules;

// 获取 modules
let modules: Vec<StatsJsonModuleItem> = modules_vec.borrow().iter().cloned().collect();
stats_map.modules = modules;

stats_map.modules = stats_info.get_modules();
stats_map.rsc_client_components = stats_info.get_rsc_client_components();
stats_map.rsc_css_modules = stats_info.get_rsc_css_modules();

Expand Down
11 changes: 10 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,22 @@ Configuration related to RSC client.

Configuration related to RSC server.

Child configuration items:

- `clientComponentTpl`, client component template, use `{{path}}` to represent the path of the component, and use `{{id}}` to represent the id of the module.
- `emitCSS`, whether to output CSS components.

### stats

- Type: `boolean`
- Type: `{ modules: bool } | false`
- Default: `false`

Whether to generate stats.json file.

Child configuration items:

- `modules`, whether to generate module information, it may be useful when you want to analyze the size of the module but may slow down the build speed.

### transformImport

- Type: `false | { libraryName: string, libraryDirectory: string, style: boolean }`
Expand Down
4 changes: 3 additions & 1 deletion e2e/fixtures/rsc.client.dynamic_client/mako.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
"moduleIdStrategy": "named",
"hash": true,
"minify": false,
"stats": true
"stats": {
"modules": false
}
}
4 changes: 3 additions & 1 deletion e2e/fixtures/rsc.server/mako.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"emitCSS": true
},
"minify": false,
"stats": true,
"stats": {
"modules": true
},
"optimization": {
"concatenateModules": false
}
Expand Down
Loading

0 comments on commit 56306fc

Please sign in to comment.