File size: 3,424 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use anyhow::Result;
use serde::{Deserialize, Serialize};
use turbo_tasks::{NonLocalValue, ResolvedVc, TaskInput, TryJoinIterExt, Vc, trace::TraceRawVcs};
use turbopack_core::{
    chunk::{
        ChunkItem, ChunkItemBatchGroup, ChunkItemBatchWithAsyncModuleInfo,
        ChunkItemOrBatchWithAsyncModuleInfo,
    },
    output::OutputAssets,
};

use crate::chunk::EcmascriptChunkItemWithAsyncInfo;

#[derive(
    Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, NonLocalValue, TaskInput,
)]
pub enum EcmascriptChunkItemOrBatchWithAsyncInfo {
    ChunkItem(EcmascriptChunkItemWithAsyncInfo),
    Batch(ResolvedVc<EcmascriptChunkBatchWithAsyncInfo>),
}

impl EcmascriptChunkItemOrBatchWithAsyncInfo {
    pub async fn from_chunk_item_or_batch(
        item: &ChunkItemOrBatchWithAsyncModuleInfo,
    ) -> Result<Self> {
        Ok(match item {
            ChunkItemOrBatchWithAsyncModuleInfo::ChunkItem(chunk_item) => {
                EcmascriptChunkItemOrBatchWithAsyncInfo::ChunkItem(
                    EcmascriptChunkItemWithAsyncInfo::from_chunk_item(chunk_item)?,
                )
            }
            &ChunkItemOrBatchWithAsyncModuleInfo::Batch(batch) => {
                EcmascriptChunkItemOrBatchWithAsyncInfo::Batch(
                    EcmascriptChunkBatchWithAsyncInfo::from_batch(*batch)
                        .to_resolved()
                        .await?,
                )
            }
        })
    }

    pub fn references(&self) -> Vc<OutputAssets> {
        match self {
            EcmascriptChunkItemOrBatchWithAsyncInfo::ChunkItem(item) => {
                item.chunk_item.references()
            }
            EcmascriptChunkItemOrBatchWithAsyncInfo::Batch(batch) => batch.references(),
        }
    }
}

#[turbo_tasks::value]
pub struct EcmascriptChunkBatchWithAsyncInfo {
    pub chunk_items: Vec<EcmascriptChunkItemWithAsyncInfo>,
}

#[turbo_tasks::value_impl]
impl EcmascriptChunkBatchWithAsyncInfo {
    #[turbo_tasks::function]
    pub async fn from_batch(batch: Vc<ChunkItemBatchWithAsyncModuleInfo>) -> Result<Vc<Self>> {
        Ok(Self {
            chunk_items: batch
                .await?
                .chunk_items
                .iter()
                .map(EcmascriptChunkItemWithAsyncInfo::from_chunk_item)
                .collect::<Result<Vec<_>>>()?,
        }
        .cell())
    }

    #[turbo_tasks::function]
    pub async fn references(&self) -> Result<Vc<OutputAssets>> {
        let mut references = Vec::new();
        // We expect most references to be empty, and avoiding try_join to avoid allocating the Vec
        for item in &self.chunk_items {
            references.extend(item.chunk_item.references().await?.into_iter().copied());
        }
        Ok(Vc::cell(references))
    }
}

#[turbo_tasks::value]
pub struct EcmascriptChunkItemBatchGroup {
    pub items: Vec<EcmascriptChunkItemOrBatchWithAsyncInfo>,
}

#[turbo_tasks::value_impl]
impl EcmascriptChunkItemBatchGroup {
    #[turbo_tasks::function]
    pub async fn from_chunk_item_batch_group(
        batch_group: Vc<ChunkItemBatchGroup>,
    ) -> Result<Vc<Self>> {
        Ok(Self {
            items: batch_group
                .await?
                .items
                .iter()
                .map(EcmascriptChunkItemOrBatchWithAsyncInfo::from_chunk_item_or_batch)
                .try_join()
                .await?,
        }
        .cell())
    }
}