Skip to content

Commit

Permalink
feat: support compiler.traceModuleGraph (#1654)
Browse files Browse the repository at this point in the history
  • Loading branch information
wre232114 authored Jul 22, 2024
1 parent a04d33f commit 0483fd9
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-pandas-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/core": patch
---

Support compiler.traceModuleGraph
1 change: 1 addition & 0 deletions crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub use farmfe_plugin_runtime::RUNTIME_SUFFIX;

pub mod build;
pub mod generate;
pub mod trace_module_graph;
pub mod update;

pub struct Compiler {
Expand Down
78 changes: 78 additions & 0 deletions crates/compiler/src/trace_module_graph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::collections::HashMap;

use farmfe_core::serde::{Deserialize, Serialize};

use crate::Compiler;

#[derive(Debug, Serialize, Deserialize)]
#[serde(crate = "farmfe_core::serde", rename_all = "camelCase")]
pub struct TracedModule {
pub id: String,
pub content_hash: String,
pub package_name: String,
pub package_version: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(crate = "farmfe_core::serde", rename_all = "camelCase")]
pub struct TracedModuleGraph {
pub root: String,
pub modules: Vec<TracedModule>,
pub edges: HashMap<String, Vec<String>>,
pub reverse_edges: HashMap<String, Vec<String>>,
}

impl TracedModuleGraph {
pub fn new(root: String) -> Self {
Self {
root,
modules: vec![],
edges: HashMap::new(),
reverse_edges: HashMap::new(),
}
}

pub fn add_module(&mut self, module: TracedModule) {
self.modules.push(module);
}

pub fn add_edge(&mut self, from: String, to: String) {
self.edges.entry(from.clone()).or_default().push(to.clone());
self.reverse_edges.entry(to).or_default().push(from);
}
}

impl Compiler {
pub fn trace_module_graph(&self) -> farmfe_core::error::Result<TracedModuleGraph> {
self.build()?;

let mut graph = TracedModuleGraph::new(self.context.config.root.clone());
let module_graph = self.context.module_graph.read();

for module in module_graph.modules() {
let t_module = TracedModule {
id: module.id.to_string(),
content_hash: module.content_hash.to_string(),
package_name: module.package_name.to_string(),
package_version: module.package_version.to_string(),
};
graph.add_module(t_module);
}

let watch_graph = self.context.watch_graph.read();

for module in module_graph.modules() {
let deps = module_graph.dependencies(&module.id);

for (dep, _) in deps {
graph.add_edge(module.id.to_string(), dep.to_string());
}
// watch graph added by scss, less, etc.
for dep in watch_graph.dependencies(&module.id) {
graph.add_edge(module.id.to_string(), dep.to_string());
}
}

Ok(graph)
}
}
10 changes: 10 additions & 0 deletions crates/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ impl JsCompiler {
.map_err(|e| napi::Error::new(Status::GenericFailure, format!("{}", e)))
}

#[napi]
pub fn trace_module_graph(&self, e: Env) -> napi::Result<JsUnknown> {
let graph = self
.compiler
.trace_module_graph()
.map_err(|e| napi::Error::new(Status::GenericFailure, format!("{}", e)))?;

e.to_js_value(&graph)
}

/// async compile, return promise
#[napi]
pub fn compile(&self, e: Env) -> napi::Result<JsObject> {
Expand Down
1 change: 1 addition & 0 deletions packages/core/binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export type JsCompiler = Compiler
export declare class Compiler {
constructor(config: object)
traceDependencies(): Array<string>
traceModuleGraph(): unknown
/** async compile, return promise */
compile(): object
/** sync compile */
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ export type PluginStats = Record<
>
>;

export interface TracedModuleGraph {
root: string;
modules: Array<{
id: string;
contentHash: string;
packageName: string;
packageVersion: string;
}>;
edges: Record<string, string[]>;
reverseEdges: Record<string, string[]>;
}

export class Compiler {
private _bindingCompiler: BindingCompiler;
private _updateQueue: UpdateQueueItem[] = [];
Expand All @@ -47,6 +59,10 @@ export class Compiler {
return this._bindingCompiler.traceDependencies();
}

async traceModuleGraph(): Promise<TracedModuleGraph> {
return this._bindingCompiler.traceModuleGraph() as TracedModuleGraph;
}

async compile() {
if (this.compiling) {
this.logger.error('Already compiling', {
Expand Down

0 comments on commit 0483fd9

Please sign in to comment.