Skip to main content

next_core/next_dynamic/
dynamic_module.rs

1use anyhow::Result;
2use indoc::formatdoc;
3use turbo_rcstr::rcstr;
4use turbo_tasks::{ResolvedVc, Vc};
5use turbopack_core::{
6    chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt},
7    ident::AssetIdent,
8    module::{Module, ModuleSideEffects},
9    module_graph::ModuleGraph,
10    reference::{ModuleReference, ModuleReferences, SingleChunkableModuleReference},
11    resolve::ExportUsage,
12    source::OptionSource,
13};
14use turbopack_ecmascript::{
15    chunk::{
16        EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
17        ecmascript_chunk_item,
18    },
19    references::esm::EsmExports,
20    runtime_functions::{TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_IMPORT},
21    utils::StringifyJs,
22};
23
24/// A [`NextDynamicEntryModule`] is a marker asset used to indicate which
25/// dynamic assets should appear in the dynamic manifest.
26#[turbo_tasks::value(shared)]
27pub struct NextDynamicEntryModule {
28    module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
29}
30
31#[turbo_tasks::value_impl]
32impl NextDynamicEntryModule {
33    #[turbo_tasks::function]
34    pub fn new(module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>) -> Vc<Self> {
35        NextDynamicEntryModule { module }.cell()
36    }
37}
38
39impl NextDynamicEntryModule {
40    fn module_reference(&self) -> Vc<Box<dyn ModuleReference>> {
41        Vc::upcast(SingleChunkableModuleReference::new(
42            Vc::upcast(*self.module),
43            rcstr!("next/dynamic reference"),
44            ExportUsage::all(),
45        ))
46    }
47}
48
49#[turbo_tasks::value_impl]
50impl Module for NextDynamicEntryModule {
51    #[turbo_tasks::function]
52    fn ident(&self) -> Vc<AssetIdent> {
53        self.module
54            .ident()
55            .with_modifier(rcstr!("next/dynamic entry"))
56    }
57
58    #[turbo_tasks::function]
59    fn source(&self) -> Vc<OptionSource> {
60        Vc::cell(None)
61    }
62
63    #[turbo_tasks::function]
64    async fn references(&self) -> Result<Vc<ModuleReferences>> {
65        Ok(Vc::cell(vec![self.module_reference().to_resolved().await?]))
66    }
67
68    #[turbo_tasks::function]
69    fn side_effects(self: Vc<Self>) -> Vc<ModuleSideEffects> {
70        // This just exports another import
71        ModuleSideEffects::ModuleEvaluationIsSideEffectFree.cell()
72    }
73}
74
75#[turbo_tasks::value_impl]
76impl ChunkableModule for NextDynamicEntryModule {
77    #[turbo_tasks::function]
78    fn as_chunk_item(
79        self: ResolvedVc<Self>,
80        module_graph: ResolvedVc<ModuleGraph>,
81        chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
82    ) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
83        ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
84    }
85}
86
87#[turbo_tasks::value_impl]
88impl EcmascriptChunkPlaceable for NextDynamicEntryModule {
89    #[turbo_tasks::function]
90    fn get_exports(&self) -> Vc<EcmascriptExports> {
91        let module_reference = self.module_reference();
92        EsmExports::reexport_including_default(module_reference)
93    }
94
95    #[turbo_tasks::function]
96    async fn chunk_item_content(
97        &self,
98        chunking_context: Vc<Box<dyn ChunkingContext>>,
99        _module_graph: Vc<ModuleGraph>,
100        _async_module_info: Option<Vc<AsyncModuleInfo>>,
101        _estimated: bool,
102    ) -> Result<Vc<EcmascriptChunkItemContent>> {
103        let module_id = self.module.chunk_item_id(chunking_context).await?;
104        Ok(EcmascriptChunkItemContent {
105            inner_code: formatdoc!(
106                r#"
107                    {TURBOPACK_EXPORT_NAMESPACE}({TURBOPACK_IMPORT}({}));
108                "#,
109                StringifyJs(&module_id),
110            )
111            .into(),
112            ..Default::default()
113        }
114        .cell())
115    }
116}