Unverified Commit 2a91953e authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

refactor(turbopack): Prepare removal of fake AST in tree-shaking (#78172)

### What?

Refactor `references` of `EcmascriptModulePartAsset`.

### Why?

I'll remove the fake AST with another PR on the graphite stack.
parent 8cba2141
......@@ -9,7 +9,7 @@ use turbopack_core::{
ident::AssetIdent,
module::Module,
module_graph::ModuleGraph,
reference::{ModuleReference, ModuleReferences, SingleModuleReference},
reference::{ModuleReference, ModuleReferences},
resolve::{origin::ResolveOrigin, ModulePart},
};
......@@ -24,7 +24,9 @@ use crate::{
analyse_ecmascript_module, esm::FoundExportType, follow_reexports, FollowExportsResult,
},
side_effect_optimization::facade::module::EcmascriptModuleFacadeModule,
tree_shake::{side_effect_module::SideEffectsModule, Key},
tree_shake::{
reference::EcmascriptModulePartReference, side_effect_module::SideEffectsModule, Key,
},
AnalyzeEcmascriptModuleResult, EcmascriptAnalyzable, EcmascriptModuleAsset,
EcmascriptModuleAssetType, EcmascriptModuleContent, EcmascriptParsable,
};
......@@ -289,40 +291,34 @@ 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.clone());
let deps = match &*split_data {
SplitResult::Ok { deps, .. } => deps,
SplitResult::Failed { .. } => return Ok(analyze.references()),
};
let part_dep = |part: ModulePart| -> Vc<Box<dyn ModuleReference>> {
Vc::upcast(SingleModuleReference::new(
Vc::upcast(EcmascriptModulePartAsset::new(*self.full_module, part)),
Vc::cell("ecmascript module part".into()),
))
Vc::upcast(EcmascriptModulePartReference::new(*self.full_module, part))
};
let mut references = analyze.references().owned().await?;
// Facade depends on evaluation and re-exports
if self.part == ModulePart::Facade {
if let ModulePart::Facade = self.part {
// Facade depends on evaluation and re-exports
let mut references = vec![];
references.push(part_dep(ModulePart::evaluation()).to_resolved().await?);
references.push(part_dep(ModulePart::exports()).to_resolved().await?);
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))?;
let split_data = split_module(*self.full_module).await?;
match deps.get(&part_id) {
Some(v) => &**v,
None => &[],
}
let SplitResult::Ok { deps, .. } = &*split_data else {
// If the module is not split, we don't need to add any references
return Ok(Vc::cell(vec![]));
};
let mut references = vec![];
let part_id = get_part_id(&split_data, &self.part)
.await
.with_context(|| format!("part {:?} is not found in the module", self.part))?;
let deps = match deps.get(&part_id) {
Some(v) => &**v,
None => &[],
};
references.extend(
......@@ -346,6 +342,10 @@ impl Module for EcmascriptModulePartAsset {
.await?,
);
let analyze = analyze(*self.full_module, self.part.clone());
references.extend(analyze.references().owned().await?);
Ok(Vc::cell(references))
}
}
......
......@@ -27,6 +27,7 @@ pub mod chunk_item;
mod graph;
pub mod merge;
mod optimizations;
pub mod reference;
pub mod side_effect_module;
#[cfg(test)]
mod tests;
......
use anyhow::Result;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbopack_core::{
chunk::{ChunkableModuleReference, ChunkingType, ChunkingTypeOption},
reference::ModuleReference,
resolve::{ModulePart, ModuleResolveResult},
};
use crate::{tree_shake::asset::EcmascriptModulePartAsset, EcmascriptModuleAsset};
/// A reference to the [EcmascriptModuleLocalsModule] variant of an original
/// module.
#[turbo_tasks::value]
pub struct EcmascriptModulePartReference {
pub module: ResolvedVc<EcmascriptModuleAsset>,
pub part: ModulePart,
}
#[turbo_tasks::value_impl]
impl EcmascriptModulePartReference {
#[turbo_tasks::function]
pub fn new(module: ResolvedVc<EcmascriptModuleAsset>, part: ModulePart) -> Vc<Self> {
EcmascriptModulePartReference { module, part }.cell()
}
}
#[turbo_tasks::value_impl]
impl ValueToString for EcmascriptModulePartReference {
#[turbo_tasks::function]
async fn to_string(&self) -> Result<Vc<RcStr>> {
Ok(Vc::cell(self.part.to_string().into()))
}
}
#[turbo_tasks::value_impl]
impl ModuleReference for EcmascriptModulePartReference {
#[turbo_tasks::function]
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
let module = EcmascriptModulePartAsset::new(*self.module, self.part.clone())
.to_resolved()
.await?;
Ok(*ModuleResolveResult::module(ResolvedVc::upcast(module)))
}
}
#[turbo_tasks::value_impl]
impl ChunkableModuleReference for EcmascriptModulePartReference {
#[turbo_tasks::function]
fn chunking_type(self: Vc<Self>) -> Vc<ChunkingTypeOption> {
Vc::cell(Some(ChunkingType::ParallelInheritAsync))
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment