diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 3f2d02d7ab4b..89975bc54e39 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -41,11 +41,12 @@ use crate::{ CleanTask, CleanTaskResult, CodeGenerationResult, CodeGenerationResults, CompilationLogger, CompilationLogging, CompilerOptions, ContentHashArgs, ContextDependency, DependencyId, DependencyParents, DependencyType, Entry, EntryData, EntryOptions, Entrypoint, ErrorSpan, - FactorizeQueue, FactorizeTask, FactorizeTaskResult, Filename, Logger, Module, ModuleFactory, - ModuleGraph, ModuleIdentifier, ModuleProfile, NormalModuleSource, PathData, ProcessAssetsArgs, - ProcessDependenciesQueue, ProcessDependenciesResult, ProcessDependenciesTask, RenderManifestArgs, - Resolve, ResolverFactory, RuntimeGlobals, RuntimeModule, RuntimeRequirementsInTreeArgs, - RuntimeSpec, SharedPluginDriver, SourceType, Stats, TaskResult, WorkerTask, + FactorizeQueue, FactorizeTask, FactorizeTaskResult, Filename, Logger, Module, + ModuleCreationCallback, ModuleFactory, ModuleGraph, ModuleIdentifier, ModuleProfile, + NormalModuleSource, PathData, ProcessAssetsArgs, ProcessDependenciesQueue, + ProcessDependenciesResult, ProcessDependenciesTask, RenderManifestArgs, Resolve, ResolverFactory, + RuntimeGlobals, RuntimeModule, RuntimeRequirementsInTreeArgs, RuntimeSpec, SharedPluginDriver, + SourceType, Stats, TaskResult, WorkerTask, }; use crate::{tree_shaking::visitor::OptimizeAnalyzeResult, Context}; @@ -525,6 +526,7 @@ impl Compilation { parent_module .and_then(|m| m.as_normal_module()) .and_then(|module| module.name_for_condition()), + None, ); }); @@ -638,7 +640,10 @@ impl Compilation { .module_by_identifier(original_module_identifier) .expect("Module expected"); + let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); + let mut remaining = sorted_dependencies.len(); for dependencies in sorted_dependencies.into_values() { + let tx = tx.clone(); self.handle_module_creation( &mut factorize_queue, Some(module.identifier()), @@ -650,16 +655,33 @@ impl Compilation { module .as_normal_module() .and_then(|module| module.name_for_condition()), + Some(Box::new(move |_| { + tx.send(()) + .expect("Failed to send callback to process_dependencies"); + })), ); } + drop(tx); - result_tx - .send(Ok(TaskResult::ProcessDependencies(Box::new( + let tx = result_tx.clone(); + + tokio::spawn(async move { + loop { + if remaining == 0 { + break; + } + + rx.recv().await; + remaining -= 1; + } + + tx.send(Ok(TaskResult::ProcessDependencies(Box::new( ProcessDependenciesResult { module_identifier: task.original_module_identifier, }, )))) .expect("Failed to send process dependencies result"); + }); } process_deps_time.end(start); @@ -676,6 +698,7 @@ impl Compilation { dependencies, current_profile, exports_info_related, + callback, } = task_result; if !diagnostics.is_empty() { @@ -738,6 +761,7 @@ impl Compilation { dependencies, is_entry, current_profile, + callback, }); } else { let dep = self @@ -1065,6 +1089,7 @@ impl Compilation { resolve_options: Option>, lazy_visit_modules: std::collections::HashSet, issuer: Option>, + callback: Option, ) { let current_profile = self.options.profile.then(Box::::default); let dependency = dependencies[0].get_dependency(&self.module_graph).clone(); @@ -1095,6 +1120,7 @@ impl Compilation { plugin_driver: self.plugin_driver.clone(), cache: self.cache.clone(), current_profile, + callback, }); } diff --git a/crates/rspack_core/src/compiler/queue.rs b/crates/rspack_core/src/compiler/queue.rs index b5a8a6f10c1a..748abb54bdab 100644 --- a/crates/rspack_core/src/compiler/queue.rs +++ b/crates/rspack_core/src/compiler/queue.rs @@ -9,7 +9,7 @@ use crate::{ ModuleGraph, ModuleGraphModule, ModuleIdentifier, ModuleProfile, Resolve, ResolverFactory, SharedPluginDriver, WorkerQueue, }; -use crate::{DependencyId, ExportInfo, ExportsInfo, UsageState}; +use crate::{BoxModule, DependencyId, ExportInfo, ExportsInfo, UsageState}; #[derive(Debug)] pub enum TaskResult { @@ -41,6 +41,7 @@ pub struct FactorizeTask { pub plugin_driver: SharedPluginDriver, pub cache: Arc, pub current_profile: Option>, + pub callback: Option, } /// a struct temporarily used creating ExportsInfo @@ -50,7 +51,7 @@ pub struct ExportsInfoRelated { pub other_exports_info: ExportInfo, pub side_effects_info: ExportInfo, } -#[derive(Debug)] + pub struct FactorizeTaskResult { pub original_module_identifier: Option, /// Result will be available if [crate::ModuleFactory::create] returns `Ok`. @@ -62,6 +63,25 @@ pub struct FactorizeTaskResult { pub is_entry: bool, pub current_profile: Option>, pub exports_info_related: ExportsInfoRelated, + pub callback: Option, +} + +impl std::fmt::Debug for FactorizeTaskResult { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FactorizeTaskResult") + .field( + "original_module_identifier", + &self.original_module_identifier, + ) + .field("factory_result", &self.factory_result) + .field("module_graph_module", &self.module_graph_module) + .field("dependencies", &self.dependencies) + .field("diagnostics", &self.diagnostics) + .field("is_entry", &self.is_entry) + .field("current_profile", &self.current_profile) + .field("exports_info_related", &self.exports_info_related) + .finish() + } } impl FactorizeTaskResult { @@ -118,6 +138,7 @@ impl WorkerTask for FactorizeTask { diagnostics: vec![], module_graph_module: None, factory_result: None, + callback: self.callback, }; match self @@ -180,6 +201,7 @@ pub struct AddTask { pub dependencies: Vec, pub is_entry: bool, pub current_profile: Option>, + pub callback: Option, } #[derive(Debug)] @@ -212,6 +234,10 @@ impl AddTask { module_identifier, )?; + if let Some(callback) = self.callback { + callback(&self.module); + } + return Ok(TaskResult::Add(Box::new(AddTaskResult::ModuleReused { module: self.module, }))); @@ -238,6 +264,10 @@ impl AddTask { current_profile.mark_integration_end(); } + if let Some(callback) = self.callback { + callback(&self.module); + } + Ok(TaskResult::Add(Box::new(AddTaskResult::ModuleAdded { module: self.module, current_profile: self.current_profile, @@ -419,3 +449,5 @@ impl CleanTask { } pub type CleanQueue = WorkerQueue; + +pub type ModuleCreationCallback = Box;