Skip to content

Commit

Permalink
Preserve exports if content is preserved
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Sep 6, 2024
1 parent a8b9440 commit 6b66991
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
11 changes: 8 additions & 3 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, SplitResult,
Key, PartId, SplitResult,
};
use crate::{
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
Expand Down Expand Up @@ -219,11 +219,16 @@ 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,
ModulePart::internal(part_id),
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(),
},
self.import_externals,
)),
Vc::cell("ecmascript module part".into()),
Expand Down
48 changes: 44 additions & 4 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<u32>>,
pub part_deps: FxHashMap<u32, Vec<PartId>>,
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<_>>::default();
let mut part_deps = FxHashMap::<_, Vec<PartId>>::default();

let star_reexports: Vec<_> = data
.values()
Expand All @@ -270,6 +270,7 @@ 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 @@ -278,6 +279,10 @@ 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 @@ -388,7 +393,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 @@ -407,7 +412,10 @@ impl DepGraph {
is_type_only: false,
})];

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

chunk
.body
Expand All @@ -423,6 +431,38 @@ 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<u32>>,
deps: FxHashMap<u32, Vec<PartId>>,

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

0 comments on commit 6b66991

Please sign in to comment.