Unverified Commit 93496a5f authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

refactor(turbopack): resolve terminal exports before side effects

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 2a16a1c6
......@@ -268,7 +268,17 @@ async fn follow_reexports_with_side_effects(
current_module = *module;
current_export_name = export_name.clone().unwrap_or(current_export_name);
}
_ => break result,
_ => {
// `follow_reexports` returns terminal exports before checking their side effects.
// When it followed to a different terminal module, retain that module's evaluation
// just like the next loop iteration used to do for `SideEffects`.
if *module != current_module
&& *module.side_effects().await? != ModuleSideEffects::SideEffectFree
{
side_effects.push(only_effects(**module).to_resolved().await?);
}
break result;
}
}
};
......
......@@ -35,7 +35,6 @@ use crate::{
module_fragments::part::module::EcmascriptModulePartAsset,
references::esm::base::ReferencedAsset,
runtime_functions::{TURBOPACK_DYNAMIC, TURBOPACK_ESM},
side_effect_optimization::locals::module::EcmascriptModuleLocalsModule,
utils::module_id_to_lit,
};
......@@ -160,17 +159,31 @@ pub async fn follow_reexports(
let mut module = module;
let mut export_name = export_name;
loop {
// A locals module only exposes local bindings, so there are no more reexports to follow.
// Returning it still preserves its evaluation and side effects. Avoid asking it for side
// effects or exports from the original module: resolving the facade's synthetic locals
// reference can happen while that module is still being analyzed, and either request would
// create a dependency cycle.
if ResolvedVc::try_downcast_type::<EcmascriptModuleLocalsModule>(module).is_some() {
return Ok(FollowExportsResult::cell(FollowExportsResult {
module,
export_name: Some(export_name),
ty: FoundExportType::Found,
}));
let exports = module.get_exports().await?;
// Side effects only matter when following an export past the current module. Resolve
// terminal ESM outcomes first, since computing side effects can require the current
// module's analysis while reexport following itself may run during that analysis.
if let EcmascriptExports::EsmExports(esm_exports) = &*exports {
let esm_exports = esm_exports.await?;
match esm_exports.exports.get(&export_name) {
Some(export @ (EsmExport::LocalBinding(..) | EsmExport::Error)) => {
let ControlFlow::Break(result) =
handle_declared_export(module, export_name, export).await?
else {
bail!("local and error exports must be terminal");
};
return Ok(result.cell());
}
None if esm_exports.star_exports.is_empty() || &*export_name == "default" => {
return Ok(FollowExportsResult::cell(FollowExportsResult {
module,
export_name: Some(export_name),
ty: FoundExportType::NotFound,
}));
}
_ => {}
}
}
if !ignore_side_effects
......@@ -187,7 +200,6 @@ pub async fn follow_reexports(
}
ignore_side_effects = false;
let exports = module.get_exports().await?;
let EcmascriptExports::EsmExports(exports) = &*exports else {
return Ok(FollowExportsResult::cell(FollowExportsResult {
module,
......
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