turbopack_core/
module.rs

1use turbo_rcstr::RcStr;
2use turbo_tasks::{ResolvedVc, TaskInput, ValueToString, Vc};
3
4use crate::{asset::Asset, ident::AssetIdent, reference::ModuleReferences, source::OptionSource};
5
6#[derive(Clone, Copy, Debug, TaskInput, Hash)]
7#[turbo_tasks::value(shared)]
8pub enum StyleType {
9    IsolatedStyle,
10    GlobalStyle,
11}
12
13#[derive(Hash, Debug, Copy, Clone)]
14#[turbo_tasks::value(shared)]
15pub enum ModuleSideEffects {
16    /// Analysis determined that the module evaluation is side effect free
17    /// the module may still be side effectful based on its imports.
18    ModuleEvaluationIsSideEffectFree,
19    /// Is known to be side effect free either due to static analysis or some kind of configuration.
20    /// ```js
21    /// "use turbopack no side effects"
22    /// ```
23    SideEffectFree,
24    // Neither of the above, so we should assume it has side effects.
25    SideEffectful,
26}
27
28/// A module. This usually represents parsed source code, which has references
29/// to other modules.
30#[turbo_tasks::value_trait]
31pub trait Module: Asset {
32    /// The identifier of the [Module]. It's expected to be unique and capture
33    /// all properties of the [Module].
34    #[turbo_tasks::function]
35    fn ident(&self) -> Vc<AssetIdent>;
36
37    /// The identifier of the [Module] as string. It's expected to be unique and capture
38    /// all properties of the [Module].
39    #[turbo_tasks::function]
40    fn ident_string(self: Vc<Self>) -> Vc<RcStr> {
41        self.ident().to_string()
42    }
43
44    /// The source of the [Module].
45    #[turbo_tasks::function]
46    fn source(&self) -> Vc<OptionSource>;
47
48    /// Other [Module]s or [OutputAsset]s referenced from this [Module].
49    // TODO refactor to avoid returning [OutputAsset]s here
50    #[turbo_tasks::function]
51    fn references(self: Vc<Self>) -> Vc<ModuleReferences> {
52        ModuleReferences::empty()
53    }
54
55    /// Signifies the module itself is async, e.g. it uses top-level await, is a wasm module, etc.
56    #[turbo_tasks::function]
57    fn is_self_async(self: Vc<Self>) -> Vc<bool> {
58        Vc::cell(false)
59    }
60
61    /// Returns true if the module is marked as side effect free in package.json or by other means.
62    #[turbo_tasks::function]
63    fn side_effects(self: Vc<Self>) -> Vc<ModuleSideEffects>;
64}
65
66#[turbo_tasks::value_trait]
67pub trait StyleModule: Module + Asset {
68    /// The style type of the module.
69    #[turbo_tasks::function]
70    fn style_type(&self) -> Vc<StyleType>;
71}
72
73#[turbo_tasks::value(transparent)]
74pub struct OptionModule(Option<ResolvedVc<Box<dyn Module>>>);
75
76#[turbo_tasks::value(transparent)]
77pub struct Modules(Vec<ResolvedVc<Box<dyn Module>>>);
78
79#[turbo_tasks::value_impl]
80impl Modules {
81    #[turbo_tasks::function]
82    pub fn empty() -> Vc<Self> {
83        Vc::cell(Vec::new())
84    }
85}