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
use std::{
  cmp::Ordering,
  fmt::{self, Display},
};

use itertools::Itertools;
use rspack_cacheable::cacheable;
use rspack_collections::IdentifierMap;
use rspack_error::{Result, error};
use rspack_util::fx_hash::FxIndexSet;
use rustc_hash::{FxHashMap as HashMap, FxHashSet};

use crate::{
  Chunk, ChunkByUkey, ChunkGroupByUkey, ChunkGroupUkey, ChunkLoading, ChunkUkey, Compilation,
  DependencyLocation, DynamicImportFetchPriority, Filename, LibraryOptions, ModuleIdentifier,
  ModuleLayer, PublicPath, WasmLoading, compare_chunk_group,
};

#[derive(Debug, Clone)]
pub struct OriginRecord {
  pub module: Option<ModuleIdentifier>,
  pub loc: Option<DependencyLocation>,
  pub request: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ChunkGroup {
  pub ukey: ChunkGroupUkey,
  pub kind: ChunkGroupKind,
  pub chunks: Vec<ChunkUkey>,
  pub index: Option<u32>,
  pub parents: FxHashSet<ChunkGroupUkey>,
  pub(crate) module_pre_order_indices: IdentifierMap<u32>,
  pub(crate) module_post_order_indices: IdentifierMap<u32>,

  // keep order for children
  pub children: FxIndexSet<ChunkGroupUkey>,
  async_entrypoints: FxHashSet<ChunkGroupUkey>,
  // ChunkGroupInfo
  pub(crate) next_pre_order_index: u32,
  pub(crate) next_post_order_index: u32,
  // Entrypoint
  pub(crate) runtime_chunk: Option<ChunkUkey>,
  pub(crate) entrypoint_chunk: Option<ChunkUkey>,
  origins: Vec<OriginRecord>,
  pub(crate) is_over_size_limit: Option<bool>,
}

impl Default for ChunkGroup {
  fn default() -> Self {
    Self::new(ChunkGroupKind::Normal {
      options: Default::default(),
    })
  }
}

impl ChunkGroup {
  pub fn ukey(&self) -> ChunkGroupUkey {
    self.ukey
  }

  pub fn new(kind: ChunkGroupKind) -> Self {
    Self {
      ukey: ChunkGroupUkey::new(),
      chunks: vec![],
      module_post_order_indices: Default::default(),
      module_pre_order_indices: Default::default(),
      parents: Default::default(),
      children: Default::default(),
      async_entrypoints: Default::default(),
      kind,
      next_pre_order_index: 0,
      next_post_order_index: 0,
      runtime_chunk: None,
      entrypoint_chunk: None,
      index: None,
      origins: vec![],
      is_over_size_limit: None,
    }
  }

  pub fn parents_iterable(&self) -> impl Iterator<Item = &ChunkGroupUkey> {
    self.parents.iter()
  }

  pub fn module_pre_order_index(&self, module_identifier: &ModuleIdentifier) -> Option<u32> {
    // A module could split into another ChunkGroup, which doesn't have the module_post_order_indices of the module
    self
      .module_pre_order_indices
      .get(module_identifier)
      .copied()
  }

  pub fn children_iterable(&self) -> impl Iterator<Item = &ChunkGroupUkey> {
    self.children.iter()
  }

  pub fn module_post_order_index(&self, module_identifier: &ModuleIdentifier) -> Option<u32> {
    // A module could split into another ChunkGroup, which doesn't have the module_post_order_indices of the module
    self
      .module_post_order_indices
      .get(module_identifier)
      .copied()
  }

  pub fn get_files(&self, chunk_by_ukey: &ChunkByUkey) -> Vec<String> {
    self
      .chunks
      .iter()
      .flat_map(|chunk_ukey| chunk_by_ukey.expect_get(chunk_ukey).files().iter().cloned())
      .collect()
  }

  pub(crate) fn connect_chunk(&mut self, chunk: &mut Chunk) {
    self.chunks.push(chunk.ukey());
    chunk.add_group(self.ukey);
  }

  pub fn unshift_chunk(&mut self, chunk: ChunkUkey) -> bool {
    if let Ok(index) = self.chunks.binary_search(&chunk) {
      if index > 0 {
        self.chunks.remove(index);
        self.chunks.insert(0, chunk);
      }
      false
    } else {
      self.chunks.insert(0, chunk);
      true
    }
  }

