Skip to content

Commit 98a2b01

Browse files
authored
fix(hmr): panic when call compiler.update for a full new module multi… (#2120)
fix(hmr): panic when call compiler.update for a full new module multiple times
1 parent 350daea commit 98a2b01

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

.changeset/curvy-buttons-hug.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@farmfe/core": patch
3+
---
4+
5+
Fix panic when call `compiler.update` for a full new module multiple times

crates/compiler/src/update/diff_and_patch_module_graph.rs

+7
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ pub fn patch_module_graph(
154154

155155
// add new modules first, as we need to add edges to them later
156156
for added in &diff_result.added_modules {
157+
// if the added module is a start point, we should mark it as a new entry of the module graph
158+
if start_points.contains(added) {
159+
module_graph
160+
.entries
161+
.insert(added.clone(), added.to_string());
162+
}
163+
157164
let module = update_module_graph.take_module(added);
158165
module_graph.add_module(module);
159166
}

crates/compiler/src/update/patch_module_group_graph.rs

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use farmfe_core::module::{
55
module_group::{ModuleGroup, ModuleGroupGraph},
66
Module, ModuleId,
77
};
8+
use farmfe_plugin_partial_bundling::module_group_graph_from_entries;
89

910
use super::diff_and_patch_module_graph::DiffResult;
1011

@@ -18,6 +19,32 @@ pub fn patch_module_group_graph(
1819
let mut affected_module_groups = HashSet::new();
1920

2021
for updated_module_id in &updated_module_ids {
22+
// create new module group for the new entry
23+
if diff_result.added_modules.contains(updated_module_id)
24+
&& module_graph.entries.contains_key(updated_module_id)
25+
{
26+
let mut added_module_group_graph =
27+
module_group_graph_from_entries(&vec![updated_module_id.clone()], module_graph);
28+
let edges = added_module_group_graph.edges();
29+
let module_group_ids = added_module_group_graph
30+
.module_groups()
31+
.into_iter()
32+
.map(|g| g.id.clone())
33+
.collect::<Vec<_>>();
34+
35+
for module_group_id in module_group_ids {
36+
if !module_group_graph.has(&module_group_id) {
37+
let module_group = added_module_group_graph
38+
.remove_module_group(&module_group_id)
39+
.unwrap();
40+
module_group_graph.add_module_group(module_group);
41+
}
42+
}
43+
44+
for (from, to) in edges {
45+
module_group_graph.add_edge(&from, &to);
46+
}
47+
}
2148
let module = module_graph.module(updated_module_id).unwrap();
2249
let module_group_ids = module.module_groups.clone();
2350
affected_module_groups.extend(module_group_ids);

crates/core/src/module/module_group.rs

+13
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ impl ModuleGroupGraph {
9292
self.g.node_weights().collect()
9393
}
9494

95+
pub fn edges(&self) -> Vec<(ModuleGroupId, ModuleGroupId)> {
96+
self
97+
.g
98+
.edge_references()
99+
.map(|edge| {
100+
let source = self.g[edge.source()].id.clone();
101+
let target = self.g[edge.target()].id.clone();
102+
103+
(source, target)
104+
})
105+
.collect()
106+
}
107+
95108
/// the same as [ModuleGroupGraph::module_groups], but mutable.
96109
pub fn module_groups_mut(&mut self) -> Vec<&mut ModuleGroup> {
97110
self.g.node_weights_mut().collect()

0 commit comments

Comments
 (0)