use rspack_core::{ChunkGroupUkey, ChunkUkey, Compilation};
use rustc_hash::FxHashSet;
use crate::common::ModuleChunkMap;
pub(super) fn placement_preserves_copies(
placed: &ModuleChunkMap,
source_chunks: &FxHashSet<ChunkUkey>,
target: ChunkUkey,
compilation: &Compilation,
) -> bool {
let artifact = &compilation.build_chunk_graph_artifact;
let target_chunk = artifact.chunk_by_ukey.expect_get(&target);
let consumers = |chunks: &FxHashSet<ChunkUkey>| -> FxHashSet<ChunkGroupUkey> {
chunks
.iter()
.flat_map(|chunk| {
artifact
.chunk_by_ukey
.expect_get(chunk)
.groups()
.iter()
.copied()
})
.collect()
};
let mut after = consumers(source_chunks);
after.extend(target_chunk.groups().iter().copied());
let target_modules = artifact.chunk_graph.get_chunk_modules_identifier(&target);
for module in target_modules {
let mut before = placed.get(module).map_or_default(consumers);
before.extend(target_chunk.groups().iter().copied());
if before != after {
return false;
}
}
match placed {
ModuleChunkMap::Shared { modules, chunks } => {
(!target_modules.is_empty() || !modules.is_empty())
&& (modules.is_subset(target_modules) || consumers(chunks) == after)
}
ModuleChunkMap::ByModule(modules) => {
(!target_modules.is_empty() || !modules.is_empty())
&& modules
.iter()
.all(|(module, chunks)| target_modules.contains(module) || consumers(chunks) == after)
}
}
}