Skip to main content

rspack_core/dependency/
dependency_trait.rs

1use std::{any::Any, fmt::Debug};
2
3use dyn_clone::{DynClone, clone_trait_object};
4use rspack_cacheable::cacheable_dyn;
5use rspack_collections::{IdentifierMap, IdentifierSet};
6use rspack_error::Diagnostic;
7use rspack_location::DependencyLocation;
8use rspack_util::ext::AsAny;
9
10use super::{
11  DependencyCategory, DependencyId, DependencyRange, DependencyType, ExportsSpec,
12  dependency_template::AsDependencyCodeGeneration, module_dependency::*,
13};
14use crate::{
15  AsContextDependency, ConnectionState, Context, ExtendedReferencedExport, ForwardId,
16  ImportAttributes, ImportPhase, LazyUntil, ModuleGraph, ModuleGraphCacheArtifact, ModuleLayer,
17  RuntimeSpec, create_exports_object_referenced,
18};
19
20#[derive(Debug, Clone, Copy)]
21pub enum AffectType {
22  True,
23  False,
24  Transitive,
25}
26
27#[cacheable_dyn]
28pub trait Dependency:
29  AsDependencyCodeGeneration
30  + AsContextDependency
31  + AsModuleDependency
32  + AsAny
33  + DynClone
34  + Send
35  + Sync
36  + Debug
37{
38  fn id(&self) -> &DependencyId;
39
40  fn category(&self) -> &DependencyCategory {
41    &DependencyCategory::Unknown
42  }
43
44  fn dependency_type(&self) -> &DependencyType {
45    &DependencyType::Unknown
46  }
47
48  // get issuer context
49  fn get_context(&self) -> Option<&Context> {
50    None
51  }
52
53  // get issuer layer
54  fn get_layer(&self) -> Option<&ModuleLayer> {
55    None
56  }
57
58  fn get_phase(&self) -> ImportPhase {
59    ImportPhase::Evaluation
60  }
61
62  fn get_attributes(&self) -> Option<&ImportAttributes> {
63    None
64  }
65
66  fn get_exports(
67    &self,
68    _mg: &ModuleGraph,
69    _module_graph_cache: &ModuleGraphCacheArtifact,
70  ) -> Option<ExportsSpec> {
71    None
72  }
73
74  fn get_module_evaluation_side_effects_state(
75    &self,
76    _module_graph: &ModuleGraph,
77    _module_graph_cache: &ModuleGraphCacheArtifact,
78    _module_chain: &mut IdentifierSet,
79    _connection_state_cache: &mut IdentifierMap<ConnectionState>,
80  ) -> ConnectionState {
81    ConnectionState::Active(true)
82  }
83
84  fn loc(&self) -> Option<DependencyLocation> {
85    None
86  }
87
88  fn range(&self) -> Option<DependencyRange> {
89    None
90  }
91
92  fn source_order(&self) -> Option<i32> {
93    None
94  }
95
96  fn resource_identifier(&self) -> Option<&str> {
97    None
98  }
99
100  fn get_diagnostics(
101    &self,
102    _module_graph: &ModuleGraph,
103    _module_graph_cache: &ModuleGraphCacheArtifact,
104  ) -> Option<Vec<Diagnostic>> {
105    None
106  }
107
108  fn get_referenced_exports(
109    &self,
110    _module_graph: &ModuleGraph,
111    _module_graph_cache: &ModuleGraphCacheArtifact,
112    _runtime: Option<&RuntimeSpec>,
113  ) -> Vec<ExtendedReferencedExport> {
114    create_exports_object_referenced()
115  }
116
117  fn could_affect_referencing_module(&self) -> AffectType;
118
119  fn forward_id(&self) -> ForwardId {
120    ForwardId::All
121  }
122
123  fn lazy(&self) -> Option<LazyUntil> {
124    None
125  }
126
127  fn unset_lazy(&mut self) -> bool {
128    false
129  }
130}
131
132impl dyn Dependency + '_ {
133  pub fn downcast_ref<D: Any>(&self) -> Option<&D> {
134    self.as_any().downcast_ref::<D>()
135  }
136
137  pub fn downcast_mut<D: Any>(&mut self) -> Option<&mut D> {
138    self.as_any_mut().downcast_mut::<D>()
139  }
140
141  pub fn is<D: Any>(&self) -> bool {
142    self.downcast_ref::<D>().is_some()
143  }
144}
145
146clone_trait_object!(Dependency);
147
148pub type BoxDependency = Box<dyn Dependency>;