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

Turbopack: pass scope-hoisting analysis explicitly

Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent b8eac180
......@@ -286,7 +286,7 @@ impl EcmascriptChunkPlaceable for EcmascriptClientReferenceModule {
async fn chunk_item_content(
self: ResolvedVc<Self>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
......@@ -294,6 +294,7 @@ impl EcmascriptChunkPlaceable for EcmascriptClientReferenceModule {
*ResolvedVc::upcast(self),
self.proxy_module(),
*chunking_context,
module_graph,
async_module_info,
))
}
......
......@@ -28,7 +28,6 @@ use turbopack_core::{
ModuleGraph,
binding_usage_info::{BindingUsageInfo, ModuleExportUsage},
chunk_group_info::{ChunkGroup, ChunkGroupKey},
merged_modules::{MergedModuleInfo, OptionMergedModuleGroup},
},
output::{ExpandOutputAssetsInput, OutputAsset, OutputAssets, expand_output_assets},
};
......@@ -389,7 +388,6 @@ pub struct BrowserChunkingContext {
module_id_strategy: Option<ResolvedVc<ModuleIdStrategy>>,
/// The module export usage info, if available.
export_usage: Option<ResolvedVc<BindingUsageInfo>>,
merged_modules: Option<ResolvedVc<MergedModuleInfo>>,
/// Which references are unused and should be skipped (e.g. during codegen).
unused_references: Option<ResolvedVc<UnusedReferences>>,
/// The chunking configs
......@@ -460,7 +458,6 @@ impl BrowserChunkingContext {
manifest_chunks: false,
module_id_strategy: None,
export_usage: None,
merged_modules: None,
unused_references: None,
chunking_configs: Default::default(),
should_use_absolute_url_references: false,
......@@ -1315,32 +1312,6 @@ impl ChunkingContext for BrowserChunkingContext {
})
}
#[turbo_tasks::function]
async fn with_module_graph(
self: Vc<Self>,
module_graph: Vc<ModuleGraph>,
) -> Result<Vc<Box<dyn ChunkingContext>>> {
let this = self.await?;
if !this.enable_module_merging {
return Ok(Vc::upcast(self));
}
let mut this = (*this).clone();
this.merged_modules = Some(module_graph.merged_modules().to_resolved().await?);
Ok(Vc::upcast(this.cell()))
}
#[turbo_tasks::function]
async fn module_merging_group(
&self,
module: ResolvedVc<Box<dyn Module>>,
) -> Result<Vc<OptionMergedModuleGroup>> {
Ok(Vc::cell(if let Some(info) = self.merged_modules {
info.await?.groups.get(&module).await?.as_deref().copied()
} else {
None
}))
}
#[turbo_tasks::function]
async fn module_export_usage(
&self,
......
......@@ -20,8 +20,7 @@ use crate::{
module::Module,
module_graph::{
ModuleGraph, binding_usage_info::ModuleExportUsage, chunk_group_info::ChunkGroupKey,
merged_modules::OptionMergedModuleGroup, module_batches::BatchingConfig,
style_groups::StyleGroupsAlgorithm,
module_batches::BatchingConfig, style_groups::StyleGroupsAlgorithm,
},
output::{
ExpandOutputAssetsInput, OutputAsset, OutputAssets, OutputAssetsReferences,
......@@ -544,24 +543,6 @@ pub trait ChunkingContext {
#[turbo_tasks::function]
async fn chunk_item_id_strategy(self: Vc<Self>) -> Result<Vc<ModuleIdStrategy>>;
/// Attach scope-hoisting analysis for code generation without changing the graph itself.
#[turbo_tasks::function]
fn with_module_graph(
self: Vc<Self>,
_module_graph: Vc<ModuleGraph>,
) -> Vc<Box<dyn ChunkingContext>> {
self
}
/// Read only the scope-hoisting group containing this module.
#[turbo_tasks::function]
fn module_merging_group(
self: Vc<Self>,
_module: Vc<Box<dyn Module>>,
) -> Vc<OptionMergedModuleGroup> {
Vc::cell(None)
}
#[turbo_tasks::function]
async fn module_export_usage(
self: Vc<Self>,
......
......@@ -60,9 +60,6 @@ impl MergedModuleGroup {
}
}
#[turbo_tasks::value(transparent)]
pub struct OptionMergedModuleGroup(pub Option<ResolvedVc<MergedModuleGroup>>);
#[turbo_tasks::value]
pub struct MergedModuleInfo {
pub groups: ResolvedVc<MergedModuleGroups>,
......
......@@ -407,7 +407,7 @@ impl EcmascriptChunkItem for EcmascriptModuleChunkItem {
estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
Ok(self.module.chunk_item_content(
self.chunking_context.with_module_graph(*self.module_graph),
*self.chunking_context,
*self.module_graph,
async_module_info,
estimated,
......
......@@ -15,7 +15,7 @@ use turbo_tasks::{NonLocalValue, ResolvedVc, Vc, debug::ValueDebugFormat};
use turbopack_core::{chunk::ChunkingContext, reference::ModuleReference};
use crate::{
ScopeHoistingContext,
ModuleMergingAnalysis, ScopeHoistingContext,
ast_path_trie::{AstPathId, AstPathTrie, AstPathTrieBuilder},
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
references::{
......@@ -215,6 +215,7 @@ impl CodeGen {
&self,
trie: &AstPathTrie,
ctx: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
scope_hoisting_context: ScopeHoistingContext<'_>,
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
exports: ResolvedVc<EcmascriptExports>,
......@@ -222,12 +223,18 @@ impl CodeGen {
match self {
Self::AmdDefineWithDependenciesCodeGen(v) => v.code_generation(trie, ctx).await,
Self::ConstantConditionCodeGen(v) => v.code_generation(trie, ctx).await,
Self::CollectReferenceCodeGen(v) => v.code_generation(trie, ctx).await,
Self::CollectReferenceCodeGen(v) => v.code_generation(trie, ctx, module_merging).await,
Self::ConstantValueCodeGen(v) => v.code_generation(trie, ctx).await,
Self::DynamicExpression(v) => v.code_generation(trie, ctx).await,
Self::EsmBindings(v) => v.code_generation(trie, ctx, scope_hoisting_context).await,
Self::EsmBindings(v) => {
v.code_generation(trie, ctx, module_merging, scope_hoisting_context)
.await
}
Self::EsmModuleItem(v) => v.code_generation(trie, ctx).await,
Self::ExportsInfoBinding(v) => v.code_generation(trie, ctx, module, exports).await,
Self::ExportsInfoBinding(v) => {
v.code_generation(trie, ctx, module_merging, module, exports)
.await
}
Self::ExportsInfoRef(v) => v.code_generation(trie, ctx).await,
Self::CjsExportsDropCodeGen(v) => v.code_generation(trie, ctx, module, exports).await,
Self::IdentReplacement(v) => v.code_generation(trie, ctx).await,
......@@ -245,7 +252,8 @@ impl CodeGen {
Self::WorkerAssetReferenceCodeGen(v) => v.code_generation(trie, ctx).await,
Self::ServiceWorkerAssetReferenceCodeGen(v) => v.code_generation(trie, ctx).await,
Self::ModuleHotReferenceCodeGen(v) => {
v.code_generation(trie, ctx, scope_hoisting_context).await
v.code_generation(trie, ctx, module_merging, scope_hoisting_context)
.await
}
Self::WorkerGlobalsReplacementCodeGen(v) => v.code_generation(trie, ctx).await,
}
......
......@@ -281,9 +281,13 @@ impl EcmascriptChunkPlaceable for EcmascriptCollectModuleWithChunkGroup {
// The *key* this is exposed under may be mangled; the local binding keeps its own name.
// `references::emit_collect` resolves the same key for the read side, so both agree.
let module_merging =
crate::references::esm::mangle::module_merging_analysis(module_graph, chunking_context)
.await?;
let export = generated_export_key(
ResolvedVc::upcast(self.to_resolved().await?),
chunking_context,
module_merging,
&COLLECT_LIST_EXPORT,
)
.await?;
......
......@@ -491,10 +491,16 @@ pub trait EcmascriptAnalyzable: Module {
) -> Result<Vc<EcmascriptModuleContentOptions>>;
}
/// Existing scope-hoisting results, passed explicitly to code generation. Each consumer reads
/// only its target modules through the keyed map; `None` means module merging is disabled.
pub type ModuleMergingAnalysis =
Option<ResolvedVc<turbopack_core::module_graph::merged_modules::MergedModuleGroups>>;
pub trait EcmascriptAnalyzableExt {
fn module_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
async_module_info: Option<Vc<AsyncModuleInfo>>,
) -> Vc<EcmascriptModuleContent>;
}
......@@ -506,11 +512,12 @@ where
fn module_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
async_module_info: Option<Vc<AsyncModuleInfo>>,
) -> Vc<EcmascriptModuleContent> {
let analyzable = Vc::upcast_non_strict::<Box<dyn EcmascriptAnalyzable>>(self);
let own_options = analyzable.module_content_options(chunking_context, async_module_info);
EcmascriptModuleContent::new(own_options)
EcmascriptModuleContent::new(own_options, module_merging)
}
}
......@@ -973,7 +980,7 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleAsset {
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
......@@ -983,7 +990,10 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleAsset {
);
async {
let async_module_options = self.get_async_module().module_options(async_module_info);
let content = self.module_content(chunking_context, async_module_info);
let module_merging =
references::esm::mangle::module_merging_analysis(module_graph, chunking_context)
.await?;
let content = self.module_content(chunking_context, module_merging, async_module_info);
EcmascriptChunkItemContent::new(content, chunking_context, async_module_options)
.to_resolved()
.await
......@@ -1080,6 +1090,7 @@ pub async fn chunk_item_content_with_code_from(
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
code_source: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let Some(analyzable) = ResolvedVc::try_sidecast::<Box<dyn EcmascriptAnalyzable>>(code_source)
......@@ -1091,7 +1102,9 @@ pub async fn chunk_item_content_with_code_from(
.await?;
let mut options = (*source_options).clone();
options.module = module;
let content = EcmascriptModuleContent::new(options.cell());
let module_merging =
references::esm::mangle::module_merging_analysis(module_graph, *chunking_context).await?;
let content = EcmascriptModuleContent::new(options.cell(), module_merging);
let async_module_options = code_source
.get_async_module()
.module_options(async_module_info);
......@@ -1106,6 +1119,7 @@ impl EcmascriptModuleContentOptions {
async fn merged_code_gens(
&self,
scope_hoisting_context: ScopeHoistingContext<'_>,
module_merging: ModuleMergingAnalysis,
eval_context: &EvalContext,
) -> Result<(Vec<CodeGeneration>, ReadRef<CodeGens>)> {
// Don't read `parsed` here again, it will cause a recomputation as `process_parse_result`
......@@ -1135,6 +1149,7 @@ impl EcmascriptModuleContentOptions {
let (code_gen, subsumed) = exports
.code_generation(
**chunking_context,
module_merging,
scope_hoisting_context,
eval_context,
*module,
......@@ -1158,6 +1173,7 @@ impl EcmascriptModuleContentOptions {
async_module_info.map(|info| *info),
**references,
**chunking_context,
module_merging,
)
.await?,
)
......@@ -1172,6 +1188,7 @@ impl EcmascriptModuleContentOptions {
.map(|r| {
r.code_generation(
**chunking_context,
module_merging,
scope_hoisting_context,
&subsumed_imports.namespaces,
)
......@@ -1184,6 +1201,7 @@ impl EcmascriptModuleContentOptions {
.map(|r| {
r.code_generation(
**chunking_context,
module_merging,
scope_hoisting_context,
&subsumed_imports,
)
......@@ -1199,6 +1217,7 @@ impl EcmascriptModuleContentOptions {
c.code_generation(
&code_generation.ast_paths,
**chunking_context,
module_merging,
scope_hoisting_context,
*module,
*exports,
......@@ -1226,7 +1245,10 @@ impl EcmascriptModuleContentOptions {
impl EcmascriptModuleContent {
/// Creates a new [`Vc<EcmascriptModuleContent>`].
#[turbo_tasks::function]
pub async fn new(input: Vc<EcmascriptModuleContentOptions>) -> Result<Vc<Self>> {
pub async fn new(
input: Vc<EcmascriptModuleContentOptions>,
module_merging: ModuleMergingAnalysis,
) -> Result<Vc<Self>> {
let input = input.await?;
let EcmascriptModuleContentOptions {
parsed,
......@@ -1248,6 +1270,7 @@ impl EcmascriptModuleContent {
*original_source_map,
*minify,
Some(&*input),
module_merging,
None,
)
.await?;
......@@ -1271,6 +1294,7 @@ impl EcmascriptModuleContent {
MinifyType::NoMinify,
None,
None,
None,
)
.await?;
emit_content(content, Default::default()).await
......@@ -1290,6 +1314,7 @@ impl EcmascriptModuleContent {
)>,
module_options: Vec<Vc<EcmascriptModuleContentOptions>>,
entry_points: Vec<ResolvedVc<Box<dyn EcmascriptAnalyzable>>>,
module_merging: ModuleMergingAnalysis,
) -> Result<Vc<Self>> {
async {
let modules = modules
......@@ -1334,6 +1359,7 @@ impl EcmascriptModuleContent {
*original_source_map,
*chunking_context.minify_type().await?,
Some(&*options),
module_merging,
Some(ScopeHoistingOptions {
module: *module,
modules: &modules,
......@@ -1352,6 +1378,7 @@ impl EcmascriptModuleContent {
let flat_group = *references::esm::mangle::flat_export_group(
*entry_points[0].0,
*options.chunking_context,
module_merging,
)
.await?;
let flat_exports = flat_group.is_some();
......@@ -2000,6 +2027,7 @@ async fn process_parse_result(
original_source_map: Option<ResolvedVc<Box<dyn GenerateSourceMap>>>,
minify: MinifyType,
options: Option<&EcmascriptModuleContentOptions>,
module_merging: ModuleMergingAnalysis,
scope_hoisting_options: Option<ScopeHoistingOptions<'_>>,
) -> Result<CodeGenResult> {
with_consumed_parse_result(
......@@ -2041,6 +2069,7 @@ async fn process_parse_result(
.unwrap()
.merged_code_gens(
ctx,
module_merging,
match &eval_context {
Either::Left(e) => e,
Either::Right(e) => e,
......@@ -2097,6 +2126,7 @@ async fn process_parse_result(
let (code_gens, ast_paths) = options
.merged_code_gens(
ScopeHoistingContext::None,
module_merging,
match &eval_context {
Either::Left(e) => e,
Either::Right(e) => e,
......
......@@ -114,7 +114,7 @@ impl EcmascriptChunkPlaceable for MergedEcmascriptModule {
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
......@@ -126,10 +126,14 @@ impl EcmascriptChunkPlaceable for MergedEcmascriptModule {
.map(|(m, _)| m.module_content_options(chunking_context, async_module_info))
.collect::<Vec<_>>();
let module_merging =
crate::references::esm::mangle::module_merging_analysis(module_graph, chunking_context)
.await?;
let content = EcmascriptModuleContent::new_merged(
modules.clone(),
options,
ResolvedVc::deref_vec(entry_points.clone()),
module_merging,
);
// Currently, merged modules never include async modules.
......
......@@ -378,14 +378,17 @@ impl EcmascriptChunkPlaceable for EcmascriptModulePartAsset {
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let analyze = self.analyze().await?;
let async_module_options = analyze.async_module.module_options(async_module_info);
let content = self.module_content(chunking_context, async_module_info);
let module_merging =
crate::references::esm::mangle::module_merging_analysis(module_graph, chunking_context)
.await?;
let content = self.module_content(chunking_context, module_merging, async_module_info);
Ok(EcmascriptChunkItemContent::new(
content,
......
......@@ -14,7 +14,7 @@ use turbopack_core::{
};
use crate::{
ScopeHoistingContext,
ModuleMergingAnalysis, ScopeHoistingContext,
code_gen::{CodeGeneration, CodeGenerationHoistedStmt},
references::esm::base::ReferencedAsset,
utils::AstSyntaxContext,
......@@ -111,6 +111,7 @@ impl AsyncModule {
async_module_info: Vc<AsyncModuleInfo>,
references: Vc<ModuleReferences>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
) -> Result<Vc<AsyncModuleIdents>> {
let async_module_info = async_module_info.await?;
......@@ -125,7 +126,12 @@ impl AsyncModule {
ReferencedAsset::External(_, ExternalType::EcmaScriptModule) => {
if self.import_externals {
referenced_asset
.get_ident(chunking_context, None, ScopeHoistingContext::None)
.get_ident(
chunking_context,
module_merging,
None,
ScopeHoistingContext::None,
)
.await?
.map(|i| i.into_module_namespace_ident().unwrap())
.map(|(i, ctx)| (i, ctx.unwrap_or_default().into()))
......@@ -139,7 +145,12 @@ impl AsyncModule {
.contains(&ResolvedVc::upcast(*placeable))
{
referenced_asset
.get_ident(chunking_context, None, ScopeHoistingContext::None)
.get_ident(
chunking_context,
module_merging,
None,
ScopeHoistingContext::None,
)
.await?
.map(|i| i.into_module_namespace_ident().unwrap())
.map(|(i, ctx)| (i, ctx.unwrap_or_default().into()))
......@@ -210,10 +221,16 @@ impl AsyncModule {
async_module_info: Option<Vc<AsyncModuleInfo>>,
references: Vc<ModuleReferences>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
) -> Result<CodeGeneration> {
if let Some(async_module_info) = async_module_info {
let async_idents = self
.get_async_idents(async_module_info, references, chunking_context)
.get_async_idents(
async_module_info,
references,
chunking_context,
module_merging,
)
.await?;
if !async_idents.is_empty() {
......
......@@ -24,6 +24,7 @@ use turbopack_core::{
use turbopack_resolve::ecmascript::esm_resolve;
use crate::{
ModuleMergingAnalysis,
analyzer::imports::ImportAnnotations,
ast_path_trie::{AstPathId, AstPathTrie, AstPathTrieBuilder},
code_gen::{CodeGen, CodeGeneration, IntoCodeGenReference},
......@@ -248,6 +249,7 @@ impl CollectReferenceCodeGen {
&self,
trie: &AstPathTrie,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
) -> Result<CodeGeneration> {
let reference = self.reference.await?;
......@@ -269,7 +271,13 @@ impl CollectReferenceCodeGen {
let export =
match ReferencedAsset::from_resolve_result(self.reference.resolve_reference()).await? {
ReferencedAsset::Some(module) => {
generated_export_key(module, chunking_context, &COLLECT_LIST_EXPORT).await?
generated_export_key(
module,
chunking_context,
module_merging,
&COLLECT_LIST_EXPORT,
)
.await?
}
_ => COLLECT_LIST_EXPORT,
};
......
......@@ -39,7 +39,7 @@ use turbopack_core::{
use turbopack_resolve::ecmascript::esm_resolve;
use crate::{
EcmascriptModuleAsset, ScopeHoistingContext,
EcmascriptModuleAsset, ModuleMergingAnalysis, ScopeHoistingContext,
analyzer::imports::ImportAnnotations,
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
code_gen::{CodeGeneration, CodeGenerationHoistedStmt},
......@@ -209,16 +209,24 @@ impl ReferencedAsset {
pub async fn get_ident(
&self,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
export: Option<RcStr>,
scope_hoisting_context: ScopeHoistingContext<'_>,
) -> Result<Option<ReferencedAssetIdent>> {
self.get_ident_inner(chunking_context, export, scope_hoisting_context, None)
.await
self.get_ident_inner(
chunking_context,
module_merging,
export,
scope_hoisting_context,
None,
)
.await
}
async fn get_ident_inner(
&self,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
export: Option<RcStr>,
scope_hoisting_context: ScopeHoistingContext<'_>,
initial: Option<&ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>>,
......@@ -275,6 +283,7 @@ impl ReferencedAsset {
return Ok(
match Box::pin(referenced_asset.get_ident_inner(
chunking_context,
module_merging,
imported,
scope_hoisting_context,
Some(asset),
......@@ -306,12 +315,13 @@ impl ReferencedAsset {
}
}
let target =
if let Some(group) = *flat_export_group(**asset, chunking_context).await? {
group.await?.entry
} else {
*asset
};
let target = if let Some(group) =
*flat_export_group(**asset, chunking_context, module_merging).await?
{
group.await?.entry
} else {
*asset
};
let import_source = ImportSource::Module { asset: target };
Some(ReferencedAssetIdent::Module {
namespace_ident: import_source.get_namespace_ident(chunking_context).await?,
......@@ -321,9 +331,10 @@ impl ReferencedAsset {
// the same map the producing module uses (see
// `EsmExports::code_generation`), so the two always agree.
export: match &export {
Some(export) => {
Some(generated_export_key(*asset, chunking_context, export).await?)
}
Some(export) => Some(
generated_export_key(*asset, chunking_context, module_merging, export)
.await?,
),
None => None,
},
import_source,
......@@ -810,6 +821,7 @@ impl EsmAssetReference {
pub(crate) async fn code_generation(
self: ResolvedVc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
scope_hoisting_context: ScopeHoistingContext<'_>,
// References a compact re-export registration (`TURBOPACK_ESM_REEXPORT`) already imports
// for itself. Emitting them here too would instantiate the module twice.
......@@ -886,6 +898,7 @@ impl EsmAssetReference {
let ident = referenced_asset
.get_ident(
chunking_context,
module_merging,
this.export_name
.as_ref()
.and_then(ModulePart::get_export)
......
......@@ -10,7 +10,7 @@ use turbo_tasks::{FxIndexMap, NonLocalValue, ResolvedVc, Vc};
use turbopack_core::chunk::ChunkingContext;
use crate::{
ScopeHoistingContext,
ModuleMergingAnalysis, ScopeHoistingContext,
ast_path_trie::{AstPathId, AstPathTrie},
code_gen::CodeGeneration,
create_visitor,
......@@ -92,6 +92,7 @@ impl EsmBindings {
&self,
trie: &AstPathTrie,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
scope_hoisting_context: ScopeHoistingContext<'_>,
) -> Result<CodeGeneration> {
let mut visitors = vec![];
......@@ -136,7 +137,12 @@ impl EsmBindings {
imported_module => match exports_cache.entry(export.clone()) {
std::collections::hash_map::Entry::Vacant(entry) => {
let ident: ImportedIdent = imported_module
.get_ident(chunking_context, export.clone(), scope_hoisting_context)
.get_ident(
chunking_context,
module_merging,
export.clone(),
scope_hoisting_context,
)
.await?
.map_or(ImportedIdent::Unresolvable, ImportedIdent::Module);
entry.insert(ident.clone());
......
......@@ -26,7 +26,7 @@ use turbopack_core::{
};
use crate::{
EcmascriptModuleAsset, ScopeHoistingContext,
EcmascriptModuleAsset, ModuleMergingAnalysis, ScopeHoistingContext,
analyzer::{
graph::EvalContext,
imports::{Export, ExportRegistrationMode},
......@@ -649,6 +649,7 @@ impl EsmExports {
async fn compact_reference_target(
reference: ResolvedVc<Box<dyn ModuleReference>>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
scope_hoisting_context: ScopeHoistingContext<'_>,
) -> Result<Option<(NamespaceKey, ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>)>> {
let referenced_asset =
......@@ -659,7 +660,12 @@ async fn compact_reference_target(
import_source: ImportSource::Module { asset },
..
}) = referenced_asset
.get_ident(chunking_context, None, scope_hoisting_context)
.get_ident(
chunking_context,
module_merging,
None,
scope_hoisting_context,
)
.await?
else {
return Ok(None);
......@@ -677,6 +683,7 @@ async fn build_compact_reexports(
mode: ExportRegistrationMode,
synthetic_references: Option<SyntheticReexportReferences<'_>>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
scope_hoisting_context: ScopeHoistingContext<'_>,
) -> Result<Option<CompactReexports>> {
let unused_references = chunking_context.unused_references().await?;
......@@ -705,6 +712,7 @@ async fn build_compact_reexports(
if let Some((key, asset)) = compact_reference_target(
ResolvedVc::upcast(*reference),
chunking_context,
module_merging,
scope_hoisting_context,
)
.await?
......@@ -739,6 +747,7 @@ async fn build_compact_reexports(
if let Some((key, _)) = compact_reference_target(
ResolvedVc::upcast(*reference),
chunking_context,
module_merging,
scope_hoisting_context,
)
.await?
......@@ -781,6 +790,7 @@ async fn build_compact_reexports(
}) = referenced_asset
.get_ident(
chunking_context,
module_merging,
Some(imported_name.clone()),
scope_hoisting_context,
)
......@@ -1001,6 +1011,7 @@ impl EsmExports {
pub(crate) async fn code_generation(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
scope_hoisting_context: ScopeHoistingContext<'_>,
eval_context: &EvalContext,
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
......@@ -1068,9 +1079,9 @@ impl EsmExports {
let mut getters = Vec::new();
// The keys this module's exports are emitted under. Consumers resolve the same map for this
// module (see `ReferencedAsset::get_ident_inner`), so both sides always agree.
let mangled_names = mangled_export_names(*module, chunking_context).await?;
let mangled_names = mangled_export_names(*module, chunking_context, module_merging).await?;
let flat_group = if scope_hoisting_context.module().is_some() {
*flat_export_group(*module, chunking_context).await?
*flat_export_group(*module, chunking_context, module_merging).await?
} else {
None
};
......@@ -1099,6 +1110,7 @@ impl EsmExports {
export_registration_mode,
synthetic_references,
chunking_context,
module_merging,
scope_hoisting_context,
)
.await?
......@@ -1173,7 +1185,7 @@ impl EsmExports {
let referenced_asset =
ReferencedAsset::from_resolve_result(esm_ref.resolve_reference()).await?;
referenced_asset
.get_ident(chunking_context, Some(name.clone()), scope_hoisting_context)
.get_ident(chunking_context, module_merging, Some(name.clone()), scope_hoisting_context)
.await?
.map(|ident| {
let expr = ident.as_expr_individual(DUMMY_SP);
......@@ -1238,7 +1250,12 @@ impl EsmExports {
let referenced_asset =
ReferencedAsset::from_resolve_result(esm_ref.resolve_reference()).await?;
referenced_asset
.get_ident(chunking_context, None, scope_hoisting_context)
.get_ident(
chunking_context,
module_merging,
None,
scope_hoisting_context,
)
.await?
.map(|ident| {
let imported = ident.as_expr(DUMMY_SP, false);
......
......@@ -27,11 +27,29 @@ use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
chunk::{ChunkingContext, MergeableModuleExposure},
module_graph::{binding_usage_info::ModuleExportUsageInfo, merged_modules::MergedModuleGroup},
module_graph::{
ModuleGraph, binding_usage_info::ModuleExportUsageInfo, merged_modules::MergedModuleGroup,
},
};
use self::table::shorten_to_unique_names;
use crate::chunk::{EcmascriptChunkPlaceable, EcmascriptExports};
use crate::{
ModuleMergingAnalysis,
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
};
/// Obtain the existing scope-hoisting analysis at a graph-aware code-generation boundary.
/// Consumers below read only the keyed entries for their target modules.
pub(crate) async fn module_merging_analysis(
module_graph: Vc<ModuleGraph>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<ModuleMergingAnalysis> {
if *chunking_context.is_module_merging_enabled().await? {
Ok(Some(module_graph.merged_modules().await?.groups))
} else {
Ok(None)
}
}
/// A module's export name mapping: original export name -> the key actually used in the output.
///
......@@ -64,12 +82,12 @@ pub struct OptionFlatExportGroup(pub Option<ResolvedVc<FlatExportGroup>>);
pub async fn flat_export_group(
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
) -> Result<Vc<OptionFlatExportGroup>> {
let group = chunking_context
.module_merging_group(*ResolvedVc::upcast(module))
.await?;
if let Some(group) = *group {
return Ok(flat_group_exports(*group, chunking_context));
if let Some(groups) = module_merging
&& let Some(group) = groups.get(&ResolvedVc::upcast(module)).await?.as_deref()
{
return Ok(flat_group_exports(**group, chunking_context));
}
Ok(Vc::cell(None))
}
......@@ -164,8 +182,9 @@ async fn flat_group_exports(
pub async fn mangled_export_names(
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
) -> Result<Vc<MangledExportNames>> {
if let Some(group) = *flat_export_group(*module, chunking_context).await? {
if let Some(group) = *flat_export_group(*module, chunking_context, module_merging).await? {
return Ok(Vc::cell(Some(
group.await?.names.get(&module).cloned().unwrap_or_default(),
)));
......@@ -236,9 +255,10 @@ pub async fn mangled_export_names(
pub async fn generated_export_key(
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
export: &RcStr,
) -> Result<RcStr> {
let names = mangled_export_names(*module, chunking_context).await?;
let names = mangled_export_names(*module, chunking_context, module_merging).await?;
Ok(names
.as_ref()
.and_then(|names| names.get(export))
......
......@@ -10,6 +10,7 @@ use turbo_tasks::{NonLocalValue, ResolvedVc, Vc, debug::ValueDebugFormat};
use turbopack_core::chunk::ChunkingContext;
use crate::{
ModuleMergingAnalysis,
ast_path_trie::{AstPathId, AstPathTrie},
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
code_gen::{CodeGen, CodeGeneration},
......@@ -37,6 +38,7 @@ impl ExportsInfoBinding {
&self,
_trie: &AstPathTrie,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
exports: ResolvedVc<EcmascriptExports>,
) -> Result<CodeGeneration> {
......@@ -50,7 +52,7 @@ impl ExportsInfoBinding {
// whether export mangling is enabled at all — see the `map` closure below for exactly
// what each of the three fields means.
let exports = exports.await?;
let mangled_names = mangled_export_names(*module, chunking_context).await?;
let mangled_names = mangled_export_names(*module, chunking_context, module_merging).await?;
let props = if let EcmascriptExports::EsmExports(exports) = &*exports {
exports
......
......@@ -23,7 +23,7 @@ use turbopack_core::{
use turbopack_resolve::ecmascript::{cjs_resolve, esm_resolve};
use crate::{
ScopeHoistingContext,
ModuleMergingAnalysis, ScopeHoistingContext,
ast_path_trie::{AstPathId, AstPathTrie},
code_gen::{CodeGen, CodeGeneration},
create_visitor,
......@@ -128,6 +128,7 @@ impl ModuleHotReferenceCodeGen {
&self,
trie: &AstPathTrie,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
scope_hoisting_context: ScopeHoistingContext<'_>,
) -> Result<CodeGeneration> {
let resolved_ids: Vec<ReadRef<PatternMapping>> = self
......@@ -162,7 +163,12 @@ impl ModuleHotReferenceCodeGen {
match &referenced_asset {
ReferencedAsset::Some(asset) => {
let ident = referenced_asset
.get_ident(chunking_context, None, scope_hoisting_context)
.get_ident(
chunking_context,
module_merging,
None,
scope_hoisting_context,
)
.await?;
if let Some((namespace_ident, ctxt)) =
ident.and_then(|i| i.into_module_namespace_ident())
......
......@@ -274,12 +274,15 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleRenameModule {
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let async_module_options = self.get_async_module().module_options(async_module_info);
let content = self.module_content(chunking_context, async_module_info);
let module_merging =
crate::references::esm::mangle::module_merging_analysis(module_graph, chunking_context)
.await?;
let content = self.module_content(chunking_context, module_merging, async_module_info);
Ok(EcmascriptChunkItemContent::new(
content,
chunking_context,
......
......@@ -283,11 +283,14 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule {
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let content = self.module_content(chunking_context, async_module_info);
let module_merging =
crate::references::esm::mangle::module_merging_analysis(module_graph, chunking_context)
.await?;
let content = self.module_content(chunking_context, module_merging, async_module_info);
let async_module_options = self.get_async_module().module_options(async_module_info);
......
......@@ -200,14 +200,17 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleLocalsModule {
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let analyze = self.await?.module.analyze().await?;
let async_module_options = analyze.async_module.module_options(async_module_info);
let content = self.module_content(chunking_context, async_module_info);
let module_merging =
crate::references::esm::mangle::module_merging_analysis(module_graph, chunking_context)
.await?;
let content = self.module_content(chunking_context, module_merging, async_module_info);
Ok(EcmascriptChunkItemContent::new(
content,
......
......@@ -15,7 +15,7 @@ use turbopack_core::{
};
use crate::{
ScopeHoistingContext,
ModuleMergingAnalysis, ScopeHoistingContext,
chunk::EcmascriptChunkPlaceable,
code_gen::{CodeGeneration, CodeGenerationHoistedStmt},
references::esm::base::{ReferencedAsset, ReferencedAssetIdent},
......@@ -161,6 +161,7 @@ impl EcmascriptModulePartReference {
pub async fn code_generation(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis,
scope_hoisting_context: ScopeHoistingContext<'_>,
// Namespace variables a compact re-export registration already imports for itself.
subsumed_namespaces: &FxHashSet<(String, Option<SyntaxContext>)>,
......@@ -206,6 +207,7 @@ impl EcmascriptModulePartReference {
let ident = referenced_asset
.get_ident(
chunking_context,
module_merging,
match export_usage {
ExportUsage::Named(export) => Some(export.clone()),
ExportUsage::PartialNamespaceObject(_)
......
......@@ -27,7 +27,10 @@ use crate::{
EcmascriptExports, data::EcmascriptChunkData, ecmascript_chunk_item,
},
embed_js::embed_fs,
references::esm::{generated_export_key, mangle::flat_export_group},
references::esm::{
generated_export_key,
mangle::{flat_export_group, module_merging_analysis},
},
runtime_functions::{TURBOPACK_EXPORT_VALUE, TURBOPACK_REQUIRE},
utils::{StringifyJs, StringifyModuleId},
};
......@@ -292,7 +295,7 @@ impl EcmascriptChunkPlaceable for WorkerLoaderModule {
.cell());
}
let chunking_context = chunking_context.with_module_graph(module_graph);
let module_merging = module_merging_analysis(module_graph, chunking_context).await?;
let create_worker_module = self.create_worker_module().to_resolved().await?;
let mut create_worker_target = create_worker_module;
// The helper's `default` export is read here as a string, so it has to go through the same
......@@ -304,10 +307,18 @@ impl EcmascriptChunkPlaceable for WorkerLoaderModule {
Some(placeable) => {
// Resolve the factory identity as well as the key, just like a generated ESM
// import.
if let Some(group) = *flat_export_group(*placeable, chunking_context).await? {
if let Some(group) =
*flat_export_group(*placeable, chunking_context, module_merging).await?
{
create_worker_target = ResolvedVc::upcast(group.await?.entry);
}
generated_export_key(placeable, chunking_context, &rcstr!("default")).await?
generated_export_key(
placeable,
chunking_context,
module_merging,
&rcstr!("default"),
)
.await?
}
None => rcstr!("default"),
};
......
......@@ -26,7 +26,6 @@ use turbopack_core::{
ModuleGraph,
binding_usage_info::{BindingUsageInfo, ModuleExportUsage},
chunk_group_info::ChunkGroupKey,
merged_modules::{MergedModuleInfo, OptionMergedModuleGroup},
},
output::{OutputAsset, OutputAssets},
};
......@@ -239,7 +238,6 @@ pub struct NodeJsChunkingContext {
module_id_strategy: Option<ResolvedVc<ModuleIdStrategy>>,
/// The module export usage info, if available.
export_usage: Option<ResolvedVc<BindingUsageInfo>>,
merged_modules: Option<ResolvedVc<MergedModuleInfo>>,
/// Which references are unused and should be skipped (e.g. during codegen).
unused_references: Option<ResolvedVc<UnusedReferences>>,
/// The strategy to use for generating source map source uris
......@@ -296,7 +294,6 @@ impl NodeJsChunkingContext {
source_map_source_type: SourceMapSourceType::TurbopackUri,
module_id_strategy: None,
export_usage: None,
merged_modules: None,
unused_references: None,
chunking_configs: Default::default(),
debug_ids: false,
......@@ -804,32 +801,6 @@ impl ChunkingContext for NodeJsChunkingContext {
})
}
#[turbo_tasks::function]
async fn with_module_graph(
self: Vc<Self>,
module_graph: Vc<ModuleGraph>,
) -> Result<Vc<Box<dyn ChunkingContext>>> {
let this = self.await?;
if !this.enable_module_merging {
return Ok(Vc::upcast(self));
}
let mut this = (*this).clone();
this.merged_modules = Some(module_graph.merged_modules().to_resolved().await?);
Ok(Vc::upcast(this.cell()))
}
#[turbo_tasks::function]
async fn module_merging_group(
&self,
module: ResolvedVc<Box<dyn Module>>,
) -> Result<Vc<OptionMergedModuleGroup>> {
Ok(Vc::cell(if let Some(info) = self.merged_modules {
info.await?.groups.get(&module).await?.as_deref().copied()
} else {
None
}))
}
#[turbo_tasks::function]
async fn module_export_usage(
&self,
......
......@@ -227,7 +227,7 @@ impl EcmascriptChunkPlaceable for WebAssemblyModuleAsset {
async fn chunk_item_content(
self: ResolvedVc<Self>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
......@@ -247,6 +247,7 @@ impl EcmascriptChunkPlaceable for WebAssemblyModuleAsset {
*ResolvedVc::upcast(self),
self.loader(),
*chunking_context,
module_graph,
async_module_info,
))
}
......
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