turbopack_ecmascript/chunk/
chunk_type.rs1use anyhow::{Result, bail};
2use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueDefault, ValueToString, Vc};
3use turbopack_core::chunk::{
4 AsyncModuleInfo, Chunk, ChunkItem, ChunkItemBatchGroup, ChunkItemOrBatchWithAsyncModuleInfo,
5 ChunkType, ChunkingContext, round_chunk_item_size,
6};
7
8use super::{EcmascriptChunk, EcmascriptChunkContent, EcmascriptChunkItem};
9use crate::chunk::batch::{EcmascriptChunkItemBatchGroup, EcmascriptChunkItemOrBatchWithAsyncInfo};
10
11#[turbo_tasks::value]
12#[derive(Default, ValueToString)]
13#[value_to_string("ecmascript")]
14pub struct EcmascriptChunkType {}
15
16#[turbo_tasks::value_impl]
17impl ChunkType for EcmascriptChunkType {
18 #[turbo_tasks::function]
19 fn is_style(self: Vc<Self>) -> Vc<bool> {
20 Vc::cell(false)
21 }
22
23 #[turbo_tasks::function]
24 async fn chunk(
25 &self,
26 chunking_context: Vc<Box<dyn ChunkingContext>>,
27 chunk_items: Vec<ChunkItemOrBatchWithAsyncModuleInfo>,
28 batch_groups: Vec<ResolvedVc<ChunkItemBatchGroup>>,
29 ) -> Result<Vc<Box<dyn Chunk>>> {
30 let content = EcmascriptChunkContent {
31 chunk_items: chunk_items
32 .iter()
33 .map(EcmascriptChunkItemOrBatchWithAsyncInfo::from_chunk_item_or_batch)
34 .try_join()
35 .await?,
36 batch_groups: batch_groups
37 .into_iter()
38 .map(|batch_group| {
39 EcmascriptChunkItemBatchGroup::from_chunk_item_batch_group(*batch_group)
40 .to_resolved()
41 })
42 .try_join()
43 .await?,
44 }
45 .cell();
46 Ok(Vc::upcast(EcmascriptChunk::new(chunking_context, content)))
47 }
48
49 #[turbo_tasks::function]
50 async fn chunk_item_size(
51 &self,
52 _chunking_context: Vc<Box<dyn ChunkingContext>>,
53 chunk_item: ResolvedVc<Box<dyn ChunkItem>>,
54 async_module_info: Option<Vc<AsyncModuleInfo>>,
55 ) -> Result<Vc<usize>> {
56 let Some(chunk_item) = ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkItem>>(chunk_item)
57 else {
58 bail!("Chunk item is not an ecmascript chunk item but reporting chunk type ecmascript");
59 };
60 Ok(Vc::cell(
61 chunk_item
62 .content_with_async_module_info(async_module_info, true)
63 .await
64 .map_or(0, |content| round_chunk_item_size(content.inner_code.len())),
65 ))
66 }
67}
68
69#[turbo_tasks::value_impl]
70impl ValueDefault for EcmascriptChunkType {
71 #[turbo_tasks::function]
72 fn value_default() -> Vc<Self> {
73 Self::default().cell()
74 }
75}