turbopack_ecmascript/chunk/
content.rs

1use std::future::IntoFuture;
2
3use anyhow::Result;
4use either::Either;
5use turbo_tasks::{ReadRef, ResolvedVc, TryJoinIterExt, Vc};
6use turbopack_core::chunk::{ChunkItem, ChunkItems, batch_info};
7
8use crate::chunk::{
9    CodeAndIds,
10    batch::{EcmascriptChunkItemBatchGroup, EcmascriptChunkItemOrBatchWithAsyncInfo},
11    batch_group_code_and_ids, item_code_and_ids,
12};
13
14#[turbo_tasks::value(shared)]
15pub struct EcmascriptChunkContent {
16    pub chunk_items: Vec<EcmascriptChunkItemOrBatchWithAsyncInfo>,
17    pub batch_groups: Vec<ResolvedVc<EcmascriptChunkItemBatchGroup>>,
18}
19
20#[turbo_tasks::value_impl]
21impl EcmascriptChunkContent {
22    #[turbo_tasks::function]
23    pub async fn included_chunk_items(&self) -> Result<Vc<ChunkItems>> {
24        Ok(ChunkItems(
25            self.chunk_items
26                .iter()
27                .map(async |item| match item {
28                    EcmascriptChunkItemOrBatchWithAsyncInfo::ChunkItem(item) => {
29                        Ok(Either::Left(item.chunk_item))
30                    }
31                    EcmascriptChunkItemOrBatchWithAsyncInfo::Batch(batch) => {
32                        Ok(Either::Right(batch.await?))
33                    }
34                })
35                .try_join()
36                .await?
37                .iter()
38                .flat_map(|item| match item {
39                    Either::Left(item) => Either::Left(std::iter::once(*item)),
40                    Either::Right(batch) => {
41                        Either::Right(batch.chunk_items.iter().map(|item| item.chunk_item))
42                    }
43                })
44                .map(ResolvedVc::upcast::<Box<dyn ChunkItem>>)
45                .collect(),
46        )
47        .cell())
48    }
49}
50
51impl EcmascriptChunkContent {
52    pub async fn chunk_item_code_and_ids(&self) -> Result<Vec<ReadRef<CodeAndIds>>> {
53        batch_info(
54            &self.batch_groups,
55            &self.chunk_items,
56            |batch| batch_group_code_and_ids(batch).into_future(),
57            |item| item_code_and_ids(item.clone()).into_future(),
58        )
59        .await
60    }
61}