From 720518020cc850984301adc59ce6251b49cecb8f Mon Sep 17 00:00:00 2001 From: Benjamin Woodruff Date: Wed, 8 Jan 2025 21:54:46 -0800 Subject: [PATCH] refactor(turbopack-ecmascript) Use ResolvedVc in EsmExport type (#74508) This removes the `local` opt-out from `EsmExports`. --- .../src/next_dynamic/dynamic_module.rs | 14 ++++--- .../server_component_module.rs | 10 +++-- .../server_utility_module.rs | 10 +++-- .../src/references/esm/export.rs | 23 +++++----- .../src/references/mod.rs | 38 +++++++---------- .../side_effect_optimization/facade/module.rs | 42 ++++++++++++------- .../side_effect_optimization/locals/module.rs | 4 +- 7 files changed, 78 insertions(+), 63 deletions(-) diff --git a/crates/next-core/src/next_dynamic/dynamic_module.rs b/crates/next-core/src/next_dynamic/dynamic_module.rs index 3febdadfb2ede..363d59d3fdb51 100644 --- a/crates/next-core/src/next_dynamic/dynamic_module.rs +++ b/crates/next-core/src/next_dynamic/dynamic_module.rs @@ -94,10 +94,14 @@ impl ChunkableModule for NextDynamicEntryModule { impl EcmascriptChunkPlaceable for NextDynamicEntryModule { #[turbo_tasks::function] async fn get_exports(&self) -> Result> { - let module_reference = Vc::upcast(SingleChunkableModuleReference::new( - Vc::upcast(*self.module), - dynamic_ref_description(), - )); + let module_reference = ResolvedVc::upcast( + SingleChunkableModuleReference::new( + Vc::upcast(*self.module), + dynamic_ref_description(), + ) + .to_resolved() + .await?, + ); let mut exports = BTreeMap::new(); exports.insert( @@ -108,7 +112,7 @@ impl EcmascriptChunkPlaceable for NextDynamicEntryModule { Ok(EcmascriptExports::EsmExports( EsmExports { exports, - star_exports: vec![module_reference.to_resolved().await?], + star_exports: vec![module_reference], } .resolved_cell(), ) diff --git a/crates/next-core/src/next_server_component/server_component_module.rs b/crates/next-core/src/next_server_component/server_component_module.rs index 90194a1056670..35b566d97743f 100644 --- a/crates/next-core/src/next_server_component/server_component_module.rs +++ b/crates/next-core/src/next_server_component/server_component_module.rs @@ -114,9 +114,11 @@ impl ChunkableModule for NextServerComponentModule { impl EcmascriptChunkPlaceable for NextServerComponentModule { #[turbo_tasks::function] async fn get_exports(&self) -> Result> { - let module_reference = Vc::upcast(NextServerComponentModuleReference::new(Vc::upcast( - *self.module, - ))); + let module_reference = ResolvedVc::upcast( + NextServerComponentModuleReference::new(Vc::upcast(*self.module)) + .to_resolved() + .await?, + ); let mut exports = BTreeMap::new(); exports.insert( @@ -127,7 +129,7 @@ impl EcmascriptChunkPlaceable for NextServerComponentModule { Ok(EcmascriptExports::EsmExports( EsmExports { exports, - star_exports: vec![module_reference.to_resolved().await?], + star_exports: vec![module_reference], } .resolved_cell(), ) diff --git a/crates/next-core/src/next_server_utility/server_utility_module.rs b/crates/next-core/src/next_server_utility/server_utility_module.rs index 4348e72589893..fd7a21b5c0144 100644 --- a/crates/next-core/src/next_server_utility/server_utility_module.rs +++ b/crates/next-core/src/next_server_utility/server_utility_module.rs @@ -92,9 +92,11 @@ impl ChunkableModule for NextServerUtilityModule { impl EcmascriptChunkPlaceable for NextServerUtilityModule { #[turbo_tasks::function] async fn get_exports(&self) -> Result> { - let module_reference = Vc::upcast(NextServerUtilityModuleReference::new(Vc::upcast( - *self.module, - ))); + let module_reference = ResolvedVc::upcast( + NextServerUtilityModuleReference::new(Vc::upcast(*self.module)) + .to_resolved() + .await?, + ); let mut exports = BTreeMap::new(); exports.insert( @@ -105,7 +107,7 @@ impl EcmascriptChunkPlaceable for NextServerUtilityModule { Ok(EcmascriptExports::EsmExports( EsmExports { exports, - star_exports: vec![module_reference.to_resolved().await?], + star_exports: vec![module_reference], } .resolved_cell(), ) diff --git a/turbopack/crates/turbopack-ecmascript/src/references/esm/export.rs b/turbopack/crates/turbopack-ecmascript/src/references/esm/export.rs index eca5d86787161..eb4e9af6754ed 100644 --- a/turbopack/crates/turbopack-ecmascript/src/references/esm/export.rs +++ b/turbopack/crates/turbopack-ecmascript/src/references/esm/export.rs @@ -35,7 +35,7 @@ use crate::{ magic_identifier, }; -#[derive(Clone, Hash, Debug, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs)] +#[derive(Clone, Hash, Debug, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, NonLocalValue)] pub enum EsmExport { /// A local binding that is exported (export { a } or export const a = 1) /// @@ -44,9 +44,9 @@ pub enum EsmExport { /// An imported binding that is exported (export { a as b } from "...") /// /// The last bool is true if the binding is a mutable binding - ImportedBinding(Vc>, RcStr, bool), + ImportedBinding(ResolvedVc>, RcStr, bool), /// An imported namespace that is exported (export * from "...") - ImportedNamespace(Vc>), + ImportedNamespace(ResolvedVc>), /// An error occurred while resolving the export Error, } @@ -427,19 +427,18 @@ async fn emit_star_exports_issue(source_ident: Vc, message: RcStr) - Ok(()) } -#[turbo_tasks::value(shared, local)] +#[turbo_tasks::value(shared)] #[derive(Hash, Debug)] pub struct EsmExports { pub exports: BTreeMap, pub star_exports: Vec>>, } -/// The expanded version of [EsmExports], the `exports` field here includes all -/// exports that could be expanded from `star_exports`. +/// The expanded version of [`EsmExports`], the `exports` field here includes all exports that could +/// be expanded from `star_exports`. /// -/// `star_exports` that could not be (fully) expanded end up in -/// `dynamic_exports`. -#[turbo_tasks::value(shared, local)] +/// [`EsmExports::star_exports`] that could not be (fully) expanded end up in `dynamic_exports`. +#[turbo_tasks::value(shared)] #[derive(Hash, Debug)] pub struct ExpandedExports { pub exports: BTreeMap, @@ -469,7 +468,11 @@ impl EsmExports { if !exports.contains_key(export) { exports.insert( export.clone(), - EsmExport::ImportedBinding(Vc::upcast(*esm_ref), export.clone(), false), + EsmExport::ImportedBinding( + ResolvedVc::upcast(esm_ref), + export.clone(), + false, + ), ); } } diff --git a/turbopack/crates/turbopack-ecmascript/src/references/mod.rs b/turbopack/crates/turbopack-ecmascript/src/references/mod.rs index 06138fbeb73e2..68b6b07acbea7 100644 --- a/turbopack/crates/turbopack-ecmascript/src/references/mod.rs +++ b/turbopack/crates/turbopack-ecmascript/src/references/mod.rs @@ -653,7 +653,6 @@ pub(crate) async fn analyse_ecmascript_module_internal( let (webpack_runtime, webpack_entry, webpack_chunks, esm_exports, esm_star_exports) = set_handler_and_globals(&handler, globals, || { // TODO migrate to effects - let import_references: Vec<_> = import_references.iter().map(|&rvc| *rvc).collect(); // TODO(ResolvedVc): reinterpret directly let mut visitor = ModuleReferencesVisitor::new(eval_context, &import_references, &mut analysis); @@ -661,12 +660,14 @@ pub(crate) async fn analyse_ecmascript_module_internal( let import_ref = import_references[i]; match reexport { Reexport::Star => { - visitor.esm_star_exports.push(Vc::upcast(import_ref)); + visitor + .esm_star_exports + .push(ResolvedVc::upcast(import_ref)); } Reexport::Namespace { exported: n } => { visitor.esm_exports.insert( n.as_str().into(), - EsmExport::ImportedNamespace(Vc::upcast(import_ref)), + EsmExport::ImportedNamespace(ResolvedVc::upcast(import_ref)), ); } Reexport::Named { @@ -676,7 +677,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( visitor.esm_exports.insert( e.as_str().into(), EsmExport::ImportedBinding( - Vc::upcast(import_ref), + ResolvedVc::upcast(import_ref), i.to_string().into(), false, ), @@ -700,26 +701,19 @@ pub(crate) async fn analyse_ecmascript_module_internal( match *export { EsmExport::LocalBinding(..) => {} EsmExport::ImportedNamespace(reference) => { - let reference = reference.to_resolved().await?; analysis.add_reexport_reference(reference); analysis.add_import_reference(reference); } EsmExport::ImportedBinding(reference, ..) => { - let reference = reference.to_resolved().await?; analysis.add_reexport_reference(reference); analysis.add_import_reference(reference); } EsmExport::Error => {} } } - for reference in esm_star_exports - .iter() - .map(|r| r.to_resolved()) - .try_join() - .await? - { - analysis.add_reexport_reference(reference); - analysis.add_import_reference(reference); + for reference in &esm_star_exports { + analysis.add_reexport_reference(*reference); + analysis.add_import_reference(*reference); } let mut ignore_effect_span = None; @@ -780,11 +774,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( let esm_exports = EsmExports { exports: esm_exports, - star_exports: esm_star_exports - .into_iter() - .map(|v| v.to_resolved()) - .try_join() - .await?, + star_exports: esm_star_exports, } .cell(); @@ -2880,10 +2870,10 @@ impl StaticAnalyser { struct ModuleReferencesVisitor<'a> { eval_context: &'a EvalContext, old_analyser: StaticAnalyser, - import_references: &'a [Vc], + import_references: &'a [ResolvedVc], analysis: &'a mut AnalyzeEcmascriptModuleResultBuilder, esm_exports: BTreeMap, - esm_star_exports: Vec>>, + esm_star_exports: Vec>>, webpack_runtime: Option<(RcStr, Span)>, webpack_entry: bool, webpack_chunks: Vec, @@ -2892,7 +2882,7 @@ struct ModuleReferencesVisitor<'a> { impl<'a> ModuleReferencesVisitor<'a> { fn new( eval_context: &'a EvalContext, - import_references: &'a [Vc], + import_references: &'a [ResolvedVc], analysis: &'a mut AnalyzeEcmascriptModuleResultBuilder, ) -> Self { Self { @@ -3025,12 +3015,12 @@ impl VisitAstPath for ModuleReferencesVisitor<'_> { let esm_ref = self.import_references[index]; if let Some(export) = export { EsmExport::ImportedBinding( - Vc::upcast(esm_ref), + ResolvedVc::upcast(esm_ref), export, is_fake_esm, ) } else { - EsmExport::ImportedNamespace(Vc::upcast(esm_ref)) + EsmExport::ImportedNamespace(ResolvedVc::upcast(esm_ref)) } } else { EsmExport::LocalBinding(binding_name, is_fake_esm) diff --git a/turbopack/crates/turbopack-ecmascript/src/side_effect_optimization/facade/module.rs b/turbopack/crates/turbopack-ecmascript/src/side_effect_optimization/facade/module.rs index 809b855b4e4f2..1d038647fd356 100644 --- a/turbopack/crates/turbopack-ecmascript/src/side_effect_optimization/facade/module.rs +++ b/turbopack/crates/turbopack-ecmascript/src/side_effect_optimization/facade/module.rs @@ -186,10 +186,14 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule { exports.insert( name.clone(), EsmExport::ImportedBinding( - Vc::upcast(EcmascriptModulePartReference::new_part( - *self.module, - ModulePart::locals(), - )), + ResolvedVc::upcast( + EcmascriptModulePartReference::new_part( + *self.module, + ModulePart::locals(), + ) + .to_resolved() + .await?, + ), name, *mutable, ), @@ -229,10 +233,14 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule { exports.insert( "default".into(), EsmExport::ImportedBinding( - Vc::upcast(EcmascriptModulePartReference::new_part( - *self.module, - ModulePart::exports(), - )), + ResolvedVc::upcast( + EcmascriptModulePartReference::new_part( + *self.module, + ModulePart::exports(), + ) + .to_resolved() + .await?, + ), "default".into(), false, ), @@ -252,7 +260,11 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule { exports.insert( export.await?.clone_value(), EsmExport::ImportedBinding( - Vc::upcast(EcmascriptModulePartReference::new(*self.module)), + ResolvedVc::upcast( + EcmascriptModulePartReference::new(*self.module) + .to_resolved() + .await?, + ), original_export.clone_value(), false, ), @@ -261,9 +273,11 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule { ModulePart::RenamedNamespace { export } => { exports.insert( export.await?.clone_value(), - EsmExport::ImportedNamespace(Vc::upcast(EcmascriptModulePartReference::new( - *self.module, - ))), + EsmExport::ImportedNamespace(ResolvedVc::upcast( + EcmascriptModulePartReference::new(*self.module) + .to_resolved() + .await?, + )), ); } ModulePart::Evaluation => { @@ -276,8 +290,8 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule { exports, star_exports, } - .cell(); - Ok(EcmascriptExports::EsmExports(exports.to_resolved().await?).cell()) + .resolved_cell(); + Ok(EcmascriptExports::EsmExports(exports).cell()) } #[turbo_tasks::function] diff --git a/turbopack/crates/turbopack-ecmascript/src/side_effect_optimization/locals/module.rs b/turbopack/crates/turbopack-ecmascript/src/side_effect_optimization/locals/module.rs index 2dae886936bc3..76d32819e2516 100644 --- a/turbopack/crates/turbopack-ecmascript/src/side_effect_optimization/locals/module.rs +++ b/turbopack/crates/turbopack-ecmascript/src/side_effect_optimization/locals/module.rs @@ -93,8 +93,8 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleLocalsModule { exports, star_exports: vec![], } - .cell(); - Ok(EcmascriptExports::EsmExports(exports.to_resolved().await?).cell()) + .resolved_cell(); + Ok(EcmascriptExports::EsmExports(exports).cell()) } #[turbo_tasks::function]