Skip to content

Commit 6b5443d

Browse files
committed
debugging
1 parent edae5d5 commit 6b5443d

File tree

11 files changed

+77
-69
lines changed

11 files changed

+77
-69
lines changed

packages/mach/src/cmd/build/build.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::core::packaging::package;
1212
use crate::core::plugins::load_plugins;
1313
use crate::core::resolve_and_transform::resolve_and_transform;
1414
use crate::types::Compilation;
15+
use crate::types::DebugAssetGraphOptions;
1516
use crate::types::MachConfig;
1617

1718
#[derive(Debug)]
@@ -63,7 +64,7 @@ pub fn build(
6364

6465
// This will resolve imports, transform files and build the AssetGraph.
6566
resolve_and_transform(&mut c)?;
66-
emit_file(&c, "asset_graph.dot", c.debug_asset_graph_dot())?;
67+
emit_file(&c, "asset_graph.dot", c.debug_asset_graph_dot(DebugAssetGraphOptions{ show_specifiers: false }))?;
6768

6869
// This will read the asset graph and organize related assets into groupings (a.k.a bundles)
6970
bundle(&mut c)?;

packages/mach/src/types/debug.rs

+52-66
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,72 @@
11
use std::collections::HashSet;
22

3-
use petgraph::dot::Config;
4-
use petgraph::dot::Dot;
3+
use petgraph::visit::EdgeRef;
4+
use petgraph::visit::NodeRef;
55

6-
use crate::core::config::ROOT_ASSET;
76
use crate::types::DependencyPriority;
87

98
use super::Compilation;
109

10+
#[derive(Debug, Default)]
11+
pub struct DebugAssetGraphOptions {
12+
pub show_specifiers: bool,
13+
}
14+
1115
impl Compilation {
12-
pub fn debug_asset_graph_dot(&self) -> String {
16+
pub fn debug_asset_graph_dot(&self, DebugAssetGraphOptions{ show_specifiers }: DebugAssetGraphOptions) -> String {
17+
let mut output = "digraph {\n".to_string();
18+
output += "graph [shape=box];\n";
19+
output += "node [shape=box];\n";
20+
1321
let asset_graph = self.asset_graph.as_graph();
14-
let dot = Dot::with_attr_getters(
15-
&asset_graph,
16-
&[Config::EdgeNoLabel, Config::NodeNoLabel],
17-
&|_, edge_ref| -> String {
18-
let dependency = edge_ref.weight();
19-
let mut label = String::new();
20-
21-
let mut specifier = dependency.specifier.clone();
22-
if dependency.specifier.starts_with("/") || dependency.specifier.starts_with("\\") {
23-
specifier = format!("");
24-
}
22+
let root_nx = self.asset_graph.root_node();
23+
24+
for asset_nx in asset_graph.node_indices() {
25+
let asset = self.asset_graph.get_with_nx(asset_nx).unwrap();
26+
output += &format!("{} [label=\"", asset.id.0);
27+
28+
if asset_nx == root_nx {
29+
output += &format!("Asset Graph\"]\n");
30+
continue;
31+
}
2532

26-
label += &format!("label=\" {}\" ", specifier);
33+
output += &format!("{}\"]\n", asset.file_path.file_name().unwrap().to_str().unwrap());
34+
}
35+
36+
let mut nodes = vec![self.bundle_graph.root_node()];
37+
let mut completed = HashSet::new();
38+
39+
while let Some(current_nx) = nodes.pop() {
40+
let current_asset = self.asset_graph.get_with_nx(current_nx.clone()).unwrap();
41+
42+
for next_ref in self.asset_graph.get_dependencies(&current_nx) {
43+
let next_nx = next_ref.target().id();
44+
let next_asset = self.asset_graph.get_with_nx(next_nx.clone()).unwrap();
45+
let dependency = next_ref.weight();
46+
47+
output += &format!("{} -> {} [", current_asset.id.0, next_asset.id.0);
2748

2849
if let DependencyPriority::Lazy = dependency.priority {
29-
label += &format!("; style = \"dashed\" ")
50+
output += &format!("style=\"dashed\"");
51+
}
52+
53+
if show_specifiers && current_nx != root_nx {
54+
output += &format!("label=\"{}\"", dependency.specifier);
3055
}
3156

32-
label
33-
},
34-
&|_, (_, asset)| {
35-
if asset.id == ROOT_ASSET.id {
36-
return format!("shape=box label=\"Asset Graph\" ");
57+
output += &format!("]\n");
58+
59+
60+
if !completed.contains(&next_nx) {
61+
nodes.push(next_nx);
3762
}
63+
}
3864

39-
let mut label = String::new();
40-
label += &format!("[{}] ", asset.id.0);
41-
label += &asset.file_path.to_str().unwrap().to_string();
65+
completed.insert(current_nx);
66+
}
4267

43-
format!("shape=box label=\"{}\" ", label)
44-
},
45-
);
46-
format!("{:?}", dot)
68+
output += "}";
69+
output
4770
}
4871

4972
pub fn debug_bundle_graph_dot(&self) -> String {
@@ -105,40 +128,3 @@ impl Compilation {
105128
output
106129
}
107130
}
108-
109-
/*
110-
111-
digraph G {
112-
graph [shape=box fontsize=10 fontname="monospace"];
113-
node [shape=box fontsize=10 fontname="monospace", margin="0.2,0" height=0];
114-
115-
subgraph cluster_0 {
116-
label = "index.html";
117-
graph [ranksep="0.02"];
118-
edge[style=invis];
119-
0 [label="src/index.html"]
120-
}
121-
122-
subgraph cluster_1 {
123-
label = "index.css";
124-
graph [ranksep="0.02"];
125-
edge[style=invis];
126-
1 [label="src/index.css"]
127-
}
128-
129-
130-
subgraph cluster_2 {
131-
label = "index.js";
132-
graph [ranksep="0.02"];
133-
edge[style=invis];
134-
2[label="src/index.js"]
135-
3[label="src/a.js"]
136-
2 -> 3
137-
}
138-
139-
0 -> 1 [style = "dashed" color=grey]
140-
0 -> 2 [style = "dashed" color=grey]
141-
}
142-
143-
144-
*/

testing/fixtures/js-dynamic/a.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
await import('./d.js')
2+
await import('./e.js')

testing/fixtures/js-dynamic/b.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
await import('./d.js')
2+
await import('./e.js')

testing/fixtures/js-dynamic/c.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
await import('./d.js')
2+
await import('./e.js')

testing/fixtures/js-dynamic/d.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './f.js'

testing/fixtures/js-dynamic/e.js

Whitespace-only changes.

testing/fixtures/js-dynamic/f.js

Whitespace-only changes.

testing/fixtures/js-dynamic/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './a.js'
2+
import './b.js'
3+
import './c.js'
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@workspace/fixture-js-dynamic",
3+
"scripts": {
4+
"build": "npx mach build"
5+
},
6+
"devDependencies": {
7+
"@alshdavid/mach": "*"
8+
}
9+
}

testing/setup.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ console.log(Mach)
44

55
// const FIXTURE = 'js-css'
66
// const FIXTURE = 'js-css-mutli-entry'
7-
const FIXTURE = 'html-js-css-2'
7+
// const FIXTURE = 'html-js-css-2'
8+
const FIXTURE = 'js-dynamic'
89

910
const mach = new Mach({
1011
threads: 16,
1112
nodeWorkers: 4,
1213
entries: [
13-
`/home/dalsh/Development/alshdavid/mach/testing/fixtures/${FIXTURE}/src/index.html`,
14+
`/home/dalsh/Development/alshdavid/mach/testing/fixtures/${FIXTURE}/index.js`,
15+
// `/home/dalsh/Development/alshdavid/mach/testing/fixtures/${FIXTURE}/src/index.html`,
1416
// `/home/dalsh/Development/alshdavid/mach/testing/fixtures/${FIXTURE}/src/index2.html`
1517
],
1618
env: {},

0 commit comments

Comments
 (0)