rspack_core 0.100.0-rc.2

rspack core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
pub mod internal;
pub mod rollback;

use std::hash::BuildHasherDefault;

use internal::try_get_module_graph_module_mut_by_identifier;
use rayon::prelude::*;
use rspack_collections::{IdentifierHasher, IdentifierMap};
use rspack_error::Result;
use rspack_hash::RspackHashDigest;
use rustc_hash::{FxHashMap as HashMap, FxHasher};
use swc_core::ecma::atoms::Atom;

use crate::{
  AsyncDependenciesBlock, AsyncDependenciesBlockIdentifier, AsyncDependenciesBlockIdentifierMap,
  AsyncModulesArtifact, Compilation, DependenciesBlock, Dependency, ExportInfo, ExportName,
  ImportedByDeferModulesArtifact, ModuleGraphCacheArtifact, RuntimeSpec, SideEffectsStateArtifact,
  UsedNameItem,
};
mod module;
pub use module::*;
mod connection;
pub use connection::*;

use crate::{
  BoxDependency, BoxModule, DependencyCondition, DependencyId, ExportsInfoArtifact,
  ModuleIdentifier,
};

// TODO Here request can be used Atom
pub type ImportVarMap = HashMap<(Option<ModuleIdentifier>, bool), String /* import_var */>;

pub type BuildDependency = (
  DependencyId,
  Option<ModuleIdentifier>, /* parent module */
);

/// https://github.com/webpack/webpack/blob/ac7e531436b0d47cd88451f497cdfd0dad41535d/lib/ModuleGraph.js#L742-L748
#[derive(Debug, Clone)]
pub struct DependencyExtraMeta {
  pub ids: Vec<Atom>,
  pub explanation: Option<&'static str>,
}

#[derive(Debug, Default, Clone)]
pub struct DependencyParents {
  pub block: Option<AsyncDependenciesBlockIdentifier>,
  pub module: ModuleIdentifier,
  pub index_in_block: usize,
}

#[derive(Debug, Default)]
pub struct IncomingConnectionsByOriginModule<'a> {
  non_modules: Vec<&'a ModuleGraphConnection>,
  modules: IdentifierMap<Vec<&'a ModuleGraphConnection>>,
}

impl<'a> IncomingConnectionsByOriginModule<'a> {
  fn with_capacity(capacity: usize) -> Self {
    Self {
      non_modules: Vec::new(),
      modules: IdentifierMap::with_capacity_and_hasher(capacity, Default::default()),
    }
  }

  fn push(
    &mut self,
    origin_module: Option<ModuleIdentifier>,
    connection: &'a ModuleGraphConnection,
  ) {
    if let Some(origin_module) = origin_module {
      self
        .modules
        .entry(origin_module)
        .or_default()
        .push(connection);
    } else {
      self.non_modules.push(connection);
    }
  }

  pub fn non_modules(&self) -> &[&'a ModuleGraphConnection] {
    &self.non_modules
  }

  pub fn modules(&self) -> &IdentifierMap<Vec<&'a ModuleGraphConnection>> {
    &self.modules
  }

  pub fn into_parts(
    self,
  ) -> (
    Vec<&'a ModuleGraphConnection>,
    IdentifierMap<Vec<&'a ModuleGraphConnection>>,
  ) {
    (self.non_modules, self.modules)
  }
}

/// Internal data structure for ModuleGraph
/// There're 3 kinds of data in module_graph here
/// 1. Data only setting during Make Phase which no need for clone or overlay to recover
/// 2. Data modified during Seal Phase which no need for clone or overlay to recover
/// 3. Data modified both during Seal Phase and Make Phase which need for clone or overlay to recover
///    3.1 only the contained modified in seal phase which can be reverted
///    3.2 the item is modified which can only use overlay or clone to recover
#[derive(Debug, Default)]
pub(crate) struct ModuleGraphData {
  /****** only modified during Make Phase */
  /// Module indexed by `ModuleIdentifier`.
  pub(crate) modules:
    rollback::RollbackMap<ModuleIdentifier, BoxModule, BuildHasherDefault<IdentifierHasher>>,

  /// Dependencies indexed by `DependencyId`.
  dependencies: HashMap<DependencyId, BoxDependency>,
  /// AsyncDependenciesBlocks indexed by `AsyncDependenciesBlockIdentifier`.
  blocks: AsyncDependenciesBlockIdentifierMap<Box<AsyncDependenciesBlock>>,

  /// Dependency_id to parent module identifier and parent block
  ///
  /// # Example
  ///
  /// ```ignore
  /// let parent_module_id = parent_module.identifier();
  /// parent_module
  ///   .get_dependencies()
  ///   .iter()
  ///   .map(|dependency_id| {
  ///     let parents_info = module_graph_partial
  ///       .dependency_id_to_parents
  ///       .get(dependency_id)
  ///       .unwrap();
  ///     assert_eq!(parents_info.module, parent_module_id);
  ///   })
  /// ```
  dependency_id_to_parents: HashMap<DependencyId, DependencyParents>,
  // TODO try move condition as connection field
  connection_to_condition: HashMap<DependencyId, DependencyCondition>,

