Skip to content

Commit

Permalink
Merge branch 'refactor/node' of github.com:farm-fe/farm into refactor…
Browse files Browse the repository at this point in the history
…/node
  • Loading branch information
ErKeLost committed Oct 8, 2024
2 parents f31203a + 2648325 commit e8a06f3
Show file tree
Hide file tree
Showing 39 changed files with 944 additions and 160 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-drinks-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/js-plugin-visualizer": patch
---

Optimize filters for Plugin Analysis
4 changes: 0 additions & 4 deletions .changeset/hip-shoes-fix.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/short-roses-dream.md

This file was deleted.

8 changes: 8 additions & 0 deletions bench/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# bench

## 1.0.13

### Patch Changes

- Updated dependencies [772381b0]
- Updated dependencies [732c046d]
- @farmfe/core@1.3.24

## 1.0.12

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion bench/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bench",
"version": "1.0.12",
"version": "1.0.13",
"private": true,
"description": "",
"scripts": {},
Expand Down
41 changes: 34 additions & 7 deletions crates/compiler/src/update/diff_and_patch_module_graph.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
//! diff the module_graph and update_module_graph, analyze the changes and then patch the module_graph
use std::collections::{HashMap, HashSet, VecDeque};
use std::{
cmp::Ordering,
collections::{HashMap, HashSet, VecDeque},
};

