Skip to content

Commit

Permalink
feat(stats): support isOverSizeLimit in stats (#7483)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Aug 7, 2024
1 parent 255707a commit 1772f7c
Show file tree
Hide file tree
Showing 22 changed files with 460 additions and 418 deletions.
4 changes: 4 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ export interface JsAssetInfo {
* Related: packages/rspack/src/Compilation.ts
*/
extras: Record<string, any>
/** whether this asset is over the size limit */
isOverSizeLimit?: boolean
}

export interface JsAssetInfoRelated {
Expand Down Expand Up @@ -664,6 +666,7 @@ export interface JsStatsAssetInfo {
contenthash: Array<string>
fullhash: Array<string>
related: Array<JsStatsAssetInfoRelated>
isOverSizeLimit?: boolean
}

export interface JsStatsAssetInfoRelated {
Expand Down Expand Up @@ -707,6 +710,7 @@ export interface JsStatsChunkGroup {
auxiliaryAssets?: Array<JsStatsChunkGroupAsset>
auxiliaryAssetsSize?: number
children?: JsStatsChunkGroupChildren
isOverSizeLimit?: boolean
}

export interface JsStatsChunkGroupAsset {
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_binding_values/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub struct JsAssetInfo {
/// in the rust struct and have the Js side to reshape and align with webpack
/// Related: packages/rspack/src/Compilation.ts
pub extras: serde_json::Map<String, serde_json::Value>,
/// whether this asset is over the size limit
pub is_over_size_limit: Option<bool>,
}

impl From<JsAssetInfo> for rspack_core::AssetInfo {
Expand All @@ -64,6 +66,7 @@ impl From<JsAssetInfo> for rspack_core::AssetInfo {
javascript_module: i.javascript_module,
css_unused_idents: i.css_unused_idents.map(|i| i.into_iter().collect()),
extras: i.extras,
is_over_size_limit: i.is_over_size_limit,
}
}
}
Expand Down Expand Up @@ -97,6 +100,7 @@ impl From<rspack_core::AssetInfo> for JsAssetInfo {
javascript_module: info.javascript_module,
css_unused_idents: info.css_unused_idents.map(|i| i.into_iter().collect()),
extras: info.extras,
is_over_size_limit: info.is_over_size_limit,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_binding_values/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ pub struct JsStatsAssetInfo {
pub contenthash: Vec<String>,
pub fullhash: Vec<String>,
pub related: Vec<JsStatsAssetInfoRelated>,
pub is_over_size_limit: Option<bool>,
}

impl FromNapiValue for JsStatsAssetInfo {
Expand Down Expand Up @@ -446,6 +447,7 @@ impl From<rspack_core::StatsAssetInfo> for JsStatsAssetInfo {
.into_iter()
.map(Into::into)
.collect::<Vec<_>>(),
is_over_size_limit: stats.is_over_size_limit,
}
}
}
Expand Down Expand Up @@ -1006,6 +1008,7 @@ pub struct JsStatsChunkGroup {
pub auxiliary_assets: Option<Vec<JsStatsChunkGroupAsset>>,
pub auxiliary_assets_size: Option<f64>,
pub children: Option<JsStatsChunkGroupChildren>,
pub is_over_size_limit: Option<bool>,
}

impl FromNapiValue for JsStatsChunkGroup {
Expand All @@ -1029,6 +1032,7 @@ impl From<rspack_core::StatsChunkGroup> for JsStatsChunkGroup {
.map(|assets| assets.into_iter().map(Into::into).collect()),
auxiliary_assets_size: stats.auxiliary_assets_size,
children: stats.children.map(|i| i.into()),
is_over_size_limit: stats.is_over_size_limit,
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_core/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub struct ChunkGroup {
pub(crate) runtime_chunk: Option<ChunkUkey>,
pub(crate) entry_point_chunk: Option<ChunkUkey>,
origins: Vec<OriginRecord>,
pub(crate) is_over_size_limit: Option<bool>,
}

impl ChunkGroup {
Expand All @@ -78,6 +79,7 @@ impl ChunkGroup {
entry_point_chunk: None,
index: None,
origins: vec![],
is_over_size_limit: None,
}
}

Expand Down Expand Up @@ -366,6 +368,10 @@ impl ChunkGroup {

children_by_orders
}

pub fn set_is_over_size_limit(&mut self, v: bool) {
self.is_over_size_limit = Some(v);
}
}

#[derive(Debug, Clone)]
Expand Down
7 changes: 7 additions & 0 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,8 @@ pub struct AssetInfo {
/// in the rust struct and have the Js side to reshape and align with webpack.
/// Related: packages/rspack/src/Compilation.ts
pub extras: serde_json::Map<String, serde_json::Value>,
/// whether this asset is over the size limit
pub is_over_size_limit: Option<bool>,
}

impl AssetInfo {
Expand Down Expand Up @@ -1846,6 +1848,10 @@ impl AssetInfo {
self.css_unused_idents = Some(v);
}

pub fn set_is_over_size_limit(&mut self, v: bool) {
self.is_over_size_limit = Some(v);
}

// https://github.com/webpack/webpack/blob/7b80b2b18db66abca6feb7b02a9089aca4bc8186/lib/asset/AssetGenerator.js#L43-L70
pub fn merge_another(&mut self, another: AssetInfo) {
// "another" first fields
Expand All @@ -1868,6 +1874,7 @@ impl AssetInfo {
self.immutable = self.immutable || another.immutable;
self.development = self.development || another.development;
self.hot_module_replacement = self.hot_module_replacement || another.hot_module_replacement;
self.is_over_size_limit = self.is_over_size_limit.or(another.is_over_size_limit);
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ impl Stats<'_> {
development: asset.info.development,
hot_module_replacement: asset.info.hot_module_replacement,
source_filename: asset.info.source_filename.clone(),
is_over_size_limit: asset.info.is_over_size_limit,
},
emitted: self.compilation.emitted_assets.contains(name),
},
Expand Down Expand Up @@ -519,6 +520,7 @@ impl Stats<'_> {
.then(|| auxiliary_assets.iter().map(|i| i.size).sum()),
auxiliary_assets: chunk_group_auxiliary.then_some(auxiliary_assets),
children,
is_over_size_limit: cg.is_over_size_limit,
}
}

Expand Down Expand Up @@ -1433,6 +1435,7 @@ pub struct StatsAssetInfo {
pub content_hash: Vec<String>,
pub full_hash: Vec<String>,
pub related: Vec<StatsAssetInfoRelated>,
pub is_over_size_limit: Option<bool>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -1543,6 +1546,7 @@ pub struct StatsChunkGroup {
pub auxiliary_assets: Option<Vec<StatsChunkGroupAsset>>,
pub auxiliary_assets_size: Option<f64>,
pub children: Option<StatsChunkGroupChildren>,
pub is_over_size_limit: Option<bool>,
}

#[derive(Debug)]
Expand Down
29 changes: 24 additions & 5 deletions crates/rspack_plugin_size_limits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::fmt::Debug;
use std::{collections::HashMap, fmt::Debug};

use derivative::Derivative;
use futures::future::BoxFuture;
use rspack_core::{
ApplyContext, ChunkGroup, Compilation, CompilationAsset, CompilerAfterEmit, CompilerOptions,
Plugin, PluginContext,
ApplyContext, ChunkGroup, ChunkGroupUkey, Compilation, CompilationAsset, CompilerAfterEmit,
CompilerOptions, Plugin, PluginContext,
};
use rspack_error::{Diagnostic, Result};
use rspack_hook::{plugin, plugin_hook};
Expand Down Expand Up @@ -136,6 +136,8 @@ async fn after_emit(&self, compilation: &mut Compilation) -> Result<()> {
let hints = &self.options.hints;
let max_asset_size = self.options.max_asset_size.unwrap_or(250000.0);
let max_entrypoint_size = self.options.max_entrypoint_size.unwrap_or(250000.0);
let mut checked_assets: HashMap<String, bool> = HashMap::default();
let mut checked_chunk_groups: HashMap<ChunkGroupUkey, bool> = HashMap::default();

let mut assets_over_size_limit = vec![];

Expand All @@ -148,8 +150,10 @@ async fn after_emit(&self, compilation: &mut Compilation) -> Result<()> {

if let Some(source) = source {
let size = source.size() as f64;
let is_over_size_limit = size > max_asset_size;

if size > max_asset_size {
checked_assets.insert(name.to_owned(), is_over_size_limit);
if is_over_size_limit {
assets_over_size_limit.push((name, size));
}
}
Expand All @@ -160,8 +164,10 @@ async fn after_emit(&self, compilation: &mut Compilation) -> Result<()> {
for (name, ukey) in compilation.entrypoints.iter() {
let entry = compilation.chunk_group_by_ukey.expect_get(ukey);
let size = self.get_entrypoint_size(entry, compilation).await;
let is_over_size_limit = size > max_entrypoint_size;

if size > max_entrypoint_size {
checked_chunk_groups.insert(ukey.to_owned(), is_over_size_limit);
if is_over_size_limit {
let mut files = vec![];

for filename in entry.get_files(&compilation.chunk_by_ukey) {
Expand Down Expand Up @@ -216,6 +222,19 @@ async fn after_emit(&self, compilation: &mut Compilation) -> Result<()> {
}
}

for (name, asset) in compilation.assets_mut() {
if let Some(checked) = checked_assets.get(name) {
asset.info.set_is_over_size_limit(*checked)
}
}

for (ukey, checked) in checked_chunk_groups.iter() {
compilation
.chunk_group_by_ukey
.expect_get_mut(ukey)
.set_is_over_size_limit(*checked);
}

Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ Object {
"fullhash": Array [],
"hotModuleReplacement": false,
"immutable": false,
"isOverSizeLimit": false,
"javascriptModule": false,
"minimized": true,
"related": Object {},
},
"isOverSizeLimit": false,
"name": "main.js",
"related": Array [],
"size": 207,
Expand Down Expand Up @@ -164,6 +166,7 @@ Object {
"909",
],
"filteredAssets": 0,
"isOverSizeLimit": false,
"name": "main",
},
},
Expand Down Expand Up @@ -256,6 +259,7 @@ Object {
"909",
],
"filteredAssets": 0,
"isOverSizeLimit": false,
"name": "main",
},
},
Expand Down Expand Up @@ -290,10 +294,12 @@ Object {
"fullhash": Array [],
"hotModuleReplacement": false,
"immutable": false,
"isOverSizeLimit": false,
"javascriptModule": false,
"minimized": true,
"related": Object {},
},
"isOverSizeLimit": false,
"name": "main.js",
"related": Array [],
"size": 441,
Expand Down Expand Up @@ -674,6 +680,7 @@ Object {
"909",
],
"filteredAssets": 0,
"isOverSizeLimit": false,
"name": "main",
},
},
Expand Down Expand Up @@ -1390,6 +1397,7 @@ Object {
"909",
],
"filteredAssets": 0,
"isOverSizeLimit": false,
"name": "main",
},
},
Expand Down Expand Up @@ -1423,6 +1431,7 @@ Object {
"fullhash": Array [],
"hotModuleReplacement": false,
"immutable": false,
"isOverSizeLimit": false,
"javascriptModule": false,
"minimized": true,
"related": Object {},
Expand Down Expand Up @@ -1673,10 +1682,12 @@ Object {
"fullhash": Array [],
"hotModuleReplacement": false,
"immutable": false,
"isOverSizeLimit": false,
"javascriptModule": false,
"minimized": true,
"related": Object {},
},
"isOverSizeLimit": false,
"name": "main.js",
"related": Array [],
"size": 346,
Expand Down Expand Up @@ -2038,6 +2049,7 @@ exports.c = require(\\"./c?c=3\\");
"909",
],
"filteredAssets": 0,
"isOverSizeLimit": false,
"name": "main",
},
},
Expand Down Expand Up @@ -2355,6 +2367,7 @@ exports.c = require(\\"./c?c=3\\");
"909",
],
"filteredAssets": 0,
"isOverSizeLimit": false,
"name": "main",
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ Rspack x.x.x compiled successfully in X s"
`;
exports[`statsOutput statsOutput/performance-error should print correct stats for 1`] = `
"asset main.js 303 KiB [emitted] (name: main)
"asset main.js 303 KiB [emitted] [big] (name: main)
asset 697.js 130 bytes [emitted]
asset 753.js 130 bytes [emitted]
Entrypoint main 303 KiB = main.js
Entrypoint main [big] 303 KiB = main.js
runtime modules 8.39 KiB 12 modules
cacheable modules 293 KiB
./index.js 48 bytes [built] [code generated]
Expand All @@ -342,10 +342,10 @@ Rspack x.x.x compiled with 2 errors in X s"
`;
exports[`statsOutput statsOutput/performance-no-hints should print correct stats for 1`] = `
"asset main.js 303 KiB [emitted] (name: main)
"asset main.js 303 KiB [emitted] [big] (name: main)
asset 697.js 130 bytes [emitted]
asset 753.js 130 bytes [emitted]
Entrypoint main 303 KiB = main.js
Entrypoint main [big] 303 KiB = main.js
runtime modules 8.39 KiB 12 modules
cacheable modules 293 KiB
./index.js 48 bytes [built] [code generated]
Expand Down
4 changes: 4 additions & 0 deletions packages/rspack-test-tools/tests/statsAPICases/asset-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ module.exports = {
],
"hotModuleReplacement": false,
"immutable": true,
"isOverSizeLimit": false,
"minimized": false,
"related": Object {},
"sourceFilename": "fixtures/asset/image.png",
},
"isOverSizeLimit": false,
"name": "89a353e9c515885abd8e.png",
"related": Array [],
"size": 14910,
Expand All @@ -84,6 +86,7 @@ module.exports = {
"fullhash": Array [],
"hotModuleReplacement": false,
"immutable": false,
"isOverSizeLimit": false,
"javascriptModule": false,
"minimized": false,
"related": Object {
Expand All @@ -92,6 +95,7 @@ module.exports = {
],
},
},
"isOverSizeLimit": false,
"name": "main.js",
"related": Array [],
"size": 2425,
Expand Down
Loading

