rspack_core 0.100.1

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
1203
1204
1205
1206
1207
1208
1209
1210
1211
//!  There are methods whose verb is `ChunkGraphChunk`

use std::{
  collections::VecDeque,
  fmt,
  hash::{BuildHasherDefault, Hash},
};

use hashlink::LinkedHashMap;
use indexmap::IndexMap;
use itertools::Itertools;
use rspack_cacheable::{cacheable, with::AsPreset};
use rspack_collections::{IdentifierHasher, IdentifierLinkedMap, IdentifierMap, IdentifierSet};
use rspack_util::fx_hash::FxIndexSet;
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{Serialize, Serializer};
use ustr::Ustr;

use crate::{
  BoxModule, Chunk, ChunkByUkey, ChunkGraph, ChunkGraphModule, ChunkGroupByUkey, ChunkGroupUkey,
  ChunkUkey, Compilation, ExportsInfoArtifact, Module, ModuleGraph, ModuleGraphCacheArtifact,
  ModuleIdentifier, RuntimeGlobals, RuntimeModule, SideEffectsStateArtifact, SourceType,
  find_graph_roots, merge_runtime,
};

#[derive(Debug, Clone, Default)]
pub struct ChunkSizeOptions {
  // constant overhead for a chunk
  pub chunk_overhead: Option<f64>,
  // multiplicator for initial chunks
  pub entry_chunk_multiplicator: Option<f64>,
}

pub type ChunkIdMap<V> =
  std::collections::HashMap<ChunkId, V, BuildHasherDefault<IdentifierHasher>>;
pub type IndexChunkIdMap<V> = IndexMap<ChunkId, V, BuildHasherDefault<IdentifierHasher>>;
pub type ChunkIdSet = std::collections::HashSet<ChunkId, BuildHasherDefault<IdentifierHasher>>;

#[cacheable]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ChunkId(#[cacheable(with=AsPreset)] Ustr);

impl From<String> for ChunkId {
  fn from(s: String) -> Self {
    Self(s.into())
  }
}

impl From<&str> for ChunkId {
  fn from(s: &str) -> Self {
    Self(s.into())
  }
}

impl fmt::Display for ChunkId {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "{}", self.0)
  }
}

impl Serialize for ChunkId {
  fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  where
    S: Serializer,
  {
    if let Some(n) = self.as_number() {
      serializer.serialize_u32(n)
    } else {
      serializer.serialize_str(self.0.as_str())
    }
  }
}

impl ChunkId {
  pub fn as_number(&self) -> Option<u32> {
    rspack_util::numeric_id_value(self.0.as_str())
  }

  pub fn as_str(&self) -> &str {
    self.0.as_str()
  }
}

#[derive(Debug, Clone, Default)]
pub struct ChunkGraphChunk {
  /// URI of modules => ChunkGroupUkey
  ///
  /// use `LinkedHashMap` to keep the ordered from entry array.
  pub(super) entry_modules: IdentifierLinkedMap<ChunkGroupUkey>,
  pub(super) modules: IdentifierSet,
  pub(super) runtime_modules: Vec<ModuleIdentifier>,

  pub(super) source_types_by_module: Option<IdentifierMap<FxHashSet<SourceType>>>,
}

impl ChunkGraphChunk {
  pub fn new() -> Self {
    Self {
      entry_modules: Default::default(),
      modules: Default::default(),
      runtime_modules: Default::default(),
      source_types_by_module: Default::default(),
    }
  }

  pub fn modules(&self) -> &IdentifierSet {
    &self.modules
  }
}

fn get_modules_size(modules: &[&BoxModule], compilation: &Compilation) -> f64 {
  let mut size = 0f64;
  let module_graph = compilation.get_module_graph();
  for module in modules {
    for source_type in module.source_types(module_graph) {
      size += module.size(Some(source_type), Some(compilation));
    }
  }
  size
}

impl ChunkGraph {
  pub fn add_chunk(&mut self, chunk_ukey: ChunkUkey) {
    self
      .chunk_graph_chunk_by_chunk_ukey
      .entry(chunk_ukey)
      .or_default();
  }

  pub fn remove_chunk(&mut self, chunk_ukey: &ChunkUkey) -> Option<ChunkGraphChunk> {
    self.chunk_graph_chunk_by_chunk_ukey.remove(chunk_ukey)
  }

