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