@@ -3,21 +3,13 @@ use next_core::{
33 next_client_reference:: { CssClientReferenceModule , EcmascriptClientReferenceModule } ,
44 next_server_component:: server_component_module:: NextServerComponentModule ,
55} ;
6- use roaring:: RoaringBitmap ;
76use rustc_hash:: FxHashMap ;
87use serde:: { Deserialize , Serialize } ;
98use turbo_tasks:: {
10- FxIndexSet , NonLocalValue , ResolvedVc , TryFlatJoinIterExt , Vc , debug:: ValueDebugFormat ,
11- trace:: TraceRawVcs ,
9+ NonLocalValue , ResolvedVc , TryFlatJoinIterExt , Vc , debug:: ValueDebugFormat , trace:: TraceRawVcs ,
1210} ;
1311use turbopack:: css:: chunk:: CssChunkPlaceable ;
14- use turbopack_core:: {
15- module:: Module ,
16- module_graph:: {
17- GraphTraversalAction , SingleModuleGraph , SingleModuleGraphModuleNode ,
18- chunk_group_info:: RoaringBitmapWrapper ,
19- } ,
20- } ;
12+ use turbopack_core:: { module:: Module , module_graph:: SingleModuleGraph } ;
2113
2214#[ derive(
2315 Copy , Clone , Serialize , Deserialize , Eq , PartialEq , TraceRawVcs , ValueDebugFormat , NonLocalValue ,
@@ -31,42 +23,14 @@ pub enum ClientManifestEntryType {
3123 ServerComponent ( ResolvedVc < NextServerComponentModule > ) ,
3224}
3325
34- /// Tracks information about all the css and js client references in the graph as well as how server
35- /// components depend on them.
36- #[ turbo_tasks:: value]
37- pub struct ClientReferenceManifest {
38- pub manifest : FxHashMap < ResolvedVc < Box < dyn Module > > , ClientManifestEntryType > ,
39- // All the server components in the graph.
40- server_components : FxIndexSet < ResolvedVc < NextServerComponentModule > > ,
41- // All the server components that depend on each module
42- // This only includes mappings for modules with client references and the bitmaps reference
43- // indices into `[server_components]`
44- server_components_for_client_references :
45- FxHashMap < ResolvedVc < Box < dyn Module > > , RoaringBitmapWrapper > ,
46- }
47-
48- impl ClientReferenceManifest {
49- /// Returns all the server components that depend on the given client reference
50- pub fn server_components_for_client_reference (
51- & self ,
52- module : ResolvedVc < Box < dyn Module > > ,
53- ) -> impl Iterator < Item = ResolvedVc < NextServerComponentModule > > {
54- let bitmap = & self
55- . server_components_for_client_references
56- . get ( & module)
57- . expect ( "Module should be a client reference module" )
58- . 0 ;
59-
60- bitmap
61- . iter ( )
62- . map ( |index| * self . server_components . get_index ( index as usize ) . unwrap ( ) )
63- }
64- }
26+ /// Tracks information about all the css and js client references in the graph.
27+ #[ turbo_tasks:: value( transparent) ]
28+ pub struct ClientReferenceData ( FxHashMap < ResolvedVc < Box < dyn Module > > , ClientManifestEntryType > ) ;
6529
6630#[ turbo_tasks:: function]
6731pub async fn map_client_references (
6832 graph : Vc < SingleModuleGraph > ,
69- ) -> Result < Vc < ClientReferenceManifest > > {
33+ ) -> Result < Vc < ClientReferenceData > > {
7034 let graph = graph. await ?;
7135 let manifest = graph
7236 . iter_nodes ( )
@@ -108,96 +72,5 @@ pub async fn map_client_references(
10872 . into_iter ( )
10973 . collect :: < FxHashMap < _ , _ > > ( ) ;
11074
111- let mut server_components = FxIndexSet :: default ( ) ;
112- let mut module_to_server_component_bits = FxHashMap :: default ( ) ;
113- if !manifest. is_empty ( ) {
114- graph. traverse_edges_from_entries_fixed_point (
115- graph. entry_modules ( ) ,
116- |parent_info, node| {
117- let module = node. module ( ) ;
118- let module_type = manifest. get ( & module) ;
119- let mut should_visit_children = match module_to_server_component_bits. entry ( module)
120- {
121- std:: collections:: hash_map:: Entry :: Occupied ( _) => false ,
122- std:: collections:: hash_map:: Entry :: Vacant ( vacant_entry) => {
123- // only do this the first time we visit the node.
124- let bits = vacant_entry. insert ( RoaringBitmap :: new ( ) ) ;
125- if let Some ( ClientManifestEntryType :: ServerComponent (
126- server_component_module,
127- ) ) = module_type
128- {
129- let index = server_components. insert_full ( * server_component_module) . 0 ;
130-
131- bits. insert ( index. try_into ( ) . unwrap ( ) ) ;
132- }
133- true
134- }
135- } ;
136- if let Some ( ( SingleModuleGraphModuleNode { module : parent_module} , _) ) = parent_info
137- // Skip self cycles such as in
138- // test/e2e/app-dir/dynamic-import/app/page.tsx where a very-dynamic import induces a
139- // self cycle. They don't introduce new bits anyway.
140- && module != * parent_module
141- {
142- // Copy parent bits down. `traverse_edges_from_entries_fixed_point` always
143- // visits parents before children so we can simply assert
144- // that the parent it set.
145- let [ Some ( current) , Some ( parent) ] =
146- module_to_server_component_bits. get_disjoint_mut ( [ & module, parent_module] )
147- else {
148- unreachable ! ( )
149- } ;
150- // Check if we are adding new bits and thus need to revisit children unless we
151- // are already planning to because this is a new node.
152- if !should_visit_children {
153- let len = current. len ( ) ;
154- * current |= & * parent;
155- // did we find new bits? If so visit the children again
156- should_visit_children = len != current. len ( ) ;
157- } else {
158- * current |= & * parent;
159- }
160- }
161-
162- Ok ( match module_type {
163- Some (
164- ClientManifestEntryType :: EcmascriptClientReference { .. }
165- | ClientManifestEntryType :: CssClientReference { .. } ,
166- ) => {
167- // No need to explore these subgraphs ever, these are the leaves in the
168- // server component graph
169- GraphTraversalAction :: Skip
170- }
171- // Continue on server components and through graphs of non-ClientReference
172- // modules, but only if our set of parent components has changed.
173- _ => {
174- if should_visit_children {
175- GraphTraversalAction :: Continue
176- } else {
177- GraphTraversalAction :: Skip
178- }
179- }
180- } )
181- } ,
182- ) ?;
183- }
184-
185- // Filter down to just the client reference modules to reduce datastructure size
186- let server_components_for_client_references = module_to_server_component_bits
187- . into_iter ( )
188- . filter_map ( |( k, v) | match manifest. get ( & k) {
189- Some (
190- ClientManifestEntryType :: CssClientReference ( _)
191- | ClientManifestEntryType :: EcmascriptClientReference { .. } ,
192- ) => Some ( ( k, RoaringBitmapWrapper ( v) ) ) ,
193- _ => None ,
194- } )
195- . collect ( ) ;
196-
197- Ok ( ClientReferenceManifest {
198- manifest,
199- server_components,
200- server_components_for_client_references,
201- }
202- . cell ( ) )
75+ Ok ( Vc :: cell ( manifest) )
20376}
0 commit comments