  pub fn is_initial(&self) -> bool {
    matches!(self.kind, ChunkGroupKind::Entrypoint { initial, .. } if initial)
  }

  pub fn set_runtime_chunk(&mut self, chunk_ukey: ChunkUkey) {
    self.runtime_chunk = Some(chunk_ukey);
  }

  pub fn get_runtime_chunk(&self, chunk_group_by_ukey: &ChunkGroupByUkey) -> ChunkUkey {
    match self.kind {
      ChunkGroupKind::Entrypoint { .. } => self.runtime_chunk.unwrap_or_else(|| {
        for parent in self.parents_iterable() {
          let parent = chunk_group_by_ukey.expect_get(parent);
          if matches!(parent.kind, ChunkGroupKind::Entrypoint { .. }) {
            return parent.get_runtime_chunk(chunk_group_by_ukey);
          }
        }
        panic!(
          "Entrypoint({:?}) should set_runtime_chunk at build_chunk_graph before get_runtime_chunk",
          self.name()
        )
      }),
      ChunkGroupKind::Normal { .. } => {
        unreachable!("Normal chunk group doesn't have runtime chunk")
      }
    }
  }

  pub fn set_entrypoint_chunk(&mut self, chunk_ukey: ChunkUkey) {
    self.entrypoint_chunk = Some(chunk_ukey);
  }

  pub fn get_entrypoint_chunk(&self) -> ChunkUkey {
    match self.kind {
      ChunkGroupKind::Entrypoint { .. } => self
        .entrypoint_chunk
        .expect("EntryPoint runtime chunk not set"),
      ChunkGroupKind::Normal { .. } => {
        unreachable!("Normal chunk group doesn't have runtime chunk")
      }
    }
  }

  pub fn add_async_entrypoint(&mut self, async_entrypoint: ChunkGroupUkey) -> bool {
    self.async_entrypoints.insert(async_entrypoint)
  }

  pub fn async_entrypoints_iterable(&self) -> impl Iterator<Item = &ChunkGroupUkey> {
    self.async_entrypoints.iter()
  }

  pub fn ancestors(&self, chunk_group_by_ukey: &ChunkGroupByUkey) -> FxHashSet<ChunkGroupUkey> {
    let mut queue = vec![];
    let mut ancestors = FxHashSet::default();

    queue.extend(self.parents.iter().copied());

    while let Some(chunk_group_ukey) = queue.pop() {
      if ancestors.contains(&chunk_group_ukey) {
        continue;
      }
      ancestors.insert(chunk_group_ukey);
      let chunk_group = chunk_group_by_ukey.expect_get(&chunk_group_ukey);
      for parent in &chunk_group.parents {
        queue.push(*parent);
      }
    }

    ancestors
  }

  pub fn insert_chunk(&mut self, chunk: ChunkUkey, before: ChunkUkey) -> bool {
    let old_idx = self.chunks.iter().position(|ukey| *ukey == chunk);
    let idx = self
      .chunks
      .iter()
      .position(|ukey| *ukey == before)
      .expect("before chunk not found");

    if let Some(old_idx) = old_idx
      && old_idx > idx
    {
      self.chunks.remove(old_idx);
      self.chunks.insert(idx, chunk);
    } else if old_idx.is_none() {
      self.chunks.insert(idx, chunk);
      return true;
    }

    false
  }

  pub fn remove_chunk(&mut self, chunk: &ChunkUkey) -> bool {
    let idx = self.chunks.iter().position(|ukey| ukey == chunk);
    if let Some(idx) = idx {
      self.chunks.remove(idx);
      return true;
    }

    false
  }

  pub fn replace_chunk(&mut self, old_chunk: &ChunkUkey, new_chunk: &ChunkUkey) -> bool {
    if let Some(runtime_chunk) = self.runtime_chunk
      && runtime_chunk == *old_chunk
    {
      self.runtime_chunk = Some(*new_chunk);
    }

    if let Some(entry_point_chunk) = self.entrypoint_chunk
      && entry_point_chunk == *old_chunk
    {
      self.entrypoint_chunk = Some(*new_chunk);
    }

    match self.chunks.iter().position(|x| x == old_chunk) {
      // when old_chunk doesn't exist
      None => false,
      // when old_chunk exists
      Some(old_idx) => {
        match self.chunks.iter().position(|x| x == new_chunk) {
          // when new_chunk doesn't exist
          None => {
            self.chunks[old_idx] = *new_chunk;
            true
          }
          // when new_chunk exists
          Some(new_idx) => {
            if new_idx < old_idx {
              self.chunks.remove(old_idx);
              true
            } else if new_idx != old_idx {
              self.chunks[old_idx] = *new_chunk;
              self.chunks.remove(new_idx);
              true
            } else {
              false
            }
          }
        }
      }
    }
  }

