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::{ ...@@ -9,7 +9,7 @@ use turbopack_core::{
ident::AssetIdent, ident::AssetIdent,
module::Module, module::Module,
module_graph::ModuleGraph, module_graph::ModuleGraph,
reference::{ModuleReference, ModuleReferences, SingleModuleReference}, reference::{ModuleReference, ModuleReferences},
resolve::{origin::ResolveOrigin, ModulePart}, resolve::{origin::ResolveOrigin, ModulePart},
}; };
...@@ -24,7 +24,9 @@ use crate::{ ...@@ -24,7 +24,9 @@ use crate::{
analyse_ecmascript_module, esm::FoundExportType, follow_reexports, FollowExportsResult, analyse_ecmascript_module, esm::FoundExportType, follow_reexports, FollowExportsResult,
}, },
side_effect_optimization::facade::module::EcmascriptModuleFacadeModule, 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, AnalyzeEcmascriptModuleResult, EcmascriptAnalyzable, EcmascriptModuleAsset,
EcmascriptModuleAssetType, EcmascriptModuleContent, EcmascriptParsable, EcmascriptModuleAssetType, EcmascriptModuleContent, EcmascriptParsable,
}; };
...@@ -289,40 +291,34 @@ impl Module for EcmascriptModulePartAsset { ...@@ -289,40 +291,34 @@ impl Module for EcmascriptModulePartAsset {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn references(&self) -> Result<Vc<ModuleReferences>> { 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>> { let part_dep = |part: ModulePart| -> Vc<Box<dyn ModuleReference>> {
Vc::upcast(SingleModuleReference::new( Vc::upcast(EcmascriptModulePartReference::new(*self.full_module, part))
Vc::upcast(EcmascriptModulePartAsset::new(*self.full_module, part)),
Vc::cell("ecmascript module part".into()),
))
}; };
let mut references = analyze.references().owned().await?; if let ModulePart::Facade = self.part {
// Facade depends on evaluation and re-exports
// Facade depends on evaluation and re-exports let mut references = vec![];
if self.part == ModulePart::Facade {
references.push(part_dep(ModulePart::evaluation()).to_resolved().await?); references.push(part_dep(ModulePart::evaluation()).to_resolved().await?);
references.push(part_dep(ModulePart::exports()).to_resolved().await?); references.push(part_dep(ModulePart::exports()).to_resolved().await?);
return Ok(Vc::cell(references)); return Ok(Vc::cell(references));
} }
let deps = { let split_data = split_module(*self.full_module).await?;
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) { let SplitResult::Ok { deps, .. } = &*split_data else {
Some(v) => &**v, // If the module is not split, we don't need to add any references
None => &[], 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( references.extend(
...@@ -346,6 +342,10 @@ impl Module for EcmascriptModulePartAsset { ...@@ -346,6 +342,10 @@ impl Module for EcmascriptModulePartAsset {
.await?, .await?,
); );
let analyze = analyze(*self.full_module, self.part.clone());
references.extend(analyze.references().owned().await?);
Ok(Vc::cell(references)) Ok(Vc::cell(references))
} }
} }
......
...@@ -27,6 +27,7 @@ pub mod chunk_item; ...@@ -27,6 +27,7 @@ pub mod chunk_item;
mod graph; mod graph;
pub mod merge; pub mod merge;
mod optimizations; mod optimizations;
pub mod reference;
pub mod side_effect_module; pub mod side_effect_module;
#[cfg(test)] #[cfg(test)]
mod tests; 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