Skip to content

Commit

Permalink
refactor(turbopack): Cleanup code for tree shaking (#70385)
Browse files Browse the repository at this point in the history
### What?

Minor refactorings extracted from other PRs.

### Why?

To make WIP small
  • Loading branch information
kdy1 authored Sep 25, 2024
1 parent 4fdfca9 commit dfd5bb3
Showing 1 changed file with 5 additions and 53 deletions.
58 changes: 5 additions & 53 deletions turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use turbopack_core::{

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

let analyze = analyze(self.full_module, self.part).await?;

let (deps, entrypoints) = match &*split_data {
SplitResult::Ok {
deps, entrypoints, ..
} => (deps, entrypoints),
let deps = match &*split_data {
SplitResult::Ok { deps, .. } => deps,
SplitResult::Failed { .. } => return Ok(analyze.references),
};

// Facade depends on evaluation and re-exports
if matches!(&*self.part.await?, ModulePart::Facade) {
let mut references = vec![];

let reference = Vc::upcast(SingleModuleReference::new(
Vc::upcast(EcmascriptModulePartAsset::new(
self.full_module,
ModulePart::evaluation(),
)),
Vc::cell("ecmascript module evaluation".into()),
));

references.push(reference);

let reference = Vc::upcast(SingleModuleReference::new(
Vc::upcast(EcmascriptModulePartAsset::new(
self.full_module,
ModulePart::exports(),
)),
Vc::cell("ecmascript reexports".into()),
));

references.push(reference);

references.extend(analyze.references.await?.iter().cloned());

return Ok(Vc::cell(references));
if matches!(&*self.part.await?, ModulePart::Facade | ModulePart::Exports) {
return Ok(analyze.references);
}

// ModulePart::Exports contains all reexports and a reexport of the Locals
if matches!(&*self.part.await?, ModulePart::Exports) {
let mut references = vec![];

for key in entrypoints.keys() {
if let Key::Export(e) = key {
let reference = Vc::upcast(SingleModuleReference::new(
Vc::upcast(EcmascriptModulePartAsset::new(
self.full_module,
ModulePart::export(e.clone()),
)),
Vc::cell(format!("ecmascript export '{e}'").into()),
));

references.push(reference);
}
}

references.extend(analyze.references.await?.iter().cloned());

return Ok(Vc::cell(references));
}
let deps = {
let part_id = get_part_id(&split_data, self.part)
.await
Expand Down

0 comments on commit dfd5bb3

Please sign in to comment.