  pub fn id(&self, compilation: &Compilation) -> String {
    self
      .chunks
      .iter()
      .filter_map(|chunk| {
        compilation
          .build_chunk_graph_artifact
          .chunk_by_ukey
          .get(chunk)
          .and_then(|item| item.id())
      })
      .join("+")
  }

  pub fn name(&self) -> Option<&str> {
    match &self.kind {
      ChunkGroupKind::Entrypoint { options, .. } => options.name.as_deref(),
      ChunkGroupKind::Normal { options } => options.name.as_deref(),
    }
  }

  pub fn add_child(&mut self, child_group: ChunkGroupUkey) -> bool {
    let size = self.children.len();
    self.children.insert(child_group);
    size != self.children.len()
  }

  pub fn add_parent(&mut self, parent_group: ChunkGroupUkey) -> bool {
    self.parents.insert(parent_group)
  }

  pub fn add_origin(
    &mut self,
    module_id: Option<ModuleIdentifier>,
    loc: Option<DependencyLocation>,
    request: Option<String>,
  ) {
    self.origins.push(OriginRecord {
      module: module_id,
      loc,
      request,
    });
  }

  pub fn origins(&self) -> &[OriginRecord] {
    &self.origins
  }

  pub fn get_children_by_orders(
    &self,
    compilation: &Compilation,
  ) -> HashMap<ChunkGroupOrderKey, Vec<ChunkGroupUkey>> {
    let mut children_by_orders = HashMap::<ChunkGroupOrderKey, Vec<ChunkGroupUkey>>::default();

    let orders = vec![ChunkGroupOrderKey::Preload, ChunkGroupOrderKey::Prefetch];

    for order_key in orders {
      let mut list = vec![];
      for child_ukey in &self.children {
        let Some(child_group) = compilation
          .build_chunk_graph_artifact
          .chunk_group_by_ukey
          .get(child_ukey)
        else {
          continue;
        };
        if let Some(order) = child_group
          .kind
          .get_normal_options()
          .and_then(|o| match order_key {
            ChunkGroupOrderKey::Prefetch => o.prefetch_order,
            ChunkGroupOrderKey::Preload => o.preload_order,
          })
        {
          list.push((order, child_group.ukey));
        }
      }

      list.sort_by(|a, b| {
        let cmp = b.0.cmp(&a.0);
        match cmp {
          Ordering::Equal => compare_chunk_group(&a.1, &b.1, compilation),
          _ => cmp,
        }
      });

      children_by_orders.insert(order_key, list.iter().map(|i| i.1).collect_vec());
    }

    children_by_orders
  }

  pub fn set_is_over_size_limit(&mut self, v: bool) {
    self.is_over_size_limit = Some(v);
  }
}

#[derive(Debug, Clone)]
pub enum ChunkGroupKind {
  Entrypoint {
    initial: bool,
    options: Box<EntryOptions>,
  },
  Normal {
    options: ChunkGroupOptions,
  },
}

impl ChunkGroupKind {
  pub fn new_entrypoint(initial: bool, options: Box<EntryOptions>) -> Self {
    Self::Entrypoint { initial, options }
  }

  pub fn is_entrypoint(&self) -> bool {
    matches!(self, Self::Entrypoint { .. })
  }

  pub fn get_entry_options(&self) -> Option<&EntryOptions> {
    match self {
      ChunkGroupKind::Entrypoint { options, .. } => Some(options),
      ChunkGroupKind::Normal { .. } => None,
    }
  }

  pub fn get_normal_options(&self) -> Option<&ChunkGroupOptions> {
    match self {
      ChunkGroupKind::Entrypoint { .. } => None,
      ChunkGroupKind::Normal { options, .. } => Some(options),
    }
  }

