Skip to content

Commit 5728c28

Browse files
committed
GraphCollectingMode
1 parent 39f82d5 commit 5728c28

File tree

11 files changed

+125
-41
lines changed

11 files changed

+125
-41
lines changed

crates/next-api/src/app.rs

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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
)

crates/next-api/src/pages.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use turbopack_core::{
5252
ident::{AssetIdent, Layer},
5353
module::Module,
5454
module_graph::{
55-
GraphEntries, ModuleGraph, SingleModuleGraph, VisitedModules,
55+
GraphCollectingMode, GraphEntries, ModuleGraph, SingleModuleGraph, VisitedModules,
5656
binding_usage_info::compute_binding_usage_info,
5757
chunk_group_info::{ChunkGroup, ChunkGroupEntry},
5858
},
@@ -735,6 +735,9 @@ impl PageEndpoint {
735735
visited_modules,
736736
should_trace,
737737
should_read_binding_usage,
738+
GraphCollectingMode::IncompleteGraph {
739+
ignored_collected_namespace: Default::default(),
740+
},
738741
);
739742
graphs.push(graph);
740743
visited_modules = VisitedModules::concatenate(visited_modules, graph);
@@ -745,6 +748,7 @@ impl PageEndpoint {
745748
visited_modules,
746749
should_trace,
747750
should_read_binding_usage,
751+
GraphCollectingMode::CompleteGraph,
748752
);
749753
graphs.push(graph);
750754

crates/next-api/src/project.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use turbopack_core::{
6565
},
6666
module::Module,
6767
module_graph::{
68-
GraphEntries, ModuleGraph, SingleModuleGraph,
68+
GraphCollectingMode, GraphEntries, ModuleGraph, SingleModuleGraph,
6969
binding_usage_info::{
7070
BindingUsageInfo, OptionBindingUsageInfo, compute_binding_usage_info,
7171
},
@@ -1440,6 +1440,7 @@ impl Project {
14401440
ChunkGroupEntry::Entry(vec![entry]),
14411441
is_production,
14421442
is_production,
1443+
GraphCollectingMode::CompleteGraph,
14431444
)],
14441445
None,
14451446
)
@@ -1467,6 +1468,7 @@ impl Project {
14671468
ResolvedVc::cell(vec![ChunkGroupEntry::Entry(entries)]),
14681469
is_production,
14691470
is_production,
1471+
GraphCollectingMode::CompleteGraph,
14701472
)],
14711473
None,
14721474
)
@@ -2435,6 +2437,7 @@ async fn whole_app_module_graph_operation(project: ResolvedVc<Project>) -> Resul
24352437
project.get_all_entries().to_resolved().await?,
24362438
should_trace,
24372439
should_read_binding_usage,
2440+
GraphCollectingMode::CompleteGraph,
24382441
);
24392442

24402443
let base = ModuleGraph::from_graphs(vec![module_graph], None);

crates/next-core/src/next_font/google/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use turbopack_core::{
2121
context::AssetContext,
2222
ident::Layer,
2323
issue::{IssueExt, IssueSeverity, StyledString},
24-
module_graph::{ModuleGraph, SingleModuleGraph},
24+
module_graph::{GraphCollectingMode, ModuleGraph, SingleModuleGraph},
2525
reference_type::{InnerAssets, ReferenceType},
2626
resolve::{
2727
ResolveResult,
@@ -777,6 +777,7 @@ async fn get_mock_stylesheet(
777777
entries.graph_entries().to_resolved().await?,
778778
false,
779779
false,
780+
GraphCollectingMode::CompleteGraph,
780781
)],
781782
None,
782783
);

turbopack/crates/turbopack-cli/src/build/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use turbopack_core::{
2929
issue::{IssueReporter, IssueSeverity, handle_issues},
3030
module::Module,
3131
module_graph::{
32-
ModuleGraph, SingleModuleGraph,
32+
GraphCollectingMode, ModuleGraph, SingleModuleGraph,
3333
binding_usage_info::compute_binding_usage_info,
3434
chunk_group_info::{ChunkGroup, ChunkGroupEntry},
3535
},
@@ -311,6 +311,7 @@ async fn build_internal(
311311
ResolvedVc::cell(vec![ChunkGroupEntry::Entry(entries.clone())]),
312312
false,
313313
true,
314+
GraphCollectingMode::CompleteGraph,
314315
);
315316
let mut module_graph = ModuleGraph::from_graphs(vec![single_graph], None);
316317
let binding_usage = compute_binding_usage_info(module_graph, true);

turbopack/crates/turbopack-cli/src/dev/web_entry_source.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use turbopack_core::{
1212
environment::Environment,
1313
file_source::FileSource,
1414
module::Module,
15-
module_graph::{ModuleGraph, SingleModuleGraph, chunk_group_info::ChunkGroupEntry},
15+
module_graph::{
16+
GraphCollectingMode, ModuleGraph, SingleModuleGraph, chunk_group_info::ChunkGroupEntry,
17+
},
1618
reference_type::{EntryReferenceSubType, ReferenceType},
1719
resolve::{
1820
origin::{PlainResolveOrigin, ResolveOrigin, ResolveOriginExt},
@@ -166,6 +168,7 @@ pub async fn create_web_entry_source(
166168
ResolvedVc::cell(vec![ChunkGroupEntry::Entry(all_modules)]),
167169
false,
168170
false,
171+
GraphCollectingMode::CompleteGraph,
169172
)],
170173
None,
171174
);

0 commit comments

Comments
 (0)