turbopack_ecmascript/side_effect_optimization/locals/
chunk_item.rs

1use anyhow::Result;
2use turbo_tasks::{ResolvedVc, Vc};
3use turbopack_core::{
4    chunk::{AsyncModuleInfo, ChunkItem, ChunkType, ChunkingContext},
5    ident::AssetIdent,
6    module::Module,
7};
8
9use super::module::EcmascriptModuleLocalsModule;
10use crate::{
11    EcmascriptAnalyzable,
12    chunk::{EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkType},
13};
14
15/// The chunk item for [EcmascriptModuleLocalsModule].
16#[turbo_tasks::value(shared)]
17pub struct EcmascriptModuleLocalsChunkItem {
18    pub(super) module: ResolvedVc<EcmascriptModuleLocalsModule>,
19    pub(super) chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
20}
21
22#[turbo_tasks::value_impl]
23impl EcmascriptChunkItem for EcmascriptModuleLocalsChunkItem {
24    #[turbo_tasks::function]
25    fn content(self: Vc<Self>) -> Vc<EcmascriptChunkItemContent> {
26        panic!("content() should never be called");
27    }
28
29    #[turbo_tasks::function]
30    async fn content_with_async_module_info(
31        &self,
32        async_module_info: Option<Vc<AsyncModuleInfo>>,
33    ) -> Result<Vc<EcmascriptChunkItemContent>> {
34        let module = self.module.await?;
35        let chunking_context = self.chunking_context;
36        let original_module = module.module;
37
38        let analyze = original_module.analyze();
39        let analyze_result = analyze.await?;
40        let async_module_options = analyze_result
41            .async_module
42            .module_options(async_module_info);
43
44        let content = self
45            .module
46            .module_content(*chunking_context, async_module_info);
47
48        Ok(EcmascriptChunkItemContent::new(
49            content,
50            *chunking_context,
51            async_module_options,
52        ))
53    }
54}
55
56#[turbo_tasks::value_impl]
57impl ChunkItem for EcmascriptModuleLocalsChunkItem {
58    #[turbo_tasks::function]
59    fn asset_ident(&self) -> Vc<AssetIdent> {
60        self.module.ident()
61    }
62
63    #[turbo_tasks::function]
64    fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
65        *ResolvedVc::upcast(self.chunking_context)
66    }
67
68    #[turbo_tasks::function]
69    async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
70        Ok(Vc::upcast(
71            Vc::<EcmascriptChunkType>::default().resolve().await?,
72        ))
73    }
74
75    #[turbo_tasks::function]
76    fn module(&self) -> Vc<Box<dyn Module>> {
77        *ResolvedVc::upcast(self.module)
78    }
79}