Skip to content

Commit

Permalink
refactor(turbopack): Cleanup tree shaking graph logic (#72836)
Browse files Browse the repository at this point in the history
### 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?
  • Loading branch information
kdy1 authored Nov 15, 2024
1 parent 4740839 commit 55f9fd1
Show file tree
Hide file tree
Showing 25 changed files with 17 additions and 378 deletions.
25 changes: 7 additions & 18 deletions turbopack/crates/turbopack-ecmascript/src/tree_shake/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl DepGraph {
data: &FxHashMap<ItemId, ItemData>,
) -> SplitModuleResult {
let groups = self.finalize(data);
let mut exports = FxHashMap::default();
let mut outputs = FxHashMap::default();
let mut part_deps = FxHashMap::<_, Vec<PartId>>::default();

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

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

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

if let Some(export) = &item.export {
exporter.insert(export.clone(), ix as u32);
}

if let ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
specifiers,
src,
Expand Down Expand Up @@ -345,12 +340,6 @@ impl DepGraph {
})
.collect::<FxIndexSet<_>>();

for item in group {
if let ItemId::Group(ItemIdGroupKind::Export(id, _)) = item {
required_vars.insert(id);
}
}

for id in group {
let data = data.get(id).unwrap();

Expand All @@ -367,7 +356,7 @@ impl DepGraph {
{
if !specifiers.is_empty() {
if let Some(dep) = importer.get(&src.value) {
if *dep != ix as u32 {
if *dep != ix as u32 && part_deps_done.insert(*dep) {
part_deps
.entry(ix as u32)
.or_default()
Expand Down Expand Up @@ -398,7 +387,7 @@ impl DepGraph {
ItemId::Group(ItemIdGroupKind::Export(..)) => {
if let Some(export) = &data[item].export {
use_export_instead_of_declarator = true;
exports.insert(Key::Export(export.as_str().into()), ix as u32);
outputs.insert(Key::Export(export.as_str().into()), ix as u32);

let s = ExportSpecifier::Named(ExportNamedSpecifier {
span: DUMMY_SP,
Expand All @@ -424,7 +413,7 @@ impl DepGraph {
}
}
ItemId::Group(ItemIdGroupKind::ModuleEvaluation) => {
exports.insert(Key::ModuleEvaluation, ix as u32);
outputs.insert(Key::ModuleEvaluation, ix as u32);
}

_ => {}
Expand Down Expand Up @@ -699,7 +688,7 @@ impl DepGraph {
modules.push(chunk);
}

exports.insert(Key::Exports, modules.len() as u32);
outputs.insert(Key::Exports, modules.len() as u32);

for star in &star_reexports {
exports_module
Expand All @@ -710,7 +699,7 @@ impl DepGraph {
modules.push(exports_module);

SplitModuleResult {
entrypoints: exports,
entrypoints: outputs,
part_deps,
modules,
star_reexports,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 55f9fd1

Please sign in to comment.