Skip to content

Commit

Permalink
feat(stats): add module reason locations and module trace dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder committed Jan 20, 2025
1 parent 1d58294 commit c30e6a6
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 18 deletions.
8 changes: 8 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1043,11 +1043,19 @@ export interface JsStatsModuleReason {
moduleChunks?: number
type?: string
userRequest?: string
explanation?: string
active: boolean
loc?: string
}

export interface JsStatsModuleTrace {
origin: JsStatsModuleTraceModule
module: JsStatsModuleTraceModule
dependencies: Array<JsStatsModuleTraceDependency>
}

export interface JsStatsModuleTraceDependency {
loc: string
}

export interface JsStatsModuleTraceModule {
Expand Down
19 changes: 19 additions & 0 deletions crates/rspack_binding_values/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,15 @@ impl From<rspack_core::StatsWarning<'_>> for JsStatsWarning {
pub struct JsStatsModuleTrace {
pub origin: JsStatsModuleTraceModule,
pub module: JsStatsModuleTraceModule,
pub dependencies: Vec<JsStatsModuleTraceDependency>,
}

impl From<rspack_core::StatsModuleTrace> for JsStatsModuleTrace {
fn from(stats: rspack_core::StatsModuleTrace) -> Self {
Self {
origin: stats.origin.into(),
module: stats.module.into(),
dependencies: stats.dependencies.into_iter().map(Into::into).collect(),
}
}
}
Expand All @@ -187,6 +189,17 @@ impl From<rspack_core::StatsErrorModuleTraceModule> for JsStatsModuleTraceModule
}
}

#[napi(object, object_from_js = false)]
pub struct JsStatsModuleTraceDependency {
pub loc: String,
}

impl From<rspack_core::StatsErrorModuleTraceDependency> for JsStatsModuleTraceDependency {
fn from(stats: rspack_core::StatsErrorModuleTraceDependency) -> Self {
Self { loc: stats.loc }
}
}

#[napi(object, object_from_js = false)]
pub struct JsStatsLogging {
pub name: String,
Expand Down Expand Up @@ -673,6 +686,9 @@ pub struct JsStatsModuleReason {
pub module_chunks: Option<u32>,
pub r#type: Option<&'static str>,
pub user_request: Option<String>,
pub explanation: Option<&'static str>,
pub active: bool,
pub loc: Option<String>,
}

impl From<rspack_core::StatsModuleReason<'_>> for JsStatsModuleReason {
Expand All @@ -697,6 +713,9 @@ impl From<rspack_core::StatsModuleReason<'_>> for JsStatsModuleReason {
module_chunks: stats.module_chunks,
r#type: stats.r#type,
user_request: stats.user_request.map(|i| i.to_owned()),
explanation: stats.explanation,
active: stats.active,
loc: stats.loc,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/module_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub type ImportVarMap =
#[derive(Debug)]
pub struct DependencyExtraMeta {
pub ids: Vec<Atom>,
pub explanation: Option<&'static str>,
}

#[derive(Debug, Default, Clone)]
Expand Down
7 changes: 7 additions & 0 deletions crates/rspack_core/src/stats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,10 @@ impl Stats<'_> {
} else {
(None, None)
};
let loc = dependency.and_then(|d| d.loc()).map(|l| l.to_string());
let explanation = module_graph
.get_dep_meta_if_existing(&connection.dependency_id)
.and_then(|extra| extra.explanation);
Some(StatsModuleReason {
module_identifier: connection.original_module_identifier,
module_name,
Expand All @@ -967,6 +971,9 @@ impl Stats<'_> {
resolved_module_id: resolved_module_id.and_then(|i| i),
r#type,
user_request,
explanation,
active: connection.active,
loc,
})
})
.collect();
Expand Down
9 changes: 9 additions & 0 deletions crates/rspack_core/src/stats/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub struct StatsWarning<'s> {
pub struct StatsModuleTrace {
pub origin: StatsErrorModuleTraceModule,
pub module: StatsErrorModuleTraceModule,
pub dependencies: Vec<StatsErrorModuleTraceDependency>,
}

#[derive(Debug)]
Expand All @@ -87,6 +88,11 @@ pub struct StatsErrorModuleTraceModule {
pub id: Option<String>,
}

#[derive(Debug)]
pub struct StatsErrorModuleTraceDependency {
pub loc: String,
}

#[derive(Debug)]
pub struct StatsAsset {
pub r#type: &'static str,
Expand Down Expand Up @@ -267,6 +273,9 @@ pub struct StatsModuleReason<'s> {

pub r#type: Option<&'static str>,
pub user_request: Option<&'s str>,
pub explanation: Option<&'static str>,
pub active: bool,
pub loc: Option<String>,
}

#[derive(Debug)]
Expand Down
14 changes: 13 additions & 1 deletion crates/rspack_core/src/stats/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use rspack_collections::Identifier;
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};

use super::{Stats, StatsChunkGroup, StatsErrorModuleTraceModule, StatsModule, StatsModuleTrace};
use super::{
Stats, StatsChunkGroup, StatsErrorModuleTraceDependency, StatsErrorModuleTraceModule,
StatsModule, StatsModuleTrace,
};
use crate::{
BoxModule, Chunk, ChunkByUkey, ChunkGraph, ChunkGroupByUkey, ChunkGroupOrderKey, ChunkGroupUkey,
Compilation, CompilerOptions, ModuleGraph,
Expand Down Expand Up @@ -189,10 +192,19 @@ pub fn get_module_trace(
)
.map(|s| s.to_string()),
};
let dependencies = module_graph
.get_incoming_connections(&module_identifier)
.filter_map(|c| {
let dep = module_graph.dependency_by_id(&c.dependency_id)?;
let loc = dep.loc().map(|loc| loc.to_string())?;
Some(StatsErrorModuleTraceDependency { loc })
})
.collect::<Vec<_>>();

module_trace.push(StatsModuleTrace {
origin: origin_stats_module,
module: current_stats_module,
dependencies,
});

current_module_identifier = Some(origin_module.identifier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,13 @@ fn do_optimize_connection(
need_move_target,
} = do_optimize;
module_graph.do_update_module(&dependency, &target_module);
module_graph.set_dependency_extra_meta(dependency, DependencyExtraMeta { ids });
module_graph.set_dependency_extra_meta(
dependency,
DependencyExtraMeta {
ids,
explanation: Some("(skipped side-effect-free modules)"),
},
);
if let Some(SideEffectsDoOptimizeMoveTarget {
export_info,
target_export,
Expand Down
Loading

0 comments on commit c30e6a6

Please sign in to comment.