Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(turbopack-ecmascript): Use ResolvedVc in CodeGen type #74709

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions turbopack/crates/turbopack-ecmascript/src/code_gen.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};
use swc_core::ecma::{
ast::Stmt,
visit::{AstParentKind, VisitMut},
};
use turbo_rcstr::RcStr;
use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, ResolvedVc, Vc};
use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, NonLocalValue, ResolvedVc, Vc};
use turbopack_core::chunk::{AsyncModuleInfo, ChunkingContext};

/// impl of code generation inferred from a ModuleReference.
Expand Down Expand Up @@ -106,13 +107,31 @@ pub trait CodeGenerateableWithAsyncModuleInfo {
) -> Vc<CodeGeneration>;
}

#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat)]
pub enum CodeGen {
pub enum UnresolvedCodeGen {
CodeGenerateable(Vc<Box<dyn CodeGenerateable>>),
CodeGenerateableWithAsyncModuleInfo(Vc<Box<dyn CodeGenerateableWithAsyncModuleInfo>>),
}

impl UnresolvedCodeGen {
pub async fn to_resolved(&self) -> Result<CodeGen> {
Ok(match self {
Self::CodeGenerateable(vc) => CodeGen::CodeGenerateable(vc.to_resolved().await?),
Self::CodeGenerateableWithAsyncModuleInfo(vc) => {
CodeGen::CodeGenerateableWithAsyncModuleInfo(vc.to_resolved().await?)
}
})
}
}

#[derive(
Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue,
)]
pub enum CodeGen {
CodeGenerateable(ResolvedVc<Box<dyn CodeGenerateable>>),
CodeGenerateableWithAsyncModuleInfo(ResolvedVc<Box<dyn CodeGenerateableWithAsyncModuleInfo>>),
}

#[turbo_tasks::value(transparent, local)]
#[turbo_tasks::value(transparent)]
pub struct CodeGenerateables(Vec<CodeGen>);

pub fn path_to(
Expand Down
37 changes: 19 additions & 18 deletions turbopack/crates/turbopack-ecmascript/src/references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ use crate::{
ConstantNumber, ConstantString, JsValueUrlKind, RequireContextValue,
},
chunk::EcmascriptExports,
code_gen::{CodeGen, CodeGenerateable, CodeGenerateableWithAsyncModuleInfo, CodeGenerateables},
code_gen::{
CodeGenerateable, CodeGenerateableWithAsyncModuleInfo, CodeGenerateables, UnresolvedCodeGen,
},
magic_identifier,
parse::parse,
references::{
Expand Down Expand Up @@ -167,7 +169,9 @@ pub struct AnalyzeEcmascriptModuleResultBuilder {
local_references: FxIndexSet<ResolvedVc<Box<dyn ModuleReference>>>,
reexport_references: FxIndexSet<ResolvedVc<Box<dyn ModuleReference>>>,
evaluation_references: FxIndexSet<ResolvedVc<Box<dyn ModuleReference>>>,
code_gens: Vec<CodeGen>,
// Many of these `code_gens` are accumulated inside of a synchronous SWC visitor, so we don't
// resolve them until `build()`.
code_gens: Vec<UnresolvedCodeGen>,
exports: EcmascriptExports,
async_module: ResolvedVc<OptionAsyncModule>,
successful: bool,
Expand Down Expand Up @@ -235,18 +239,18 @@ impl AnalyzeEcmascriptModuleResultBuilder {
C: Upcast<Box<dyn CodeGenerateable>>,
{
self.code_gens
.push(CodeGen::CodeGenerateable(Vc::upcast(code_gen)));
.push(UnresolvedCodeGen::CodeGenerateable(Vc::upcast(code_gen)));
}

/// Adds a codegen to the analysis result.
#[allow(dead_code)]
pub fn add_code_gen_with_availability_info<C>(&mut self, code_gen: ResolvedVc<C>)
pub fn add_code_gen_with_availability_info<C>(&mut self, code_gen: Vc<C>)
where
C: Upcast<Box<dyn CodeGenerateableWithAsyncModuleInfo>>,
{
self.code_gens
.push(CodeGen::CodeGenerateableWithAsyncModuleInfo(
ResolvedVc::upcast(code_gen),
.push(UnresolvedCodeGen::CodeGenerateableWithAsyncModuleInfo(
Vc::upcast(code_gen),
));
}

Expand Down Expand Up @@ -274,8 +278,7 @@ impl AnalyzeEcmascriptModuleResultBuilder {
self.successful = successful;
}

/// Builds the final analysis result. Resolves internal Vcs for performance
/// in using them.
/// Builds the final analysis result. Resolves internal Vcs.
pub async fn build(
mut self,
track_reexport_references: bool,
Expand All @@ -301,14 +304,6 @@ impl AnalyzeEcmascriptModuleResultBuilder {
.into_iter()
.flatten()
.collect();
for c in self.code_gens.iter_mut() {
match c {
CodeGen::CodeGenerateable(c) => {
*c = c.resolve().await?;
}
CodeGen::CodeGenerateableWithAsyncModuleInfo(..) => {}
}
}

let source_map = if let Some(source_map) = self.source_map {
source_map
Expand All @@ -321,7 +316,13 @@ impl AnalyzeEcmascriptModuleResultBuilder {
local_references: ResolvedVc::cell(local_references),
reexport_references: ResolvedVc::cell(reexport_references),
evaluation_references: ResolvedVc::cell(evaluation_references),
code_generation: ResolvedVc::cell(self.code_gens),
code_generation: ResolvedVc::cell(
self.code_gens
.iter()
.map(UnresolvedCodeGen::to_resolved)
.try_join()
.await?,
),
exports: self.exports.resolved_cell(),
async_module: self.async_module,
successful: self.successful,
Expand Down Expand Up @@ -2976,7 +2977,6 @@ impl VisitAstPath for ModuleReferencesVisitor<'_> {
export: &'ast NamedExport,
ast_path: &mut AstNodePath<AstParentNodeRef<'r>>,
) {
let path = Vc::cell(as_parent_path(ast_path));
// We create mutable exports for fake ESMs generated by module splitting
let is_fake_esm = export
.with
Expand Down Expand Up @@ -3032,6 +3032,7 @@ impl VisitAstPath for ModuleReferencesVisitor<'_> {
}
}

let path = Vc::cell(as_parent_path(ast_path));
self.analysis.add_code_gen(EsmModuleItem::new(path));
export.visit_children_with_ast_path(self, ast_path);
}
Expand Down
Loading