@@ -88,6 +88,30 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
8888 . flat_map ( |g| g. entries ( ) )
8989 . collect :: < Vec < _ > > ( ) ;
9090
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 =
96+ FxHashMap :: with_capacity_and_hasher ( module_count, Default :: default ( ) ) ;
97+ module_graph. traverse_edges_from_entries_bfs (
98+ entries. iter ( ) . copied ( ) ,
99+ |parent, node| {
100+ if let Some ( ( parent, _) ) = parent {
101+ let parent_depth = * module_depth
102+ . get ( & parent. module )
103+ . context ( "Module depth not found" ) ?;
104+ module_depth. entry ( node. module ) . or_insert ( parent_depth + 1 ) ;
105+ } else {
106+ module_depth. insert ( node. module , 0 ) ;
107+ } ;
108+
109+ Ok ( GraphTraversalAction :: Continue )
110+ } ,
111+ ) ?;
112+ module_depth
113+ } ;
114+
91115 // For each module, the indices in the bitmap store which merge group entry modules
92116 // transitively import that module. The bitmap can be treated as an opaque value, merging
93117 // all modules with the same bitmap.
@@ -121,7 +145,10 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
121145
122146 let mut next_index = 0u32 ;
123147 let visit_count = module_graph. traverse_edges_fixed_point_with_priority (
124- entries. iter ( ) . map ( |e| ( * e, 0 ) ) ,
148+ entries
149+ . iter ( )
150+ . map ( |e| Ok ( ( * e, -* module_depth. get ( e) . context ( "Module depth not found" ) ?) ) )
151+ . collect :: < Result < Vec < _ > > > ( ) ?,
125152 & mut ( ) ,
126153 |parent_info : Option < ( & ' _ SingleModuleGraphModuleNode , & ' _ RefData ) > ,
127154 node : & ' _ SingleModuleGraphModuleNode ,
@@ -196,7 +223,13 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
196223 }
197224 } )
198225 } ,
199- |_, _| Ok ( 0 ) ,
226+ |successor, _| {
227+ // Invert the ordering here. High priority values get visited first, and we want to
228+ // visit the low-depth nodes first, as we are propagating bitmaps downwards.
229+ Ok ( -* module_depth
230+ . get ( & successor. module )
231+ . context ( "Module depth not found" ) ?)
232+ } ,
200233 ) ?;
201234
202235 drop ( inner_span) ;
0 commit comments