rspack_core/module_graph/
module.rs1use std::fmt;
2
3use rspack_cacheable::{cacheable, with::Skip};
4use rustc_hash::FxHashSet;
5
6use crate::{DependencyId, ModuleIdentifier, ModuleIssuer};
7
8#[cacheable]
9#[derive(Debug, Clone)]
10pub enum OptimizationBailoutItem {
11 Message(String),
12 SideEffects {
13 node_type: String,
14 loc: String,
15 short_id: String,
16 },
17}
18
19impl fmt::Display for OptimizationBailoutItem {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 match self {
22 Self::Message(msg) => write!(f, "{msg}"),
23 Self::SideEffects {
24 node_type,
25 loc,
26 short_id,
27 } => {
28 write!(
29 f,
30 "{node_type} with side_effects in source code at {short_id}:{loc}"
31 )
32 }
33 }
34 }
35}
36
37#[cacheable]
38#[derive(Debug, Clone)]
39pub struct ModuleGraphModule {
40 outgoing_connections: FxHashSet<DependencyId>,
42 #[cacheable(with=Skip)]
44 incoming_connections: FxHashSet<DependencyId>,
45
46 issuer: ModuleIssuer,
47
48 pub module_identifier: ModuleIdentifier,
50 all_dependencies: Vec<DependencyId>,
53 pub(crate) pre_order_index: Option<u32>,
54 pub post_order_index: Option<u32>,
55 pub depth: Option<usize>,
56 pub optimization_bailout: Vec<OptimizationBailoutItem>,
57}
58
59impl ModuleGraphModule {
60 pub fn new(module_identifier: ModuleIdentifier) -> Self {
61 Self {
62 outgoing_connections: Default::default(),
63 incoming_connections: Default::default(),
64 issuer: ModuleIssuer::Unset,
65 module_identifier,
67 all_dependencies: Default::default(),
68 pre_order_index: None,
69 post_order_index: None,
70 depth: None,
71 optimization_bailout: vec![],
72 }
73 }
74
75 pub fn add_incoming_connection(&mut self, dependency_id: DependencyId) {
76 self.incoming_connections.insert(dependency_id);
77 }
78
79 pub fn remove_incoming_connection(&mut self, dependency_id: &DependencyId) {
80 self.incoming_connections.remove(dependency_id);
81 }
82
83 pub fn add_outgoing_connection(&mut self, dependency_id: DependencyId) {
84 self.outgoing_connections.insert(dependency_id);
85 }
86
87 pub fn remove_outgoing_connection(&mut self, dependency_id: &DependencyId) {
88 self.outgoing_connections.remove(dependency_id);
89 }
90
91 pub fn incoming_connections(&self) -> &FxHashSet<DependencyId> {
92 &self.incoming_connections
93 }
94
95 pub fn outgoing_connections(&self) -> &FxHashSet<DependencyId> {
96 &self.outgoing_connections
97 }
98
99 pub fn all_dependencies(&self) -> &[DependencyId] {
100 &self.all_dependencies
101 }
102
103 pub(crate) fn all_dependencies_mut(&mut self) -> &mut Vec<DependencyId> {
104 &mut self.all_dependencies
105 }
106
107 pub fn set_issuer_if_unset(&mut self, issuer: Option<ModuleIdentifier>) {
108 if matches!(self.issuer, ModuleIssuer::Unset) {
109 self.issuer = ModuleIssuer::from_identifier(issuer);
110 }
111 }
112
113 pub fn set_issuer(&mut self, issuer: ModuleIssuer) {
114 self.issuer = issuer;
115 }
116
117 pub fn issuer(&self) -> &ModuleIssuer {
118 &self.issuer
119 }
120
121 pub(crate) fn optimization_bailout_mut(&mut self) -> &mut Vec<OptimizationBailoutItem> {
122 &mut self.optimization_bailout
123 }
124}