@@ -57,7 +57,7 @@ use turbopack_core::{
5757 ident:: { AssetIdent , Layer } ,
5858 module:: { Module , Modules } ,
5959 module_graph:: {
60- GraphEntries , ModuleGraph , SingleModuleGraph , VisitedModules ,
60+ GraphCollectingMode , GraphEntries , ModuleGraph , SingleModuleGraph , VisitedModules ,
6161 binding_usage_info:: compute_binding_usage_info,
6262 chunk_group_info:: { ChunkGroup , ChunkGroupEntry } ,
6363 } ,
@@ -974,6 +974,7 @@ impl AppProject {
974974 & self ,
975975 rsc_entry : ResolvedVc < Box < dyn Module > > ,
976976 server_action_loader_modules : ResolvedVc < Modules > ,
977+ ignored_collected_namespace : Vc < Vec < RcStr > > ,
977978 client_shared_entries : Vc < EvaluatableAssets > ,
978979 has_layout_segments : bool ,
979980 ) -> Result < Vc < ModuleGraph > > {
@@ -1002,6 +1003,14 @@ impl AppProject {
10021003
10031004 let mut graphs = vec ! [ ] ;
10041005 let visited_modules = if has_layout_segments {
1006+ let graph_collecting_mode = GraphCollectingMode :: IncompleteGraph {
1007+ ignored_collected_namespace : ignored_collected_namespace
1008+ . await ?
1009+ . into_iter ( )
1010+ . cloned ( )
1011+ . collect ( ) ,
1012+ } ;
1013+
10051014 let ServerEntries {
10061015 server_utils,
10071016 server_component_entries,
@@ -1024,6 +1033,7 @@ impl AppProject {
10241033 VisitedModules :: empty ( ) ,
10251034 should_trace,
10261035 should_read_binding_usage,
1036+ graph_collecting_mode. clone ( ) ,
10271037 ) ;
10281038 graphs. push ( graph) ;
10291039 let mut visited_modules = VisitedModules :: from_graph ( graph) ;
@@ -1040,6 +1050,7 @@ impl AppProject {
10401050 visited_modules,
10411051 should_trace,
10421052 should_read_binding_usage,
1053+ graph_collecting_mode. clone ( ) ,
10431054 ) ;
10441055 graphs. push ( graph) ;
10451056 let is_layout = module. server_path ( ) . await ?. file_stem ( ) == Some ( "layout" ) ;
@@ -1062,6 +1073,7 @@ impl AppProject {
10621073 VisitedModules :: empty ( ) ,
10631074 should_trace,
10641075 should_read_binding_usage,
1076+ GraphCollectingMode :: CompleteGraph ,
10651077 ) ;
10661078 graphs. push ( graph) ;
10671079 VisitedModules :: from_graph ( graph)
@@ -1072,6 +1084,7 @@ impl AppProject {
10721084 visited_modules,
10731085 should_trace,
10741086 should_read_binding_usage,
1087+ GraphCollectingMode :: CompleteGraph ,
10751088 ) ;
10761089 graphs. push ( graph) ;
10771090
@@ -1285,41 +1298,51 @@ impl AppEndpoint {
12851298 }
12861299
12871300 #[ turbo_tasks:: function]
1288- async fn server_action_loader_modules ( self : Vc < Self > ) -> Result < Vc < Modules > > {
1289- let this = self . await ?;
1301+ async fn server_action_loader_collect_namespaces ( self : Vc < Self > ) -> Result < Vc < Vec < RcStr > > > {
12901302 let app_entry = self . app_endpoint_entry ( ) . await ?;
12911303 let runtime = app_entry. config . await ?. runtime . unwrap_or_default ( ) ;
12921304
12931305 // By only collecting the correct runtime, modules emitted for the other runtime are
12941306 // never even compiled if unused.
12951307 Ok ( Vc :: cell ( vec ! [
12961308 // Collect inline "use server" function inside of RSC modules
1297- ResolvedVc :: upcast(
1298- ServerActionCollectModule :: new(
1299- match runtime {
1300- NextRuntime :: NodeJs => rcstr!( "next/server-actions/rsc-nodejs" ) ,
1301- NextRuntime :: Edge => rcstr!( "next/server-actions/rsc-edge" ) ,
1302- } ,
1303- this. page. to_string( ) . into( ) ,
1304- )
1305- . to_resolved( )
1306- . await ?,
1307- ) ,
1309+ match runtime {
1310+ NextRuntime :: NodeJs => rcstr!( "next/server-actions/rsc-nodejs" ) ,
1311+ NextRuntime :: Edge => rcstr!( "next/server-actions/rsc-edge" ) ,
1312+ } ,
13081313 // Collect "use server" modules imported from client components.
1309- ResolvedVc :: upcast(
1310- ServerActionCollectModule :: new(
1311- match runtime {
1312- NextRuntime :: NodeJs => rcstr!( "next/server-actions/browser-nodejs" ) ,
1313- NextRuntime :: Edge => rcstr!( "next/server-actions/browser-edge" ) ,
1314- } ,
1315- this. page. to_string( ) . into( ) ,
1316- )
1317- . to_resolved( )
1318- . await ?,
1319- ) ,
1314+ match runtime {
1315+ NextRuntime :: NodeJs => rcstr!( "next/server-actions/browser-nodejs" ) ,
1316+ NextRuntime :: Edge => rcstr!( "next/server-actions/browser-edge" ) ,
1317+ } ,
13201318 ] ) )
13211319 }
13221320
1321+ #[ turbo_tasks:: function]
1322+ async fn server_action_loader_modules ( self : Vc < Self > ) -> Result < Vc < Modules > > {
1323+ let this = self . await ?;
1324+ let namespaces = self . server_action_loader_collect_namespaces ( ) . await ?;
1325+
1326+ // By only collecting the correct runtime, modules emitted for the other runtime are
1327+ // never even compiled if unused.
1328+ Ok ( Vc :: cell (
1329+ namespaces
1330+ . iter ( )
1331+ . map ( async |namespace| {
1332+ Ok ( ResolvedVc :: upcast (
1333+ ServerActionCollectModule :: new (
1334+ namespace. clone ( ) ,
1335+ this. page . to_string ( ) . into ( ) ,
1336+ )
1337+ . to_resolved ( )
1338+ . await ?,
1339+ ) )
1340+ } )
1341+ . try_join ( )
1342+ . await ?,
1343+ ) )
1344+ }
1345+
13231346 #[ turbo_tasks:: function]
13241347 async fn output ( self : Vc < Self > ) -> Result < Vc < AppEndpointOutput > > {
13251348 let this = self . await ?;
@@ -1372,11 +1395,10 @@ impl AppEndpoint {
13721395
13731396 let is_app_page = matches ! ( this. ty, AppEndpointType :: Page { .. } ) ;
13741397
1375- let server_action_loader_modules = self . server_action_loader_modules ( ) ;
1376-
13771398 let module_graph = this. app_project . app_module_graph (
13781399 * rsc_entry,
1379- server_action_loader_modules,
1400+ self . server_action_loader_modules ( ) ,
1401+ self . server_action_loader_collect_namespaces ( ) ,
13801402 // We only need the client runtime entries for pages not for Route Handlers
13811403 if is_app_page {
13821404 this. app_project . client_runtime_entries ( )
@@ -2212,12 +2234,12 @@ impl Endpoint for AppEndpoint {
22122234 async fn module_graphs ( self : Vc < Self > ) -> Result < Vc < ModuleGraphs > > {
22132235 let this = self . await ?;
22142236 let app_entry = self . app_endpoint_entry ( ) . await ?;
2215- let server_action_loader_modules = self . server_action_loader_modules ( ) ;
22162237 let module_graph = this
22172238 . app_project
22182239 . app_module_graph (
22192240 * app_entry. rsc_entry ,
2220- server_action_loader_modules,
2241+ self . server_action_loader_modules ( ) ,
2242+ self . server_action_loader_collect_namespaces ( ) ,
22212243 this. app_project . client_runtime_entries ( ) ,
22222244 matches ! ( this. ty, AppEndpointType :: Page { .. } ) ,
22232245 )
0 commit comments