Skip to content

Commit 25f3d3f

Browse files
authored
feat(hmr): support calling compiler.update to compile a full new module (#2119)
1 parent 7e4a256 commit 25f3d3f

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

.changeset/loud-tigers-bathe.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@farmfe/core": minor
3+
---
4+
5+
Support calling `compiler.update` to compile a full new module dynamically

crates/compiler/src/update/diff_and_patch_module_graph.rs

+27
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ pub fn patch_module_graph(
170170

171171
// we must remove updated module at last, cause petgraph will remove edge when remove node
172172
for updated in start_points {
173+
// the start point module may be a full new module that is handled by added_modules already
174+
// we should skip it if it is in added_modules
175+
if diff_result.added_modules.contains(&updated) {
176+
continue;
177+
}
178+
173179
let module = {
174180
let mut m = update_module_graph.take_module(&updated);
175181
let previous_module = module_graph.module(&updated).unwrap();
@@ -260,6 +266,27 @@ fn diff_module_deps(
260266
let mut added_deps = Vec::new();
261267
let mut removed_deps = Vec::new();
262268

269+
// if entry module_id is not in module_graph, it means it is a new separate entry module
270+
if !module_graph.has_module(module_id) {
271+
let update_deps = update_module_graph.dependencies_ids(module_id);
272+
273+
for dep in update_deps {
274+
let edge_info = update_module_graph.edge_info(module_id, &dep).unwrap();
275+
added_deps.push((dep, edge_info.clone()));
276+
}
277+
278+
diff_result.push((
279+
module_id.clone(),
280+
ModuleDepsDiffResult {
281+
added: added_deps.clone(),
282+
removed: removed_deps.clone(),
283+
},
284+
));
285+
286+
added_modules.insert(module_id.clone());
287+
continue;
288+
}
289+
263290
let deps = module_graph.dependencies_ids(module_id);
264291
let update_deps = update_module_graph.dependencies_ids(module_id);
265292

crates/compiler/src/update/handle_update_modules.rs

-8
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ pub fn handle_update_modules(
8585
let paths = [paths, additional_paths]
8686
.concat()
8787
.into_iter()
88-
.filter(|(p, _)| {
89-
let id = ModuleId::from_resolved_path_with_query(p, &context.config.root);
90-
module_graph.has_module(&id)
91-
})
9288
.collect::<Vec<_>>();
9389

9490
// group the paths by same resolved_path
@@ -145,10 +141,6 @@ pub fn handle_update_modules(
145141

146142
let result: Vec<(String, UpdateType)> = filtered_paths
147143
.into_iter()
148-
.filter(|p| {
149-
let id = ModuleId::from_resolved_path_with_query(p, &context.config.root);
150-
module_graph.has_module(&id)
151-
})
152144
.map(|p| {
153145
if let Some((_, ty)) = paths.iter().find(|(pp, _)| *pp == p) {
154146
(p, ty.clone())

crates/compiler/src/update/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,10 @@ impl Compiler {
584584

585585
(
586586
affected_module_groups,
587-
start_points,
587+
start_points
588+
.into_iter()
589+
.filter(|id| !diff_result.added_modules.contains(id))
590+
.collect(), // remove added modules from start_points
588591
diff_result,
589592
removed_modules,
590593
)

0 commit comments

Comments
 (0)