diff --git a/crates/rspack_core/src/exports_info.rs b/crates/rspack_core/src/exports_info.rs index fbc42a06303..42d3beced1a 100644 --- a/crates/rspack_core/src/exports_info.rs +++ b/crates/rspack_core/src/exports_info.rs @@ -1018,7 +1018,7 @@ impl ExportInfo { .get_max_target() .values() .map(|item| UnResolvedExportInfoTarget { - connection: item.connection.clone(), + connection: item.connection, exports: item.exports.clone(), }) .clone(); diff --git a/crates/rspack_core/src/module_graph/connection.rs b/crates/rspack_core/src/module_graph/connection.rs index ae9e354e256..5b24eb2da8f 100644 --- a/crates/rspack_core/src/module_graph/connection.rs +++ b/crates/rspack_core/src/module_graph/connection.rs @@ -19,7 +19,7 @@ impl From for ConnectionId { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct ModuleGraphConnection { /// The referencing module identifier pub original_module_identifier: Option, @@ -31,7 +31,6 @@ pub struct ModuleGraphConnection { pub dependency_id: DependencyId, active: bool, conditional: bool, - condition: Option, } /// implementing hash by hand because condition maybe a function, which can't be hash @@ -65,17 +64,15 @@ impl ModuleGraphConnection { original_module_identifier: Option, dependency_id: DependencyId, module_identifier: ModuleIdentifier, - condition: Option, + active: bool, + conditional: bool, ) -> Self { - let active = !matches!(condition, Some(DependencyCondition::False)); - let conditional = condition.is_some(); Self { original_module_identifier, module_identifier, dependency_id, active, conditional, - condition, } } @@ -123,7 +120,11 @@ impl ModuleGraphConnection { module_graph: &ModuleGraph, runtime: Option<&RuntimeSpec>, ) -> ConnectionState { - match self.condition.as_ref().expect("should have condition") { + match module_graph + .connection_to_condition + .get(&self) + .expect("should have condition") + { DependencyCondition::False => ConnectionState::Bool(false), DependencyCondition::Fn(f) => f(self, runtime, module_graph), } diff --git a/crates/rspack_core/src/module_graph/mod.rs b/crates/rspack_core/src/module_graph/mod.rs index 04171a7e7e0..af2f748336b 100644 --- a/crates/rspack_core/src/module_graph/mod.rs +++ b/crates/rspack_core/src/module_graph/mod.rs @@ -42,11 +42,11 @@ pub struct ModuleGraph { /// Module graph connections table index for `ConnectionId` connections_map: HashMap, - connection_to_condition: HashMap, import_var_map: IdentifierMap, pub exports_info_map: HashMap, pub export_info_map: HashMap, + connection_to_condition: HashMap, } impl ModuleGraph { @@ -82,13 +82,6 @@ impl ModuleGraph { self.dependencies.insert(*dependency.id(), dependency); } - pub fn get_condition_by_connection_id( - &self, - connection_id: &ConnectionId, - ) -> Option<&DependencyCondition> { - self.connection_to_condition.get(connection_id) - } - pub fn dependency_by_id(&self, dependency_id: &DependencyId) -> Option<&BoxDependency> { self.dependencies.get(dependency_id) } @@ -122,7 +115,7 @@ impl ModuleGraph { dependency: BoxDependency, module_identifier: ModuleIdentifier, ) -> Result<()> { - let module_dependency = dependency.as_module_dependency().is_some(); + let is_module_dependency = dependency.as_module_dependency().is_some(); let dependency_id = *dependency.id(); let condition = dependency .as_module_dependency() @@ -131,28 +124,36 @@ impl ModuleGraph { self .dependency_id_to_module_identifier .insert(dependency_id, module_identifier); - if !module_dependency { + if !is_module_dependency { return Ok(()); } + let active = !matches!(condition, Some(DependencyCondition::False)); + let conditional = condition.is_some(); // TODO: just a placeholder here, finish this when we have basic `getCondition` logic let new_connection = ModuleGraphConnection::new( original_module_identifier, dependency_id, module_identifier, - None, + active, + conditional, ); let connection_id = if let Some(connection_id) = self.connections_map.get(&new_connection) { *connection_id } else { let new_connection_id = ConnectionId::from(self.connections.len()); - self.connections.push(Some(new_connection.clone())); + self.connections.push(Some(new_connection)); self .connections_map .insert(new_connection, new_connection_id); new_connection_id }; + if let Some(condition) = condition { + self + .connection_to_condition + .insert(new_connection, condition); + } self .dependency_id_to_connection_id diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_dependency.rs index 9abdc8db6fc..9b847e9858b 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_dependency.rs @@ -240,21 +240,22 @@ impl ModuleDependency for HarmonyImportDependency { vec![] } - // It's from HarmonyImportSideEffectDependency. + // TODO: It's from HarmonyImportSideEffectDependency. fn get_condition(&self) -> Option { - let id = self.id; - Some(DependencyCondition::Fn(Box::new( - move |_, _, module_graph| { - if let Some(module) = module_graph - .parent_module_by_dependency_id(&id) - .and_then(|module_identifier| module_graph.module_by_identifier(&module_identifier)) - { - module.get_side_effects_connection_state(module_graph, &mut HashSet::default()) - } else { - ConnectionState::Bool(true) - } - }, - ))) + None + // let id = self.id; + // Some(DependencyCondition::Fn(Box::new( + // move |_, _, module_graph| { + // if let Some(module) = module_graph + // .parent_module_by_dependency_id(&id) + // .and_then(|module_identifier| module_graph.module_by_identifier(&module_identifier)) + // { + // module.get_side_effects_connection_state(module_graph, &mut HashSet::default()) + // } else { + // ConnectionState::Bool(true) + // } + // }, + // ))) } // It's from HarmonyImportSideEffectDependency.