Skip to content

Commit 55f9fd1

Browse files
authored
refactor(turbopack): Cleanup tree shaking graph logic (#72836)
### What? Cleanup logic. - Remove duplicate imports. - Rename `exports` to `outputs` as it's more intuitive. - Do not track the `ix` of each exports, as it's not used. ### Why? ### How?
1 parent 4740839 commit 55f9fd1

File tree

25 files changed

+17
-378
lines changed

25 files changed

+17
-378
lines changed

turbopack/crates/turbopack-ecmascript/src/tree_shake/graph.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl DepGraph {
261261
data: &FxHashMap<ItemId, ItemData>,
262262
) -> SplitModuleResult {
263263
let groups = self.finalize(data);
264-
let mut exports = FxHashMap::default();
264+
let mut outputs = FxHashMap::default();
265265
let mut part_deps = FxHashMap::<_, Vec<PartId>>::default();
266266

267267
let star_reexports: Vec<_> = data
@@ -280,15 +280,14 @@ impl DepGraph {
280280
body: data.values().map(|v| v.content.clone()).collect(),
281281
shebang: None,
282282
});
283-
exports.insert(Key::ModuleEvaluation, 0);
283+
outputs.insert(Key::ModuleEvaluation, 0);
284284
}
285285

286286
// See https://github.com/vercel/next.js/pull/71234#issuecomment-2409810084
287287
// ImportBinding should depend on actual import statements because those imports may have
288288
// side effects.
289289
let mut importer = FxHashMap::default();
290290
let mut declarator = FxHashMap::default();
291-
let mut exporter = FxHashMap::default();
292291

293292
for (ix, group) in groups.graph_ix.iter().enumerate() {
294293
for id in group {
@@ -298,10 +297,6 @@ impl DepGraph {
298297
declarator.entry(var.clone()).or_insert_with(|| ix as u32);
299298
}
300299

301-
if let Some(export) = &item.export {
302-
exporter.insert(export.clone(), ix as u32);
303-
}
304-
305300
if let ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
306301
specifiers,
307302
src,
@@ -345,12 +340,6 @@ impl DepGraph {
345340
})
346341
.collect::<FxIndexSet<_>>();
347342

348-
for item in group {
349-
if let ItemId::Group(ItemIdGroupKind::Export(id, _)) = item {
350-
required_vars.insert(id);
351-
}
352-
}
353-
354343
for id in group {
355344
let data = data.get(id).unwrap();
356345

@@ -367,7 +356,7 @@ impl DepGraph {
367356
{
368357
if !specifiers.is_empty() {
369358
if let Some(dep) = importer.get(&src.value) {
370-
if *dep != ix as u32 {
359+
if *dep != ix as u32 && part_deps_done.insert(*dep) {
371360
part_deps
372361
.entry(ix as u32)
373362
.or_default()
@@ -398,7 +387,7 @@ impl DepGraph {
398387
ItemId::Group(ItemIdGroupKind::Export(..)) => {
399388
if let Some(export) = &data[item].export {
400389
use_export_instead_of_declarator = true;
401-
exports.insert(Key::Export(export.as_str().into()), ix as u32);
390+
outputs.insert(Key::Export(export.as_str().into()), ix as u32);
402391

403392
let s = ExportSpecifier::Named(ExportNamedSpecifier {
404393
span: DUMMY_SP,
@@ -424,7 +413,7 @@ impl DepGraph {
424413
}
425414
}
426415
ItemId::Group(ItemIdGroupKind::ModuleEvaluation) => {
427-
exports.insert(Key::ModuleEvaluation, ix as u32);
416+
outputs.insert(Key::ModuleEvaluation, ix as u32);
428417
}
429418

430419
_ => {}
@@ -699,7 +688,7 @@ impl DepGraph {
699688
modules.push(chunk);
700689
}
701690

702-
exports.insert(Key::Exports, modules.len() as u32);
691+
outputs.insert(Key::Exports, modules.len() as u32);
703692

704693
for star in &star_reexports {
705694
exports_module
@@ -710,7 +699,7 @@ impl DepGraph {
710699
modules.push(exports_module);
711700

712701
SplitModuleResult {
713-
entrypoints: exports,
702+
entrypoints: outputs,
714703
part_deps,
715704
modules,
716705
star_reexports,

turbopack/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/1/output.md

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/2/output.md

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/amphtml-document/output.md

Lines changed: 0 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/app-route/output.md

Lines changed: 0 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/dce/output.md

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)