rspack_plugin_split_chunks 0.101.10

rspack split chunks plugin
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
mod chunk;
mod max_request;
pub mod max_size;
pub mod min_size;
mod module_group;

use std::{borrow::Cow, cmp::Ordering, fmt::Debug};

use itertools::Itertools;
use rayon::iter::{
  IndexedParallelIterator, IntoParallelRefIterator, IntoParallelRefMutIterator, ParallelIterator,
};
use rspack_collections::IdentifierMap;
use rspack_core::{ChunkUkey, Compilation, CompilationOptimizeChunks, Logger, Plugin};
use rspack_error::Result;
use rspack_hook::{plugin, plugin_hook};
use rspack_util::{fx_hash::FxIndexMap, tracing_preset::TRACING_BENCH_TARGET};
use rustc_hash::{FxHashMap, FxHashSet};
use tracing::instrument;

use crate::{
  CacheGroup, SplitChunkSizes,
  common::{FallbackCacheGroup, ModuleChunkMap},
  get_module_sizes,
  module_group::{IndexedCacheGroup, ModuleGroup, ModuleGroupKey},
};

type ModuleGroupMap = FxIndexMap<ModuleGroupKey, ModuleGroup>;

#[derive(Debug)]
pub struct PluginOptions {
  pub cache_groups: Vec<CacheGroup>,
  pub fallback_cache_group: FallbackCacheGroup,
  pub hide_path_info: Option<bool>,
}

#[plugin]
pub struct SplitChunksPlugin {
  cache_groups: Box<[CacheGroup]>,
  fallback_cache_group: FallbackCacheGroup,
  hide_path_info: bool,
}