use farmfe_core::module::{
module_graph::{ModuleGraph, ModuleGraphEdge},
Module, ModuleId,
use farmfe_core::{
module::{
module_graph::{ModuleGraph, ModuleGraphEdge},
Module, ModuleId,
},
serde::Serialize,
};

/// the diff result of a module's dependencies
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize)]
#[serde(rename_all = "camelCase", crate = "farmfe_core::serde")]
pub struct ModuleDepsDiffResult {
/// added dependencies
pub added: Vec<(ModuleId, ModuleGraphEdge)>,
Expand All @@ -19,12 +26,14 @@ pub struct ModuleDepsDiffResult {
pub type ModuleDepsDiffResultMap = Vec<(ModuleId, ModuleDepsDiffResult)>;
/// the diff result of a module, this records all related changes of the module graph
/// for example, deeply added or removed dependencies also be recorded here
#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, Serialize)]
#[serde(rename_all = "camelCase", crate = "farmfe_core::serde")]
pub struct DiffResult {
pub deps_changes: ModuleDepsDiffResultMap,
pub added_modules: HashSet<ModuleId>,
pub removed_modules: HashSet<ModuleId>,
}

#[cfg(test)]
impl DiffResult {
pub fn readable_print(&self) {
Expand Down Expand Up @@ -91,9 +100,27 @@ pub fn diff_module_graph(
removed_modules: HashSet::new(),
};

let (diff_result, added_modules, remove_modules) =
let (mut diff_result, added_modules, remove_modules) =
diff_module_deps(&start_points, module_graph, update_module_graph);

// sort diff_result.deps_changes in topological order
diff_result.sort_by(|a, b| {
let a_module_id = &a.0;
let b_module_id = &b.0;

let a_module = update_module_graph.module(a_module_id);
let b_module = update_module_graph.module(b_module_id);

if a_module.is_none() || b_module.is_none() {
return Ordering::Equal;
}
// if a import b, then execution_order of a should be greater than b. so we need to sort them in reverse order
b_module
.unwrap()
.execution_order
.cmp(&a_module.unwrap().execution_order)
});

res.deps_changes.extend(diff_result);
res.added_modules.extend(added_modules);
res.removed_modules.extend(remove_modules);
Expand Down
76 changes: 75 additions & 1 deletion crates/compiler/src/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use farmfe_core::{
module::{module_graph::ModuleGraphEdgeDataItem, module_group::ModuleGroupId, Module, ModuleId},
plugin::{PluginResolveHookParam, ResolveKind, UpdateResult, UpdateType},
resource::ResourceType,
serde_json::json,
serde::Serialize,
serde_json::{self, json},
stats::CompilationPluginHookStats,
};

use farmfe_toolkit::get_dynamic_resources_map::get_dynamic_resources_map;
Expand Down Expand Up @@ -82,6 +84,58 @@ impl Compiler {
}
}

fn set_module_group_graph_stats(&self) {
if self.context.config.record {
let module_group_graph = self.context.module_group_graph.read();
self
.context
.record_manager
.add_plugin_hook_stats(CompilationPluginHookStats {
plugin_name: "HmrUpdate".to_string(),
hook_name: "analyze_module_graph".to_string(),
hook_context: None,
module_id: "".into(),
input: "".to_string(),
output: serde_json::to_string(&module_group_graph.print_graph()).unwrap(),
duration: 0,
start_time: 0,
end_time: 0,
})
}
}

fn set_hmr_diff_stats(
&self,
removed_modules: &HashMap<ModuleId, Module>,
affected_module_groups: &HashSet<ModuleGroupId>,
diff_result: &DiffResult,
start_points: &Vec<ModuleId>,
) {
// record diff and patch result
if self.context.config.record {
self
.context
.record_manager
.add_plugin_hook_stats(CompilationPluginHookStats {
plugin_name: "HmrUpdate".to_string(),
hook_name: "diffAndPatchContext".to_string(),
module_id: "".into(),
hook_context: None,
input: "".to_string(),
output: serde_json::to_string(&PrintedDiffAndPatchContext {
removed_modules: removed_modules.iter().map(|m| m.0.clone()).collect(),
affected_module_groups: affected_module_groups.clone(),
diff_result: diff_result.clone(),
start_points: start_points.clone(),
})
.unwrap_or_default(),
duration: 0,
start_time: 0,
end_time: 0,
});
}
}

pub fn update<F>(
&self,
paths: Vec<(String, UpdateType)>,
Expand Down Expand Up @@ -202,6 +256,8 @@ impl Compiler {

let (affected_module_groups, updated_module_ids, diff_result, removed_modules) =
self.diff_and_patch_context(paths, &update_context);
// record graph patch result
self.set_module_group_graph_stats();

// update cache
set_updated_modules_cache(&updated_module_ids, &diff_result, &self.context);
Expand Down Expand Up @@ -515,6 +571,7 @@ impl Compiler {
.collect();
let mut module_graph = self.context.module_graph.write();
let mut update_module_graph = update_context.module_graph.write();
update_module_graph.update_execution_order_for_modules();

let diff_result = diff_module_graph(start_points.clone(), &module_graph, &update_module_graph);

Expand All @@ -535,6 +592,14 @@ impl Compiler {
&mut module_group_graph,
);

// record diff and patch result
self.set_hmr_diff_stats(
&removed_modules,
&affected_module_groups,
&diff_result,
&start_points,
);

(
affected_module_groups,
start_points,
Expand Down Expand Up @@ -715,3 +780,12 @@ fn resolve_module(
resolve_module_id_result,
})))
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase", crate = "farmfe_core::serde")]
struct PrintedDiffAndPatchContext {
affected_module_groups: HashSet<ModuleGroupId>,
start_points: Vec<ModuleId>,
diff_result: DiffResult,
removed_modules: HashSet<ModuleId>,
}
4 changes: 2 additions & 2 deletions crates/core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ pub enum CompilationError {
},

// #[error("Transform `{resolved_path}` failed.\n {msg}")]
#[error("{}", serde_json::to_string(&serialize_transform_error(&resolved_path, &msg))
#[error("{}", serde_json::to_string(&serialize_transform_error(resolved_path, msg))
.map_err(|_| "Failed to serialize transform error type message".to_string())
.unwrap_or_else(|e| e))]
TransformError { resolved_path: String, msg: String },

// TODO, give the specific recommended plugin of this kind of module
// #[error("Parse `{resolved_path}` failed.\n {msg}\nPotential Causes:\n1.The module have syntax error.\n2.This kind of module is not supported, you may need plugins to support it\n")]
#[error("{}", serde_json::to_string(&serialize_parse_error(&resolved_path, &msg))
#[error("{}", serde_json::to_string(&serialize_parse_error(resolved_path, msg))
.map_err(|_| "Failed to serialize parse error type message".to_string())
.unwrap_or_else(|e| e))]
ParseError { resolved_path: String, msg: String },
Expand Down
32 changes: 18 additions & 14 deletions crates/core/src/module/module_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use petgraph::{
stable_graph::{DefaultIx, NodeIndex, StableDiGraph},
visit::{Bfs, Dfs, DfsPostOrder, EdgeRef, IntoEdgeReferences},
};
use serde::{Deserialize, Serialize};

