Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove dynamic entry should re-build chunk graph #9425

Merged
merged 3 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions crates/rspack_core/src/build_chunk_graph/incremental.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{collections::HashSet, hash::BuildHasherDefault};

use num_bigint::BigUint;
use rspack_collections::{IdentifierHasher, IdentifierIndexSet, IdentifierMap, UkeySet};
use rspack_collections::{
IdentifierHasher, IdentifierIndexSet, IdentifierMap, IdentifierSet, UkeySet,
};
use rspack_error::Result;
use tracing::instrument;

Expand Down Expand Up @@ -443,26 +445,51 @@ impl CodeSplitter {
self.stat_cache_miss_by_available_modules = 0;
self.stat_cache_miss_by_cant_rebuild = 0;

let modules = if let Some(mutations) = compilation
let (affected_modules, removed_modules) = if let Some(mutations) = compilation
.incremental
.mutations_read(IncrementalPasses::BUILD_CHUNK_GRAPH)
{
mutations.get_affected_modules_with_module_graph(&module_graph)
let affected_modules = mutations.get_affected_modules_with_module_graph(&module_graph);
let removed_modules: IdentifierSet = mutations
.iter()
.filter_map(|mutation| match mutation {
Mutation::ModuleRemove { module } => Some(*module),
_ => None,
})
.collect();
(affected_modules, removed_modules)
} else {
compilation
.get_module_graph()
.modules()
.keys()
.copied()
.collect()
(
compilation
.get_module_graph()
.modules()
.keys()
.copied()
.collect(),
Default::default(),
)
};

let mut edges = vec![];

// collect invalidate caches before we do anything to the chunk graph
let dirty_blocks = self.collect_dirty_caches(compilation, modules.iter().copied());
let dirty_blocks = self.collect_dirty_caches(
compilation,
affected_modules
.iter()
.chain(removed_modules.iter())
.copied(),
);

for m in removed_modules {
for module_map in self.block_modules_runtime_map.values_mut() {
module_map.swap_remove(&DependenciesBlockIdentifier::Module(m));
}

for m in modules {
self.invalidate_from_module(m, compilation)?;
}

for m in affected_modules {
for module_map in self.block_modules_runtime_map.values_mut() {
module_map.swap_remove(&DependenciesBlockIdentifier::Module(m));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ impl HasModuleGraphChange {
if self.disabled {
return;
}
if artifact.built_modules.len() != self.expect_built_modules_len {
if artifact.built_modules.len() != self.expect_built_modules_len
|| artifact.built_modules.len() != artifact.revoked_modules.len()
{
// contain unexpected module built
artifact.has_module_graph_change = true;
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
it("should have correct entrypoints", function() {
expect(Object.keys(__STATS__.entrypoints)).toEqual(["bundle0", "bundle1"]);

const index0 = __STATS__.modules.find(m => m.name === "./index0.js");
expect(index0.built).toBe(true);
expect(index0.reasons.length).toBe(1);
expect(index0.reasons[0].type).toBe("entry");

const index1 = __STATS__.modules.find(m => m.name === "./index1.js");
expect(index1.built).toBe(true);
expect(index1.reasons.length).toBe(1);
expect(index1.reasons[0].type).toBe("entry");
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log.bind(console, 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
it("should have correct entrypoints", function() {
expect(Object.keys(__STATS__.entrypoints)).toEqual(["bundle0"]);

const index0 = __STATS__.modules.find(m => m.name === "./index0.js");
expect(index0.built).toBe(true);
expect(index0.reasons.length).toBe(1);
expect(index0.reasons[0].type).toBe("entry");
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const path = require("path");
const fs = require("fs");

let compiler;
let step = 0;
let entries;

/** @type {import("@rspack/core").Configuration} */
module.exports = {
entry: async () => {
const context = compiler.context;
if (step === 0) {
const files = await fs.promises.readdir(context);
entries = files.filter(f => f.startsWith("index"));
entries.sort();
} else if (step === 1) {
entries.pop();
} else {
throw new Error(`unreachable step: ${step}`);
}
return entries.reduce((acc, e, i) => {
acc[`bundle${i}`] = path.resolve(context, e);
return acc;
}, {});
},
output: {
filename: "[name].js"
},
plugins: [
function (c) {
compiler = c;
c.hooks.done.tap("test", () => {
step += 1;
});
}
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
findBundle(i) {
return ["bundle0.js"];
}
};
Loading