  pub fn replace_module(
    &mut self,
    old_module_id: &ModuleIdentifier,
    new_module_id: &ModuleIdentifier,
  ) {
    if !self
      .chunk_graph_module_by_module_identifier
      .contains_key(new_module_id)
    {
      let new_chunk_graph_module = ChunkGraphModule::new();
      self
        .chunk_graph_module_by_module_identifier
        .insert(*new_module_id, new_chunk_graph_module);
    }

    let old_cgm = self.expect_chunk_graph_module(*old_module_id);
    // Using clone to avoid using mutable borrow and immutable borrow at the same time.
    for chunk in old_cgm.chunks.clone() {
      let cgc = self.expect_chunk_graph_chunk_mut(chunk);
      cgc.modules.remove(old_module_id);
      cgc.modules.insert(*new_module_id);
      let new_cgm = self.expect_chunk_graph_module_mut(*new_module_id);
      new_cgm.chunks.insert(chunk);
    }

    // shadowing the mut ref to avoid violating rustc borrow rules
    let old_cgm = self.expect_chunk_graph_module_mut(*old_module_id);
    old_cgm.chunks.clear();

    for chunk in old_cgm.entry_in_chunks.clone() {
      let cgc = self.expect_chunk_graph_chunk_mut(chunk);
      if let Some(old) = cgc.entry_modules.get(old_module_id).copied() {
        let mut new_entry_modules = LinkedHashMap::default();
        for (m, cg) in cgc.entry_modules.iter() {
          if m == old_module_id {
            new_entry_modules.insert(*new_module_id, old);
          } else {
            new_entry_modules.insert(*m, *cg);
          }
        }
        cgc.entry_modules = new_entry_modules;
      }

      let new_cgm = self.expect_chunk_graph_module_mut(*new_module_id);
      new_cgm.entry_in_chunks.insert(chunk);
    }

    let old_cgm = self.expect_chunk_graph_module_mut(*old_module_id);
    old_cgm.entry_in_chunks.clear();
    let old_cgm = self.expect_chunk_graph_module(*old_module_id);

    for chunk in old_cgm.runtime_in_chunks.clone() {
      let cgc = self.expect_chunk_graph_chunk_mut(chunk);
      // delete old module
      cgc.runtime_modules = std::mem::take(&mut cgc.runtime_modules)
        .into_iter()
        .filter(|id| old_module_id != id)
        .collect::<Vec<_>>();
      cgc.runtime_modules.push(*new_module_id);
      let new_cgm = self.expect_chunk_graph_module_mut(*new_module_id);
      new_cgm.runtime_in_chunks.insert(chunk);

      // TODO: full_hash_modules and dependent_hash_modules, we don't have now https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/ChunkGraph.js#L445-L462
      // if let Some(full_hash_modules) = cgc.full_hash_modules.as_mut() {
      //   if full_hash_modules.contains(old_module as &RuntimeModule) {
      //     full_hash_modules.remove(old_module as &RuntimeModule);
      //     full_hash_modules.insert(new_module as &RuntimeModule);
      //   }
      // }
      // if let Some(dependent_hash_modules) = cgc.dependent_hash_modules.as_mut() {
      //   if dependent_hash_modules.contains(old_module as &RuntimeModule) {
      //     dependent_hash_modules.remove(old_module as &RuntimeModule);
      //     dependent_hash_modules.insert(new_module as &RuntimeModule);
      //   }
      // }
    }

    let old_cgm = self.expect_chunk_graph_module_mut(*old_module_id);
    old_cgm.runtime_in_chunks.clear();
  }

  pub fn get_chunk_entry_modules(&self, chunk_ukey: &ChunkUkey) -> Vec<ModuleIdentifier> {
    let chunk_graph_chunk = self.expect_chunk_graph_chunk(chunk_ukey);

    chunk_graph_chunk.entry_modules.keys().copied().collect()
  }

  pub fn get_chunk_entry_modules_with_chunk_group_iterable(
    &self,
    chunk_ukey: &ChunkUkey,
  ) -> &IdentifierLinkedMap<ChunkGroupUkey> {
    let cgc = self.expect_chunk_graph_chunk(chunk_ukey);
    &cgc.entry_modules
  }

  pub fn expect_chunk_graph_chunk_mut(&mut self, chunk_ukey: ChunkUkey) -> &mut ChunkGraphChunk {
    self
      .chunk_graph_chunk_by_chunk_ukey
      .get_mut(&chunk_ukey)
      .expect("Chunk should be added before")
  }

  pub fn expect_chunk_graph_chunk(&self, chunk_ukey: &ChunkUkey) -> &ChunkGraphChunk {
    self
      .chunk_graph_chunk_by_chunk_ukey
      .get(chunk_ukey)
      .expect("Chunk should be added before")
  }

  pub fn connect_chunk_and_entry_module(
    &mut self,
    chunk: ChunkUkey,
    module_identifier: ModuleIdentifier,
    entrypoint: ChunkGroupUkey,
  ) {
    let chunk_graph_module = self
      .chunk_graph_module_by_module_identifier
      .entry(module_identifier)
      .or_default();
    chunk_graph_module.entry_in_chunks.insert(chunk);

    let chunk_graph_chunk = self
      .chunk_graph_chunk_by_chunk_ukey
      .entry(chunk)
      .or_default();
    chunk_graph_chunk
      .entry_modules
      .insert(module_identifier, entrypoint);
  }

  pub fn disconnect_chunk_and_module(
    &mut self,
    chunk: &ChunkUkey,
    module_identifier: ModuleIdentifier,
  ) {
    let chunk_graph_module: &mut ChunkGraphModule =
      self.expect_chunk_graph_module_mut(module_identifier);
    chunk_graph_module.chunks.remove(chunk);

    let chunk_graph_chunk = self.expect_chunk_graph_chunk_mut(*chunk);
    chunk_graph_chunk.modules.remove(&module_identifier);

    if let Some(source_types_by_module) = &mut chunk_graph_chunk.source_types_by_module {
      source_types_by_module.remove(&module_identifier);
    }
  }

  pub fn connect_chunk_and_module(
    &mut self,
    chunk: ChunkUkey,
    module_identifier: ModuleIdentifier,
  ) {
    let chunk_graph_module = self
      .chunk_graph_module_by_module_identifier
      .entry(module_identifier)
      .or_default();
    chunk_graph_module.chunks.insert(chunk);

    let chunk_graph_chunk = self
      .chunk_graph_chunk_by_chunk_ukey
      .entry(chunk)
      .or_default();
    chunk_graph_chunk.modules.insert(module_identifier);
  }

