Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(turbopack): Cleanup tree shaking graph logic #72836

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading