Skip to main content

turbopack_core/
raw_module.rs

1use anyhow::Result;
2use turbo_rcstr::RcStr;
3use turbo_tasks::{ResolvedVc, Vc};
4
5use crate::{
6    ident::AssetIdent,
7    module::{Module, ModuleSideEffects},
8    source::{OptionSource, Source},
9};
10
11/// A module where source code doesn't need to be parsed but can be used as is.
12/// This module has no references to other modules.
13#[turbo_tasks::value]
14pub struct RawModule {
15    source: ResolvedVc<Box<dyn Source>>,
16    modifier: Option<RcStr>,
17}
18
19#[turbo_tasks::value_impl]
20impl Module for RawModule {
21    #[turbo_tasks::function]
22    async fn ident(&self) -> Result<Vc<AssetIdent>> {
23        Ok(match &self.modifier {
24            Some(modifier) => self
25                .source
26                .ident()
27                .owned()
28                .await?
29                .with_modifier(modifier.clone())
30                .into_vc(),
31            None => self.source.ident(),
32        })
33    }
34
35    #[turbo_tasks::function]
36    fn source(&self) -> Vc<OptionSource> {
37        Vc::cell(Some(self.source))
38    }
39    #[turbo_tasks::function]
40    fn side_effects(self: Vc<Self>) -> Vc<ModuleSideEffects> {
41        ModuleSideEffects::SideEffectful.cell()
42    }
43}
44
45impl RawModule {
46    pub fn new(source: Vc<Box<dyn Source>>) -> Vc<RawModule> {
47        Self::new_inner(source, None)
48    }
49
50    pub fn new_with_modifier(source: Vc<Box<dyn Source>>, modifier: RcStr) -> Vc<RawModule> {
51        Self::new_inner(source, Some(modifier))
52    }
53}
54
55#[turbo_tasks::value_impl]
56impl RawModule {
57    #[turbo_tasks::function]
58    fn new_inner(source: ResolvedVc<Box<dyn Source>>, modifier: Option<RcStr>) -> Vc<RawModule> {
59        RawModule { source, modifier }.cell()
60    }
61}