use anyhow::{Context, Result}; use turbo_tasks::{ResolvedVc, Vc}; use turbopack_core::{ asset::{Asset, AssetContent}, chunk::{ AsyncModuleInfo, ChunkItem, ChunkType, ChunkableModule, ChunkingContext, MergeableModuleExposure, MergeableModules, MergeableModulesExposed, }, ident::AssetIdent, module::Module, module_graph::ModuleGraph, reference::ModuleReferences, }; use crate::{ EcmascriptAnalyzable, EcmascriptModuleContent, EcmascriptOptions, chunk::{EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkType}, }; #[turbo_tasks::value(shared)] pub(crate) struct MergedEcmascriptModule { modules: Vec<( ResolvedVc>, MergeableModuleExposure, )>, entry_points: Vec>>, options: ResolvedVc, } impl MergedEcmascriptModule { pub async fn new( modules: Vc, entry_points: Vc, options: ResolvedVc, ) -> Result> { Ok(MergedEcmascriptModule { modules: modules .await? .iter() .map(|(m, exposed)| { Ok(( ResolvedVc::try_sidecast::>(*m) .context("expected EcmascriptAnalyzable")?, *exposed, )) }) .collect::>>()?, entry_points: entry_points .await? .iter() .map(|m| { ResolvedVc::try_sidecast::>(*m) .context("expected EcmascriptAnalyzable") }) .collect::>>()?, options, } .resolved_cell()) } } #[turbo_tasks::value_impl] impl Asset for MergedEcmascriptModule { #[turbo_tasks::function] fn content(&self) -> Vc { panic!("content() should not be called"); } } #[turbo_tasks::value_impl] impl Module for MergedEcmascriptModule { #[turbo_tasks::function] fn ident(&self) -> Vc { // This purposely reuses the module's ident as it has replaced the original module, thus // there can never be a collision. self.entry_points.first().unwrap().ident() } #[turbo_tasks::function] async fn references(self: Vc) -> Result> { panic!("references() should not be called"); } #[turbo_tasks::function] async fn is_self_async(&self) -> Result> { panic!("is_self_async() should not be called"); } } #[turbo_tasks::value_impl] impl ChunkableModule for MergedEcmascriptModule { #[turbo_tasks::function] fn as_chunk_item( self: ResolvedVc, _module_graph: ResolvedVc, chunking_context: ResolvedVc>, ) -> Vc> { Vc::upcast( MergedEcmascriptModuleChunkItem { module: self, chunking_context, } .cell(), ) } } #[turbo_tasks::value] struct MergedEcmascriptModuleChunkItem { module: ResolvedVc, chunking_context: ResolvedVc>, } #[turbo_tasks::value_impl] impl ChunkItem for MergedEcmascriptModuleChunkItem { #[turbo_tasks::function] fn asset_ident(&self) -> Vc { self.module.ident() } #[turbo_tasks::function] fn chunking_context(&self) -> Vc> { *ResolvedVc::upcast(self.chunking_context) } #[turbo_tasks::function] async fn ty(&self) -> Result>> { Ok(Vc::upcast( Vc::::default().resolve().await?, )) } #[turbo_tasks::function] fn module(&self) -> Vc> { *ResolvedVc::upcast(self.module) } } #[turbo_tasks::value_impl] impl EcmascriptChunkItem for MergedEcmascriptModuleChunkItem { #[turbo_tasks::function] fn content(self: Vc) -> Vc { panic!("content() should not be called"); } #[turbo_tasks::function] async fn content_with_async_module_info( &self, async_module_info: Option>, ) -> Result> { let module = self.module.await?; let modules = &module.modules; let entry_points = &module.entry_points; let options = modules .iter() .map(|(m, _)| { let Some(m) = ResolvedVc::try_downcast::>(*m) else { anyhow::bail!("Expected EcmascriptAnalyzable in scope hoisting group"); }; Ok(m.module_content_options(*self.chunking_context, async_module_info)) }) .collect::>>()?; let content = EcmascriptModuleContent::new_merged( modules.clone(), options, ResolvedVc::deref_vec(entry_points.clone()), ); // Currently, merged modules never include async modules. let async_module_options = Vc::cell(None); Ok(EcmascriptChunkItemContent::new( content, *self.chunking_context, async_module_options, )) } }