impl SplitChunksPlugin {
  pub fn new(options: PluginOptions) -> Self {
    tracing::debug!("Create `SplitChunksPlugin` with {:#?}", options);
    Self::new_inner(
      options.cache_groups.into(),
      options.fallback_cache_group,
      options.hide_path_info.unwrap_or(false),
    )
  }
  #[instrument(name = "Compilation:SplitChunks",target=TRACING_BENCH_TARGET, skip_all)]
  async fn inner_impl(&self, compilation: &mut Compilation) -> Result<()> {
    let logger = compilation.get_logger(self.name());
    let start = logger.time("prepare module data");

    let mut all_modules = compilation
      .get_module_graph()
      .modules_keys()
      .copied()
      .collect::<Vec<_>>();
    // Sort modules to ensure deterministic processing order.
    // Use the precomputed identifier hash first to avoid repeated long string comparisons.
    all_modules.sort_unstable_by_key(|module| (module.precomputed_hash(), *module));

    let module_sizes = get_module_sizes(all_modules.par_iter().copied(), compilation);
    let module_chunks = Self::get_module_chunks(&all_modules, compilation);
    logger.time_end(start);

    let chunk_index_map: FxHashMap<ChunkUkey, u32> = {
      let mut ordered_chunks = compilation
        .build_chunk_graph_artifact
        .chunk_by_ukey
        .values()
        .collect::<Vec<_>>();

      ordered_chunks.sort_by_cached_key(|chunk| {
        // sort by (group.index, chunk index in group)
        let group = chunk
          .groups()
          .iter()
          .map(|group| {
            compilation
              .build_chunk_graph_artifact
              .chunk_group_by_ukey
              .expect_get(group)
          })
          .min_by(|group1, group2| group1.index.cmp(&group2.index))
          .expect("chunk should have at least one group");
        let chunk_index = group
          .chunks
          .iter()
          .position(|c| *c == chunk.ukey())
          .expect("chunk should be in its group");
        (group.index, chunk_index)
      });

      ordered_chunks
        .iter()
        .enumerate()
        .map(|(index, chunk)| {
          (
            chunk.ukey(),
            u32::try_from(index + 1).expect("chunk index should fit in u32"),
          )
        })
        .collect()
    };

    let start = logger.time("prepare cache groups");
    let mut priority_cache_groups = vec![];

    for (priority, cache_groups) in &self
      .cache_groups
      .iter()
      .enumerate()
      .map(|v| IndexedCacheGroup {
        cache_group_index: u32::try_from(v.0).expect("cache group index should fit in u32"),
        cache_group: v.1,
      })
      .sorted_by(|a, b| match b.compare_by_priority(a) {
        Ordering::Equal => a.compare_by_index(b),
        v => v,
      })
      .chunk_by(|v| v.cache_group.priority)
    {
      priority_cache_groups.push((priority, cache_groups.into_iter().collect::<Vec<_>>()));
    }

    let mut max_size_setting_map: FxHashMap<ChunkUkey, MaxSizeSetting> = Default::default();
    let mut removed_module_chunks: IdentifierMap<FxHashSet<ChunkUkey>> = IdentifierMap::default();

    logger.time_end(start);

    let start = logger.time("process cache groups");
    let priority_len = priority_cache_groups.len();
    for (index, (_, cache_groups)) in priority_cache_groups.into_iter().enumerate() {
      // A higher-priority cache group consumes module-chunk edges, not the whole module. Build the
      // combinations for this priority from the original chunk sets minus the consumed edges so a
      // lower-priority cache group can still group a newly formed residual chunk set. Do not read
      // the current chunk graph here because it also contains chunks created by earlier splits.
      let available_module_chunks = if removed_module_chunks.is_empty() {
        Cow::Borrowed(&module_chunks)
      } else {
        Cow::Owned(
          all_modules
            .par_iter()
            .enumerate()
            .map(|(module_index, module)| {
              let chunks = module_chunks
                .get(module_index)
                .expect("should have module chunks");
              if let Some(removed_chunks) = removed_module_chunks.get(module) {
                chunks.difference(removed_chunks).copied().collect()
              } else {
                chunks.clone()
              }
            })
            .collect(),
        )
      };

      let mut combinator = module_group::Combinator::default();
      let non_used_exports_min_chunks = cache_groups
        .iter()
        .filter(|cache_group| !cache_group.cache_group.used_exports)
        .map(|cache_group| cache_group.cache_group.min_chunks as usize)
        .min();

      if let Some(min_chunks) = non_used_exports_min_chunks {
        combinator.prepare_group_by_chunks(
          &all_modules,
          available_module_chunks.as_ref(),
          &chunk_index_map,
          min_chunks,
        );
      }

      if cache_groups
        .iter()
        .any(|cache_group| cache_group.cache_group.used_exports)
      {
        combinator.prepare_group_by_used_exports(
          &all_modules,
          &compilation.exports_info_artifact,
          &compilation.build_chunk_graph_artifact.chunk_by_ukey,
          available_module_chunks.as_ref(),
          &chunk_index_map,
        );
      }

      let mut module_group_map = self
        .prepare_module_group_map(
          &combinator,
          &all_modules,
          cache_groups,
          compilation,
          available_module_chunks.as_ref(),
          &chunk_index_map,
        )
        .await?;
      rayon::spawn(move || drop(combinator));
      tracing::trace!("prepared module_group_map {:#?}", module_group_map);

      module_group_map
        .par_iter_mut()
        .for_each(|(_, module_group)| module_group.prepare_modules_for_sizes_and_compare());
      self.ensure_min_size_fit(&mut module_group_map, &module_sizes);

      while !module_group_map.is_empty() {
        let (module_group_key, mut module_group) =
          self.find_best_module_group(&mut module_group_map);

        tracing::trace!(
          "ModuleGroup({}) wins, {:?} `ModuleGroup` remains",
          module_group_key,
          module_group_map.len(),
        );
        let cache_group = module_group.get_cache_group(&self.cache_groups);

        let mut is_reuse_existing_chunk = false;
        let mut is_reuse_existing_chunk_with_all_modules = false;
        let new_chunk = self.get_corresponding_chunk(
          compilation,
          &mut module_group,
          &mut is_reuse_existing_chunk,
          &mut is_reuse_existing_chunk_with_all_modules,
        );

        tracing::trace!(
          "{module_group_key}, get Chunk {:?} with is_reuse_existing_chunk: {is_reuse_existing_chunk:?} and {is_reuse_existing_chunk_with_all_modules:?}",
          compilation
            .build_chunk_graph_artifact
            .chunk_by_ukey
            .expect_get(&new_chunk)
            .chunk_reason()
        );

        if is_reuse_existing_chunk {
          // The chunk is not new but created in code splitting. We need remove `new_chunk` since we would remove
          // modules in this `Chunk/ModuleGroup` from other chunks. Other chunks is stored in `ModuleGroup.chunks`.
          module_group.remove_group_chunk(&new_chunk);
        }

        // If the module group size exceeds enforceSizeThreshold, skip maxRequest constraints
        // https://webpack.js.org/plugins/split-chunks-plugin/#splitchunksenforcesizethreshold
        let enforce_size_exceeded = !cache_group.enforce_size_threshold.is_empty()
          && module_group
            .get_sizes(&module_sizes)
            .bigger_than(&cache_group.enforce_size_threshold);

        let mut used_chunks = Cow::Borrowed(&module_group.chunks);

        if !enforce_size_exceeded {
          self.ensure_max_request_fit(compilation, cache_group, &mut used_chunks);
        }

        // `ensure_max_request_fit` can remove all source chunks for only some modules in a named
        // group. Track the exact original module-chunk edges that this split will consume instead
        // of treating the remaining modules and chunks as a cross product.
        let mut used_chunks = used_chunks;
        let mut placed_module_chunks = self
          .get_module_chunks_to_move(&module_group, new_chunk, &used_chunks, compilation)
          .await?;
        {
          let chunk_graph = &compilation.build_chunk_graph_artifact.chunk_graph;
          if is_reuse_existing_chunk {
            // A module already in the reused destination does not need to move, but that original
            // placement still belongs to the winning group and must be unavailable to competing
            // groups at this and lower priorities.
            for module in module_group
              .modules
              .iter()
              .filter(|module| chunk_graph.is_module_in_chunk(module, new_chunk))
            {
              placed_module_chunks.insert_chunk(*module, new_chunk);
            }
          }
        }

        let modules_without_placement = match &placed_module_chunks {
          ModuleChunkMap::Shared { modules, chunks }
            if module_group.uses_shared_module_chunks() =>
          {
            debug_assert!(modules.is_subset(&module_group.modules));
            debug_assert!(
              chunks.is_subset(
                module_group
                  .shared_module_chunks()
                  .expect("should have shared module chunks")
              )
            );
            if chunks.len() < cache_group.min_chunks as usize {
              module_group.modules.iter().copied().collect::<Vec<_>>()
            } else if modules.len() == module_group.modules.len() {
              Vec::new()
            } else {
              module_group
                .modules
                .difference(modules)
                .copied()
                .collect::<Vec<_>>()
            }
          }
          _ => module_group
            .modules
            .iter()
            .filter(|module| {
              let Some(placed_chunks) = placed_module_chunks.get(module) else {
                return true;
              };
              let Some(selected_chunks) = module_group.get_module_chunks(module) else {
                return true;
              };
              placed_chunks
                .iter()
                .filter(|chunk| selected_chunks.contains(*chunk))
                .count()
                < cache_group.min_chunks as usize
            })
            .copied()
            .collect::<Vec<_>>(),
        };
        if !modules_without_placement.is_empty() {
          // End the borrow of `module_group.chunks` only on the slow path that mutates the group.
          // The common path can keep using the original set without cloning it.
          let used_chunks_owned = used_chunks.into_owned();
          for module in modules_without_placement {
            module_group.remove_module(module);
          }

          // Max-request pruning and chunk conditions can change the module subset, so size checks
          // performed before selecting `used_chunks` are no longer sufficient.
          if min_size::remove_min_size_violating_modules(
            &module_group_key,
            &mut module_group,
            cache_group,
            &module_sizes,
          ) {
            tracing::trace!(
              "ModuleGroup({module_group_key}) is skipped after selecting its actual placements because it violates min_size {:#?}",
              cache_group.min_size,
            );
            continue;
          }

          placed_module_chunks.retain_modules(&module_group.modules);
          used_chunks = Cow::Owned(used_chunks_owned);
        }

        // Keep a reused destination in `placed_module_chunks` for ownership cleanup, even when the
        // cache group's chunk filter did not select it. Such an excluded destination must not count
        // towards `minChunks` or the source chunks split into the destination.
        let selected_placement_chunks = match &placed_module_chunks {
          ModuleChunkMap::Shared { chunks, .. } => Cow::Borrowed(chunks),
          ModuleChunkMap::ByModule(module_chunks) => Cow::Owned(
            module_chunks
              .iter()
              .flat_map(|(module, placed_chunks)| {
                let selected_chunks = module_group
                  .get_module_chunks(module)
                  .expect("should have selected module chunks");
                placed_chunks
                  .iter()
                  .filter(|chunk| selected_chunks.contains(*chunk))
                  .copied()
              })
              .collect::<FxHashSet<_>>(),
          ),
        };
        if used_chunks
          .iter()
          .any(|chunk| !selected_placement_chunks.contains(chunk))
        {
          used_chunks
            .to_mut()
            .retain(|chunk| selected_placement_chunks.contains(chunk));
        }

        if selected_placement_chunks.len() < cache_group.min_chunks as usize {
          tracing::trace!(
            "ModuleGroup({module_group_key}) is skipped. Reason: selected_placement_chunks.len()({:?}) < cache_group.min_chunks({:?})",
            selected_placement_chunks.len(),
            cache_group.min_chunks
          );
          continue;
        }

        if !Self::check_min_size_reduction_for_module_chunks(
          &placed_module_chunks,
          new_chunk,
          &module_sizes,
          &cache_group.min_size_reduction,
        ) {
          tracing::trace!(
            "ModuleGroup({module_group_key}) is skipped after selecting its actual placements because it violates min_size_reduction {:#?}",
            cache_group.min_size_reduction,
          );
          continue;
        }

        // Only mutate metadata on an existing destination after the winning group has passed all
        // checks. A group skipped after max-request pruning must not leave cache-group metadata on
        // a chunk it did not actually use.
        let new_chunk_mut = compilation
          .build_chunk_graph_artifact
          .chunk_by_ukey
          .expect_get_mut(&new_chunk);
        if let Some(chunk_reason) = new_chunk_mut.chunk_reason_mut() {
          chunk_reason.push_str(&format!(" (cache group: {})", cache_group.key.as_str()));
          if let Some(chunk_name) = &module_group.chunk_name {
            chunk_reason.push_str(&format!(" (name: {chunk_name})"));
          }
        }
        if let Some(filename) = &cache_group.filename {
          new_chunk_mut.set_filename_template(Some(filename.clone()));
        }
        new_chunk_mut.add_id_name_hints(cache_group.id_hint.clone());

        if !cache_group.max_initial_size.is_empty() || !cache_group.max_async_size.is_empty() {
          max_size_setting_map.insert(
            new_chunk,
            MaxSizeSetting {
              min_size: cache_group.min_size.clone(),
              max_async_size: cache_group.max_async_size.clone(),
              max_initial_size: cache_group.max_initial_size.clone(),
              automatic_name_delimiter: cache_group.automatic_name_delimiter.clone(),
            },
          );
        }

        self.move_modules_to_new_chunk_and_remove_from_old_chunks(
          &placed_module_chunks,
          new_chunk,
          compilation,
        );

        self.split_from_original_chunks(&module_group, &used_chunks, new_chunk, compilation);

        self.remove_all_modules_from_other_module_groups(
          &placed_module_chunks,
          &mut module_group_map,
          &module_sizes,
        );

        if index != priority_len - 1 {
          match &placed_module_chunks {
            ModuleChunkMap::Shared { modules, chunks } => {
              for module in modules {
                removed_module_chunks
                  .entry(*module)
                  .or_default()
                  .extend(chunks.iter().copied());
              }
            }
            ModuleChunkMap::ByModule(module_chunks) => {
              for (module, chunks) in module_chunks {
                removed_module_chunks
                  .entry(*module)
                  .or_default()
                  .extend(chunks.iter().copied());
              }
            }
          }
        }
      }
    }
    logger.time_end(start);

    let start = logger.time("ensure max size fit");
    self
      .ensure_max_size_fit(compilation, &max_size_setting_map)
      .await?;
    logger.time_end(start);

    Ok(())
  }
}

impl Debug for SplitChunksPlugin {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("SplitChunksPlugin").finish()
  }
}

#[plugin_hook(CompilationOptimizeChunks for SplitChunksPlugin, stage = Compilation::OPTIMIZE_CHUNKS_STAGE_ADVANCED)]
async fn optimize_chunks(&self, compilation: &mut Compilation) -> Result<Option<bool>> {
  self.inner_impl(compilation).await?;
  Ok(None)
}

impl Plugin for SplitChunksPlugin {
  fn name(&self) -> &'static str {
    "rspack.SplitChunksPlugin"
  }

  fn apply(&self, ctx: &mut rspack_core::ApplyContext<'_>) -> Result<()> {
    ctx
      .compilation_hooks
      .optimize_chunks
      .tap(optimize_chunks::new(self));
    Ok(())
  }
}

#[derive(Debug)]
struct MaxSizeSetting {
  pub min_size: SplitChunkSizes,
  pub max_async_size: SplitChunkSizes,
  pub max_initial_size: SplitChunkSizes,
  pub automatic_name_delimiter: String,
}