@@ -88,6 +88,29 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
88
88
. flat_map ( |g| g. entries ( ) )
89
89
. collect :: < Vec < _ > > ( ) ;
90
90
91
+ // First, compute the depth for each module in the graph
92
+ let module_depth = {
93
+ let _inner_span = tracing:: info_span!( "compute depth" ) . entered ( ) ;
94
+
95
+ let mut module_depth = FxHashMap :: default ( ) ;
96
+ module_graph. traverse_edges_from_entries_bfs (
97
+ entries. iter ( ) . copied ( ) ,
98
+ |parent, node| {
99
+ if let Some ( ( parent, _) ) = parent {
100
+ let parent_depth = * module_depth
101
+ . get ( & parent. module )
102
+ . context ( "Module depth not found" ) ?;
103
+ module_depth. entry ( node. module ) . or_insert ( parent_depth + 1 ) ;
104
+ } else {
105
+ module_depth. insert ( node. module , 0 ) ;
106
+ } ;
107
+
108
+ Ok ( GraphTraversalAction :: Continue )
109
+ } ,
110
+ ) ?;
111
+ module_depth
112
+ } ;
113
+
91
114
// For each module, the indices in the bitmap store which merge group entry modules
92
115
// transitively import that module. The bitmap can be treated as an opaque value, merging
93
116
// all modules with the same bitmap.
@@ -121,7 +144,10 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
121
144
122
145
let mut next_index = 0u32 ;
123
146
let visit_count = module_graph. traverse_edges_fixed_point_with_priority (
124
- entries. iter ( ) . map ( |e| ( * e, 0 ) ) ,
147
+ entries
148
+ . iter ( )
149
+ . map ( |e| Ok ( ( * e, * module_depth. get ( e) . context ( "Module depth not found" ) ?) ) )
150
+ . collect :: < Result < Vec < _ > > > ( ) ?,
125
151
& mut ( ) ,
126
152
|parent_info : Option < ( & ' _ SingleModuleGraphModuleNode , & ' _ RefData ) > ,
127
153
node : & ' _ SingleModuleGraphModuleNode ,
@@ -196,7 +222,13 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
196
222
}
197
223
} )
198
224
} ,
199
- |_, _| Ok ( 0 ) ,
225
+ |successor, _| {
226
+ // Invert the ordering here. High priority values get visited first, and we want to
227
+ // visit the low-depth nodes first, as we are propagating bitmaps downwards.
228
+ Ok ( -* module_depth
229
+ . get ( & successor. module )
230
+ . context ( "Module depth not found" ) ?)
231
+ } ,
200
232
) ?;
201
233
202
234
drop ( inner_span) ;
0 commit comments