diff --git a/turbopack/crates/turbopack-ecmascript/src/tree_shake/graph.rs b/turbopack/crates/turbopack-ecmascript/src/tree_shake/graph.rs index 4a66c22dee418d..ebae2efc8dfb24 100644 --- a/turbopack/crates/turbopack-ecmascript/src/tree_shake/graph.rs +++ b/turbopack/crates/turbopack-ecmascript/src/tree_shake/graph.rs @@ -1494,20 +1494,25 @@ impl DepGraph { continue; } + let dependants = g + .edges_directed(declarator, Direction::Incoming) + .map(|e| (e.source(), *e.weight())) + .collect::>(); + // We depend on the export node to preserve the export - queue.push((declarator, node, dependencies)); + queue.push((node, declarator, dependants)); } } - for (original, dependant, dependencies) in queue { + for (node, declarator, dependants) in queue { // Move all edges from node to dependant - for dependency in dependencies { - g.add_edge(dependant, dependency, Dependency::Strong); - } + for (dependant, weight) in dependants { + g.add_edge(dependant, node, weight); - // Move items from original to dependant - let items = g.node_weight(original).expect("Node should exist").clone(); - g.node_weight_mut(dependant).unwrap().extend(items); + if let Some(edge) = g.edges_connecting(dependant, declarator).next() { + g.remove_edge(edge.id()); + } + } } } }