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

refactor(turbopack): Remove needless code (#78331)

### What?

- Use `analyze.references()` beacuse we need imports to be in `esm_references`.

### Why?

It's identical but cleaner.


Closes PACK-4397
parent b8675611
use anyhow::{Context, Result};
use anyhow::Result;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::glob::Glob;
use turbopack_core::{
asset::{Asset, AssetContent},
......@@ -9,13 +9,13 @@ use turbopack_core::{
ident::AssetIdent,
module::Module,
module_graph::ModuleGraph,
reference::{ModuleReference, ModuleReferences},
reference::{ModuleReference, ModuleReferences, SingleChunkableModuleReference},
resolve::{origin::ResolveOrigin, ModulePart},
};
use super::{
chunk_item::EcmascriptModulePartChunkItem, get_part_id, part_of_module, split, split_module,
PartId, SplitResult,
SplitResult,
};
use crate::{
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
......@@ -24,9 +24,7 @@ use crate::{
analyse_ecmascript_module, esm::FoundExportType, follow_reexports, FollowExportsResult,
},
side_effect_optimization::facade::module::EcmascriptModuleFacadeModule,
tree_shake::{
reference::EcmascriptModulePartReference, side_effect_module::SideEffectsModule, Key,
},
tree_shake::{side_effect_module::SideEffectsModule, Key},
AnalyzeEcmascriptModuleResult, EcmascriptAnalyzable, EcmascriptModuleAsset,
EcmascriptModuleAssetType, EcmascriptModuleContent, EcmascriptParsable,
};
......@@ -293,7 +291,13 @@ impl Module for EcmascriptModulePartAsset {
#[turbo_tasks::function]
async fn references(&self) -> Result<Vc<ModuleReferences>> {
let part_dep = |part: ModulePart| -> Vc<Box<dyn ModuleReference>> {
Vc::upcast(EcmascriptModulePartReference::new(*self.full_module, part))
Vc::upcast(SingleChunkableModuleReference::new(
Vc::upcast(EcmascriptModulePartAsset::new_with_resolved_part(
*self.full_module,
part,
)),
Vc::cell("part reference".into()),
))
};
if let ModulePart::Facade = self.part {
......@@ -304,50 +308,9 @@ impl Module for EcmascriptModulePartAsset {
return Ok(Vc::cell(references));
}
let split_data = split_module(*self.full_module).await?;
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(
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,
// Because of this we still need `PartId::Internal` to have `is_for_eval`
// flag.
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"
),
}))
})
.map(|v| async move { v.to_resolved().await })
.try_join()
.await?,
);
let analyze = analyze(*self.full_module, self.part.clone());
references.extend(analyze.references().owned().await?);
Ok(Vc::cell(references))
Ok(analyze.references())
}
}
......
......@@ -27,7 +27,6 @@ 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_with_resolved_part(*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