1
1
use std:: collections:: HashSet ;
2
2
3
- use petgraph:: dot :: Config ;
4
- use petgraph:: dot :: Dot ;
3
+ use petgraph:: visit :: EdgeRef ;
4
+ use petgraph:: visit :: NodeRef ;
5
5
6
- use crate :: core:: config:: ROOT_ASSET ;
7
6
use crate :: types:: DependencyPriority ;
8
7
9
8
use super :: Compilation ;
10
9
10
+ #[ derive( Debug , Default ) ]
11
+ pub struct DebugAssetGraphOptions {
12
+ pub show_specifiers : bool ,
13
+ }
14
+
11
15
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
+
13
21
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
+ }
25
32
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 ) ;
27
48
28
49
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) ;
30
55
}
31
56
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 ) ;
37
62
}
63
+ }
38
64
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
+ }
42
67
43
- format ! ( "shape=box label=\" {}\" " , label)
44
- } ,
45
- ) ;
46
- format ! ( "{:?}" , dot)
68
+ output += "}" ;
69
+ output
47
70
}
48
71
49
72
pub fn debug_bundle_graph_dot ( & self ) -> String {
@@ -105,40 +128,3 @@ impl Compilation {
105
128
output
106
129
}
107
130
}
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
- */
0 commit comments