  /************************** Modified by Seal Phase **********************/
  /// ModuleGraphModule indexed by `ModuleIdentifier`.
  /// modified here https://github.com/web-infra-dev/rspack/blob/9ae2f0f3be22370197cd9ed3308982f84f2bb738/crates/rspack_core/src/compilation/build_chunk_graph/code_splitter.rs#L1216
  module_graph_modules:
    rollback::OverlayMap<ModuleIdentifier, ModuleGraphModule, BuildHasherDefault<IdentifierHasher>>,

  /// ModuleGraphConnection indexed by `DependencyId`.
  /// modified here https://github.com/web-infra-dev/rspack/blob/9ae2f0f3be22370197cd9ed3308982f84f2bb738/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs#L820
  connections:
    rollback::OverlayMap<DependencyId, ModuleGraphConnection, BuildHasherDefault<FxHasher>>,

  /***************** only Modified during Seal Phase ********************/
  // setting here https://github.com/web-infra-dev/rspack/blob/9ae2f0f3be22370197cd9ed3308982f84f2bb738/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs#L318
  dep_meta_map: HashMap<DependencyId, DependencyExtraMeta>,
}
impl ModuleGraphData {
  fn checkpoint(&mut self) {
    self.modules.checkpoint();
    self.module_graph_modules.checkpoint();
    self.connections.checkpoint();
    // dep_meta_map is not used for build_module_graph
  }
  // reset to checkpoint
  fn recover(&mut self) {
    self.modules.reset();
    self.module_graph_modules.reset();
    self.connections.reset();
    // reset data to save memory
    self.dep_meta_map.clear();
  }
}

#[derive(Debug, Default)]
pub struct ModuleGraph {
  pub(super) inner: ModuleGraphData,
}
impl ModuleGraph {
  // checkpoint
  pub fn checkpoint(&mut self) {
    self.inner.checkpoint()
  }
  // reset to last checkpoint
  pub fn reset(&mut self) {
    self.inner.recover()
  }
}

impl ModuleGraph {
  #[inline]
  pub fn modules_len(&self) -> usize {
    self.inner.modules.len()
  }

  #[inline]
  pub fn modules(&self) -> impl Iterator<Item = (&ModuleIdentifier, &BoxModule)> {
    self.inner.modules.iter()
  }

  #[inline]
  pub fn modules_par(
    &self,
  ) -> impl rayon::prelude::ParallelIterator<Item = (&ModuleIdentifier, &BoxModule)> {
    self.inner.modules.par_iter()
  }

  #[inline]
  pub fn modules_keys(&self) -> impl Iterator<Item = &ModuleIdentifier> {
    self.inner.modules.iter().map(|(k, _)| k)
  }

  #[inline]
  pub fn module_graph_modules(
    &self,
  ) -> impl Iterator<Item = (&ModuleIdentifier, &ModuleGraphModule)> {
    self.inner.module_graph_modules.iter()
  }

  // #[tracing::instrument(skip_all, fields(module = ?module_id))]
  pub fn get_outcoming_connections_by_module(
    &self,
    module_id: &ModuleIdentifier,
  ) -> IdentifierMap<Vec<&ModuleGraphConnection>> {
    let connections = self
      .module_graph_module_by_identifier(module_id)
      .expect("should have mgm")
      .outgoing_connections();

    let mut map: IdentifierMap<Vec<&ModuleGraphConnection>> =
      IdentifierMap::with_capacity_and_hasher(connections.len(), Default::default());
    for dep_id in connections {
      let con = self
        .connection_by_dependency_id(dep_id)
        .expect("should have connection");
      map.entry(*con.module_identifier()).or_default().push(con);
    }
    map
  }

  pub fn get_active_outcoming_connections_by_module(
    &self,
    module_id: &ModuleIdentifier,
    runtime: Option<&RuntimeSpec>,
    module_graph: &ModuleGraph,
    module_graph_cache: &ModuleGraphCacheArtifact,
    side_effects_state_artifact: &SideEffectsStateArtifact,
    exports_info_artifact: &ExportsInfoArtifact,
  ) -> IdentifierMap<Vec<&ModuleGraphConnection>> {
    let connections = self
      .module_graph_module_by_identifier(module_id)
      .expect("should have mgm")
      .outgoing_connections();

    let mut map: IdentifierMap<Vec<&ModuleGraphConnection>> =
      IdentifierMap::with_capacity_and_hasher(connections.len(), Default::default());
    for dep_id in connections {
      let con = self
        .connection_by_dependency_id(dep_id)
        .expect("should have connection");
      if !con.is_active(
        module_graph,
        runtime,
        module_graph_cache,
        side_effects_state_artifact,
        exports_info_artifact,
      ) {
        continue;
      }
      map.entry(*con.module_identifier()).or_default().push(con);
    }
    map
  }