use crate::resource::{resource_pot::ResourcePotId, resource_pot_map::ResourcePotMap};

Expand Down Expand Up @@ -199,25 +200,32 @@ impl ModuleGroupGraph {
sorted
}

pub fn print_graph(&self) {
println!("digraph {{\n nodes:");
pub fn print_graph(&self) -> PrintedModuleGroupGraph {
let mut graph = PrintedModuleGroupGraph {
module_groups: Vec::new(),
edges: Vec::new(),
};

for node in self.g.node_weights() {
println!(" \"{}\";", node.id.to_string());
for module_group in self.module_groups() {
graph.module_groups.push(module_group.clone());
}

println!("\nedges:");

for edge in self.g.edge_references() {
let source = self.g[edge.source()].id.to_string();
let target = self.g[edge.target()].id.to_string();
println!(" \"{}\" -> \"{}\";", source, target);
graph.edges.push((source, target));
}

println!("}}");
graph
}
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct PrintedModuleGroupGraph {
pub module_groups: Vec<ModuleGroup>,
pub edges: Vec<(String, String)>,
}

impl Default for ModuleGroupGraph {
fn default() -> Self {
Self::new()
Expand Down Expand Up @@ -264,7 +272,7 @@ impl Eq for ModuleGroupGraph {}

pub type ModuleGroupId = ModuleId;

#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct ModuleGroup {
/// the module group's id is the same as its entry module's id.
pub id: ModuleGroupId,
Expand Down Expand Up @@ -313,11 +321,7 @@ impl ModuleGroup {
resource_pot_map: &ResourcePotMap,
) -> Vec<ResourcePotId> {
let mut resource_pots_order_map = HashMap::<String, usize>::new();
let mut sorted_resource_pots = self
.resource_pots()
.iter()
.cloned()
.collect::<Vec<_>>();
let mut sorted_resource_pots = self.resource_pots().iter().cloned().collect::<Vec<_>>();

sorted_resource_pots.iter().for_each(|rp| {
let rp = resource_pot_map.resource_pot(rp).unwrap();
Expand Down
24 changes: 24 additions & 0 deletions crates/core/src/plugin/plugin_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,30 @@ impl PluginDriver {
hook_first!(
analyze_module_graph,
Result<Option<ModuleGroupGraph>>,
|result: & Option<ModuleGroupGraph>,
plugin_name: String,
start_time: u128,
end_time: u128,
_param: &mut ModuleGraph,
context: &Arc<CompilationContext>,
hook_context: &PluginHookContext| {
if result.is_none() {
return;
}
context.record_manager.add_plugin_hook_stats(
CompilationPluginHookStats {
plugin_name: plugin_name.to_string(),
hook_name: "analyze_module_graph".to_string(),
hook_context: Some(hook_context.clone()),
module_id: "".into(),
input: "".to_string(),
output: serde_json::to_string(&result.as_ref().unwrap().print_graph()).unwrap(),
duration: end_time - start_time,
start_time,
end_time,
},
)
},
param: &mut ModuleGraph,
context: &Arc<CompilationContext>,
_hook_context: &PluginHookContext
Expand Down
11 changes: 11 additions & 0 deletions crates/core/src/stats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ impl Stats {
pub fn set_module_graph_stats(&self, module_graph: &ModuleGraph) {
handle_compilation_stats!(self, |compilation_stats: &mut CompilationStats| {
compilation_stats.set_module_graph_stats(module_graph);
compilation_stats.add_plugin_hook_stats(CompilationPluginHookStats {
plugin_name: "INTERNAL_COMPILER".to_string(),
hook_name: "build_end".to_string(),
module_id: "".into(),
hook_context: None,
input: "".to_string(),
output: serde_json::to_string(&compilation_stats.module_graph_stats).unwrap(),
duration: 0,
start_time: 0,
end_time: 0,
});
})
}

Expand Down
Loading

0 comments on commit e8a06f3

Please sign in to comment.