Skip to content

Commit

Permalink
feat: stats chunkRelations and chunkModules (#2096)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Mar 10, 2023
1 parent 7eb7be0 commit bb22416
Show file tree
Hide file tree
Showing 16 changed files with 446 additions and 193 deletions.
6 changes: 6 additions & 0 deletions .changeset/old-apricots-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rspack/binding": patch
"@rspack/core": patch
---

feat: stats for chunkRelations and chunkModules
29 changes: 17 additions & 12 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

/* auto-generated by NAPI-RS */

export interface ThreadsafeNodeFS {
writeFile: (...args: any[]) => any
mkdir: (...args: any[]) => any
mkdirp: (...args: any[]) => any
}
export interface NodeFS {
writeFile: (...args: any[]) => any
mkdir: (...args: any[]) => any
mkdirp: (...args: any[]) => any
}
export interface RawPattern {
from: string
to?: string
Expand Down Expand Up @@ -330,6 +340,7 @@ export interface RawCacheGroupOptions {
}
export interface RawStatsOptions {
colors: boolean
reasons: boolean
}
export interface RawOptions {
entry: Record<string, RawEntryItem>
Expand All @@ -351,16 +362,6 @@ export interface RawOptions {
experiments: RawExperiments
node: RawNodeOption
}
export interface ThreadsafeNodeFS {
writeFile: (...args: any[]) => any
mkdir: (...args: any[]) => any
mkdirp: (...args: any[]) => any
}
export interface NodeFS {
writeFile: (...args: any[]) => any
mkdir: (...args: any[]) => any
mkdirp: (...args: any[]) => any
}
export interface JsAssetInfoRelated {
sourceMap?: string
}
Expand Down Expand Up @@ -479,6 +480,10 @@ export interface JsStatsChunk {
initial: boolean
names: Array<string>
size: number
modules?: Array<JsStatsModule>
parents?: Array<string>
children?: Array<string>
siblings?: Array<string>
}
export interface JsStatsChunkGroupAsset {
name: string
Expand Down Expand Up @@ -531,8 +536,8 @@ export class JsCompilation {
}
export class JsStats {
getAssets(): JsStatsGetAssets
getModules(showReasons: boolean): Array<JsStatsModule>
getChunks(): Array<JsStatsChunk>
getModules(): Array<JsStatsModule>
getChunks(chunkModules: boolean, chunksRelations: boolean): Array<JsStatsChunk>
getEntrypoints(): Array<JsStatsChunkGroup>
getNamedChunkGroups(): Array<JsStatsChunkGroup>
getErrors(): Array<JsStatsError>
Expand Down
19 changes: 15 additions & 4 deletions crates/node_binding/src/js_values/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ pub struct JsStatsChunk {
pub initial: bool,
pub names: Vec<String>,
pub size: f64,
pub modules: Option<Vec<JsStatsModule>>,
pub parents: Option<Vec<String>>,
pub children: Option<Vec<String>>,
pub siblings: Option<Vec<String>>,
}

impl From<rspack_core::StatsChunk> for JsStatsChunk {
Expand All @@ -166,6 +170,12 @@ impl From<rspack_core::StatsChunk> for JsStatsChunk {
initial: stats.initial,
names: stats.names,
size: stats.size,
modules: stats
.modules
.map(|i| i.into_iter().map(Into::into).collect()),
parents: stats.parents,
children: stats.children,
siblings: stats.siblings,
}
}
}
Expand Down Expand Up @@ -250,21 +260,22 @@ impl JsStats {
}

#[napi]
pub fn get_modules(&self, show_reasons: bool) -> Vec<JsStatsModule> {
pub fn get_modules(&self) -> Vec<JsStatsModule> {
self
.inner
.get_modules(show_reasons)
.get_modules()
.expect("Failed to get modules")
.into_iter()
.map(Into::into)
.collect()
}

#[napi]
pub fn get_chunks(&self) -> Vec<JsStatsChunk> {
pub fn get_chunks(&self, chunk_modules: bool, chunks_relations: bool) -> Vec<JsStatsChunk> {
self
.inner
.get_chunks()
.get_chunks(chunk_modules, chunks_relations)
.expect("Failed to get chunks")
.into_iter()
.map(Into::into)
.collect()
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_options/src/options/raw_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use serde::Deserialize;
#[napi(object)]
pub struct RawStatsOptions {
pub colors: bool,
pub reasons: bool,
}

impl From<RawStatsOptions> for StatsOptions {
fn from(value: RawStatsOptions) -> Self {
Self {
colors: value.colors,
reasons: value.reasons,
}
}
}
21 changes: 7 additions & 14 deletions crates/rspack_core/src/chunk_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use rspack_identifier::{IdentifierLinkedMap, IdentifierMap, IdentifierSet};
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};

use crate::{
find_graph_roots, Chunk, ChunkByUkey, ChunkGroup, ChunkGroupByUkey, ChunkGroupUkey, ChunkUkey,
Module, ModuleGraph, ModuleGraphModule, ModuleIdentifier, RuntimeSpec, RuntimeSpecMap,
find_graph_roots, BoxModule, Chunk, ChunkByUkey, ChunkGroup, ChunkGroupByUkey, ChunkGroupUkey,
ChunkUkey, Module, ModuleGraph, ModuleGraphModule, ModuleIdentifier, RuntimeSpec, RuntimeSpecMap,
RuntimeSpecSet, SourceType,
};

Expand Down Expand Up @@ -168,12 +168,12 @@ impl ChunkGraph {
&self,
chunk: &ChunkUkey,
module_graph: &'module ModuleGraph,
) -> Vec<&'module ModuleGraphModule> {
) -> Vec<&'module BoxModule> {
let chunk_graph_chunk = self.get_chunk_graph_chunk(chunk);
chunk_graph_chunk
.modules
.iter()
.filter_map(|uri| module_graph.module_graph_module_by_identifier(uri))
.filter_map(|uri| module_graph.module_by_identifier(uri))
.collect()
}

Expand All @@ -186,10 +186,10 @@ impl ChunkGraph {
&self,
chunk: &ChunkUkey,
module_graph: &'module ModuleGraph,
) -> Vec<&'module ModuleGraphModule> {
) -> Vec<&'module BoxModule> {
let mut modules = self.get_chunk_modules(chunk, module_graph);
// SAFETY: module identifier is unique
modules.sort_unstable_by_key(|m| m.module_identifier.as_str());
modules.sort_unstable_by_key(|m| m.identifier().as_str());
modules
}

Expand Down Expand Up @@ -234,14 +234,7 @@ impl ChunkGraph {
.get_chunk_modules(chunk, module_graph)
.iter()
.fold(0.0, |acc, m| {
let module = module_graph
.module_by_identifier(&m.module_identifier)
.unwrap_or_else(|| panic!("Module({}) does not exist", m.module_identifier));
acc
+ module
.source_types()
.iter()
.fold(0.0, |acc, t| acc + module.size(t))
acc + m.source_types().iter().fold(0.0, |acc, t| acc + m.size(t))
})
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ impl Compilation {
{
if let Some(runtime_requirements) = self
.chunk_graph
.get_module_runtime_requirements(module.module_identifier, &chunk.runtime)
.get_module_runtime_requirements(module.identifier(), &chunk.runtime)
{
set.extend(runtime_requirements);
}
Expand Down Expand Up @@ -1153,7 +1153,7 @@ impl Compilation {
.chunk_graph
.get_ordered_chunk_modules(&chunk.ukey, &self.module_graph)
.into_iter()
.filter_map(|mgm| self.module_graph.get_module_hash(&mgm.module_identifier))
.filter_map(|m| self.module_graph.get_module_hash(&m.identifier()))
.inspect(|hash| hash.hash(&mut chunk.hash))
})
.collect::<Vec<_>>()
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/options/stats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[derive(Debug, Default)]
pub struct StatsOptions {
pub colors: bool,
pub reasons: bool,
}
Loading

0 comments on commit bb22416

Please sign in to comment.