  pub fn name(&self) -> Option<&str> {
    match self {
      ChunkGroupKind::Entrypoint { options, .. } => options.name.as_deref(),
      ChunkGroupKind::Normal { options } => options.name.as_deref(),
    }
  }
}

#[cacheable]
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum EntryRuntime {
  String(String),
  #[default]
  False,
}

impl From<&str> for EntryRuntime {
  fn from(value: &str) -> Self {
    Self::String(value.to_owned())
  }
}

impl From<String> for EntryRuntime {
  fn from(value: String) -> Self {
    Self::String(value)
  }
}

impl EntryRuntime {
  pub fn as_string(&self) -> Option<&str> {
    match self {
      EntryRuntime::String(s) => Some(s),
      EntryRuntime::False => None,
    }
  }
}

// pub type EntryRuntime = String;
#[cacheable]
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq)]
pub struct EntryOptions {
  pub name: Option<String>,
  pub runtime: Option<EntryRuntime>,
  pub chunk_loading: Option<ChunkLoading>,
  pub wasm_loading: Option<WasmLoading>,
  pub async_chunks: Option<bool>,
  pub public_path: Option<PublicPath>,
  pub base_uri: Option<String>,
  pub filename: Option<Filename>,
  pub library: Option<LibraryOptions>,
  pub depend_on: Option<Vec<String>>,
  pub layer: Option<ModuleLayer>,
}

impl EntryOptions {
  pub fn merge(&mut self, other: EntryOptions) -> Result<()> {
    macro_rules! merge_field {
      ($field:ident) => {
        if Self::should_merge_field(
          self.$field.as_ref(),
          other.$field.as_ref(),
          stringify!($field),
        )? {
          self.$field = other.$field;
        }
      };
    }
    merge_field!(name);
    merge_field!(runtime);
    merge_field!(chunk_loading);
    merge_field!(wasm_loading);
    merge_field!(async_chunks);
    merge_field!(public_path);
    merge_field!(base_uri);
    merge_field!(filename);
    merge_field!(library);
    merge_field!(depend_on);
    merge_field!(layer);
    Ok(())
  }

  fn should_merge_field<T: Eq + fmt::Debug>(
    a: Option<&T>,
    b: Option<&T>,
    key: &str,
  ) -> Result<bool> {
    match (a, b) {
      (Some(a), Some(b)) if a != b => {
        Err(error!("Conflicting entry option {key} = ${a:?} vs ${b:?}"))
      }
      (None, Some(_)) => Ok(true),
      _ => Ok(false),
    }
  }
}

#[derive(Debug, Hash, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ChunkGroupOrderKey {
  Preload,
  Prefetch,
}

impl Display for ChunkGroupOrderKey {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.write_str(match self {
      ChunkGroupOrderKey::Preload => "preload",
      ChunkGroupOrderKey::Prefetch => "prefetch",
    })
  }
}

#[cacheable]
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ChunkGroupOptions {
  pub name: Option<String>,
  pub preload_order: Option<i32>,
  pub prefetch_order: Option<i32>,
  pub fetch_priority: Option<DynamicImportFetchPriority>,
}

impl ChunkGroupOptions {
  pub fn new(
    name: Option<String>,
    preload_order: Option<i32>,
    prefetch_order: Option<i32>,
    fetch_priority: Option<DynamicImportFetchPriority>,
  ) -> Self {
    Self {
      name,
      preload_order,
      prefetch_order,
      fetch_priority,
    }
  }
  pub fn name_optional(mut self, name: Option<String>) -> Self {
    self.name = name;
    self
  }
}

#[cacheable]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum GroupOptions {
  Entrypoint(Box<EntryOptions>),
  ChunkGroup(ChunkGroupOptions),
}

impl GroupOptions {
  pub fn name(&self) -> Option<&str> {
    match self {
      Self::Entrypoint(e) => e.name.as_deref(),
      Self::ChunkGroup(n) => n.name.as_deref(),
    }
  }

  pub fn entry_options(&self) -> Option<&EntryOptions> {
    match self {
      GroupOptions::Entrypoint(e) => Some(e),
      GroupOptions::ChunkGroup(_) => None,
    }
  }

  pub fn normal_options(&self) -> Option<&ChunkGroupOptions> {
    match self {
      GroupOptions::Entrypoint(_) => None,
      GroupOptions::ChunkGroup(e) => Some(e),
    }
  }
}