Skip to main content

turbopack_core/
module.rs

1use turbo_rcstr::RcStr;
2use turbo_tasks::{ResolvedVc, TaskInput, ValueToString, Vc};
3
4use crate::{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    ///
19    /// This module might not be chunked after Turbopack performed a global analysis on the module
20    /// graph.
21    ModuleEvaluationIsSideEffectFree,
22    /// Is known to be side effect free either due to static analysis or some kind of configuration.
23    /// ```js
24    /// "use turbopack no side effects"
25    /// ```
26    ///
27    /// This module might not even be parsed (and thus chunked) if no other module depends on any of
28    /// its exports.
29    SideEffectFree,
30    // Neither of the above, so we should assume it has side effects.
31    SideEffectful,
32}
33
34/// A module. This usually represents parsed source code, which has references
35/// to other modules.
36#[turbo_tasks::value_trait]
37pub trait Module {
38    /// The identifier of the [Module]. It's expected to be unique and capture
39    /// all properties of the [Module].
40    #[turbo_tasks::function]
41    fn ident(&self) -> Vc<AssetIdent>;
42
43    /// The identifier of the [Module] as string. It's expected to be unique and capture
44    /// all properties of the [Module].
45    #[turbo_tasks::function]
46    fn ident_string(self: Vc<Self>) -> Vc<RcStr> {
47        self.ident().to_string()
48    }
49
50    /// The source of the [Module].
51    #[turbo_tasks::function]
52    fn source(&self) -> Vc<OptionSource>;
53
54    /// Other [Module]s or [OutputAsset]s referenced from this [Module].
55    // TODO refactor to avoid returning [OutputAsset]s here
56    #[turbo_tasks::function]
57    fn references(self: Vc<Self>) -> Vc<ModuleReferences> {
58        ModuleReferences::empty()
59    }
60
61    /// Signifies the module itself is async, e.g. it uses top-level await, is a wasm module, etc.
62    #[turbo_tasks::function]
63    fn is_self_async(self: Vc<Self>) -> Vc<bool> {
64        Vc::cell(false)
65    }
66
67    /// Returns true if the module is marked as side effect free in package.json or by other means.
68    #[turbo_tasks::function]
69    fn side_effects(self: Vc<Self>) -> Vc<ModuleSideEffects>;
70}
71
72#[turbo_tasks::value_trait]
73pub trait StyleModule: Module {
74    /// The style type of the module.
75    #[turbo_tasks::function]
76    fn style_type(&self) -> Vc<StyleType>;
77}
78
79#[turbo_tasks::value(transparent)]
80pub struct OptionModule(Option<ResolvedVc<Box<dyn Module>>>);
81
82#[turbo_tasks::value(transparent)]
83pub struct Modules(Vec<ResolvedVc<Box<dyn Module>>>);
84
85#[turbo_tasks::value_impl]
86impl Modules {
87    #[turbo_tasks::function]
88    pub fn empty() -> Vc<Self> {
89        Vc::cell(Vec::new())
90    }
91}