turbopack_core/
module.rs

1use turbo_rcstr::RcStr;
2use turbo_tasks::{ResolvedVc, TaskInput, ValueToString, Vc};
3
4use crate::{asset::Asset, ident::AssetIdent, reference::ModuleReferences};
5
6#[derive(Clone, Copy, Debug, TaskInput, Hash)]
7#[turbo_tasks::value(shared)]
8pub enum StyleType {
9    IsolatedStyle,
10    GlobalStyle,
11}
12
13/// A module. This usually represents parsed source code, which has references
14/// to other modules.
15#[turbo_tasks::value_trait]
16pub trait Module: Asset {
17    /// The identifier of the [Module]. It's expected to be unique and capture
18    /// all properties of the [Module].
19    #[turbo_tasks::function]
20    fn ident(&self) -> Vc<AssetIdent>;
21
22    /// The identifier of the [Module] as string. It's expected to be unique and capture
23    /// all properties of the [Module].
24    #[turbo_tasks::function]
25    fn ident_string(self: Vc<Self>) -> Vc<RcStr> {
26        self.ident().to_string()
27    }
28
29    /// Other [Module]s or [OutputAsset]s referenced from this [Module].
30    // TODO refactor to avoid returning [OutputAsset]s here
31    #[turbo_tasks::function]
32    fn references(self: Vc<Self>) -> Vc<ModuleReferences> {
33        ModuleReferences::empty()
34    }
35
36    /// Signifies the module itself is async, e.g. it uses top-level await, is a wasm module, etc.
37    #[turbo_tasks::function]
38    fn is_self_async(self: Vc<Self>) -> Vc<bool> {
39        Vc::cell(false)
40    }
41}
42
43#[turbo_tasks::value_trait]
44pub trait StyleModule: Module + Asset {
45    /// The style type of the module.
46    #[turbo_tasks::function]
47    fn style_type(&self) -> Vc<StyleType>;
48}
49
50#[turbo_tasks::value(transparent)]
51pub struct OptionModule(Option<ResolvedVc<Box<dyn Module>>>);
52
53#[turbo_tasks::value(transparent)]
54pub struct Modules(Vec<ResolvedVc<Box<dyn Module>>>);
55
56#[turbo_tasks::value_impl]
57impl Modules {
58    #[turbo_tasks::function]
59    pub fn empty() -> Vc<Self> {
60        Vc::cell(Vec::new())
61    }
62}