  pub fn connect_chunk_and_runtime_module(
    &mut self,
    chunk: ChunkUkey,
    module_identifier: ModuleIdentifier,
  ) {
    self
      .chunk_graph_module_by_module_identifier
      .entry(module_identifier)
      .or_default();
    let cgm = self.expect_chunk_graph_module_mut(module_identifier);
    cgm.runtime_in_chunks.insert(chunk);

    self
      .chunk_graph_chunk_by_chunk_ukey
      .entry(chunk)
      .or_default();
    let cgc = self.expect_chunk_graph_chunk_mut(chunk);
    if !cgc.runtime_modules.contains(&module_identifier) {
      cgc.runtime_modules.push(module_identifier);
    }
  }

  pub fn disconnect_chunk_and_runtime_module(
    &mut self,
    chunk: &ChunkUkey,
    module_identifier: &ModuleIdentifier,
  ) {
    let cgm = self
      .chunk_graph_module_by_module_identifier
      .get_mut(module_identifier);
    if let Some(cgm) = cgm {
      cgm.runtime_in_chunks.remove(chunk);
    }

    let cgc = self.chunk_graph_chunk_by_chunk_ukey.get_mut(chunk);
    if let Some(cgc) = cgc {
      cgc.runtime_modules = cgc
        .runtime_modules
        .iter()
        .copied()
        .filter(|id| *id != *module_identifier)
        .collect::<Vec<_>>();
    }
  }

  pub fn disconnect_chunks_and_modules(
    &mut self,
    chunks: &[ChunkUkey],
    modules: &[ModuleIdentifier],
  ) {
    for chunk in chunks.iter() {
      let cgc = self.expect_chunk_graph_chunk_mut(*chunk);
      for module in modules.iter() {
        cgc.modules.remove(module);
        if let Some(source_types_by_module) = &mut cgc.source_types_by_module {
          source_types_by_module.remove(module);
        }
      }
    }
    for module in modules.iter() {
      let cgm = self.expect_chunk_graph_module_mut(*module);
      for chunk in chunks.iter() {
        cgm.chunks.remove(chunk);
      }
    }
  }

  pub fn connect_chunk_and_modules(&mut self, chunk: ChunkUkey, modules: &[ModuleIdentifier]) {
    for module in modules.iter() {
      let cgm = self.expect_chunk_graph_module_mut(*module);
      cgm.chunks.insert(chunk);
    }
    let cgc = self.expect_chunk_graph_chunk_mut(chunk);
    for module in modules.iter() {
      cgc.modules.insert(*module);
    }
  }

