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