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