  // Get chunk modules without runtime modules
  pub fn get_chunk_modules<'module>(
    &self,
    chunk: &ChunkUkey,
    module_graph: &'module ModuleGraph,
  ) -> Vec<&'module BoxModule> {
    let chunk_graph_chunk = self.expect_chunk_graph_chunk(chunk);
    chunk_graph_chunk
      .modules
      .iter()
      .filter_map(|uri| module_graph.module_by_identifier(uri))
      .collect()
  }

  /// Get all identifiers of chunk modules, including runtime modules
  pub fn get_chunk_modules_identifier(&self, chunk: &ChunkUkey) -> &IdentifierSet {
    let chunk_graph_chunk = self.expect_chunk_graph_chunk(chunk);
    &chunk_graph_chunk.modules
  }

  /// Get ordered identifiers of chunk modules, including runtime modules
  pub fn get_ordered_chunk_modules_identifier(&self, chunk: &ChunkUkey) -> Vec<ModuleIdentifier> {
    let chunk_graph_chunk = self.expect_chunk_graph_chunk(chunk);
    let mut modules: Vec<ModuleIdentifier> = chunk_graph_chunk.modules.iter().copied().collect();
    // SAFETY: module identifier is unique
    modules.sort_unstable_by_key(|m| m.as_str());
    modules
  }

  /// Get ordered modules of chunk, including runtime modules
  pub fn get_ordered_chunk_modules<'module>(
    &self,
    chunk: &ChunkUkey,
    module_graph: &'module ModuleGraph,
  ) -> Vec<&'module BoxModule> {
    let mut modules = self.get_chunk_modules(chunk, module_graph);
    // SAFETY: module identifier is unique
    modules.sort_unstable_by_key(|m| m.identifier().as_str());
    modules
  }

  /// Check if the chunk has a module with the given source type
  /// returning `false` if source type is `Runtime`
  pub fn has_chunk_module_by_source_type(
    &self,
    chunk: &ChunkUkey,
    source_type: SourceType,
    module_graph: &ModuleGraph,
  ) -> bool {
    let chunk_graph_chunk = self.expect_chunk_graph_chunk(chunk);
    let source_types = &chunk_graph_chunk.source_types_by_module;
    if source_type == SourceType::Runtime {
      return false;
    }

    chunk_graph_chunk.modules.iter().any(|mid| {
      if let Some(source_types) = source_types
        && let Some(module_source_types) = source_types.get(mid)
      {
        if module_source_types.contains(&source_type) {
          module_graph.module_by_identifier(mid).is_some()
        } else {
          false
        }
      } else {
        module_graph
          .module_by_identifier(mid)
          .is_some_and(|module| module.source_types(module_graph).contains(&source_type))
      }
    })
  }

  /// Get chunk modules by source type
  /// returning empty vector if source type is `Runtime`
  pub fn get_chunk_modules_by_source_type<'module>(
    &self,
    chunk: &ChunkUkey,
    source_type: SourceType,
    module_graph: &'module ModuleGraph,
  ) -> Vec<&'module dyn Module> {
    let chunk_graph_chunk = self.expect_chunk_graph_chunk(chunk);
    let source_types = &chunk_graph_chunk.source_types_by_module;

    if source_type == SourceType::Runtime {
      return vec![];
    }

    chunk_graph_chunk
      .modules
      .iter()
      .filter_map(|mid| {
        if let Some(source_types) = source_types
          && let Some(module_source_types) = source_types.get(mid)
        {
          if module_source_types.contains(&source_type) {
            module_graph.module_by_identifier(mid).map(|m| m.as_ref())
          } else {
            None
          }
        } else {
          let module = module_graph.module_by_identifier(mid)?;
          if module.source_types(module_graph).contains(&source_type) {
            Some(module.as_ref())
          } else {
            None
          }
        }
      })
      .collect::<Vec<_>>()
  }

  /// Get identifiers of chunk modules by source type
  /// returning empty vector if source type is `Runtime`
  pub fn get_chunk_modules_identifier_by_source_type(
    &self,
    chunk: &ChunkUkey,
    source_type: SourceType,
    module_graph: &ModuleGraph,
  ) -> Vec<ModuleIdentifier> {
    let chunk_graph_chunk = self.expect_chunk_graph_chunk(chunk);
    let source_types = &chunk_graph_chunk.source_types_by_module;

    if source_type == SourceType::Runtime {
      return vec![];
    }

    chunk_graph_chunk
      .modules
      .iter()
      .filter_map(|mid| {
        if let Some(source_types) = source_types
          && let Some(module_source_types) = source_types.get(mid)
        {
          if module_source_types.contains(&source_type) {
            Some(*mid)
          } else {
            None
          }
        } else {
          let module = module_graph.module_by_identifier(mid)?;
          if module.source_types(module_graph).contains(&source_type) {
            Some(module.identifier())
          } else {
            None
          }
        }
      })
      .collect::<Vec<_>>()
  }

  pub fn get_chunk_modules_size(&self, chunk: &ChunkUkey, compilation: &Compilation) -> f64 {
    let module_graph = &compilation.get_module_graph();
    self
      .get_chunk_modules(chunk, module_graph)
      .iter()
      .fold(0.0, |acc, m| {
        acc
          + m
            .source_types(module_graph)
            .iter()
            .fold(0.0, |acc, t| acc + m.size(Some(t), Some(compilation)))
      })
  }

  pub fn get_chunk_modules_sizes(
    &self,
    chunk: &ChunkUkey,
    compilation: &Compilation,
  ) -> FxHashMap<SourceType, f64> {
    let mut sizes = FxHashMap::<SourceType, f64>::default();
    let cgc = self.expect_chunk_graph_chunk(chunk);
    let module_graph = &compilation.get_module_graph();
    for identifier in &cgc.modules {
      let module = module_graph.module_by_identifier(identifier);
      if let Some(module) = module {
        for source_type in module.source_types(module_graph) {
          let size = module.size(Some(source_type), Some(compilation));
          sizes
            .entry(*source_type)
            .and_modify(|s| *s += size)
            .or_insert(size);
        }
      } else {
        let runtime_module = compilation.runtime_modules.get(identifier);
        if let Some(runtime_module) = runtime_module {
          let size = runtime_module.size(Some(&SourceType::Runtime), Some(compilation));
          sizes
            .entry(SourceType::Runtime)
            .and_modify(|s| *s += size)
            .or_insert(size);
        }
      }
    }
    sizes
  }

  pub fn get_number_of_chunk_modules(&self, chunk: &ChunkUkey) -> usize {
    let cgc = self.expect_chunk_graph_chunk(chunk);
    cgc.modules.len()
  }

  pub fn get_number_of_entry_modules(&self, chunk: &ChunkUkey) -> usize {
    let cgc = self.expect_chunk_graph_chunk(chunk);
    cgc.entry_modules.len()
  }

  pub fn has_chunk_full_hash_modules(
    &self,
    chunk: &ChunkUkey,
    runtime_modules: &IdentifierMap<Box<dyn RuntimeModule>>,
  ) -> bool {
    let cgc = self.expect_chunk_graph_chunk(chunk);
    for runtime_module in &cgc.runtime_modules {
      let runtime_module = runtime_modules
        .get(runtime_module)
        .expect("should have runtime_module");
      if runtime_module.full_hash() {
        return true;
      }
    }
    false
  }

  pub fn has_chunk_dependent_hash_modules(
    &self,
    chunk: &ChunkUkey,
    runtime_modules: &IdentifierMap<Box<dyn RuntimeModule>>,
  ) -> bool {
    let cgc = self.expect_chunk_graph_chunk(chunk);
    for runtime_module in &cgc.runtime_modules {
      let runtime_module = runtime_modules
        .get(runtime_module)
        .expect("should have runtime_module");
      if runtime_module.dependent_hash() {
        return true;
      }
    }
    false
  }

  pub fn set_chunk_runtime_requirements(
    compilation: &mut Compilation,
    chunk_ukey: ChunkUkey,
    runtime_requirements: RuntimeGlobals,
  ) {
    compilation
      .cgc_runtime_requirements_artifact
      .insert(chunk_ukey, runtime_requirements);
  }

  pub fn set_tree_runtime_requirements(
    compilation: &mut Compilation,
    chunk_ukey: ChunkUkey,
    runtime_requirements: RuntimeGlobals,
  ) {
    Self::set_chunk_runtime_requirements(compilation, chunk_ukey, runtime_requirements);
  }

  pub fn get_chunk_runtime_requirements<'a>(
    compilation: &'a Compilation,
    chunk_ukey: &ChunkUkey,
  ) -> &'a RuntimeGlobals {
    compilation
      .cgc_runtime_requirements_artifact
      .get(chunk_ukey)
      .unwrap_or_else(|| {
        let chunk = compilation
          .build_chunk_graph_artifact
          .chunk_by_ukey
          .expect_get(chunk_ukey);
        let cgc = compilation
          .build_chunk_graph_artifact
          .chunk_graph
          .expect_chunk_graph_chunk(chunk_ukey);
        panic!("Should have runtime requirements for chunk:\n{chunk:#?}\n{cgc:#?}")
      })
  }

  pub fn get_tree_runtime_requirements<'a>(
    compilation: &'a Compilation,
    chunk_ukey: &ChunkUkey,
  ) -> &'a RuntimeGlobals {
    Self::get_chunk_runtime_requirements(compilation, chunk_ukey)
  }

  pub fn get_chunk_runtime_modules_in_order<'a>(
    &self,
    chunk_ukey: &ChunkUkey,
    compilation: &'a Compilation,
  ) -> impl Iterator<Item = (&ModuleIdentifier, &'a dyn RuntimeModule)> {
    let cgc = self.expect_chunk_graph_chunk(chunk_ukey);
    cgc
      .runtime_modules
      .iter()
      .map(|identifier| {
        (
          identifier,
          &**compilation
            .runtime_modules
            .get(identifier)
            .expect("should have runtime module"),
        )
      })
      .sorted_unstable_by(|(a_id, a), (b_id, b)| {
        let s = a.stage().cmp(&b.stage());
        if s.is_ne() {
          return s;
        }
        a_id.cmp(b_id)
      })
  }

  pub fn get_chunk_runtime_modules_iterable(
    &self,
    chunk_ukey: &ChunkUkey,
  ) -> impl Iterator<Item = &ModuleIdentifier> {
    let cgc = self.expect_chunk_graph_chunk(chunk_ukey);
    cgc.runtime_modules.iter()
  }

  pub fn has_chunk_runtime_modules(&self, chunk_ukey: &ChunkUkey) -> bool {
    let cgc = self.expect_chunk_graph_chunk(chunk_ukey);
    !cgc.runtime_modules.is_empty()
  }

  pub fn get_chunk_condition_map<F: Fn(&ChunkUkey, &Compilation) -> bool>(
    &self,
    chunk_ukey: &ChunkUkey,
    compilation: &Compilation,
    filter: F,
  ) -> FxHashMap<String, bool> {
    let mut map = FxHashMap::default();

    let chunk = compilation
      .build_chunk_graph_artifact
      .chunk_by_ukey
      .expect_get(chunk_ukey);
    for c in chunk
      .get_all_referenced_chunks(&compilation.build_chunk_graph_artifact.chunk_group_by_ukey)
      .iter()
    {
      let chunk = compilation
        .build_chunk_graph_artifact
        .chunk_by_ukey
        .expect_get(c);
      map.insert(chunk.expect_id().to_string(), filter(c, compilation));
    }

    map
  }

  pub fn get_chunk_root_modules(
    &self,
    chunk: &ChunkUkey,
    module_graph: &ModuleGraph,
    module_graph_cache: &ModuleGraphCacheArtifact,
    side_effects_state_artifact: &SideEffectsStateArtifact,
    exports_info_artifact: &ExportsInfoArtifact,
  ) -> Vec<ModuleIdentifier> {
    let cgc = self.expect_chunk_graph_chunk(chunk);
    let mut input = cgc.modules.iter().copied().collect::<Vec<_>>();
    input.sort_unstable();

    let mut modules = find_graph_roots(input, |module| {
      let mut set: IdentifierSet = Default::default();
      fn add_dependencies(
        module: ModuleIdentifier,
        set: &mut IdentifierSet,
        module_graph: &ModuleGraph,
        module_graph_cache: &ModuleGraphCacheArtifact,
        side_effects_state_artifact: &SideEffectsStateArtifact,
        exports_info_artifact: &ExportsInfoArtifact,
      ) {
        for connection in module_graph.get_outgoing_connections(&module) {
          // https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/ChunkGraph.js#L290
          let active_state = connection.active_state(
            module_graph,
            None,
            module_graph_cache,
            side_effects_state_artifact,
            exports_info_artifact,
          );
          match active_state {
            crate::ConnectionState::Active(false) => {
              continue;
            }
            crate::ConnectionState::TransitiveOnly => {
              add_dependencies(
                *connection.module_identifier(),
                set,
                module_graph,
                module_graph_cache,
                side_effects_state_artifact,
                exports_info_artifact,
              );
              continue;
            }
            _ => {}
          }
          set.insert(*connection.module_identifier());
        }
      }

      add_dependencies(
        module,
        &mut set,
        module_graph,
        module_graph_cache,
        side_effects_state_artifact,
        exports_info_artifact,
      );
      set.into_iter().collect()
    });

    modules.sort_unstable();

    modules
  }
  pub fn disconnect_chunk(
    &mut self,
    chunk: &mut Chunk,
    chunk_group_by_ukey: &mut ChunkGroupByUkey,
  ) {
    let chunk_ukey = &chunk.ukey();
    let cgc = self.expect_chunk_graph_chunk_mut(*chunk_ukey);
    let cgc_modules = std::mem::take(&mut cgc.modules);
    for module in cgc_modules {
      let cgm = self.expect_chunk_graph_module_mut(module);
      cgm.chunks.remove(chunk_ukey);
    }
    chunk.disconnect_from_groups(chunk_group_by_ukey)
  }

  pub fn has_chunk_entry_dependent_chunks(
    &self,
    chunk_ukey: &ChunkUkey,
    chunk_group_by_ukey: &ChunkGroupByUkey,
  ) -> bool {
    let cgc = self.expect_chunk_graph_chunk(chunk_ukey);
    for (_, chunk_group_ukey) in cgc.entry_modules.iter() {
      let chunk_group = chunk_group_by_ukey.expect_get(chunk_group_ukey);
      for c in chunk_group.chunks.iter() {
        if c != chunk_ukey {
          return true;
        }
      }
    }
    false
  }

  pub fn get_runtime_chunk_dependent_chunks_iterable(
    &self,
    chunk_ukey: &ChunkUkey,
    chunk_by_ukey: &ChunkByUkey,
    chunk_group_by_ukey: &ChunkGroupByUkey,
  ) -> impl Iterator<Item = ChunkUkey> {
    let mut set = FxIndexSet::default();
    let mut entrypoints = FxIndexSet::default();

    let chunk = chunk_by_ukey.expect_get(chunk_ukey);

    for chunk_group_key in chunk.get_sorted_groups_iter(chunk_group_by_ukey) {
      let chunk_group = chunk_group_by_ukey.expect_get(chunk_group_key);
      if chunk_group.kind.is_entrypoint() {
        let mut queue = VecDeque::new();
        queue.push_back(chunk_group);
        while let Some(current) = queue.pop_front() {
          entrypoints.insert(current.ukey);
          let mut has_children_entrypoint = false;
          for child in current.children_iterable() {
            let child_chunk_group = chunk_group_by_ukey.expect_get(child);
            if child_chunk_group.kind.is_entrypoint() {
              has_children_entrypoint = true;
              queue.push_back(child_chunk_group);
            }
          }

          if has_children_entrypoint {
            let entrypoint_chunk_ukey = current.get_entrypoint_chunk();
            let entrypoint_chunk = chunk_by_ukey.expect_get(&entrypoint_chunk_ukey);
            if entrypoint_chunk_ukey != *chunk_ukey
              && !entrypoint_chunk.has_runtime(chunk_group_by_ukey)
            {
              set.insert(entrypoint_chunk_ukey);
            }
          }
        }
      }
    }

    for entrypoint_ukey in entrypoints {
      let entrypoint = chunk_group_by_ukey.expect_get(&entrypoint_ukey);
      let entrypoint_chunk_ukey = entrypoint.get_entrypoint_chunk();
      let chunk_graph_chunk = self.expect_chunk_graph_chunk(&entrypoint_chunk_ukey);
      for chunk_group_ukey in chunk_graph_chunk.entry_modules.values() {
        let chunk_group = chunk_group_by_ukey.expect_get(chunk_group_ukey);
        for c in chunk_group.chunks.iter() {
          let chunk = chunk_by_ukey.expect_get(c);
          if c != chunk_ukey
            && c != &entrypoint_chunk_ukey
            && !chunk.has_runtime(chunk_group_by_ukey)
          {
            set.insert(*c);
          }
        }
      }
    }

    set.into_iter()
  }

  pub fn get_chunk_entry_dependent_chunks_iterable(
    &self,
    chunk_ukey: &ChunkUkey,
    chunk_by_ukey: &ChunkByUkey,
    chunk_group_by_ukey: &ChunkGroupByUkey,
  ) -> impl Iterator<Item = ChunkUkey> {
    let chunk = chunk_by_ukey.expect_get(chunk_ukey);
    let mut set = FxIndexSet::default();
    for chunk_group_ukey in chunk.get_sorted_groups_iter(chunk_group_by_ukey) {
      let chunk_group = chunk_group_by_ukey.expect_get(chunk_group_ukey);
      if chunk_group.kind.is_entrypoint() {
        let entry_point_chunk = chunk_group.get_entrypoint_chunk();
        let cgc = self.expect_chunk_graph_chunk(&entry_point_chunk);
        for (_, chunk_group_ukey) in cgc.entry_modules.iter() {
          let chunk_group = chunk_group_by_ukey.expect_get(chunk_group_ukey);
          for c in chunk_group.chunks.iter() {
            let chunk = chunk_by_ukey.expect_get(c);
            if c != chunk_ukey && c != &entry_point_chunk && !chunk.has_runtime(chunk_group_by_ukey)
            {
              set.insert(*c);
            }
          }
        }
      }
    }
    set.into_iter()
  }

  pub fn disconnect_chunk_and_entry_module(
    &mut self,
    chunk: &ChunkUkey,
    module_identifier: ModuleIdentifier,
  ) {
    let chunk_graph_module = self.expect_chunk_graph_module_mut(module_identifier);
    chunk_graph_module.chunks.remove(chunk);

    let chunk_graph_chunk = self.expect_chunk_graph_chunk_mut(*chunk);
    chunk_graph_chunk.entry_modules.remove(&module_identifier);

    if let Some(source_types_by_module) = &mut chunk_graph_chunk.source_types_by_module {
      source_types_by_module.remove(&module_identifier);
    }
  }

  pub fn can_chunks_be_integrated(
    &self,
    chunk_a_ukey: &ChunkUkey,
    chunk_b_ukey: &ChunkUkey,
    chunk_by_ukey: &ChunkByUkey,
    chunk_group_by_ukey: &ChunkGroupByUkey,
  ) -> bool {
    let chunk_a = chunk_by_ukey.expect_get(chunk_a_ukey);
    let chunk_b = chunk_by_ukey.expect_get(chunk_b_ukey);
    if chunk_a.prevent_integration() || chunk_b.prevent_integration() {
      return false;
    }

    let has_runtime_a = chunk_a.has_runtime(chunk_group_by_ukey);
    let has_runtime_b = chunk_b.has_runtime(chunk_group_by_ukey);

    // true, if a is always a parent of b
    let is_available_chunk = |a: &Chunk, b: &Chunk| {
      let mut queue = b
        .groups()
        .clone()
        .into_iter()
        .collect::<FxIndexSet<ChunkGroupUkey>>();
      let mut index: usize = 0;
      while index < queue.len() {
        let chunk_group_ukey = queue[index];
        index += 1;

        if a.is_in_group(&chunk_group_ukey) {
          continue;
        }
        let chunk_group = chunk_group_by_ukey.expect_get(&chunk_group_ukey);
        if chunk_group.is_initial() {
          return false;
        }
        for parent in chunk_group.parents_iterable() {
          queue.insert(*parent);
        }
      }
      true
    };

    if has_runtime_a != has_runtime_b {
      if has_runtime_a {
        return is_available_chunk(chunk_a, chunk_b);
      } else if has_runtime_b {
        return is_available_chunk(chunk_b, chunk_a);
      } else {
        return false;
      }
    }

    if self.get_number_of_entry_modules(&chunk_a.ukey()) > 0
      || self.get_number_of_entry_modules(&chunk_b.ukey()) > 0
    {
      return false;
    }

    true
  }

  pub fn get_chunk_size(
    &self,
    chunk_ukey: &ChunkUkey,
    options: &ChunkSizeOptions,
    chunk_by_ukey: &ChunkByUkey,
    chunk_group_by_ukey: &ChunkGroupByUkey,
    module_graph: &ModuleGraph,
    compilation: &Compilation,
  ) -> f64 {
    let cgc = self.expect_chunk_graph_chunk(chunk_ukey);
    let modules: Vec<&BoxModule> = cgc
      .modules
      .iter()
      .filter_map(|id| module_graph.module_by_identifier(id))
      .collect::<Vec<_>>();
    let modules_size = get_modules_size(&modules, compilation);
    let chunk_overhead = options.chunk_overhead.unwrap_or(10000f64);
    let entry_chunk_multiplicator = options.entry_chunk_multiplicator.unwrap_or(10f64);
    let chunk = chunk_by_ukey.expect_get(chunk_ukey);
    chunk_overhead
      + modules_size
        * (if chunk.can_be_initial(chunk_group_by_ukey) {
          entry_chunk_multiplicator
        } else {
          1f64
        })
  }

  #[allow(clippy::too_many_arguments)]
  pub fn get_integrated_chunks_size(
    &self,
    chunk_a_ukey: &ChunkUkey,
    chunk_b_ukey: &ChunkUkey,
    options: &ChunkSizeOptions,
    chunk_by_ukey: &ChunkByUkey,
    chunk_group_by_ukey: &ChunkGroupByUkey,
    module_graph: &ModuleGraph,
    compilation: &Compilation,
  ) -> f64 {
    let cgc_a = self.expect_chunk_graph_chunk(chunk_a_ukey);
    let cgc_b = self.expect_chunk_graph_chunk(chunk_b_ukey);
    let mut all_modules: Vec<&BoxModule> = cgc_a
      .modules
      .iter()
      .filter_map(|id| module_graph.module_by_identifier(id))
      .collect::<Vec<_>>();
    for id in &cgc_b.modules {
      let module = module_graph.module_by_identifier(id);
      if let Some(module) = module {
        all_modules.push(module);
      }
    }
    let modules_size = get_modules_size(&all_modules, compilation);
    let chunk_overhead = options.chunk_overhead.unwrap_or(10000f64);
    let entry_chunk_multiplicator = options.entry_chunk_multiplicator.unwrap_or(10f64);

    let chunk_a = chunk_by_ukey.expect_get(chunk_a_ukey);
    let chunk_b = chunk_by_ukey.expect_get(chunk_b_ukey);

    chunk_overhead
      + modules_size
        * (if chunk_a.can_be_initial(chunk_group_by_ukey)
          || chunk_b.can_be_initial(chunk_group_by_ukey)
        {
          entry_chunk_multiplicator
        } else {
          1f64
        })
  }

  pub fn integrate_chunks(
    &mut self,
    a: &ChunkUkey,
    b: &ChunkUkey,
    chunk_by_ukey: &mut ChunkByUkey,
    chunk_group_by_ukey: &mut ChunkGroupByUkey,
    module_graph: &ModuleGraph,
  ) {
    let chunk_b = chunk_by_ukey.expect_get(b).clone();
    let chunk_a = chunk_by_ukey.expect_get_mut(a);

    // Decide for one name (deterministic)
    if let (Some(chunk_a_name), Some(chunk_b_name)) = (chunk_a.name(), chunk_b.name()) {
      if (self.get_number_of_entry_modules(a) > 0) == (self.get_number_of_entry_modules(b) > 0) {
        // When both chunks have entry modules or none have one, use
        // shortest name
        let new_name = if chunk_a_name.len() != chunk_b_name.len() {
          if chunk_a_name.len() < chunk_b_name.len() {
            chunk_a_name
          } else {
            chunk_b_name
          }
        } else if chunk_a_name < chunk_b_name {
          chunk_a_name
        } else {
          chunk_b_name
        };
        chunk_a.set_name(Some(new_name.to_string()));
      } else if self.get_number_of_entry_modules(b) > 0 {
        // Pick the name of the chunk with the entry module
        chunk_a.set_name(chunk_b.name().map(ToOwned::to_owned));
      }
    } else if let Some(chunk_b_name) = chunk_b.name() {
      chunk_a.set_name(Some(chunk_b_name.to_string()));
    }

    // Merge id name hints
    for hint in chunk_b.id_name_hints() {
      chunk_a.add_id_name_hints(hint.clone());
    }

    // Merge runtime
    chunk_a.set_runtime(merge_runtime(chunk_a.runtime(), chunk_b.runtime()));

    // get_chunk_modules is used here to create a clone, because disconnect_chunk_and_module modifies
    for module in self.get_chunk_modules(b, module_graph) {
      self.disconnect_chunk_and_module(b, module.identifier());
      self.connect_chunk_and_module(*a, module.identifier());
    }

    let chunk_entry_modules_with_chunk_group_iterable = self
      .get_chunk_entry_modules_with_chunk_group_iterable(b)
      .clone();
    for (module, chunk_group) in chunk_entry_modules_with_chunk_group_iterable {
      self.disconnect_chunk_and_entry_module(b, module);
      self.connect_chunk_and_entry_module(*a, module, chunk_group);
    }

    let mut remove_group_ukeys = vec![];
    for &chunk_group_ukey in chunk_b.groups() {
      let chunk_group = chunk_group_by_ukey.expect_get_mut(&chunk_group_ukey);
      chunk_group.replace_chunk(b, a);
      chunk_a.add_group(chunk_group_ukey);
      remove_group_ukeys.push(chunk_group_ukey);
    }

    let chunk_b = chunk_by_ukey.expect_get_mut(b);
    for group_ukey in remove_group_ukeys {
      chunk_b.remove_group(&group_ukey);
    }
  }

  pub fn set_runtime_id(&mut self, runtime: String, id: Option<String>) {
    self.runtime_ids.insert(runtime, id);
  }

  pub fn get_runtime_id(&self, runtime: &str) -> Option<String> {
    self.runtime_ids.get(runtime).and_then(|v| v.to_owned())
  }

  pub fn set_chunk_modules_source_types(
    &mut self,
    chunk: &ChunkUkey,
    module: ModuleIdentifier,
    source_types: FxHashSet<SourceType>,
  ) {
    let cgc = self
      .chunk_graph_chunk_by_chunk_ukey
      .get_mut(chunk)
      .expect("should have cgc");

    if let Some(source_types_by_module) = &mut cgc.source_types_by_module {
      source_types_by_module.insert(module, source_types);
    } else {
      let mut map = IdentifierMap::default();
      map.insert(module, source_types);
      cgc.source_types_by_module = Some(map);
    }
  }

  pub fn get_chunk_module_source_types(
    &self,
    chunk: &ChunkUkey,
    module: &BoxModule,
    module_graph: &ModuleGraph,
  ) -> FxHashSet<SourceType> {
    self
      .chunk_graph_chunk_by_chunk_ukey
      .get(chunk)
      .and_then(|cgc| {
        if let Some(source_types_by_module) = &cgc.source_types_by_module {
          return source_types_by_module.get(&module.identifier()).cloned();
        }

        None
      })
      .unwrap_or_else(|| module.source_types(module_graph).iter().copied().collect())
  }
}

#[cfg(test)]
mod tests {
  use super::ChunkId;

  #[test]
  fn chunk_id_serialize_matches_runtime_numeric_rules() {
    assert_eq!(serde_json::to_string(&ChunkId::from("903")).unwrap(), "903");
    assert_eq!(
      serde_json::to_string(&ChunkId::from("01")).unwrap(),
      "\"01\""
    );
    assert_eq!(
      serde_json::to_string(&ChunkId::from("main")).unwrap(),
      "\"main\""
    );
    assert_eq!(
      serde_json::to_string(&ChunkId::from("4294967295")).unwrap(),
      "4294967295"
    );
    assert_eq!(
      serde_json::to_string(&ChunkId::from("4294967296")).unwrap(),
      "\"4294967296\""
    );
    assert_eq!(
      serde_json::to_string(&vec![ChunkId::from("01"), ChunkId::from("903")]).unwrap(),
      "[\"01\",903]"
    );
  }
}