Skip to content

Commit

Permalink
refactor(turbopack-ecmascript) Use ResolvedVc in EsmExport type (verc…
Browse files Browse the repository at this point in the history
…el#74508)

This removes the `local` opt-out from `EsmExports`.
  • Loading branch information
bgw authored Jan 9, 2025
1 parent 4dbb18d commit 7205180
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 63 deletions.
14 changes: 9 additions & 5 deletions crates/next-core/src/next_dynamic/dynamic_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ impl ChunkableModule for NextDynamicEntryModule {
impl EcmascriptChunkPlaceable for NextDynamicEntryModule {
#[turbo_tasks::function]
async fn get_exports(&self) -> Result<Vc<EcmascriptExports>> {
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(
Expand All @@ -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(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ impl ChunkableModule for NextServerComponentModule {
impl EcmascriptChunkPlaceable for NextServerComponentModule {
#[turbo_tasks::function]
async fn get_exports(&self) -> Result<Vc<EcmascriptExports>> {
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(
Expand All @@ -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(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ impl ChunkableModule for NextServerUtilityModule {
impl EcmascriptChunkPlaceable for NextServerUtilityModule {
#[turbo_tasks::function]
async fn get_exports(&self) -> Result<Vc<EcmascriptExports>> {
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(
Expand All @@ -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(),
)
Expand Down
23 changes: 13 additions & 10 deletions turbopack/crates/turbopack-ecmascript/src/references/esm/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
///
Expand All @@ -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<Box<dyn ModuleReference>>, RcStr, bool),
ImportedBinding(ResolvedVc<Box<dyn ModuleReference>>, RcStr, bool),
/// An imported namespace that is exported (export * from "...")
ImportedNamespace(Vc<Box<dyn ModuleReference>>),
ImportedNamespace(ResolvedVc<Box<dyn ModuleReference>>),
/// An error occurred while resolving the export
Error,
}
Expand Down Expand Up @@ -427,19 +427,18 @@ async fn emit_star_exports_issue(source_ident: Vc<AssetIdent>, message: RcStr) -
Ok(())
}

#[turbo_tasks::value(shared, local)]
#[turbo_tasks::value(shared)]
#[derive(Hash, Debug)]
pub struct EsmExports {
pub exports: BTreeMap<RcStr, EsmExport>,
pub star_exports: Vec<ResolvedVc<Box<dyn ModuleReference>>>,
}

/// 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<RcStr, EsmExport>,
Expand Down Expand Up @@ -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,
),
);
}
}
Expand Down
38 changes: 14 additions & 24 deletions turbopack/crates/turbopack-ecmascript/src/references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,20 +653,21 @@ 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);

for (i, reexport) in eval_context.imports.reexports() {
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 {
Expand All @@ -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,
),
Expand All @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -2880,10 +2870,10 @@ impl StaticAnalyser {
struct ModuleReferencesVisitor<'a> {
eval_context: &'a EvalContext,
old_analyser: StaticAnalyser,
import_references: &'a [Vc<EsmAssetReference>],
import_references: &'a [ResolvedVc<EsmAssetReference>],
analysis: &'a mut AnalyzeEcmascriptModuleResultBuilder,
esm_exports: BTreeMap<RcStr, EsmExport>,
esm_star_exports: Vec<Vc<Box<dyn ModuleReference>>>,
esm_star_exports: Vec<ResolvedVc<Box<dyn ModuleReference>>>,
webpack_runtime: Option<(RcStr, Span)>,
webpack_entry: bool,
webpack_chunks: Vec<Lit>,
Expand All @@ -2892,7 +2882,7 @@ struct ModuleReferencesVisitor<'a> {
impl<'a> ModuleReferencesVisitor<'a> {
fn new(
eval_context: &'a EvalContext,
import_references: &'a [Vc<EsmAssetReference>],
import_references: &'a [ResolvedVc<EsmAssetReference>],
analysis: &'a mut AnalyzeEcmascriptModuleResultBuilder,
) -> Self {
Self {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand Down Expand Up @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -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 => {
Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 7205180

Please sign in to comment.