| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| use anyhow::Result; |
| use next_core::{ |
| next_app::ClientReferencesChunks, next_client_reference::EcmascriptClientReferenceModule, |
| next_dynamic::NextDynamicEntryModule, |
| }; |
| use serde::{Deserialize, Serialize}; |
| use turbo_tasks::{ |
| FxIndexMap, NonLocalValue, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc, |
| debug::ValueDebugFormat, trace::TraceRawVcs, |
| }; |
| use turbopack_core::{ |
| chunk::{ |
| ChunkItem, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt, ModuleId, |
| availability_info::AvailabilityInfo, |
| }, |
| module::Module, |
| module_graph::{ModuleGraph, SingleModuleGraph, SingleModuleGraphModuleNode}, |
| output::OutputAssets, |
| }; |
|
|
| use crate::module_graph::DynamicImportEntriesWithImporter; |
|
|
| pub(crate) enum NextDynamicChunkAvailability<'a> { |
| |
| ClientReferences(&'a ClientReferencesChunks), |
| |
| AvailabilityInfo(AvailabilityInfo), |
| } |
|
|
| pub(crate) async fn collect_next_dynamic_chunks( |
| module_graph: Vc<ModuleGraph>, |
| chunking_context: Vc<Box<dyn ChunkingContext>>, |
| dynamic_import_entries: ReadRef<DynamicImportEntriesWithImporter>, |
| chunking_availability: NextDynamicChunkAvailability<'_>, |
| ) -> Result<ResolvedVc<DynamicImportedChunks>> { |
| let chunking_availability = &chunking_availability; |
| let dynamic_import_chunks = dynamic_import_entries |
| .iter() |
| .map(|(dynamic_entry, parent_client_reference)| async move { |
| let module = ResolvedVc::upcast::<Box<dyn ChunkableModule>>(*dynamic_entry); |
|
|
| |
| |
| let availability_info = match chunking_availability { |
| NextDynamicChunkAvailability::ClientReferences(client_reference_chunks) => { |
| client_reference_chunks |
| .client_component_client_chunks |
| .get(&parent_client_reference.unwrap()) |
| .unwrap() |
| .1 |
| } |
| NextDynamicChunkAvailability::AvailabilityInfo(availability_info) => { |
| *availability_info |
| } |
| }; |
|
|
| let async_loader = |
| chunking_context.async_loader_chunk_item(*module, module_graph, availability_info); |
| let async_chunk_group = async_loader.references().to_resolved().await?; |
|
|
| let module_id = dynamic_entry |
| .chunk_item_id(Vc::upcast(chunking_context)) |
| .to_resolved() |
| .await?; |
|
|
| Ok((*dynamic_entry, (module_id, async_chunk_group))) |
| }) |
| .try_join() |
| .await?; |
|
|
| Ok(ResolvedVc::cell(FxIndexMap::from_iter( |
| dynamic_import_chunks, |
| ))) |
| } |
|
|
| #[turbo_tasks::value(transparent)] |
| #[derive(Default)] |
| pub struct DynamicImportedChunks( |
| pub FxIndexMap< |
| ResolvedVc<NextDynamicEntryModule>, |
| (ResolvedVc<ModuleId>, ResolvedVc<OutputAssets>), |
| >, |
| ); |
|
|
| #[derive( |
| Clone, PartialEq, Eq, ValueDebugFormat, Serialize, Deserialize, TraceRawVcs, NonLocalValue, |
| )] |
| pub enum DynamicImportEntriesMapType { |
| DynamicEntry(ResolvedVc<NextDynamicEntryModule>), |
| ClientReference(ResolvedVc<EcmascriptClientReferenceModule>), |
| } |
|
|
| #[turbo_tasks::value(transparent)] |
| pub struct DynamicImportEntries( |
| pub FxIndexMap<ResolvedVc<Box<dyn Module>>, DynamicImportEntriesMapType>, |
| ); |
|
|
| #[turbo_tasks::function] |
| pub async fn map_next_dynamic(graph: Vc<SingleModuleGraph>) -> Result<Vc<DynamicImportEntries>> { |
| let actions = graph |
| .await? |
| .iter_nodes() |
| .map(|node| async move { |
| let SingleModuleGraphModuleNode { module } = node; |
|
|
| if module |
| .ident() |
| .await? |
| .layer |
| .as_ref() |
| .is_some_and(|layer| layer.name() == "app-client" || layer.name() == "client") |
| && let Some(dynamic_entry_module) = |
| ResolvedVc::try_downcast_type::<NextDynamicEntryModule>(*module) |
| { |
| return Ok(Some(( |
| *module, |
| DynamicImportEntriesMapType::DynamicEntry(dynamic_entry_module), |
| ))); |
| } |
| |
| |
| if let Some(client_reference_module) = |
| ResolvedVc::try_downcast_type::<EcmascriptClientReferenceModule>(*module) |
| { |
| return Ok(Some(( |
| *module, |
| DynamicImportEntriesMapType::ClientReference(client_reference_module), |
| ))); |
| } |
| |
| Ok(None) |
| }) |
| .try_flat_join() |
| .await?; |
| Ok(Vc::cell(actions.into_iter().collect())) |
| } |
|
|