Skip to content

Commit

Permalink
Revert "Preserve exports if content is preserved"
Browse files Browse the repository at this point in the history
This reverts commit 6b66991.
  • Loading branch information
kdy1 committed Sep 6, 2024
1 parent 6b66991 commit 320d40d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 53 deletions.
11 changes: 3 additions & 8 deletions turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use turbopack_core::{

use super::{
chunk_item::EcmascriptModulePartChunkItem, get_part_id, part_of_module, split, split_module,
Key, PartId, SplitResult,
Key, SplitResult,
};
use crate::{
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
Expand Down Expand Up @@ -219,16 +219,11 @@ impl Module for EcmascriptModulePartAsset {

let mut assets = deps
.iter()
.map(|part_id| {
.map(|&part_id| {
Ok(Vc::upcast(SingleModuleReference::new(
Vc::upcast(EcmascriptModulePartAsset::new(
self.full_module,
match part_id {
PartId::Internal(part_id) => ModulePart::internal(*part_id),
PartId::Export(name) => ModulePart::export(name.clone()),
PartId::ModuleEvaluation => ModulePart::evaluation(),
PartId::Exports => ModulePart::exports(),
},
ModulePart::internal(part_id),
self.import_externals,
)),
Vc::cell("ecmascript module part".into()),
Expand Down
48 changes: 4 additions & 44 deletions turbopack/crates/turbopack-ecmascript/src/tree_shake/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub(super) struct SplitModuleResult {
pub entrypoints: FxHashMap<Key, u32>,

/// Dependency between parts.
pub part_deps: FxHashMap<u32, Vec<PartId>>,
pub part_deps: FxHashMap<u32, Vec<u32>>,
pub modules: Vec<Module>,

pub star_reexports: Vec<ExportAll>,
Expand Down Expand Up @@ -248,7 +248,7 @@ impl DepGraph {
) -> SplitModuleResult {
let groups = self.finalize(data);
let mut exports = FxHashMap::default();
let mut part_deps = FxHashMap::<_, Vec<PartId>>::default();
let mut part_deps = FxHashMap::<_, Vec<_>>::default();

let star_reexports: Vec<_> = data
.values()
Expand All @@ -270,7 +270,6 @@ impl DepGraph {
}

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 @@ -279,10 +278,6 @@ impl DepGraph {
for var in item.var_decls.iter() {
declarator.entry(var.clone()).or_insert_with(|| ix as u32);
}

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

Expand Down Expand Up @@ -393,7 +388,7 @@ impl DepGraph {
}

// Import variables
for &var in &required_vars {
for var in required_vars {
let Some(&dep) = declarator.get(var) else {
continue;
};
Expand All @@ -412,10 +407,7 @@ impl DepGraph {
is_type_only: false,
})];

part_deps
.entry(ix as u32)
.or_default()
.push(PartId::Internal(dep));
part_deps.entry(ix as u32).or_default().push(dep);

chunk
.body
Expand All @@ -431,38 +423,6 @@ impl DepGraph {
})));
}

// Depend on exports.
//
// We preserve the export if the content is preserved.
// It's for server actions, where we need to preserve the export if the content is used.
for &var in &required_vars {
let Some(&dep) = exporter.get(&var.0) else {
continue;
};

if dep == ix as u32 {
continue;
}

part_deps
.entry(ix as u32)
.or_default()
.push(PartId::Export(var.0.as_str().into()));

chunk
.body
.push(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
span: DUMMY_SP,
specifiers: vec![],
src: Box::new(TURBOPACK_PART_IMPORT_SOURCE.into()),
type_only: false,
with: Some(Box::new(create_turbopack_part_id_assert(PartId::Export(
var.0.as_str().into(),
)))),
phase: Default::default(),
})));
}

for g in group {
// Skip directives, as we copy them to each modules.
if let ModuleItem::Stmt(Stmt::Expr(ExprStmt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ pub(crate) enum SplitResult {
modules: Vec<Vc<ParseResult>>,

#[turbo_tasks(trace_ignore)]
deps: FxHashMap<u32, Vec<PartId>>,
deps: FxHashMap<u32, Vec<u32>>,

#[turbo_tasks(debug_ignore, trace_ignore)]
star_reexports: Vec<ExportAll>,
Expand Down

0 comments on commit 320d40d

Please sign in to comment.