Skip to main content

turbopack_ecmascript/chunk/
code_module_ids_and_paths.rs

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