turbopack_ecmascript/chunk/
code_and_ids.rs

1use anyhow::Result;
2use rustc_hash::FxHashMap;
3use smallvec::{SmallVec, smallvec};
4use turbo_tasks::{ReadRef, TryJoinIterExt, Vc};
5use turbopack_core::{
6    chunk::{ChunkItemExt, ModuleId},
7    code_builder::Code,
8};
9
10use crate::chunk::{
11    EcmascriptChunkItemBatchGroup, EcmascriptChunkItemExt, EcmascriptChunkItemOrBatchWithAsyncInfo,
12    EcmascriptChunkItemWithAsyncInfo,
13};
14
15#[turbo_tasks::value(transparent, serialization = "none")]
16pub struct CodeAndIds(SmallVec<[(ReadRef<ModuleId>, ReadRef<Code>); 1]>);
17
18#[turbo_tasks::value(transparent, serialization = "none")]
19pub struct BatchGroupCodeAndIds(
20    FxHashMap<EcmascriptChunkItemOrBatchWithAsyncInfo, ReadRef<CodeAndIds>>,
21);
22
23#[turbo_tasks::function]
24pub async fn batch_group_code_and_ids(
25    batch_group: Vc<EcmascriptChunkItemBatchGroup>,
26) -> Result<Vc<BatchGroupCodeAndIds>> {
27    Ok(Vc::cell(
28        batch_group
29            .await?
30            .items
31            .iter()
32            .map(async |item| Ok((item.clone(), item_code_and_ids(item.clone()).await?)))
33            .try_join()
34            .await?
35            .into_iter()
36            .collect(),
37    ))
38}
39
40#[turbo_tasks::function]
41pub async fn item_code_and_ids(
42    item: EcmascriptChunkItemOrBatchWithAsyncInfo,
43) -> Result<Vc<CodeAndIds>> {
44    Ok(Vc::cell(match item {
45        EcmascriptChunkItemOrBatchWithAsyncInfo::ChunkItem(EcmascriptChunkItemWithAsyncInfo {
46            chunk_item,
47            async_info,
48            ..
49        }) => {
50            let id = chunk_item.id();
51            let code = chunk_item.code(async_info.map(|info| *info));
52            smallvec![(id.await?, code.await?)]
53        }
54        EcmascriptChunkItemOrBatchWithAsyncInfo::Batch(batch) => batch
55            .await?
56            .chunk_items
57            .iter()
58            .map(|item| async {
59                Ok((
60                    item.chunk_item.id().await?,
61                    item.chunk_item
62                        .code(item.async_info.map(|info| *info))
63                        .await?,
64                ))
65            })
66            .try_join()
67            .await?
68            .into(),
69    }))
70}