Skip to main content

fallow_types/
extract_load_kind.rs

1//! When the target of an import edge loads, relative to the importing module.
2
3/// When the target of an import edge loads, relative to the importing module.
4///
5/// The startup weight report follows only `Static` edges to find the code that
6/// loads before the entry module runs. The other kinds load later, or outside
7/// the importing thread, except `PathReference` and `AssetReference`. These
8/// two edges keep their target in use but do not run it: see
9/// [`Self::loads_target`]. Only an asset reference also stops reachability:
10/// see [`Self::runs_target_code`]. A `BuildTime` edge runs its target only in
11/// the bundler: see [`Self::runs_at_runtime`].
12#[derive(
13    Debug,
14    Clone,
15    Copy,
16    Default,
17    PartialEq,
18    Eq,
19    PartialOrd,
20    Ord,
21    Hash,
22    serde::Serialize,
23    serde::Deserialize,
24    bitcode::Encode,
25    bitcode::Decode,
26)]
27#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
28#[serde(rename_all = "snake_case")]
29#[repr(u8)]
30pub enum ImportLoadKind {
31    /// The target loads before the importer runs: `import`, `export ... from`,
32    /// `require()`, `require.context` and `import.meta.glob(..., { eager: true })`.
33    #[default]
34    Static = 0,
35    /// The target loads on demand through `import('./literal')`.
36    Dynamic = 1,
37    /// The target is one match of an on-demand pattern: a template `import()`
38    /// or a lazy `import.meta.glob`.
39    DynamicPattern = 2,
40    /// The target runs on another thread or in another process: a
41    /// `new URL(..., import.meta.url)` reference (for example a worker URL),
42    /// a webpack worker loader request (`worker-loader!./work.js`),
43    /// `child_process.fork`, a pino transport or a `module.register` hook.
44    OutOfThread = 3,
45    /// The importer gets only the path of the target and does not load it:
46    /// `require.resolve('./file')` or ``require.resolve(`./file`)``. The edge
47    /// keeps the target in use. The consumer of the path can run the target
48    /// later, so the imports of the target stay reachable.
49    PathReference = 4,
50    /// The importer reads the target as text, bytes or a URL through a
51    /// webpack asset loader, such as `raw-loader!./file.js`. The bundle never
52    /// runs the target as code, so the imports of the target are not
53    /// reachable through this edge. The graph sets this kind; extraction
54    /// never records it.
55    AssetReference = 5,
56    /// The bundler runs the target at build time and inlines the result:
57    /// `import { x } from './x.ts' with { type: 'macro' }`. The target and its
58    /// imports stay in use, but they do not run in the shipped program.
59    BuildTime = 6,
60}
61
62impl ImportLoadKind {
63    /// Whether the target loads before the importing module runs.
64    #[must_use]
65    pub const fn is_eager(self) -> bool {
66        matches!(self, Self::Static)
67    }
68
69    /// Whether the target loads on demand on the importing thread.
70    #[must_use]
71    pub const fn is_deferred(self) -> bool {
72        matches!(self, Self::Dynamic | Self::DynamicPattern)
73    }
74
75    /// Whether the edge runs its target. A path reference and an asset
76    /// reference keep the target in use but do not run it, so the edge does
77    /// not close a cycle, is not in the entry load closure, does not cross an
78    /// architecture boundary and does not carry server code into a client
79    /// bundle.
80    #[must_use]
81    pub const fn loads_target(self) -> bool {
82        !matches!(self, Self::PathReference | Self::AssetReference)
83    }
84
85    /// Whether the target can run as code at all, through this edge or
86    /// through the consumer of a path reference. Only an asset reference never
87    /// runs its target, so reachability does not continue through it.
88    #[must_use]
89    pub const fn runs_target_code(self) -> bool {
90        !matches!(self, Self::AssetReference)
91    }
92
93    /// Whether the target can run as code in the shipped program. A build-time
94    /// import runs its target only in the bundler, so production reachability
95    /// does not continue through it.
96    #[must_use]
97    pub const fn runs_at_runtime(self) -> bool {
98        self.runs_target_code() && !matches!(self, Self::BuildTime)
99    }
100
101    /// Whether an import with this load kind puts a runtime value of the
102    /// target on the startup path: the target loads before the importer runs
103    /// and the import is not type-only.
104    #[must_use]
105    pub const fn is_eager_value(self, is_type_only: bool) -> bool {
106        self.is_eager() && !is_type_only
107    }
108}