Skip to content

Commit

Permalink
feat: allow ModuleFactoryCreateData modify dependencies (#7895)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrykingxyz authored Sep 14, 2024
1 parent 0325a3d commit 9dff544
Show file tree
Hide file tree
Showing 20 changed files with 62 additions and 81 deletions.
11 changes: 4 additions & 7 deletions crates/node_binding/src/plugins/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,8 +1267,7 @@ impl CompilationAfterSeal for CompilationAfterSealTap {
#[async_trait]
impl NormalModuleFactoryBeforeResolve for NormalModuleFactoryBeforeResolveTap {
async fn run(&self, data: &mut ModuleFactoryCreateData) -> rspack_error::Result<Option<bool>> {
let dependency = data
.dependency
let dependency = data.dependencies[0]
.as_module_dependency_mut()
.expect("should be module dependency");
match self
Expand Down Expand Up @@ -1304,8 +1303,7 @@ impl NormalModuleFactoryFactorize for NormalModuleFactoryFactorizeTap {
&self,
data: &mut ModuleFactoryCreateData,
) -> rspack_error::Result<Option<BoxModule>> {
let dependency = data
.dependency
let dependency = data.dependencies[0]
.as_module_dependency_mut()
.expect("should be module dependency");
match self
Expand Down Expand Up @@ -1342,8 +1340,7 @@ impl NormalModuleFactoryResolve for NormalModuleFactoryResolveTap {
&self,
data: &mut ModuleFactoryCreateData,
) -> rspack_error::Result<Option<NormalModuleFactoryResolveResult>> {
let dependency = data
.dependency
let dependency = data.dependencies[0]
.as_module_dependency_mut()
.expect("should be module dependency");
match self
Expand Down Expand Up @@ -1486,7 +1483,7 @@ impl NormalModuleFactoryCreateModule for NormalModuleFactoryCreateModuleTap {
self
.function
.call_with_promise(JsNormalModuleFactoryCreateModuleArgs {
dependency_type: data.dependency.dependency_type().to_string(),
dependency_type: data.dependencies[0].dependency_type().to_string(),
raw_request: create_data.raw_request.clone(),
resource_resolve_data: create_data.resource_resolve_data.clone().into(),
context: data.context.to_string(),
Expand Down
13 changes: 9 additions & 4 deletions crates/rspack_core/src/compiler/make/repair/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use super::{build::BuildTask, MakeTaskContext};
use crate::{
module_graph::{ModuleGraph, ModuleGraphModule},
utils::task_loop::{Task, TaskResult, TaskType},
DependencyId, Module, ModuleIdentifier, ModuleProfile,
BoxDependency, Module, ModuleIdentifier, ModuleProfile,
};

#[derive(Debug)]
pub struct AddTask {
pub original_module_identifier: Option<ModuleIdentifier>,
pub module: Box<dyn Module>,
pub module_graph_module: Box<ModuleGraphModule>,
pub dependencies: Vec<DependencyId>,
pub dependencies: Vec<BoxDependency>,
pub current_profile: Option<Box<ModuleProfile>>,
}

Expand Down Expand Up @@ -84,11 +84,16 @@ impl Task<MakeTaskContext> for AddTask {
fn set_resolved_module(
module_graph: &mut ModuleGraph,
original_module_identifier: Option<ModuleIdentifier>,
dependencies: Vec<DependencyId>,
dependencies: Vec<BoxDependency>,
module_identifier: ModuleIdentifier,
) -> Result<()> {
for dependency in dependencies {
module_graph.set_resolved_module(original_module_identifier, dependency, module_identifier)?;
module_graph.set_resolved_module(
original_module_identifier,
*dependency.id(),
module_identifier,
)?;
module_graph.add_dependency(dependency);
}
Ok(())
}
36 changes: 19 additions & 17 deletions crates/rspack_core/src/compiler/make/repair/factorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use super::{add::AddTask, MakeTaskContext};
use crate::{
module_graph::ModuleGraphModule,
utils::task_loop::{Task, TaskResult, TaskType},
BoxDependency, CompilerOptions, Context, DependencyId, ExportInfoData, ExportsInfoData,
ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleLayer,
ModuleProfile, Resolve,
BoxDependency, CompilerOptions, Context, ExportInfoData, ExportsInfoData, ModuleFactory,
ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleLayer, ModuleProfile,
Resolve,
};

#[derive(Debug)]
Expand All @@ -21,8 +21,7 @@ pub struct FactorizeTask {
pub original_module_context: Option<Box<Context>>,
pub issuer: Option<Box<str>>,
pub issuer_layer: Option<ModuleLayer>,
pub dependency: BoxDependency,
pub dependencies: Vec<DependencyId>,
pub dependencies: Vec<BoxDependency>,
pub resolve_options: Option<Box<Resolve>>,
pub options: Arc<CompilerOptions>,
pub current_profile: Option<Box<ModuleProfile>>,
Expand All @@ -37,7 +36,7 @@ impl Task<MakeTaskContext> for FactorizeTask {
if let Some(current_profile) = &self.current_profile {
current_profile.mark_factory_start();
}
let dependency = self.dependency;
let dependency = &self.dependencies[0];

let context = if let Some(context) = dependency.get_context()
&& !context.is_empty()
Expand All @@ -64,7 +63,7 @@ impl Task<MakeTaskContext> for FactorizeTask {
// dependency: dep_id,
original_module_identifier: self.original_module_identifier,
factory_result: None,
dependencies: self.dependencies,
dependencies: vec![],
current_profile: self.current_profile,
exports_info_related: ExportsInfoRelated {
exports_info,
Expand All @@ -83,7 +82,7 @@ impl Task<MakeTaskContext> for FactorizeTask {
resolve_options: self.resolve_options,
options: self.options.clone(),
context,
dependency,
dependencies: self.dependencies,
issuer: self.issuer,
issuer_identifier: self.original_module_identifier,
issuer_layer,
Expand All @@ -101,6 +100,7 @@ impl Task<MakeTaskContext> for FactorizeTask {
let diagnostics = create_data.diagnostics.drain(..).collect();
Ok(vec![Box::new(
factorize_result_task
.with_dependencies(create_data.dependencies)
.with_factory_result(Some(result))
.with_diagnostics(diagnostics)
.with_file_dependencies(create_data.file_dependencies.drain())
Expand All @@ -125,11 +125,12 @@ impl Task<MakeTaskContext> for FactorizeTask {
return Err(e);
}
let mut diagnostics = Vec::with_capacity(create_data.diagnostics.len() + 1);
diagnostics.push(Into::<Diagnostic>::into(e).with_loc(create_data.dependency.loc()));
diagnostics.push(Into::<Diagnostic>::into(e).with_loc(create_data.dependencies[0].loc()));
diagnostics.append(&mut create_data.diagnostics);
// Continue bundling if `options.bail` set to `false`.
Ok(vec![Box::new(
factorize_result_task
.with_dependencies(create_data.dependencies)
.with_diagnostics(diagnostics)
.with_file_dependencies(create_data.file_dependencies.drain())
.with_missing_dependencies(create_data.missing_dependencies.drain())
Expand All @@ -154,7 +155,7 @@ pub struct FactorizeResultTask {
pub original_module_identifier: Option<ModuleIdentifier>,
/// Result will be available if [crate::ModuleFactory::create] returns `Ok`.
pub factory_result: Option<ModuleFactoryResult>,
pub dependencies: Vec<DependencyId>,
pub dependencies: Vec<BoxDependency>,
pub current_profile: Option<Box<ModuleProfile>>,
pub exports_info_related: ExportsInfoRelated,

Expand All @@ -165,6 +166,11 @@ pub struct FactorizeResultTask {
}

impl FactorizeResultTask {
fn with_dependencies(mut self, dependencies: Vec<BoxDependency>) -> Self {
self.dependencies = dependencies;
self
}

fn with_factory_result(mut self, factory_result: Option<ModuleFactoryResult>) -> Self {
self.factory_result = factory_result;
self
Expand Down Expand Up @@ -214,7 +220,7 @@ impl Task<MakeTaskContext> for FactorizeResultTask {
} else {
artifact
.make_failed_dependencies
.insert((dependencies[0], None));
.insert((*dependencies[0].id(), None));
}
}

Expand All @@ -236,17 +242,13 @@ impl Task<MakeTaskContext> for FactorizeResultTask {
let module_graph =
&mut MakeTaskContext::get_module_graph_mut(&mut artifact.module_graph_partial);
let Some(factory_result) = factory_result else {
let dep = module_graph
.dependency_by_id(&dependencies[0])
.expect("dep should available");
let dep = &dependencies[0];
tracing::trace!("Module created with failure, but without bailout: {dep:?}");
return Ok(vec![]);
};

let Some(module) = factory_result.module else {
let dep = module_graph
.dependency_by_id(&dependencies[0])
.expect("dep should available");
let dep = &dependencies[0];
tracing::trace!("Module ignored: {dep:?}");
return Ok(vec![]);
};
Expand Down
3 changes: 1 addition & 2 deletions crates/rspack_core/src/compiler/make/repair/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ pub fn repair(
.and_then(|module| module.name_for_condition()),
issuer_layer: parent_module.and_then(|m| m.get_layer()).cloned(),
original_module_context: parent_module.and_then(|m| m.get_context()),
dependency: dependency.clone(),
dependencies: vec![id],
dependencies: vec![dependency.clone()],
resolve_options: parent_module.and_then(|module| module.get_resolve_options()),
options: compilation.options.clone(),
current_profile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Task<MakeTaskContext> for ProcessDependenciesTask {
sorted_dependencies
.entry(resource_identifier)
.or_insert(vec![])
.push(dependency_id);
.push(dependency.clone());
}
}

Expand All @@ -71,10 +71,6 @@ impl Task<MakeTaskContext> for ProcessDependenciesTask {
.compiler_options
.profile
.then(Box::<ModuleProfile>::default);
let dependency = module_graph
.dependency_by_id(&dependencies[0])
.expect("should have dependency")
.clone();
let original_module_source = module_graph
.module_by_identifier(&original_module_identifier)
.and_then(|m| m.as_normal_module())
Expand All @@ -85,6 +81,7 @@ impl Task<MakeTaskContext> for ProcessDependenciesTask {
None
}
});
let dependency = &dependencies[0];
let dependency_type = dependency.dependency_type();
// TODO move module_factory calculate to dependency factories
let module_factory = context
Expand All @@ -107,7 +104,6 @@ impl Task<MakeTaskContext> for ProcessDependenciesTask {
.as_normal_module()
.and_then(|module| module.name_for_condition()),
issuer_layer: module.get_layer().cloned(),
dependency,
dependencies,
resolve_options: module.get_resolve_options(),
options: context.compiler_options.clone(),
Expand Down
4 changes: 1 addition & 3 deletions crates/rspack_core/src/compiler/module_executor/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ impl Task<MakeTaskContext> for EntryTask {
Ok(vec![])
}
EntryParam::Entry(dep) => {
let dep_id = *dep.id();
module_graph.add_dependency(dep.clone());
Ok(vec![Box::new(FactorizeTask {
module_factory: context
Expand All @@ -58,8 +57,7 @@ impl Task<MakeTaskContext> for EntryTask {
issuer: None,
issuer_layer: None,
original_module_context: None,
dependency: dep,
dependencies: vec![dep_id],
dependencies: vec![dep],
resolve_options: None,
options: context.compiler_options.clone(),
current_profile: context
Expand Down
12 changes: 2 additions & 10 deletions crates/rspack_core/src/compiler/module_executor/overwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ impl Task<MakeTaskContext> for OverwriteTask {
// factorize result task
if let Some(factorize_result_task) = origin_task.as_any().downcast_ref::<FactorizeResultTask>()
{
let dep_id = factorize_result_task
.dependencies
.first()
.copied()
.expect("should have dep_id");
let dep_id = *factorize_result_task.dependencies[0].id();
let original_module_identifier = factorize_result_task.original_module_identifier;
let res = origin_task.sync_run(context)?;
if res.is_empty() {
Expand All @@ -57,11 +53,7 @@ impl Task<MakeTaskContext> for OverwriteTask {
}
// add task
if let Some(add_task) = origin_task.as_any().downcast_ref::<AddTask>() {
let dep_id = add_task
.dependencies
.first()
.copied()
.expect("should have dep_id");
let dep_id = *add_task.dependencies[0].id();
let original_module_identifier = add_task.original_module_identifier;
let target_module_identifier = add_task.module.identifier();

Expand Down
3 changes: 1 addition & 2 deletions crates/rspack_core/src/context_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ impl ContextModuleFactory {
data: &mut ModuleFactoryCreateData,
) -> Result<(ModuleFactoryResult, Option<ContextModuleOptions>)> {
let plugin_driver = &self.plugin_driver;
let dependency = data
.dependency
let dependency = data.dependencies[0]
.as_context_dependency()
.expect("should be context dependency");
let mut file_dependencies = Default::default();
Expand Down
11 changes: 7 additions & 4 deletions crates/rspack_core/src/module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct ModuleFactoryCreateData {
pub resolve_options: Option<Box<Resolve>>,
pub options: Arc<CompilerOptions>,
pub context: Context,
pub dependency: BoxDependency,
pub dependencies: Vec<BoxDependency>,
pub issuer: Option<Box<str>>,
pub issuer_identifier: Option<ModuleIdentifier>,
pub issuer_layer: Option<ModuleLayer>,
Expand All @@ -25,11 +25,14 @@ pub struct ModuleFactoryCreateData {

impl ModuleFactoryCreateData {
pub fn request(&self) -> Option<&str> {
self
.dependency
self.dependencies[0]
.as_module_dependency()
.map(|d| d.request())
.or_else(|| self.dependency.as_context_dependency().map(|d| d.request()))
.or_else(|| {
self.dependencies[0]
.as_context_dependency()
.map(|d| d.request())
})
}

pub fn add_file_dependency(&mut self, file: PathBuf) {
Expand Down
7 changes: 3 additions & 4 deletions crates/rspack_core/src/normal_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ impl NormalModuleFactory {
&self,
data: &mut ModuleFactoryCreateData,
) -> Result<Option<ModuleFactoryResult>> {
let dependency = data
.dependency
let dependency = data.dependencies[0]
.as_module_dependency()
.expect("should be module dependency");
let dependency_type = *dependency.dependency_type();
Expand Down Expand Up @@ -216,7 +215,7 @@ impl NormalModuleFactory {
{
Some((pos, _)) => &request_without_match_resource[pos..],
None => {
unreachable!("Invalid dependency: {:?}", &data.dependency)
unreachable!("Invalid dependency: {:?}", &data.dependencies[0])
}
}
} else {
Expand Down Expand Up @@ -389,7 +388,7 @@ impl NormalModuleFactory {
} else {
&resource_data
},
data.dependency.as_ref(),
data.dependencies[0].as_ref(),
data.issuer.as_deref(),
data.issuer_layer.as_deref(),
)
Expand Down
3 changes: 1 addition & 2 deletions crates/rspack_plugin_externals/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ impl ExternalsPlugin {

#[plugin_hook(NormalModuleFactoryFactorize for ExternalsPlugin)]
async fn factorize(&self, data: &mut ModuleFactoryCreateData) -> Result<Option<BoxModule>> {
let dependency = data
.dependency
let dependency = data.dependencies[0]
.as_module_dependency()
.expect("should be module dependency");
let context = &data.context;
Expand Down
3 changes: 1 addition & 2 deletions crates/rspack_plugin_extract_css/src/css_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ pub(crate) struct CssModuleFactory;
#[async_trait::async_trait]
impl ModuleFactory for CssModuleFactory {
async fn create(&self, data: &mut ModuleFactoryCreateData) -> Result<ModuleFactoryResult> {
let css_dep = data
.dependency
let css_dep = data.dependencies[0]
.downcast_ref::<CssDependency>()
.expect("unreachable");

Expand Down
3 changes: 1 addition & 2 deletions crates/rspack_plugin_lazy_compilation/src/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ pub(crate) struct LazyCompilationDependency {

impl LazyCompilationDependency {
pub fn new(original_module_create_data: ModuleFactoryCreateData) -> Self {
let dep = original_module_create_data
.dependency
let dep = original_module_create_data.dependencies[0]
.as_module_dependency()
.expect("LazyCompilation: should convert to module dependency");
let request = dep.request().to_string();
Expand Down
Loading

2 comments on commit 9dff544

@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-09-14 0325a3d) Current Change
10000_development-mode + exec 2.21 s ± 28 ms 2.23 s ± 44 ms +0.51 %
10000_development-mode_hmr + exec 692 ms ± 21 ms 686 ms ± 9.1 ms -0.77 %
10000_production-mode + exec 2.83 s ± 40 ms 2.83 s ± 40 ms -0.22 %
arco-pro_development-mode + exec 1.81 s ± 84 ms 1.84 s ± 75 ms +1.53 %
arco-pro_development-mode_hmr + exec 434 ms ± 3.8 ms 433 ms ± 4.4 ms -0.41 %
arco-pro_production-mode + exec 3.25 s ± 78 ms 3.22 s ± 80 ms -1.01 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.29 s ± 75 ms 3.34 s ± 168 ms +1.46 %
threejs_development-mode_10x + exec 1.68 s ± 20 ms 1.67 s ± 15 ms -0.79 %
threejs_development-mode_10x_hmr + exec 780 ms ± 13 ms 782 ms ± 9.2 ms +0.19 %
threejs_production-mode_10x + exec 5.17 s ± 16 ms 5.16 s ± 20 ms -0.16 %

@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 ✅ success
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
examples ✅ success

Please sign in to comment.