Skip to content

Commit

Permalink
shorter idents
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Feb 3, 2025
1 parent 8a2b381 commit ffa9ccd
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 36 deletions.
7 changes: 6 additions & 1 deletion turbopack/crates/turbopack-ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,12 @@ impl EcmascriptModuleContent {
}
}
if let Some(async_module) = *async_module.await? {
code_gens.push(async_module.code_generation(async_module_info, references));
code_gens.push(async_module.code_generation(
async_module_info,
references,
module_graph,
chunking_context,
));
}
for c in code_generation.await?.iter() {
match c {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use turbo_tasks::{
TryJoinIterExt, Vc,
};
use turbopack_core::{
chunk::{AsyncModuleInfo, ChunkableModuleReference, ChunkingType},
chunk::{AsyncModuleInfo, ChunkableModuleReference, ChunkingContext, ChunkingType},
module_graph::ModuleGraph,
reference::{ModuleReference, ModuleReferences},
resolve::ExternalType,
};
Expand Down Expand Up @@ -102,6 +103,8 @@ impl AsyncModule {
&self,
async_module_info: Vc<AsyncModuleInfo>,
references: Vc<ModuleReferences>,
module_graph: Vc<ModuleGraph>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Vc<AsyncModuleIdents>> {
let async_module_info = async_module_info.await?;

Expand All @@ -115,7 +118,9 @@ impl AsyncModule {
Ok(match &*referenced_asset {
ReferencedAsset::External(_, ExternalType::EcmaScriptModule) => {
if self.import_externals {
referenced_asset.get_ident().await?
referenced_asset
.get_ident(module_graph, chunking_context)
.await?
} else {
None
}
Expand All @@ -125,7 +130,9 @@ impl AsyncModule {
.referenced_async_modules
.contains(&ResolvedVc::upcast(*placeable))
{
referenced_asset.get_ident().await?
referenced_asset
.get_ident(module_graph, chunking_context)
.await?
} else {
None
}
Expand Down Expand Up @@ -189,9 +196,18 @@ impl AsyncModule {
self: Vc<Self>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
references: Vc<ModuleReferences>,
module_graph: Vc<ModuleGraph>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Vc<CodeGeneration>> {
if let Some(async_module_info) = async_module_info {
let async_idents = self.get_async_idents(async_module_info, references).await?;
let async_idents = self
.get_async_idents(
async_module_info,
references,
module_graph,
chunking_context,
)
.await?;

if !async_idents.is_empty() {
let idents = async_idents
Expand Down
34 changes: 23 additions & 11 deletions turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::{
magic_identifier,
references::util::{request_to_string, throw_module_not_found_expr},
tree_shake::{asset::EcmascriptModulePartAsset, TURBOPACK_PART_IMPORT_SOURCE},
utils::module_id_to_lit,
utils::{module_id_to_lit, StringifyJs},
};

#[turbo_tasks::value]
Expand All @@ -49,9 +49,15 @@ pub enum ReferencedAsset {
}

impl ReferencedAsset {
pub async fn get_ident(&self) -> Result<Option<String>> {
pub async fn get_ident(
&self,
module_graph: Vc<ModuleGraph>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Option<String>> {
Ok(match self {
ReferencedAsset::Some(asset) => Some(Self::get_ident_from_placeable(asset).await?),
ReferencedAsset::Some(asset) => {
Some(Self::get_ident_from_placeable(asset, module_graph, chunking_context).await?)
}
ReferencedAsset::External(request, ty) => Some(magic_identifier::mangle(&format!(
"{ty} external {request}"
))),
Expand All @@ -61,12 +67,14 @@ impl ReferencedAsset {

pub(crate) async fn get_ident_from_placeable(
asset: &Vc<Box<dyn EcmascriptChunkPlaceable>>,
module_graph: Vc<ModuleGraph>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<String> {
let path = asset.ident().to_string().await?;
Ok(magic_identifier::mangle(&format!(
"imported module {}",
path
)))
let id = asset
.as_chunk_item(module_graph, Vc::upcast(chunking_context))
.id()
.await?;
Ok(magic_identifier::mangle(&format!("imported module {}", id)))
}
}

Expand Down Expand Up @@ -272,7 +280,10 @@ impl CodeGenerateable for EsmAssetReference {
span: DUMMY_SP,
});
Some((format!("throw {request}").into(), stmt))
} else if let Some(ident) = referenced_asset.get_ident().await? {
} else if let Some(ident) = referenced_asset
.get_ident(module_graph, chunking_context)
.await?
{
let span = this
.issue_source
.await?
Expand All @@ -290,12 +301,13 @@ impl CodeGenerateable for EsmAssetReference {
.as_chunk_item(module_graph, Vc::upcast(chunking_context))
.id()
.await?;
let name = ident;
Some((
ident.clone().into(),
id.to_string().into(),
var_decl_with_span(
quote!(
"var $name = __turbopack_import__($id);" as Stmt,
name = Ident::new(ident.clone().into(), DUMMY_SP, Default::default()),
name = Ident::new(name.clone().into(), DUMMY_SP, Default::default()),
id: Expr = module_id_to_lit(&id),
),
span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ impl EsmBinding {
async fn to_visitors(
&self,
visitors: &mut Vec<(Vec<AstParentKind>, Box<dyn VisitorFactory>)>,
module_graph: Vc<ModuleGraph>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<()> {
let item = self.clone();
let imported_module = self.reference.get_referenced_asset();

let mut ast_path = item.ast_path.await?.clone_value();
let imported_module = imported_module.await?.get_ident().await?;
let imported_module = imported_module
.await?
.get_ident(module_graph, chunking_context)
.await?;

loop {
match ast_path.last() {
Expand Down Expand Up @@ -153,14 +158,15 @@ impl CodeGenerateable for EsmBindings {
#[turbo_tasks::function]
async fn code_generation(
&self,
_module_graph: Vc<ModuleGraph>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
module_graph: Vc<ModuleGraph>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Vc<CodeGeneration>> {
let mut visitors = Vec::new();
let bindings = self.bindings.clone();

for item in bindings.into_iter() {
item.to_visitors(&mut visitors).await?;
item.to_visitors(&mut visitors, module_graph, chunking_context)
.await?;
}

Ok(CodeGeneration::visitors(visitors))
Expand Down
31 changes: 21 additions & 10 deletions turbopack/crates/turbopack-ecmascript/src/references/esm/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,14 +496,19 @@ impl CodeGenerateable for EsmExports {
#[turbo_tasks::function]
async fn code_generation(
self: Vc<Self>,
_module_graph: Vc<ModuleGraph>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
module_graph: Vc<ModuleGraph>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Vc<CodeGeneration>> {
let expanded = self.expand_exports().await?;

let mut dynamic_exports = Vec::<Box<Expr>>::new();
for dynamic_export_asset in &expanded.dynamic_exports {
let ident = ReferencedAsset::get_ident_from_placeable(dynamic_export_asset).await?;
let ident = ReferencedAsset::get_ident_from_placeable(
dynamic_export_asset,
module_graph,
chunking_context,
)
.await?;

dynamic_exports.push(quote_expr!(
"__turbopack_dynamic__($arg)",
Expand Down Expand Up @@ -543,7 +548,10 @@ impl CodeGenerateable for EsmExports {
EsmExport::ImportedBinding(esm_ref, name, mutable) => {
let referenced_asset =
ReferencedAsset::from_resolve_result(esm_ref.resolve_reference()).await?;
referenced_asset.get_ident().await?.map(|ident| {
referenced_asset.get_ident(
module_graph,
chunking_context
).await?.map(|ident| {
let expr = MemberExpr {
span: DUMMY_SP,
obj: Box::new(Expr::Ident(Ident::new(
Expand Down Expand Up @@ -582,12 +590,15 @@ impl CodeGenerateable for EsmExports {
EsmExport::ImportedNamespace(esm_ref) => {
let referenced_asset =
ReferencedAsset::from_resolve_result(esm_ref.resolve_reference()).await?;
referenced_asset.get_ident().await?.map(|ident| {
quote!(
"(() => $imported)" as Expr,
imported = Ident::new(ident.into(), DUMMY_SP, Default::default())
)
})
referenced_asset
.get_ident(module_graph, chunking_context)
.await?
.map(|ident| {
quote!(
"(() => $imported)" as Expr,
imported = Ident::new(ident.into(), DUMMY_SP, Default::default())
)
})
}
};
if let Some(expr) = expr {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ impl EcmascriptChunkItem for EcmascriptModuleFacadeChunkItem {
code_gens.push(code_gen.code_generation(*self.module_graph, *chunking_context));
}
}
code_gens.push(
self.module
.async_module()
.code_generation(async_module_info, references),
);
code_gens.push(self.module.async_module().code_generation(
async_module_info,
references,
*self.module_graph,
*chunking_context,
));
code_gens.push(exports.code_generation(*self.module_graph, *chunking_context));
let code_gens = code_gens.into_iter().try_join().await?;
let code_gens = code_gens.iter().map(|cg| &**cg).collect::<Vec<_>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl CodeGenerateable for EcmascriptModulePartReference {
let referenced_asset = ReferencedAsset::from_resolve_result(self.resolve_reference());
let referenced_asset = referenced_asset.await?;
let ident = referenced_asset
.get_ident()
.get_ident(module_graph, chunking_context)
.await?
.context("part module reference should have an ident")?;

Expand Down

0 comments on commit ffa9ccd

Please # to comment.