rolldown_common/module/
mod.rs

1pub mod external_module;
2pub mod normal_module;
3
4use arcstr::ArcStr;
5use oxc_index::IndexVec;
6use rolldown_std_utils::OptionExt;
7
8use crate::{ExternalModule, ImportRecordIdx, ModuleIdx, NormalModule, ResolvedImportRecord};
9
10#[derive(Debug, Clone)]
11pub enum Module {
12  Normal(Box<NormalModule>),
13  External(Box<ExternalModule>),
14}
15
16impl Module {
17  pub fn idx(&self) -> ModuleIdx {
18    match self {
19      Module::Normal(v) => v.idx,
20      Module::External(v) => v.idx,
21    }
22  }
23
24  #[inline]
25  pub fn exec_order(&self) -> u32 {
26    match self {
27      Module::Normal(v) => v.exec_order,
28      Module::External(v) => v.exec_order,
29    }
30  }
31
32  pub fn id(&self) -> &str {
33    match self {
34      Module::Normal(v) => &v.id,
35      Module::External(v) => &v.id,
36    }
37  }
38
39  pub fn id_clone(&self) -> &ArcStr {
40    match self {
41      Module::Normal(v) => v.id.resource_id(),
42      Module::External(v) => &v.id,
43    }
44  }
45
46  pub fn side_effects(&self) -> &crate::side_effects::DeterminedSideEffects {
47    match self {
48      Module::Normal(v) => &v.side_effects,
49      Module::External(v) => &v.side_effects,
50    }
51  }
52
53  pub fn stable_id(&self) -> &str {
54    match self {
55      Module::Normal(v) => &v.stable_id,
56      Module::External(v) => &v.name,
57    }
58  }
59
60  pub fn repr_name(&self) -> &str {
61    match self {
62      Module::Normal(v) => v.repr_name.as_str(),
63      Module::External(v) => v.identifier_name.as_str(),
64    }
65  }
66
67  pub fn normal(v: NormalModule) -> Self {
68    Module::Normal(Box::new(v))
69  }
70
71  pub fn external(v: ExternalModule) -> Self {
72    Module::External(Box::new(v))
73  }
74
75  pub fn as_normal(&self) -> Option<&NormalModule> {
76    match self {
77      Module::Normal(v) => Some(v),
78      Module::External(_) => None,
79    }
80  }
81
82  pub fn as_external(&self) -> Option<&ExternalModule> {
83    match self {
84      Module::External(v) => Some(v),
85      Module::Normal(_) => None,
86    }
87  }
88
89  pub fn as_normal_mut(&mut self) -> Option<&mut NormalModule> {
90    match self {
91      Module::Normal(v) => Some(v),
92      Module::External(_) => None,
93    }
94  }
95
96  pub fn as_external_mut(&mut self) -> Option<&mut ExternalModule> {
97    match self {
98      Module::External(v) => Some(v),
99      Module::Normal(_) => None,
100    }
101  }
102
103  pub fn import_records(&self) -> &IndexVec<ImportRecordIdx, ResolvedImportRecord> {
104    match self {
105      Module::Normal(v) => match v.module_type {
106        crate::ModuleType::Css => &v.css_view.unpack_ref().import_records,
107        _ => &v.ecma_view.import_records,
108      },
109      Module::External(v) => &v.import_records,
110    }
111  }
112
113  pub fn set_import_records(&mut self, records: IndexVec<ImportRecordIdx, ResolvedImportRecord>) {
114    match self {
115      Module::Normal(v) => match v.module_type {
116        crate::ModuleType::Css => v.css_view.unpack_ref_mut().import_records = records,
117        _ => v.ecma_view.import_records = records,
118      },
119      Module::External(v) => v.import_records = records,
120    }
121  }
122
123  /// Returns `true` if the module is [`Ecma`].
124  ///
125  /// [`Ecma`]: Module::Ecma
126  #[must_use]
127  pub fn is_normal(&self) -> bool {
128    matches!(self, Self::Normal(..))
129  }
130
131  pub fn is_external(&self) -> bool {
132    matches!(self, Self::External(..))
133  }
134
135  pub fn size(&self) -> usize {
136    match self {
137      Module::Normal(v) => v.source.len(),
138      Module::External(_) => 0,
139    }
140  }
141}
142
143impl From<NormalModule> for Module {
144  fn from(module: NormalModule) -> Self {
145    Module::Normal(Box::new(module))
146  }
147}
148
149impl From<ExternalModule> for Module {
150  fn from(module: ExternalModule) -> Self {
151    Module::External(Box::new(module))
152  }
153}