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