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, ExportsInfoArtifact, ExtendedReferencedExport,
16  ForwardId, ImportAttributes, ImportPhase, LazyUntil, ModuleGraph, ModuleGraphCacheArtifact,
17  ModuleLayer, RuntimeSpec, SideEffectsStateArtifact, 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    _exports_info_artifact: &ExportsInfoArtifact,
71  ) -> Option<ExportsSpec> {
72    None
73  }
74
75  fn get_module_evaluation_side_effects_state(
76    &self,
77    _module_graph: &ModuleGraph,
78    _module_graph_cache: &ModuleGraphCacheArtifact,
79    _side_effects_state_artifact: &SideEffectsStateArtifact,
80    _module_chain: &mut IdentifierSet,
81    _connection_state_cache: &mut IdentifierMap<ConnectionState>,
82  ) -> ConnectionState {
83    ConnectionState::Active(true)
84  }
85
86  fn loc(&self) -> Option<DependencyLocation> {
87    None
88  }
89
90  fn range(&self) -> Option<DependencyRange> {
91    None
92  }
93
94  fn source_order(&self) -> Option<i32> {
95    None
96  }
97
98  fn resource_identifier(&self) -> Option<&str> {
99    None
100  }
101
102  fn get_diagnostics(
103    &self,
104    _module_graph: &ModuleGraph,
105    _module_graph_cache: &ModuleGraphCacheArtifact,
106    _exports_info_artifact: &ExportsInfoArtifact,
107  ) -> Option<Vec<Diagnostic>> {
108    None
109  }
110
111  fn get_referenced_exports(
112    &self,
113    _module_graph: &ModuleGraph,
114    _module_graph_cache: &ModuleGraphCacheArtifact,
115    _exports_info_artifact: &ExportsInfoArtifact,
116    _runtime: Option<&RuntimeSpec>,
117  ) -> Vec<ExtendedReferencedExport> {
118    create_exports_object_referenced()
119  }
120
121  fn could_affect_referencing_module(&self) -> AffectType;
122
123  fn forward_id(&self) -> ForwardId {
124    ForwardId::All
125  }
126
127  fn lazy(&self) -> Option<LazyUntil> {
128    None
129  }
130
131  fn set_lazy(&mut self) {}
132
133  fn unset_lazy(&mut self) -> bool {
134    false
135  }
136}
137
138impl dyn Dependency + '_ {
139  pub fn downcast_ref<D: Any>(&self) -> Option<&D> {
140    self.as_any().downcast_ref::<D>()
141  }
142
143  pub fn downcast_mut<D: Any>(&mut self) -> Option<&mut D> {
144    self.as_any_mut().downcast_mut::<D>()
145  }
146
147  pub fn is<D: Any>(&self) -> bool {
148    self.downcast_ref::<D>().is_some()
149  }
150}
151
152clone_trait_object!(Dependency);
153
154pub type BoxDependency = Box<dyn Dependency>;