Skip to content

Commit fcadcef

Browse files
committed
Turbopack: Use operations for global information and module graph
1 parent 4ddf51a commit fcadcef

File tree

18 files changed

+354
-298
lines changed

18 files changed

+354
-298
lines changed

crates/next-api/src/analyze.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ pub async fn analyze_module_graphs(module_graphs: Vc<ModuleGraphs>) -> Result<Vc
424424
let mut all_modules = FxIndexSet::default();
425425
let mut all_edges = FxIndexSet::default();
426426
let mut all_async_edges = FxIndexSet::default();
427-
for &module_graph in module_graphs.await? {
428-
let module_graph = module_graph.await?;
427+
for module_graph in module_graphs.await?.iter() {
428+
let module_graph = module_graph.connect().await?;
429429
module_graph.traverse_edges_unordered(|parent, node| {
430430
if let Some((parent_node, reference)) = parent {
431431
all_modules.insert(parent_node);

crates/next-api/src/app.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,8 @@ impl AppProject {
987987
};
988988

989989
Ok(BaseAndFullModuleGraph {
990-
base: base.connect().to_resolved().await?,
991-
full: full.connect().to_resolved().await?,
990+
base,
991+
full,
992992
binding_usage_info,
993993
}
994994
.cell())
@@ -1274,7 +1274,7 @@ impl AppEndpoint {
12741274
AssetIdent::from_path(project.project_path().owned().await?)
12751275
.with_modifier(rcstr!("client-shared-chunks")),
12761276
this.app_project.client_runtime_entries(),
1277-
*module_graphs.full,
1277+
module_graphs.full.connect(),
12781278
*client_chunking_context,
12791279
);
12801280

@@ -1292,14 +1292,14 @@ impl AppEndpoint {
12921292
let per_page_module_graph = *project.per_page_module_graph().await?;
12931293

12941294
let next_dynamic_imports =
1295-
NextDynamicGraphs::new(*module_graphs.base, per_page_module_graph)
1295+
NextDynamicGraphs::new(module_graphs.base.connect(), per_page_module_graph)
12961296
.get_next_dynamic_imports_for_endpoint(*rsc_entry)
12971297
.await?;
12981298

12991299
let is_production = project.next_mode().await?.is_production();
13001300

13011301
let client_references =
1302-
ClientReferencesGraphs::new(*module_graphs.base, per_page_module_graph)
1302+
ClientReferencesGraphs::new(module_graphs.base.connect(), per_page_module_graph)
13031303
.get_client_references_for_endpoint(
13041304
*rsc_entry,
13051305
matches!(this.ty, AppEndpointType::Page { .. }),
@@ -1311,7 +1311,7 @@ impl AppEndpoint {
13111311

13121312
let client_references_chunks = get_app_client_references_chunks(
13131313
*client_references,
1314-
*module_graphs.full,
1314+
module_graphs.full.connect(),
13151315
*client_chunking_context,
13161316
availability_info,
13171317
ssr_chunking_context.map(|ctx| *ctx),
@@ -1387,7 +1387,7 @@ impl AppEndpoint {
13871387
.await?
13881388
{
13891389
let webpack_stats = generate_webpack_stats(
1390-
*module_graphs.base,
1390+
module_graphs.base.connect(),
13911391
app_entry.original_name.clone(),
13921392
client_assets.await?.into_iter().copied(),
13931393
)
@@ -1434,7 +1434,7 @@ impl AppEndpoint {
14341434
}
14351435
}
14361436

1437-
let actions = ServerActionsGraphs::new(*module_graphs.base, per_page_module_graph)
1437+
let actions = ServerActionsGraphs::new(module_graphs.base.connect(), per_page_module_graph)
14381438
.get_server_actions_for_endpoint(
14391439
*rsc_entry,
14401440
match runtime {
@@ -1453,7 +1453,7 @@ impl AppEndpoint {
14531453
NextRuntime::Edge => Vc::upcast(this.app_project.edge_rsc_module_context()),
14541454
NextRuntime::NodeJs => Vc::upcast(this.app_project.rsc_module_context()),
14551455
},
1456-
*module_graphs.full,
1456+
module_graphs.full.connect(),
14571457
this.app_project
14581458
.project()
14591459
.runtime_chunking_context(process_client_assets, runtime),
@@ -1471,7 +1471,7 @@ impl AppEndpoint {
14711471
*server_action_manifest_loader,
14721472
server_path.clone(),
14731473
process_client_assets,
1474-
*module_graphs.full,
1474+
module_graphs.full.connect(),
14751475
)
14761476
.to_resolved()
14771477
.await?;
@@ -1493,7 +1493,12 @@ impl AppEndpoint {
14931493
client_references_chunks,
14941494
client_chunking_context,
14951495
ssr_chunking_context,
1496-
async_module_info: module_graphs.full.async_module_info().to_resolved().await?,
1496+
async_module_info: module_graphs
1497+
.full
1498+
.connect()
1499+
.async_module_info()
1500+
.to_resolved()
1501+
.await?,
14971502
next_config: project.next_config().to_resolved().await?,
14981503
runtime,
14991504
mode: *project.next_mode().await?,
@@ -1566,7 +1571,7 @@ impl AppEndpoint {
15661571

15671572
if emit_manifests == EmitManifests::Full {
15681573
let dynamic_import_entries = collect_next_dynamic_chunks(
1569-
*module_graphs.full,
1574+
module_graphs.full.connect(),
15701575
*client_chunking_context,
15711576
next_dynamic_imports,
15721577
NextDynamicChunkAvailability::ClientReferences(
@@ -1682,7 +1687,7 @@ impl AppEndpoint {
16821687
let loadable_manifest_output = if emit_manifests == EmitManifests::Full {
16831688
// create react-loadable-manifest for next/dynamic
16841689
let dynamic_import_entries = collect_next_dynamic_chunks(
1685-
*module_graphs.full,
1690+
module_graphs.full.connect(),
16861691
*client_chunking_context,
16871692
next_dynamic_imports,
16881693
NextDynamicChunkAvailability::ClientReferences(

crates/next-api/src/instrumentation.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::{
3030
paths::{
3131
all_server_paths, get_js_paths_from_root, get_wasm_paths_from_root, wasm_paths_to_bindings,
3232
},
33-
project::Project,
33+
project::{Project, get_module_graph_operation},
3434
route::{Endpoint, EndpointOutput, EndpointOutputPaths, ModuleGraphs},
3535
};
3636

@@ -249,8 +249,10 @@ impl Endpoint for InstrumentationEndpoint {
249249
#[turbo_tasks::function]
250250
async fn module_graphs(self: Vc<Self>) -> Result<Vc<ModuleGraphs>> {
251251
let this = self.await?;
252-
let module = self.entry_module();
253-
let module_graph = this.project.module_graph(module).to_resolved().await?;
254-
Ok(Vc::cell(vec![module_graph]))
252+
let module = self.entry_module().to_resolved().await?;
253+
Ok(Vc::cell(vec![get_module_graph_operation(
254+
this.project,
255+
module,
256+
)]))
255257
}
256258
}

crates/next-api/src/middleware.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::{
3636
all_paths_in_root, all_server_paths, get_asset_paths_from_root, get_js_paths_from_root,
3737
get_wasm_paths_from_root, paths_to_bindings, wasm_paths_to_bindings,
3838
},
39-
project::Project,
39+
project::{Project, get_module_graph_operation},
4040
route::{Endpoint, EndpointOutput, EndpointOutputPaths, ModuleGraphs},
4141
};
4242

@@ -390,11 +390,10 @@ impl Endpoint for MiddlewareEndpoint {
390390
#[turbo_tasks::function]
391391
async fn module_graphs(self: Vc<Self>) -> Result<Vc<ModuleGraphs>> {
392392
let this = self.await?;
393-
let module_graph = this
394-
.project
395-
.module_graph(self.entry_module())
396-
.to_resolved()
397-
.await?;
398-
Ok(Vc::cell(vec![module_graph]))
393+
let module = self.entry_module().to_resolved().await?;
394+
Ok(Vc::cell(vec![get_module_graph_operation(
395+
this.project,
396+
module,
397+
)]))
399398
}
400399
}

crates/next-api/src/pages.rs

Lines changed: 83 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ use crate::{
7878
all_paths_in_root, all_server_paths, get_asset_paths_from_root, get_js_paths_from_root,
7979
get_wasm_paths_from_root, paths_to_bindings, wasm_paths_to_bindings,
8080
},
81-
project::Project,
81+
project::{Project, get_module_graph_for_modules_operation},
8282
route::{Endpoint, EndpointOutput, EndpointOutputPaths, ModuleGraphs, Route, Routes},
8383
webpack_stats::generate_webpack_stats,
8484
};
@@ -699,71 +699,13 @@ impl PageEndpoint {
699699
}
700700

701701
#[turbo_tasks::function]
702-
async fn client_module_graph(self: Vc<Self>) -> Result<Vc<ModuleGraph>> {
703-
let this = self.await?;
704-
let project = this.pages_project.project();
705-
let evaluatable_assets = self.client_evaluatable_assets();
706-
Ok(project.module_graph_for_modules(evaluatable_assets))
702+
async fn client_module_graph(self: ResolvedVc<Self>) -> Result<Vc<ModuleGraph>> {
703+
Ok(get_pages_client_module_graph_operation(self).connect())
707704
}
708705

709706
#[turbo_tasks::function]
710-
async fn ssr_module_graph(self: Vc<Self>) -> Result<Vc<ModuleGraph>> {
711-
let this = self.await?;
712-
let project = this.pages_project.project();
713-
714-
if *project.per_page_module_graph().await? {
715-
let next_mode = project.next_mode();
716-
let next_mode_ref = next_mode.await?;
717-
let should_trace = next_mode_ref.is_production();
718-
let should_read_binding_usage = next_mode_ref.is_production();
719-
720-
let ssr_chunk_module = self.internal_ssr_chunk_module().await?;
721-
// Implements layout segment optimization to compute a graph "chain" for document, app,
722-
// page
723-
let mut graphs = vec![];
724-
let mut visited_modules = VisitedModules::empty();
725-
for module in [
726-
ssr_chunk_module.document_module,
727-
ssr_chunk_module.app_module,
728-
]
729-
.into_iter()
730-
.flatten()
731-
{
732-
let graph = SingleModuleGraph::new_with_entries_visited_intern(
733-
vec![ChunkGroupEntry::Shared(module)],
734-
visited_modules,
735-
should_trace,
736-
should_read_binding_usage,
737-
);
738-
graphs.push(graph);
739-
visited_modules = VisitedModules::concatenate(visited_modules, graph);
740-
}
741-
742-
let graph = SingleModuleGraph::new_with_entries_visited_intern(
743-
vec![ChunkGroupEntry::Entry(vec![ssr_chunk_module.ssr_module])],
744-
visited_modules,
745-
should_trace,
746-
should_read_binding_usage,
747-
);
748-
graphs.push(graph);
749-
750-
let remove_unused_imports = *project
751-
.next_config()
752-
.turbopack_remove_unused_imports(next_mode)
753-
.await?;
754-
755-
let graph = if remove_unused_imports {
756-
let graph = ModuleGraph::from_graphs(graphs.clone());
757-
let binding_usage_info = compute_binding_usage_info(graph, true);
758-
ModuleGraph::from_graphs_without_unused_references(graphs, binding_usage_info)
759-
} else {
760-
ModuleGraph::from_graphs(graphs)
761-
};
762-
763-
Ok(graph.connect())
764-
} else {
765-
Ok(*project.whole_app_module_graphs().await?.full)
766-
}
707+
async fn ssr_module_graph(self: ResolvedVc<Self>) -> Result<Vc<ModuleGraph>> {
708+
Ok(get_pages_ssr_module_graph_operation(self).connect())
767709
}
768710

769711
#[turbo_tasks::function]
@@ -1726,13 +1668,12 @@ impl Endpoint for PageEndpoint {
17261668

17271669
#[turbo_tasks::function]
17281670
async fn module_graphs(self: Vc<Self>) -> Result<Vc<ModuleGraphs>> {
1729-
let client_module_graph = self.client_module_graph().to_resolved().await?;
1730-
let ssr_module_graph = self.ssr_module_graph().to_resolved().await?;
1731-
Ok(Vc::cell(if client_module_graph != ssr_module_graph {
1732-
vec![client_module_graph, ssr_module_graph]
1733-
} else {
1734-
vec![ssr_module_graph]
1735-
}))
1671+
let this = self.to_resolved().await?;
1672+
let client_module_graph = get_pages_client_module_graph_operation(this);
1673+
let ssr_module_graph = get_pages_ssr_module_graph_operation(this);
1674+
// Note: We can't easily deduplicate these anymore since OperationVc
1675+
// doesn't implement Eq, but that's fine - they're operation references
1676+
Ok(Vc::cell(vec![client_module_graph, ssr_module_graph]))
17361677
}
17371678
}
17381679

@@ -1796,3 +1737,75 @@ pub enum SsrChunk {
17961737
regions: Option<Vec<RcStr>>,
17971738
},
17981739
}
1740+
1741+
#[turbo_tasks::function(operation)]
1742+
async fn get_pages_client_module_graph_operation(
1743+
endpoint: ResolvedVc<PageEndpoint>,
1744+
) -> Result<Vc<ModuleGraph>> {
1745+
let this = endpoint.await?;
1746+
let project = this.pages_project.project().to_resolved().await?;
1747+
let evaluatable_assets = endpoint.client_evaluatable_assets().to_resolved().await?;
1748+
Ok(get_module_graph_for_modules_operation(project, evaluatable_assets).connect())
1749+
}
1750+
1751+
#[turbo_tasks::function(operation)]
1752+
async fn get_pages_ssr_module_graph_operation(
1753+
endpoint: ResolvedVc<PageEndpoint>,
1754+
) -> Result<Vc<ModuleGraph>> {
1755+
let this = endpoint.await?;
1756+
let project = this.pages_project.project();
1757+
1758+
if *project.per_page_module_graph().await? {
1759+
let next_mode = project.next_mode();
1760+
let next_mode_ref = next_mode.await?;
1761+
let should_trace = next_mode_ref.is_production();
1762+
let should_read_binding_usage = next_mode_ref.is_production();
1763+
1764+
let ssr_chunk_module = endpoint.internal_ssr_chunk_module().await?;
1765+
// Implements layout segment optimization to compute a graph "chain" for document, app,
1766+
// page
1767+
let mut graphs = vec![];
1768+
let mut visited_modules = VisitedModules::empty();
1769+
for module in [
1770+
ssr_chunk_module.document_module,
1771+
ssr_chunk_module.app_module,
1772+
]
1773+
.into_iter()
1774+
.flatten()
1775+
{
1776+
let graph = SingleModuleGraph::new_with_entries_visited_intern(
1777+
vec![ChunkGroupEntry::Shared(module)],
1778+
visited_modules,
1779+
should_trace,
1780+
should_read_binding_usage,
1781+
);
1782+
graphs.push(graph);
1783+
visited_modules = VisitedModules::concatenate(visited_modules, graph);
1784+
}
1785+
1786+
let graph = SingleModuleGraph::new_with_entries_visited_intern(
1787+
vec![ChunkGroupEntry::Entry(vec![ssr_chunk_module.ssr_module])],
1788+
visited_modules,
1789+
should_trace,
1790+
should_read_binding_usage,
1791+
);
1792+
graphs.push(graph);
1793+
1794+
let remove_unused_imports = *project
1795+
.next_config()
1796+
.turbopack_remove_unused_imports(next_mode)
1797+
.await?;
1798+
1799+
let graph = if remove_unused_imports {
1800+
let graph = ModuleGraph::from_graphs(graphs.clone());
1801+
let binding_usage_info = compute_binding_usage_info(graph, true);
1802+
ModuleGraph::from_graphs_without_unused_references(graphs, binding_usage_info)
1803+
} else {
1804+
ModuleGraph::from_graphs(graphs)
1805+
};
1806+
1807+
Ok(graph.connect())
1808+
} else {
1809+
Ok(project.whole_app_module_graphs().await?.full.connect())
1810+
}
1811+
}

0 commit comments

Comments
 (0)