Skip to content

Commit

Permalink
stats-module-assets
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Apr 21, 2023
1 parent 057829b commit 72d58f6
Show file tree
Hide file tree
Showing 23 changed files with 173 additions and 80 deletions.
6 changes: 4 additions & 2 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export interface JsLoaderContext {
contextDependencies: Array<string>
missingDependencies: Array<string>
buildDependencies: Array<string>
assetFilenames: Array<string>
currentLoader: string
isPitching: boolean
}
Expand Down Expand Up @@ -557,6 +558,7 @@ export interface JsStatsModule {
issuerId?: string
issuerPath: Array<JsStatsModuleIssuer>
reasons?: Array<JsStatsModuleReason>
assets?: Array<string>
}
export interface JsStatsModuleIssuer {
identifier: string
Expand Down Expand Up @@ -642,8 +644,8 @@ export class JsCompilation {
}
export class JsStats {
getAssets(): JsStatsGetAssets
getModules(): Array<JsStatsModule>
getChunks(chunkModules: boolean, chunksRelations: boolean): Array<JsStatsChunk>
getModules(moduleAssets: boolean): Array<JsStatsModule>
getChunks(chunkModules: boolean, chunksRelations: 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
15 changes: 11 additions & 4 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,21 +266,26 @@ impl JsStats {
}

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

#[napi]
pub fn get_chunks(&self, chunk_modules: bool, chunks_relations: bool) -> Vec<JsStatsChunk> {
pub fn get_chunks(
&self,
chunk_modules: bool,
chunks_relations: bool,
module_assets: bool,
) -> Vec<JsStatsChunk> {
self
.inner
.get_chunks(chunk_modules, chunks_relations)
.get_chunks(chunk_modules, chunks_relations, module_assets)
.expect("Failed to get chunks")
.into_iter()
.map(Into::into)
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
83 changes: 58 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,19 +114,30 @@ impl Stats<'_> {
(assets, assets_by_chunk_name)
}

pub fn get_modules(&self) -> Result<Vec<StatsModule>> {
pub fn get_modules(&self, module_assets: bool) -> Result<Vec<StatsModule>> {
let mut modules: Vec<StatsModule> = self
.compilation
.module_graph
.modules()
.values()
.map(|module| self.get_module(module, self.compilation.options.stats.reasons))
.map(|module| {
self.get_module(
module,
self.compilation.options.stats.reasons,
module_assets,
)
})
.collect::<Result<_>>()?;
Self::sort_modules(&mut modules);
Ok(modules)
}

pub fn get_chunks(&self, chunk_modules: bool, chunk_relations: bool) -> Result<Vec<StatsChunk>> {
pub fn get_chunks(
&self,
chunk_modules: bool,
chunk_relations: bool,
module_assets: bool,
) -> Result<Vec<StatsChunk>> {
let mut chunks: Vec<StatsChunk> = self
.compilation
.chunk_by_ukey
Expand All @@ -136,7 +152,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, self.compilation.options.stats.reasons))
.map(|m| self.get_module(m, self.compilation.options.stats.reasons, module_assets))
.collect::<Result<Vec<_>>>()?;
Self::sort_modules(&mut chunk_modules);
Some(chunk_modules)
Expand Down Expand Up @@ -279,7 +295,12 @@ impl Stats<'_> {
});
}

fn get_module(&self, module: &BoxModule, show_reasons: bool) -> Result<StatsModule> {
fn get_module(
&self,
module: &BoxModule,
show_reasons: bool,
module_assets: bool,
) -> Result<StatsModule> {
let identifier = module.identifier();
let mgm = self
.compilation
Expand Down Expand Up @@ -354,6 +375,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 @@ -369,6 +400,7 @@ impl Stats<'_> {
issuer_id,
issuer_path,
reasons,
assets,
})
}

Expand Down Expand Up @@ -479,6 +511,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 72d58f6

Please sign in to comment.