  // #[tracing::instrument(skip_all, fields(module = ?module_id))]
  pub fn get_incoming_connections_by_origin_module(
    &self,
    module_id: &ModuleIdentifier,
  ) -> IncomingConnectionsByOriginModule<'_> {
    let connections = self
      .module_graph_module_by_identifier(module_id)
      .expect("should have mgm")
      .incoming_connections();

    let mut map = IncomingConnectionsByOriginModule::with_capacity(connections.len());
    for dep_id in connections {
      let con = self
        .connection_by_dependency_id(dep_id)
        .expect("should have connection");
      map.push(con.original_module_identifier, con);
    }
    map
  }

  /// Remove dependency in mgm and target connection, return dependency_id and origin module identifier
  ///
  /// force will completely remove dependency, and you will not regenerate it from dependency_id
  pub fn revoke_dependency(
    &mut self,
    dep_id: &DependencyId,
    force: bool,
  ) -> Option<BuildDependency> {
    let original_module_identifier = self.get_parent_module(dep_id).copied();
    let module_identifier = self.module_identifier_by_dependency_id(dep_id).copied();
    let parent_block = self.get_parent_block(dep_id).copied();

    if module_identifier.is_some() {
      self.inner.connections.remove(dep_id);
    }
    if force {
      self.inner.dependencies.remove(dep_id);
      self.inner.dependency_id_to_parents.remove(dep_id);
      self.inner.connection_to_condition.remove(dep_id);
      if let Some(m_id) = original_module_identifier
        && let Some(module) = self.inner.modules.get_mut(&m_id)
      {
        module.remove_dependency_id(*dep_id);
      }
      if let Some(b_id) = parent_block
        && let Some(block) = self.inner.blocks.get_mut(&b_id)
      {
        block.remove_dependency_id(*dep_id);
      }
    }

    // remove outgoing from original module graph module
    if let Some(original_module_identifier) = &original_module_identifier
      && let Some(mgm) =
        try_get_module_graph_module_mut_by_identifier(self, original_module_identifier)
    {
      mgm.remove_outgoing_connection(dep_id);
      if force {
        mgm.all_dependencies.retain(|id| id != dep_id);
      }
    }
    // remove incoming from module graph module
    if let Some(module_identifier) = &module_identifier
      && let Some(mgm) = try_get_module_graph_module_mut_by_identifier(self, module_identifier)
    {
      mgm.remove_incoming_connection(dep_id);
    }

    Some((*dep_id, original_module_identifier))
  }

  pub fn revoke_module(&mut self, module_id: &ModuleIdentifier) -> Vec<BuildDependency> {
    let blocks = self
      .module_by_identifier(module_id)
      .map(|m| Vec::from(m.get_blocks()))
      .unwrap_or_default();

    let (incoming_connections, all_dependencies) = self
      .module_graph_module_by_identifier(module_id)
      .map(|mgm| {
        (
          mgm.incoming_connections().clone(),
          mgm.all_dependencies.clone(),
        )
      })
      .unwrap_or_default();

    self.inner.modules.remove(module_id);
    self.inner.module_graph_modules.remove(module_id);

    for block in blocks {
      self.inner.blocks.remove(&block);
    }

    for dep_id in all_dependencies {
      self.revoke_dependency(&dep_id, true);
    }

    incoming_connections
      .iter()
      .filter_map(|dep_id| self.revoke_dependency(dep_id, false))
      .collect()
  }

  pub fn add_module_graph_module(&mut self, module_graph_module: ModuleGraphModule) {
    self
      .inner
      .module_graph_modules
      .insert(module_graph_module.module_identifier, module_graph_module);
  }

  /// Make sure both source and target module are exists in module graph
  pub fn clone_module_attributes(
    compilation: &mut Compilation,
    source_module: &ModuleIdentifier,
    target_module: &ModuleIdentifier,
  ) {
    let module_graph = compilation.get_module_graph_mut();
    let old_mgm = module_graph
      .module_graph_module_by_identifier(source_module)
      .expect("should have mgm");

    // Using this tuple to avoid violating rustc borrow rules
    let assign_tuple = (
      old_mgm.post_order_index,
      old_mgm.pre_order_index,
      old_mgm.depth,
    );
    let new_mgm = module_graph.module_graph_module_by_identifier_mut(target_module);
    new_mgm.post_order_index = assign_tuple.0;
    new_mgm.pre_order_index = assign_tuple.1;
    new_mgm.depth = assign_tuple.2;

    let exports_info = compilation
      .exports_info_artifact
      .get_exports_info(source_module);
    compilation
      .exports_info_artifact
      .set_exports_info(*target_module, exports_info);

    let is_async = ModuleGraph::is_async(&compilation.async_modules_artifact, source_module);
    ModuleGraph::set_async(
      &mut compilation.async_modules_artifact,
      *target_module,
      is_async,
    );
  }

  pub fn move_module_connections(
    &mut self,
    old_module: &ModuleIdentifier,
    new_module: &ModuleIdentifier,
    filter_connection: impl Fn(&ModuleGraphConnection, &Box<dyn Dependency>) -> bool,
  ) {
    if old_module == new_module {
      return;
    }

    // Outgoing connections
    let outgoing_connections = self
      .module_graph_module_by_identifier(old_module)
      .expect("should have mgm")
      .outgoing_connections()
      .clone();
    let mut affected_outgoing_connection = vec![];
    for dep_id in outgoing_connections {
      let connection = self
        .connection_by_dependency_id(&dep_id)
        .expect("should have connection");
      let dependency = self.dependency_by_id(&dep_id);
      if filter_connection(connection, dependency) {
        let connection = self
          .connection_by_dependency_id_mut(&dep_id)
          .expect("should have connection");
        connection.original_module_identifier = Some(*new_module);
        affected_outgoing_connection.push(dep_id);
      }
    }

    let old_mgm = self.module_graph_module_by_identifier_mut(old_module);
    for dep_id in &affected_outgoing_connection {
      old_mgm.remove_outgoing_connection(dep_id);
    }

    let new_mgm = self.module_graph_module_by_identifier_mut(new_module);
    for dep_id in affected_outgoing_connection {
      new_mgm.add_outgoing_connection(dep_id);
    }

    // Incoming connections
    let incoming_connections = self
      .module_graph_module_by_identifier(old_module)
      .expect("should have mgm")
      .incoming_connections()
      .clone();
    let mut affected_incoming_connection = vec![];
    for dep_id in incoming_connections {
      let connection = self
        .connection_by_dependency_id(&dep_id)
        .expect("should have connection");
      let dependency = self.dependency_by_id(&dep_id);
      if filter_connection(connection, dependency) {
        let connection = self
          .connection_by_dependency_id_mut(&dep_id)
          .expect("should have connection");
        connection.set_module_identifier(*new_module);
        affected_incoming_connection.push(dep_id);
      }
    }

    let old_mgm = self.module_graph_module_by_identifier_mut(old_module);
    for dep_id in &affected_incoming_connection {
      old_mgm.remove_incoming_connection(dep_id);
    }

    let new_mgm = self.module_graph_module_by_identifier_mut(new_module);
    for dep_id in affected_incoming_connection {
      new_mgm.add_incoming_connection(dep_id);
    }
  }

  pub fn copy_outgoing_module_connections<F>(
    &mut self,
    old_module: &ModuleIdentifier,
    new_module: &ModuleIdentifier,
    filter_connection: F,
  ) where
    F: Fn(&ModuleGraphConnection, &BoxDependency) -> bool,
  {
    if old_module == new_module {
      return;
    }

    let old_mgm_connections = self
      .module_graph_module_by_identifier(old_module)
      .expect("should have mgm")
      .outgoing_connections()
      .clone();

    // Outgoing connections
    let mut affected_outgoing_connections = vec![];
    for dep_id in old_mgm_connections {
      let connection = self
        .connection_by_dependency_id(&dep_id)
        .expect("should have connection");
      let dep = self.dependency_by_id(&dep_id);
      if filter_connection(connection, dep) {
        let con = self
          .connection_by_dependency_id_mut(&dep_id)
          .expect("should have connection");
        con.original_module_identifier = Some(*new_module);
        affected_outgoing_connections.push(dep_id);
      }
    }

    let new_mgm = self.module_graph_module_by_identifier_mut(new_module);
    for dep_id in affected_outgoing_connections {
      new_mgm.add_outgoing_connection(dep_id);
    }
  }

  pub fn get_depth(&self, module_id: &ModuleIdentifier) -> Option<usize> {
    self
      .module_graph_module_by_identifier(module_id)
      .and_then(|mgm| mgm.depth)
  }

  pub fn set_depth_if_lower(&mut self, module_id: &ModuleIdentifier, depth: usize) -> bool {
    let mgm = self.module_graph_module_by_identifier_mut(module_id);
    match mgm.depth {
      Some(cur_depth) if cur_depth <= depth => false,
      _ => {
        mgm.depth = Some(depth);
        true
      }
    }
  }

  pub fn add_module(&mut self, module: BoxModule) {
    self.inner.modules.insert(module.identifier(), module);
  }

  pub fn add_block(&mut self, block: Box<AsyncDependenciesBlock>) {
    self.inner.blocks.insert(block.identifier(), block);
  }

  pub fn set_parents(&mut self, dependency_id: DependencyId, parents: DependencyParents) {
    self
      .inner
      .dependency_id_to_parents
      .insert(dependency_id, parents);
  }

  pub fn get_parent_module(&self, dependency_id: &DependencyId) -> Option<&ModuleIdentifier> {
    self
      .inner
      .dependency_id_to_parents
      .get(dependency_id)
      .map(|p| &p.module)
  }

  pub fn get_parent_block(
    &self,
    dependency_id: &DependencyId,
  ) -> Option<&AsyncDependenciesBlockIdentifier> {
    self
      .inner
      .dependency_id_to_parents
      .get(dependency_id)
      .and_then(|p| p.block.as_ref())
  }

  pub fn get_parent_block_index(&self, dependency_id: &DependencyId) -> Option<usize> {
    self
      .inner
      .dependency_id_to_parents
      .get(dependency_id)
      .map(|p| p.index_in_block)
  }

  pub fn block_by_id(
    &self,
    block_id: &AsyncDependenciesBlockIdentifier,
  ) -> Option<&AsyncDependenciesBlock> {
    self.inner.blocks.get(block_id).map(AsRef::as_ref)
  }

  pub fn block_by_id_expect(
    &self,
    block_id: &AsyncDependenciesBlockIdentifier,
  ) -> &AsyncDependenciesBlock {
    self
      .inner
      .blocks
      .get(block_id)
      .expect("should insert block before get it")
  }

  pub fn blocks(&self) -> &AsyncDependenciesBlockIdentifierMap<Box<AsyncDependenciesBlock>> {
    &self.inner.blocks
  }

  pub fn dependencies(&self) -> impl Iterator<Item = (&DependencyId, &BoxDependency)> {
    self.inner.dependencies.iter()
  }

  pub fn add_dependency(&mut self, dependency: BoxDependency) {
    self.inner.dependencies.insert(*dependency.id(), dependency);
  }

  /// Get a dependency by ID, panicking if not found.
  ///
  /// **PREFERRED METHOD**: Use this for ALL internal Rust code including:
  /// - Core compilation logic
  /// - All plugins (`rspack_plugin_*`)
  /// - Stats generation, code generation, runtime templates
  /// - Chunk graph building, export analysis
  /// - Module graph building operations
  /// - Any internal operations where dependencies should exist
  ///
  /// Dependencies should always be accessible in internal operations, so this
  /// method enforces that invariant with a clear panic message if violated.
  ///
  /// **Only the binding layer (`rspack_binding_api`) should use `internal::try_dependency_by_id()`**
  /// for graceful handling of missing dependencies in external APIs.
  pub fn dependency_by_id(&self, dependency_id: &DependencyId) -> &BoxDependency {
    self
      .inner
      .dependencies
      .get(dependency_id)
      .unwrap_or_else(|| panic!("Dependency with ID {dependency_id:?} not found"))
  }

  /// Get a mutable dependency by ID, panicking if not found.
  ///
  /// **PREFERRED METHOD**: Use this for ALL internal Rust code when you need to
  /// modify dependencies. Dependencies should always be accessible in internal
  /// operations, so this method enforces that invariant with a clear panic message.
  pub fn dependency_by_id_mut(&mut self, dependency_id: &DependencyId) -> &mut BoxDependency {
    self
      .inner
      .dependencies
      .get_mut(dependency_id)
      .unwrap_or_else(|| panic!("Dependency with ID {dependency_id:?} not found"))
  }

  /// Uniquely identify a module by its dependency
  pub fn module_graph_module_by_dependency_id(
    &self,
    id: &DependencyId,
  ) -> Option<&ModuleGraphModule> {
    self
      .module_identifier_by_dependency_id(id)
      .and_then(|module_identifier| self.module_graph_module_by_identifier(module_identifier))
  }

  pub fn module_identifier_by_dependency_id(
    &self,
    dep_id: &DependencyId,
  ) -> Option<&ModuleIdentifier> {
    self
      .inner
      .connections
      .get(dep_id)
      .map(|con| con.module_identifier())
  }

  pub fn get_module_by_dependency_id(&self, dep_id: &DependencyId) -> Option<&BoxModule> {
    self
      .module_identifier_by_dependency_id(dep_id)
      .and_then(|module_id| self.inner.modules.get(module_id))
  }

  fn add_connection(
    &mut self,
    connection: ModuleGraphConnection,
    condition: Option<DependencyCondition>,
  ) {
    if self
      .connection_by_dependency_id(&connection.dependency_id)
      .is_some()
    {
      return;
    }

    if let Some(condition) = condition {
      self
        .inner
        .connection_to_condition
        .insert(connection.dependency_id, condition);
    }

    let module_id = *connection.module_identifier();
    let origin_module_id = connection.original_module_identifier;
    let dependency_id = connection.dependency_id;

    // add to connections list
    self
      .inner
      .connections
      .insert(connection.dependency_id, connection);

    // set to module incoming connection
    {
      let mgm = self.module_graph_module_by_identifier_mut(&module_id);

      mgm.add_incoming_connection(dependency_id);
    }

    // set to origin module outgoing connection
    if let Some(identifier) = origin_module_id {
      let original_mgm = self.module_graph_module_by_identifier_mut(&identifier);
      original_mgm.add_outgoing_connection(dependency_id);
    };
  }

  /// Add a connection between two module graph modules, if a connection exists, then it will be reused.
  pub fn set_resolved_module(
    &mut self,
    original_module_identifier: Option<ModuleIdentifier>,
    dependency_id: DependencyId,
    module_identifier: ModuleIdentifier,
  ) -> Result<()> {
    let dependency = self.dependency_by_id(&dependency_id);
    let is_module_dependency =
      dependency.as_module_dependency().is_some() || dependency.as_context_dependency().is_some();
    let condition = dependency
      .as_module_dependency()
      .and_then(|dep| dep.get_condition());
    if !is_module_dependency {
      return Ok(());
    }

    let conditional = condition.is_some();
    let new_connection = ModuleGraphConnection::new(
      dependency_id,
      original_module_identifier,
      module_identifier,
      conditional,
    );
    self.add_connection(new_connection, condition);

    Ok(())
  }

  /// Uniquely identify a module by its identifier and return the aliased reference
  pub fn module_by_identifier(&self, identifier: &ModuleIdentifier) -> Option<&BoxModule> {
    self.inner.modules.get(identifier)
  }

  pub fn module_by_identifier_mut(
    &mut self,
    identifier: &ModuleIdentifier,
  ) -> Option<&mut BoxModule> {
    self.inner.modules.get_mut(identifier)
  }

  /// Uniquely identify a module graph module by its module's identifier and return the aliased reference
  pub fn module_graph_module_by_identifier(
    &self,
    identifier: &ModuleIdentifier,
  ) -> Option<&ModuleGraphModule> {
    self.inner.module_graph_modules.get(identifier)
  }

  /// Get a mutable module graph module by identifier, panicking if not found.
  ///
  /// **PREFERRED METHOD**: Use this for all internal code where the module graph module
  /// should exist. This enforces the invariant with a clear panic message if violated.
  ///
  /// Only use `try_module_graph_module_by_identifier_mut()` when you need to handle missing modules.
  pub fn module_graph_module_by_identifier_mut(
    &mut self,
    identifier: &ModuleIdentifier,
  ) -> &mut ModuleGraphModule {
    self
      .inner
      .module_graph_modules
      .get_mut(identifier)
      .unwrap_or_else(|| panic!("ModuleGraphModule with identifier {identifier:?} not found"))
  }

  pub fn get_ordered_outgoing_connections(
    &self,
    module_identifier: &ModuleIdentifier,
  ) -> impl Iterator<Item = &ModuleGraphConnection> {
    self
      .module_graph_module_by_identifier(module_identifier)
      .map(|m| {
        m.all_dependencies
          .iter()
          .filter_map(|dep_id| self.connection_by_dependency_id(dep_id))
      })
      .into_iter()
      .flatten()
  }

  pub fn get_outgoing_deps_in_order(
    &self,
    module_identifier: &ModuleIdentifier,
  ) -> impl Iterator<Item = &DependencyId> {
    self
      .module_graph_module_by_identifier(module_identifier)
      .map(|m| {
        m.all_dependencies
          .iter()
          .filter(|dep_id| self.connection_by_dependency_id(dep_id).is_some())
      })
      .into_iter()
      .flatten()
  }

  pub fn connection_by_dependency_id(
    &self,
    dependency_id: &DependencyId,
  ) -> Option<&ModuleGraphConnection> {
    self.inner.connections.get(dependency_id)
  }

  pub fn get_resolved_module(&self, dependency_id: &DependencyId) -> Option<&ModuleIdentifier> {
    match self.connection_by_dependency_id(dependency_id) {
      Some(connection) => Some(&connection.resolved_module),
      None => None,
    }
  }

  pub fn connection_by_dependency_id_mut(
    &mut self,
    dependency_id: &DependencyId,
  ) -> Option<&mut ModuleGraphConnection> {
    self.inner.connections.get_mut(dependency_id)
  }

  pub fn get_pre_order_index(&self, module_id: &ModuleIdentifier) -> Option<u32> {
    self
      .module_graph_module_by_identifier(module_id)
      .and_then(|mgm| mgm.pre_order_index)
  }

  pub fn get_post_order_index(&self, module_id: &ModuleIdentifier) -> Option<u32> {
    self
      .module_graph_module_by_identifier(module_id)
      .and_then(|mgm| mgm.post_order_index)
  }

  pub fn get_issuer(&self, module_id: &ModuleIdentifier) -> Option<&BoxModule> {
    self
      .module_graph_module_by_identifier(module_id)
      .and_then(|mgm| mgm.issuer().get_module(self))
  }

  pub fn is_optional(
    &self,
    module_id: &ModuleIdentifier,
    module_graph_cache: &ModuleGraphCacheArtifact,
    side_effects_state_artifact: &SideEffectsStateArtifact,
    exports_info_artifact: &ExportsInfoArtifact,
  ) -> bool {
    let mut has_connections = false;
    for connection in self.get_incoming_connections(module_id) {
      let dependency = self.dependency_by_id(&connection.dependency_id);
      let Some(module_dependency) = dependency.as_module_dependency() else {
        return false;
      };
      if !module_dependency.get_optional()
        || !connection.is_target_active(
          self,
          None,
          module_graph_cache,
          side_effects_state_artifact,
          exports_info_artifact,
        )
      {
        return false;
      }
      has_connections = true;
    }
    has_connections
  }

  pub fn is_async(
    async_modules_artifact: &AsyncModulesArtifact,
    module_id: &ModuleIdentifier,
  ) -> bool {
    async_modules_artifact.contains(module_id)
  }

  pub fn is_deferred(
    &self,
    imported_by_defer_modules_artifact: &ImportedByDeferModulesArtifact,
    module_id: &ModuleIdentifier,
  ) -> bool {
    let imported_by_defer = imported_by_defer_modules_artifact.contains(module_id);
    if !imported_by_defer {
      return false;
    }
    let module = self
      .module_by_identifier(module_id)
      .expect("should have module");
    !module.build_meta().has_top_level_await
  }

  pub fn set_async(
    async_modules_artifact: &mut AsyncModulesArtifact,
    module_id: ModuleIdentifier,
    is_async: bool,
  ) -> bool {
    let original = Self::is_async(async_modules_artifact, &module_id);
    if original == is_async {
      return false;
    }
    if original {
      async_modules_artifact.remove(&module_id)
    } else {
      async_modules_artifact.insert(module_id)
    }
  }

  pub fn get_outgoing_connections(
    &self,
    module_id: &ModuleIdentifier,
  ) -> impl Iterator<Item = &ModuleGraphConnection> + Clone {
    self
      .module_graph_module_by_identifier(module_id)
      .map(|mgm| {
        mgm
          .outgoing_connections()
          .iter()
          .filter_map(|id| self.connection_by_dependency_id(id))
      })
      .into_iter()
      .flatten()
  }

  pub fn get_incoming_connections(
    &self,
    module_id: &ModuleIdentifier,
  ) -> impl Iterator<Item = &ModuleGraphConnection> + Clone {
    self
      .module_graph_module_by_identifier(module_id)
      .map(|mgm| {
        mgm
          .incoming_connections()
          .iter()
          .filter_map(|id| self.connection_by_dependency_id(id))
      })
      .into_iter()
      .flatten()
  }

  pub fn get_module_hash(&self, module_id: &ModuleIdentifier) -> Option<&RspackHashDigest> {
    self
      .module_by_identifier(module_id)
      .and_then(|m| m.build_info().hash.as_ref())
  }

  /// We can't insert all sort of things into one hashmap like javascript, so we create different
  /// hashmap to store different kinds of meta.
  pub fn get_dep_meta_if_existing(&self, id: &DependencyId) -> Option<&DependencyExtraMeta> {
    self.inner.dep_meta_map.get(id)
  }

  pub fn set_dependency_extra_meta(&mut self, dep_id: DependencyId, extra: DependencyExtraMeta) {
    self.inner.dep_meta_map.insert(dep_id, extra);
  }

  pub fn can_update_module(&self, dep_id: &DependencyId, module_id: &ModuleIdentifier) -> bool {
    let connection = self
      .connection_by_dependency_id(dep_id)
      .expect("should have connection");
    connection.module_identifier() != module_id
  }

  pub fn do_update_module(&mut self, dep_id: &DependencyId, module_id: &ModuleIdentifier) {
    let connection = self
      .connection_by_dependency_id_mut(dep_id)
      .unwrap_or_else(|| panic!("{dep_id:?}"));
    let old_module_identifier = *connection.module_identifier();
    connection.set_module_identifier(*module_id);

    // remove dep_id from old module mgm incoming connection
    let old_mgm = self.module_graph_module_by_identifier_mut(&old_module_identifier);
    old_mgm.remove_incoming_connection(dep_id);

    // add dep_id to updated module mgm incoming connection
    let new_mgm = self.module_graph_module_by_identifier_mut(module_id);
    new_mgm.add_incoming_connection(*dep_id);
  }

  pub fn get_optimization_bailout_mut(
    &mut self,
    id: &ModuleIdentifier,
  ) -> &mut Vec<OptimizationBailoutItem> {
    let mgm = self.module_graph_module_by_identifier_mut(id);
    mgm.optimization_bailout_mut()
  }

  pub fn get_optimization_bailout(&self, id: &ModuleIdentifier) -> &Vec<OptimizationBailoutItem> {
    let mgm = self
      .module_graph_module_by_identifier(id)
      .expect("should have module graph module");
    &mgm.optimization_bailout
  }

  pub fn get_condition_state(
    &self,
    connection: &ModuleGraphConnection,
    runtime: Option<&RuntimeSpec>,
    module_graph_cache: &ModuleGraphCacheArtifact,
    side_effects_state_artifact: &SideEffectsStateArtifact,
    exports_info_artifact: &ExportsInfoArtifact,
  ) -> ConnectionState {
    let condition = self
      .inner
      .connection_to_condition
      .get(&connection.dependency_id)
      .expect("should have condition");
    condition.get_connection_state(
      connection,
      runtime,
      self,
      module_graph_cache,
      side_effects_state_artifact,
      exports_info_artifact,
    )
  }

  pub fn is_connection_active(
    &self,
    connection: &ModuleGraphConnection,
    runtime: Option<&RuntimeSpec>,
    module_graph_cache: &ModuleGraphCacheArtifact,
    side_effects_state_artifact: &SideEffectsStateArtifact,
    exports_info_artifact: &ExportsInfoArtifact,
  ) -> bool {
    let condition = self
      .inner
      .connection_to_condition
      .get(&connection.dependency_id)
      .expect("should have condition");
    condition.is_connection_active(
      connection,
      runtime,
      self,
      module_graph_cache,
      side_effects_state_artifact,
      exports_info_artifact,
    )
  }

  // todo remove it after module_graph_partial remove all of dependency_id_to_*
  pub fn cache_recovery_connection(&mut self, connection: ModuleGraphConnection) {
    let condition = self
      .dependency_by_id(&connection.dependency_id)
      .as_module_dependency()
      .and_then(|dep| dep.get_condition());

    // recovery condition
    if let Some(condition) = condition {
      self
        .inner
        .connection_to_condition
        .insert(connection.dependency_id, condition);
    }

    self
      .inner
      .connections
      .insert(connection.dependency_id, connection);
  }

  pub fn batch_set_connections_original_module(
    &mut self,
    tasks: Vec<(DependencyId, ModuleIdentifier)>,
  ) {
    let changed = tasks
      .into_par_iter()
      .map(|(dep_id, original_module_identifier)| {
        let mut con = self
          .connection_by_dependency_id(&dep_id)
          .expect("should have connection")
          .clone();
        con.original_module_identifier = Some(original_module_identifier);
        (dep_id, con)
      })
      .collect::<Vec<_>>();

    for (dep_id, con) in changed {
      self.inner.connections.insert(dep_id, con);
    }
  }

  pub fn batch_set_connections_module(&mut self, tasks: Vec<(DependencyId, ModuleIdentifier)>) {
    let changed = tasks
      .into_par_iter()
      .map(|(dep_id, module_identifier)| {
        let mut con = self
          .connection_by_dependency_id(&dep_id)
          .expect("should have connection")
          .clone();
        con.set_module_identifier(module_identifier);
        (dep_id, con)
      })
      .collect::<Vec<_>>();

    for (dep_id, con) in changed {
      self.inner.connections.insert(dep_id, con);
    }
  }

  pub fn batch_add_connections(
    &mut self,
    tasks: Vec<(ModuleIdentifier, Vec<DependencyId>, Vec<DependencyId>)>,
  ) {
    let changed = tasks
      .into_par_iter()
      .map(|(mid, outgoings, incomings)| {
        let mut mgm = self
          .module_graph_module_by_identifier(&mid)
          .expect("should have mgm")
          .clone();
        for outgoing in outgoings {
          mgm.add_outgoing_connection(outgoing);
        }
        for incoming in incomings {
          mgm.add_incoming_connection(incoming);
        }
        (mid, mgm)
      })
      .collect::<Vec<_>>();

    for (mid, mgm) in changed {
      self.inner.module_graph_modules.insert(mid, mgm);
    }
  }

  pub fn batch_remove_connections(
    &mut self,
    tasks: Vec<(ModuleIdentifier, Vec<DependencyId>, Vec<DependencyId>)>,
  ) {
    let changed = tasks
      .into_par_iter()
      .map(|(mid, outgoings, incomings)| {
        let mut mgm = self
          .module_graph_module_by_identifier(&mid)
          .expect("should have mgm")
          .clone();
        for outgoing in outgoings.iter() {
          mgm.remove_outgoing_connection(outgoing);
        }
        for incoming in incomings.iter() {
          mgm.remove_incoming_connection(incoming);
        }
        (mid, mgm)
      })
      .collect::<Vec<_>>();

    for (mid, mgm) in changed {
      self.inner.module_graph_modules.insert(mid, mgm);
    }
  }

  pub fn batch_set_export_info_used_name(
    &mut self,
    exports_info_artifact: &mut ExportsInfoArtifact,
    tasks: Vec<(ExportInfo, UsedNameItem)>,
  ) {
    for (export_info, used_name) in tasks {
      let ExportInfo {
        exports_info,
        export_name,
      } = export_info;

      let data = exports_info_artifact.get_exports_info_mut_by_id(&exports_info);
      match export_name {
        ExportName::Named(name) => {
          data
            .named_exports_mut(&name)
            .expect("should have named export")
            .set_used_name(used_name);
        }
        ExportName::Other => {
          data.other_exports_info_mut().set_used_name(used_name);
        }
        ExportName::SideEffects => {
          data.side_effects_only_info_mut().set_used_name(used_name);
        }
      }
    }
  }
}