Skip to content

Commit

Permalink
fix: rebuild should reset module.preOrderIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Nov 12, 2024
1 parent 17e1e38 commit 05dcb63
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 6 deletions.
34 changes: 31 additions & 3 deletions crates/rspack_core/src/old_cache/local/code_splitting_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tracing::instrument;

use crate::{
build_chunk_graph::code_splitter::CodeSplitter, incremental::IncrementalPasses, Chunk,
ChunkGraph, ChunkGroup, ChunkGroupUkey, ChunkUkey, Compilation,
ChunkGraph, ChunkGroup, ChunkGroupUkey, ChunkUkey, Compilation, ModuleIdentifier,
};

#[derive(Debug, Default)]
Expand All @@ -20,6 +20,7 @@ pub struct CodeSplittingCache {
named_chunk_groups: HashMap<String, ChunkGroupUkey>,
named_chunks: HashMap<String, ChunkUkey>,
pub(crate) code_splitter: CodeSplitter,
pub(crate) module_order_index: HashMap<ModuleIdentifier, (u32, u32)>,
}

#[instrument(skip_all)]
Expand All @@ -39,13 +40,15 @@ where
return Ok(());
}

// take to escape from borrow checker
let mut cache = std::mem::take(&mut compilation.code_splitting_cache);

let has_change = compilation.has_module_import_export_change();
if !has_change
|| compilation
.incremental
.can_read_mutations(IncrementalPasses::BUILD_CHUNK_GRAPH)
{
let cache = &mut compilation.code_splitting_cache;
rayon::scope(|s| {
s.spawn(|_| compilation.chunk_by_ukey = cache.chunk_by_ukey.clone());
s.spawn(|_| compilation.chunk_graph = cache.chunk_graph.clone());
Expand All @@ -56,13 +59,23 @@ where
s.spawn(|_| compilation.named_chunks = cache.named_chunks.clone());
});

// recover mgm.pre_order_index
let mut module_graph = compilation.get_module_graph_mut();
for (m, (pre_idx, post_idx)) in &cache.module_order_index {
let Some(mgm) = module_graph.module_graph_module_by_identifier_mut(m) else {
continue;
};
mgm.pre_order_index = Some(*pre_idx);
mgm.post_order_index = Some(*post_idx);
}

if !has_change {
compilation.code_splitting_cache = cache;
return Ok(());
}
}

let compilation = task(compilation).await?;
let cache = &mut compilation.code_splitting_cache;
rayon::scope(|s| {
s.spawn(|_| cache.chunk_by_ukey = compilation.chunk_by_ukey.clone());
s.spawn(|_| cache.chunk_graph = compilation.chunk_graph.clone());
Expand All @@ -72,5 +85,20 @@ where
s.spawn(|_| cache.named_chunk_groups = compilation.named_chunk_groups.clone());
s.spawn(|_| cache.named_chunks = compilation.named_chunks.clone());
});

let module_graph = compilation.get_module_graph();
for id in module_graph.modules().keys() {
let Some(mgm) = module_graph.module_graph_module_by_identifier(id) else {
continue;
};

let (Some(pre_idx), Some(post_idx)) = (mgm.pre_order_index, mgm.post_order_index) else {
continue;
};

cache.module_order_index.insert(*id, (pre_idx, post_idx));
}

compilation.code_splitting_cache = cache;
Ok(())
}
4 changes: 1 addition & 3 deletions crates/rspack_ids/src/deterministic_module_ids_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ fn module_ids(&self, compilation: &mut Compilation) -> Result<()> {
|m| get_full_module_name(m, context),
|a, b| compare_modules_by_pre_order_index_or_identifier(&module_graph, a, b),
|module, id| {
let size = used_ids.len();
used_ids.insert(id.to_string());
if used_ids.len() == size {
if !used_ids.insert(id.to_string()) {
conflicts += 1;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import v from './lib'

it('should compile', async () => {
expect(v).toBe(1)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// change
export default 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
optimization: {
splitChunks: false,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
checkStats(_stepName, stats) {
// should keep module preOrderIndex and postOrderIndex during build
return [...stats.modules].filter(m => {
return m.moduleType !== 'runtime'
}).every((m) => {
return m.preOrderIndex !== undefined && m.postOrderIndex !== undefined
})
}
}

0 comments on commit 05dcb63

Please sign in to comment.