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