Skip to content

Commit

Permalink
Simplfy EcmascriptModulePartAsset::references
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Oct 2, 2024
1 parent 3313082 commit ea2c600
Showing 1 changed file with 4 additions and 57 deletions.
61 changes: 4 additions & 57 deletions turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use anyhow::{Context, Result};
use anyhow::Result;
use turbo_tasks::Vc;
use turbopack_core::{
asset::{Asset, AssetContent},
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, EvaluatableAsset},
ident::AssetIdent,
module::Module,
reference::{ModuleReference, ModuleReferences, SingleModuleReference},
reference::ModuleReferences,
resolve::ModulePart,
};

use super::{
chunk_item::EcmascriptModulePartChunkItem, get_part_id, part_of_module, split, split_module,
PartId, SplitResult,
chunk_item::EcmascriptModulePartChunkItem, part_of_module, split, split_module, SplitResult,
};
use crate::{
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
Expand Down Expand Up @@ -134,60 +133,8 @@ impl Module for EcmascriptModulePartAsset {

#[turbo_tasks::function]
async fn references(&self) -> Result<Vc<ModuleReferences>> {
let split_data = split_module(self.full_module).await?;

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

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

let part_dep = |part: Vc<ModulePart>| -> Vc<Box<dyn ModuleReference>> {
Vc::upcast(SingleModuleReference::new(
Vc::upcast(EcmascriptModulePartAsset::new(self.full_module, part)),
Vc::cell("ecmascript module part".into()),
))
};

let mut references = analyze.references.await?.to_vec();

// Facade depends on evaluation and re-exports
if matches!(&*self.part.await?, ModulePart::Facade) {
references.push(part_dep(ModulePart::evaluation()));
references.push(part_dep(ModulePart::exports()));
return Ok(Vc::cell(references));
}

let deps = {
let part_id = get_part_id(&split_data, self.part)
.await
.with_context(|| format!("part {:?} is not found in the module", self.part))?;

match deps.get(&part_id) {
Some(v) => &**v,
None => &[],
}
};

references.extend(
deps.iter()
.filter_map(|part_id| {
Some(part_dep(match part_id {
// This is an internal part that is not for evaluation, so we don't need to
// force-add it.
PartId::Internal(.., false) => return None,
PartId::Internal(part_id, true) => ModulePart::internal(*part_id),
PartId::Export(name) => ModulePart::export(name.clone()),
_ => unreachable!(
"PartId other than Internal and Export should not be used here"
),
}))
})
.collect::<Vec<_>>(),
);

Ok(Vc::cell(references))
Ok(analyze.references)
}
}

Expand Down

0 comments on commit ea2c600

Please sign in to comment.