2 comments on commit 1772f7c

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rsbuild ❌ failure
examples ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-08-07 0b5ee31) Current Change
10000_development-mode + exec 2.32 s ± 22 ms 2.32 s ± 18 ms +0.22 %
10000_development-mode_hmr + exec 706 ms ± 7.7 ms 704 ms ± 6.7 ms -0.20 %
10000_production-mode + exec 2.85 s ± 23 ms 2.86 s ± 22 ms +0.57 %
arco-pro_development-mode + exec 1.88 s ± 60 ms 1.91 s ± 81 ms +1.50 %
arco-pro_development-mode_hmr + exec 434 ms ± 2.1 ms 434 ms ± 2.6 ms 0.00 %
arco-pro_production-mode + exec 3.62 s ± 262 ms 3.46 s ± 222 ms -4.38 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.55 s ± 192 ms 3.63 s ± 314 ms +2.25 %
threejs_development-mode_10x + exec 1.71 s ± 13 ms 1.72 s ± 15 ms +0.54 %
threejs_development-mode_10x_hmr + exec 826 ms ± 8.9 ms 832 ms ± 6.1 ms +0.69 %
threejs_production-mode_10x + exec 5.51 s ± 33 ms 5.51 s ± 36 ms -0.06 %

Please sign in to comment.