Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stats): add module reason locations and module trace dependencies #9049

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ chunk {909} (runtime: main) bundle.js (main) 7 bytes (asset) 159 bytes (javascri
| ./raw.png [193] 42 bytes (javascript) 7 bytes (asset) {909} [depth 1] [built] [code generated]
| [no exports]
| [no exports used]
| esm import ./raw.png ./index.js
| esm import ./raw.png ./index.js 2:0-33
./raw.png [193] 42 bytes (javascript) 7 bytes (asset) {909} [depth 1] [built] [code generated]
[no exports]
[no exports used]
esm import ./raw.png ./index.js
esm import ./raw.png ./index.js 2:0-33
modules by path ./ 235 bytes (javascript) 7 bytes (asset)
orphan modules 76 bytes [orphan]
./index.js 75 bytes [depth 0] [orphan] [built]
Expand All @@ -33,7 +33,7 @@ modules by path ./ 235 bytes (javascript) 7 bytes (asset)
[no exports]
[module unused]
ModuleConcatenation bailout: Module is not an ECMAScript module
esm import ./stringModule [686] ./index.js
esm import ./stringModule [686] ./index.js 1:0-41
code generated modules 159 bytes (javascript) 7 bytes (asset) [code generated]
./index.js + 1 modules [686] 117 bytes {909} [depth 0] [code generated]
[no exports]
Expand All @@ -46,11 +46,11 @@ modules by path ./ 235 bytes (javascript) 7 bytes (asset)
| ./raw.png [193] 42 bytes (javascript) 7 bytes (asset) {909} [depth 1] [built] [code generated]
| [no exports]
| [no exports used]
| esm import ./raw.png ./index.js
| esm import ./raw.png ./index.js 2:0-33
./raw.png [193] 42 bytes (javascript) 7 bytes (asset) {909} [depth 1] [built] [code generated]
[no exports]
[no exports used]
esm import ./raw.png ./index.js
esm import ./raw.png ./index.js 2:0-33
runtime modules 1.61 KiB
webpack/runtime/auto_public_path 1.39 KiB {909} [code generated]
[no exports]
Expand Down Expand Up @@ -374,7 +374,7 @@ exports[`new code splitting stats output new code splitting stats output/reasons
entry ./index
./a.js 55 bytes [built] [code generated]
cjs self exports reference self ./a.js
cjs require ./a ./index.js
cjs require ./a ./index.js 1:18-24
`;

exports[`new code splitting stats output new code splitting stats output/resolve-overflow-error should print correct stats for: NewCodeSplittingStatsOutput 1`] = `
Expand Down
Loading
Loading