Skip to content

Commit

Permalink
feat: add moduleAssets for stats (web-infra-dev#2864)
Browse files Browse the repository at this point in the history
* stats-module-assets

* add changset

* fmt

* fmt
  • Loading branch information
ahabhgk authored and siyou committed May 14, 2023
1 parent 6742217 commit 6ee36c3
Show file tree
Hide file tree
Showing 25 changed files with 169 additions and 82 deletions.
7 changes: 7 additions & 0 deletions .changeset/fair-buttons-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@rspack/binding": patch
"@rspack/core": patch
"@rspack/cli": patch
---

feat: add moduleAssets for stats
6 changes: 4 additions & 2 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ export interface JsLoaderContext {
contextDependencies: Array<string>
missingDependencies: Array<string>
buildDependencies: Array<string>
assetFilenames: Array<string>
currentLoader: string
isPitching: boolean
}
Expand Down Expand Up @@ -573,6 +574,7 @@ export interface JsStatsModule {
issuerId?: string
issuerPath: Array<JsStatsModuleIssuer>
reasons?: Array<JsStatsModuleReason>
assets?: Array<string>
}
export interface JsStatsModuleIssuer {
identifier: string
Expand Down Expand Up @@ -658,8 +660,8 @@ export class JsCompilation {
}
export class JsStats {
getAssets(): JsStatsGetAssets
getModules(reasons: boolean): Array<JsStatsModule>
getChunks(chunkModules: boolean, chunksRelations: boolean, reasons: boolean): Array<JsStatsChunk>
getModules(reasons: boolean, moduleAssets: boolean): Array<JsStatsModule>
getChunks(chunkModules: boolean, chunksRelations: boolean, reasons: boolean, moduleAssets: boolean): Array<JsStatsChunk>
getEntrypoints(): Array<JsStatsChunkGroup>
getNamedChunkGroups(): Array<JsStatsChunkGroup>
getErrors(): Array<JsStatsError>
Expand Down
14 changes: 7 additions & 7 deletions crates/node_binding/src/js_values/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl JsCompilation {

#[napi(ts_return_type = "Readonly<JsAsset>[]")]
pub fn get_assets(&self) -> Result<Vec<JsAsset>> {
let mut assets = Vec::<JsAsset>::with_capacity(self.inner.assets.len());
let mut assets = Vec::<JsAsset>::with_capacity(self.inner.assets().len());

for (filename, asset) in self.inner.assets() {
assets.push(JsAsset {
Expand All @@ -107,7 +107,7 @@ impl JsCompilation {

#[napi]
pub fn get_asset(&self, name: String) -> Result<Option<JsAsset>> {
match self.inner.assets.get(&name) {
match self.inner.assets().get(&name) {
Some(asset) => Ok(Some(JsAsset {
name,
source: asset
Expand All @@ -125,7 +125,7 @@ impl JsCompilation {
pub fn get_asset_source(&self, name: String) -> Result<Option<JsCompatSource>> {
self
.inner
.assets
.assets()
.get(&name)
.and_then(|v| v.source.as_ref().map(|s| s.to_js_compat_source()))
.transpose()
Expand Down Expand Up @@ -181,7 +181,7 @@ impl JsCompilation {
#[napi]
pub fn set_asset_source(&mut self, name: String, source: JsCompatSource) {
let source = CompatSource::from(source).boxed();
match self.inner.assets.entry(name) {
match self.inner.assets_mut().entry(name) {
std::collections::hash_map::Entry::Occupied(mut e) => e.get_mut().set_source(Some(source)),
std::collections::hash_map::Entry::Vacant(e) => {
e.insert(rspack_core::CompilationAsset::with_source(source));
Expand All @@ -193,7 +193,7 @@ impl JsCompilation {
pub fn delete_asset_source(&mut self, name: String) {
self
.inner
.assets
.assets_mut()
.entry(name)
.and_modify(|a| a.set_source(None));
}
Expand All @@ -202,7 +202,7 @@ impl JsCompilation {
pub fn get_asset_filenames(&self) -> Result<Vec<String>> {
let filenames = self
.inner
.assets
.assets()
.iter()
.filter(|(_, asset)| asset.get_source().is_some())
.map(|(filename, _)| filename)
Expand All @@ -213,7 +213,7 @@ impl JsCompilation {

#[napi]
pub fn has_asset(&self, name: String) -> Result<bool> {
Ok(self.inner.assets.contains_key(&name))
Ok(self.inner.assets().contains_key(&name))
}

#[napi]
Expand Down
9 changes: 6 additions & 3 deletions crates/node_binding/src/js_values/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub struct JsStatsModule {
pub issuer_id: Option<String>,
pub issuer_path: Vec<JsStatsModuleIssuer>,
pub reasons: Option<Vec<JsStatsModuleReason>>,
pub assets: Option<Vec<String>>,
}

impl From<rspack_core::StatsModule> for JsStatsModule {
Expand All @@ -107,6 +108,7 @@ impl From<rspack_core::StatsModule> for JsStatsModule {
reasons: stats
.reasons
.map(|i| i.into_iter().map(Into::into).collect()),
assets: stats.assets,
}
}
}
Expand Down Expand Up @@ -264,10 +266,10 @@ impl JsStats {
}

#[napi]
pub fn get_modules(&self, reasons: bool) -> Vec<JsStatsModule> {
pub fn get_modules(&self, reasons: bool, module_assets: bool) -> Vec<JsStatsModule> {
self
.inner
.get_modules(reasons)
.get_modules(reasons, module_assets)
.expect("Failed to get modules")
.into_iter()
.map(Into::into)
Expand All @@ -280,10 +282,11 @@ impl JsStats {
chunk_modules: bool,
chunks_relations: bool,
reasons: bool,
module_assets: bool
) -> Vec<JsStatsChunk> {
self
.inner
.get_chunks(chunk_modules, chunks_relations, reasons)
.get_chunks(chunk_modules, chunks_relations, reasons, module_assets)
.expect("Failed to get chunks")
.into_iter()
.map(Into::into)
Expand Down
4 changes: 1 addition & 3 deletions crates/node_binding/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use rspack_error::internal_error;
use rspack_napi_shared::threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode};
use rspack_napi_shared::NapiResultExt;

use crate::js_values::{
JsChunk, JsChunkAssetArgs, JsResourceData, JsStatsChunk, SchemeAndJsResourceData,
};
use crate::js_values::{JsChunkAssetArgs, JsResourceData, SchemeAndJsResourceData};
use crate::{DisabledHooks, Hook, JsCompilation, JsHooks};

pub struct JsHooksAdapter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ pub struct JsLoaderContext {
pub context_dependencies: Vec<String>,
pub missing_dependencies: Vec<String>,
pub build_dependencies: Vec<String>,
pub asset_filenames: Vec<String>,

pub current_loader: String,
pub is_pitching: bool,
Expand Down Expand Up @@ -267,6 +268,7 @@ impl TryFrom<&rspack_core::LoaderContext<'_, rspack_core::LoaderRunnerContext>>
.iter()
.map(|i| i.to_string_lossy().to_string())
.collect(),
asset_filenames: cx.asset_filenames.iter().map(|i| i.to_owned()).collect(),

current_loader: cx.current_loader().to_string(),
is_pitching: true,
Expand Down
6 changes: 5 additions & 1 deletion crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub struct Compilation {
pub chunk_by_ukey: Database<Chunk>,
pub chunk_group_by_ukey: Database<ChunkGroup>,
pub entrypoints: IndexMap<String, ChunkGroupUkey>,
pub assets: CompilationAssets,
assets: CompilationAssets,
pub emitted_assets: DashSet<String, BuildHasherDefault<FxHasher>>,
diagnostics: IndexSet<Diagnostic, BuildHasherDefault<FxHasher>>,
pub plugin_driver: SharedPluginDriver,
Expand Down Expand Up @@ -242,6 +242,10 @@ impl Compilation {
&self.assets
}

pub fn assets_mut(&mut self) -> &mut CompilationAssets {
&mut self.assets
}

pub fn entrypoints(&self) -> &IndexMap<String, ChunkGroupUkey> {
&self.entrypoints
}
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct BuildInfo {
pub context_dependencies: HashSet<PathBuf>,
pub missing_dependencies: HashSet<PathBuf>,
pub build_dependencies: HashSet<PathBuf>,
pub asset_filenames: HashSet<String>,
}

#[derive(Debug, Default, Clone)]
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ impl Module for NormalModule {
build_info.context_dependencies = loader_result.context_dependencies;
build_info.missing_dependencies = loader_result.missing_dependencies;
build_info.build_dependencies = loader_result.build_dependencies;
build_info.asset_filenames = loader_result.asset_filenames;

// TODO: match package.json type files
build_meta.strict_harmony_module = matches!(self.module_type, ModuleType::JsEsm);
Expand Down
73 changes: 48 additions & 25 deletions crates/rspack_core/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,32 @@ impl Stats<'_> {
}
}

let mut assets: HashMap<&String, StatsAsset> =
HashMap::from_iter(self.compilation.assets.iter().filter_map(|(name, asset)| {
asset.get_source().map(|source| {
(
name,
StatsAsset {
r#type: "asset",
name: name.clone(),
size: source.size() as f64,
chunks: Vec::new(),
chunk_names: Vec::new(),
info: StatsAssetInfo {
development: asset.info.development,
hot_module_replacement: asset.info.hot_module_replacement,
let mut assets: HashMap<&String, StatsAsset> = HashMap::from_iter(
self
.compilation
.assets()
.iter()
.filter_map(|(name, asset)| {
asset.get_source().map(|source| {
(
name,
StatsAsset {
r#type: "asset",
name: name.clone(),
size: source.size() as f64,
chunks: Vec::new(),
chunk_names: Vec::new(),
info: StatsAssetInfo {
development: asset.info.development,
hot_module_replacement: asset.info.hot_module_replacement,
},
emitted: self.compilation.emitted_assets.contains(name),
},
emitted: self.compilation.emitted_assets.contains(name),
},
)
})
}));
for asset in self.compilation.assets.values() {
)
})
}),
);
for asset in self.compilation.assets().values() {
if let Some(source_map) = &asset.get_info().related.source_map {
assets.remove(source_map);
}
Expand Down Expand Up @@ -109,13 +114,13 @@ impl Stats<'_> {
(assets, assets_by_chunk_name)
}

pub fn get_modules(&self, reasons: bool) -> Result<Vec<StatsModule>> {
pub fn get_modules(&self, reasons: bool, module_assets: bool) -> Result<Vec<StatsModule>> {
let mut modules: Vec<StatsModule> = self
.compilation
.module_graph
.modules()
.values()
.map(|module| self.get_module(module, reasons))
.map(|module| self.get_module(module, reasons, module_assets))
.collect::<Result<_>>()?;
Self::sort_modules(&mut modules);
Ok(modules)
Expand All @@ -126,6 +131,7 @@ impl Stats<'_> {
chunk_modules: bool,
chunk_relations: bool,
reasons: bool,
module_assets: bool,
) -> Result<Vec<StatsChunk>> {
let mut chunks: Vec<StatsChunk> = self
.compilation
Expand All @@ -141,7 +147,7 @@ impl Stats<'_> {
.get_chunk_modules(&c.ukey, &self.compilation.module_graph);
let mut chunk_modules = chunk_modules
.into_iter()
.map(|m| self.get_module(m, reasons))
.map(|m| self.get_module(m, reasons, module_assets))
.collect::<Result<Vec<_>>>()?;
Self::sort_modules(&mut chunk_modules);
Some(chunk_modules)
Expand Down Expand Up @@ -284,7 +290,12 @@ impl Stats<'_> {
});
}

fn get_module(&self, module: &BoxModule, show_reasons: bool) -> Result<StatsModule> {
fn get_module(
&self,
module: &BoxModule,
reasons: bool,
module_assets: bool,
) -> Result<StatsModule> {
let identifier = module.identifier();
let mgm = self
.compilation
Expand All @@ -309,7 +320,7 @@ impl Stats<'_> {
}
issuer_path.reverse();

let reasons = show_reasons
let reasons = reasons
.then(|| -> Result<_> {
let mut reasons: Vec<StatsModuleReason> = mgm
.incoming_connections_unordered(&self.compilation.module_graph)?
Expand Down Expand Up @@ -359,6 +370,16 @@ impl Stats<'_> {
.collect();
chunks.sort_unstable();

let assets = module_assets.then(|| {
let mut assets: Vec<_> = mgm
.build_info
.as_ref()
.map(|info| info.asset_filenames.iter().map(|i| i.to_string()).collect())
.unwrap_or_default();
assets.sort();
assets
});

Ok(StatsModule {
r#type: "module",
module_type: *module.module_type(),
Expand All @@ -374,6 +395,7 @@ impl Stats<'_> {
issuer_id,
issuer_path,
reasons,
assets,
})
}

Expand Down Expand Up @@ -484,6 +506,7 @@ pub struct StatsModule {
pub issuer_id: Option<String>,
pub issuer_path: Vec<StatsModuleIssuer>,
pub reasons: Option<Vec<StatsModuleReason>>,
pub assets: Option<Vec<String>>,
}

#[derive(Debug)]
Expand Down
5 changes: 5 additions & 0 deletions crates/rspack_loader_runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub struct LoaderContext<'c, C> {
pub missing_dependencies: HashSet<PathBuf>,
pub build_dependencies: HashSet<PathBuf>,

pub asset_filenames: HashSet<String>,

pub(crate) loader_index: usize,
pub(crate) loader_items: LoaderItemList<'c, C>,
#[derivative(Debug = "ignore")]
Expand Down Expand Up @@ -147,6 +149,7 @@ async fn create_loader_context<'c, C: 'c>(
context_dependencies: Default::default(),
missing_dependencies: Default::default(),
build_dependencies: Default::default(),
asset_filenames: Default::default(),
content: None,
resource: &resource_data.resource,
resource_path: &resource_data.resource_path,
Expand Down Expand Up @@ -230,6 +233,7 @@ pub struct LoaderResult {
pub context_dependencies: HashSet<PathBuf>,
pub missing_dependencies: HashSet<PathBuf>,
pub build_dependencies: HashSet<PathBuf>,
pub asset_filenames: HashSet<String>,
pub content: Content,
pub source_map: Option<SourceMap>,
pub additional_data: Option<String>,
Expand All @@ -254,6 +258,7 @@ impl<C> TryFrom<LoaderContext<'_, C>> for TWithDiagnosticArray<LoaderResult> {
context_dependencies: loader_context.context_dependencies,
missing_dependencies: loader_context.missing_dependencies,
build_dependencies: loader_context.build_dependencies,
asset_filenames: loader_context.asset_filenames,
content,
source_map: loader_context.source_map,
additional_data: loader_context.additional_data,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_copy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ impl Plugin for CopyPlugin {

copied_result.sort_unstable_by(|a, b| a.0.cmp(&b.0));
copied_result.into_iter().for_each(|(_priority, result)| {
if let Some(exist_asset) = args.compilation.assets.get_mut(&result.filename) {
if let Some(exist_asset) = args.compilation.assets_mut().get_mut(&result.filename) {
if !result.force {
return;
}
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_plugin_css/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl SwcCssCompiler {
}
}

#[derive(Debug, Clone)]
pub struct SwcCssSourceMapGenConfig {
pub enable: bool,
pub emit_columns: bool,
Expand Down
Loading

0 comments on commit 6ee36c3

Please sign in to comment.