Skip to content

Commit 934cecc

Browse files
committed
remove additional_entries
1 parent 3bdbed2 commit 934cecc

File tree

9 files changed

+114
-233
lines changed

9 files changed

+114
-233
lines changed

crates/next-api/src/analyze.rs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ use turbopack_core::{
1616
asset::{Asset, AssetContent},
1717
chunk::ChunkingType,
1818
module::Module,
19+
module_graph::ModuleGraph,
1920
output::{OutputAsset, OutputAssets, OutputAssetsReference},
2021
reference::all_assets_from_entries,
2122
};
2223

23-
use crate::route::ModuleGraphs;
24-
2524
pub struct EdgesData {
2625
pub offsets: Vec<u32>,
2726
pub data: Vec<u32>,
@@ -431,30 +430,28 @@ pub async fn analyze_output_assets(output_assets: Vc<OutputAssets>) -> Result<Vc
431430
}
432431

433432
#[turbo_tasks::function]
434-
pub async fn analyze_module_graphs(module_graphs: Vc<ModuleGraphs>) -> Result<Vc<FileContent>> {
433+
pub async fn analyze_module_graph(module_graph: Vc<ModuleGraph>) -> Result<Vc<FileContent>> {
435434
let mut builder = ModulesDataBuilder::new();
436435

437436
let mut all_modules = FxIndexSet::default();
438437
let mut all_edges = FxIndexSet::default();
439438
let mut all_async_edges = FxIndexSet::default();
440-
for &module_graph in module_graphs.await? {
441-
let module_graph = module_graph.await?;
442-
module_graph.traverse_edges_unordered(|parent, node| {
443-
if let Some((parent_node, reference)) = parent {
444-
all_modules.insert(parent_node);
445-
all_modules.insert(node);
446-
match reference.chunking_type {
447-
ChunkingType::Async => {
448-
all_async_edges.insert((parent_node, node));
449-
}
450-
_ => {
451-
all_edges.insert((parent_node, node));
452-
}
439+
let module_graph = module_graph.await?;
440+
module_graph.traverse_edges_unordered(|parent, node| {
441+
if let Some((parent_node, reference)) = parent {
442+
all_modules.insert(parent_node);
443+
all_modules.insert(node);
444+
match reference.chunking_type {
445+
ChunkingType::Async => {
446+
all_async_edges.insert((parent_node, node));
447+
}
448+
_ => {
449+
all_edges.insert((parent_node, node));
453450
}
454451
}
455-
Ok(())
456-
})?;
457-
}
452+
}
453+
Ok(())
454+
})?;
458455

459456
type ModulePair = (ResolvedVc<Box<dyn Module>>, ResolvedVc<Box<dyn Module>>);
460457
async fn mapper((from, to): ModulePair) -> Result<Option<(RcStr, RcStr)>> {
@@ -568,26 +565,22 @@ impl OutputAsset for AnalyzeDataOutputAsset {
568565
#[turbo_tasks::value]
569566
pub struct ModulesDataOutputAsset {
570567
pub path: FileSystemPath,
571-
pub module_graphs: ResolvedVc<ModuleGraphs>,
568+
pub module_graph: ResolvedVc<ModuleGraph>,
572569
}
573570

574571
#[turbo_tasks::value_impl]
575572
impl ModulesDataOutputAsset {
576573
#[turbo_tasks::function]
577-
pub async fn new(path: FileSystemPath, module_graphs: Vc<ModuleGraphs>) -> Result<Vc<Self>> {
578-
Ok(Self {
579-
path,
580-
module_graphs: module_graphs.to_resolved().await?,
581-
}
582-
.cell())
574+
pub fn new(path: FileSystemPath, module_graph: ResolvedVc<ModuleGraph>) -> Vc<Self> {
575+
Self { path, module_graph }.cell()
583576
}
584577
}
585578

586579
#[turbo_tasks::value_impl]
587580
impl Asset for ModulesDataOutputAsset {
588581
#[turbo_tasks::function]
589582
fn content(&self) -> Vc<AssetContent> {
590-
let file_content = analyze_module_graphs(*self.module_graphs);
583+
let file_content = analyze_module_graph(*self.module_graph);
591584
AssetContent::file(file_content)
592585
}
593586
}

crates/next-api/src/app.rs

Lines changed: 52 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ use crate::{
8888
all_asset_paths, all_paths_in_root, get_asset_paths_from_root, get_js_paths_from_root,
8989
get_wasm_paths_from_root, paths_to_bindings, wasm_paths_to_bindings,
9090
},
91-
project::{BaseAndFullModuleGraph, Project},
91+
project::Project,
9292
route::{
9393
AppPageRoute, Endpoint, EndpointOutput, EndpointOutputPaths, ModuleGraphs, Route, Routes,
9494
},
@@ -976,14 +976,13 @@ impl AppProject {
976976
}
977977

978978
#[turbo_tasks::function]
979-
pub async fn app_module_graphs(
979+
pub async fn app_module_graph(
980980
&self,
981-
endpoint: Vc<AppEndpoint>,
982981
rsc_entry: ResolvedVc<Box<dyn Module>>,
983982
server_action_loader_module: ResolvedVc<Box<dyn Module>>,
984983
client_shared_entries: Vc<EvaluatableAssets>,
985984
has_layout_segments: bool,
986-
) -> Result<Vc<BaseAndFullModuleGraph>> {
985+
) -> Result<Vc<ModuleGraph>> {
987986
if *self.project.per_page_module_graph().await? {
988987
let next_mode = self.project.next_mode();
989988
let next_mode_ref = next_mode.await?;
@@ -1002,7 +1001,7 @@ impl AppProject {
10021001
ChunkGroupEntry::Entry(vec![server_action_loader_module, rsc_entry]);
10031002

10041003
let mut graphs = vec![];
1005-
let mut visited_modules = if has_layout_segments {
1004+
let visited_modules = if has_layout_segments {
10061005
let ServerEntries {
10071006
server_utils,
10081007
server_component_entries,
@@ -1075,50 +1074,33 @@ impl AppProject {
10751074
should_read_binding_usage,
10761075
);
10771076
graphs.push(graph);
1078-
visited_modules = VisitedModules::concatenate(visited_modules, graph);
1079-
1080-
let base = ModuleGraph::from_graphs(graphs.clone(), None);
1081-
let additional_entries = endpoint.additional_entries(base.connect());
1082-
let additional_module_graph = SingleModuleGraph::new_with_entries_visited_intern(
1083-
additional_entries.owned().await?,
1084-
visited_modules,
1085-
should_trace,
1086-
should_read_binding_usage,
1087-
);
1088-
graphs.push(additional_module_graph);
10891077

10901078
let remove_unused_imports = *self
10911079
.project
10921080
.next_config()
10931081
.turbopack_remove_unused_imports(next_mode)
10941082
.await?;
1083+
let remove_unused_exports = *self
1084+
.project
1085+
.next_config()
1086+
.turbopack_remove_unused_exports(next_mode)
1087+
.await?;
10951088

1096-
let (full, binding_usage_info) = if remove_unused_imports {
1097-
let full_with_unused_references =
1098-
ModuleGraph::from_graphs(graphs.clone(), None);
1099-
let binding_usage_info = compute_binding_usage_info(
1100-
full_with_unused_references,
1101-
should_read_binding_usage,
1102-
);
1103-
(
1104-
ModuleGraph::from_graphs(graphs, Some(binding_usage_info)),
1105-
Some(binding_usage_info),
1106-
)
1089+
let graph = if remove_unused_imports || remove_unused_exports {
1090+
let graph = ModuleGraph::from_graphs(graphs.clone(), None);
1091+
let binding_usage_info =
1092+
compute_binding_usage_info(graph, remove_unused_imports);
1093+
ModuleGraph::from_graphs(graphs, Some(binding_usage_info))
11071094
} else {
1108-
(ModuleGraph::from_graphs(graphs, None), None)
1095+
ModuleGraph::from_graphs(graphs, None)
11091096
};
11101097

1111-
Ok(BaseAndFullModuleGraph {
1112-
base: base.connect().to_resolved().await?,
1113-
full: full.connect().to_resolved().await?,
1114-
binding_usage_info,
1115-
}
1116-
.cell())
1098+
Ok(graph.connect())
11171099
}
11181100
.instrument(tracing::info_span!("module graph for endpoint"))
11191101
.await
11201102
} else {
1121-
Ok(self.project.whole_app_module_graphs())
1103+
Ok(self.project.whole_app_module_graph())
11221104
}
11231105
}
11241106
}
@@ -1382,21 +1364,17 @@ impl AppEndpoint {
13821364

13831365
let server_action_loader = self.server_action_loader_module();
13841366

1385-
let module_graphs = this
1386-
.app_project
1387-
.app_module_graphs(
1388-
self,
1389-
*rsc_entry,
1390-
server_action_loader,
1391-
// We only need the client runtime entries for pages not for Route Handlers
1392-
if is_app_page {
1393-
this.app_project.client_runtime_entries()
1394-
} else {
1395-
EvaluatableAssets::empty()
1396-
},
1397-
is_app_page,
1398-
)
1399-
.await?;
1367+
let module_graph = this.app_project.app_module_graph(
1368+
*rsc_entry,
1369+
server_action_loader,
1370+
// We only need the client runtime entries for pages not for Route Handlers
1371+
if is_app_page {
1372+
this.app_project.client_runtime_entries()
1373+
} else {
1374+
EvaluatableAssets::empty()
1375+
},
1376+
is_app_page,
1377+
);
14001378

14011379
let client_chunking_context = project.client_chunking_context().to_resolved().await?;
14021380

@@ -1422,7 +1400,7 @@ impl AppEndpoint {
14221400
AssetIdent::from_path(project.project_path().owned().await?)
14231401
.with_modifier(rcstr!("client-shared-chunks")),
14241402
this.app_project.client_runtime_entries(),
1425-
*module_graphs.full,
1403+
module_graph,
14261404
*client_chunking_context,
14271405
);
14281406

@@ -1439,27 +1417,25 @@ impl AppEndpoint {
14391417

14401418
let per_page_module_graph = *project.per_page_module_graph().await?;
14411419

1442-
let next_dynamic_imports =
1443-
NextDynamicGraphs::new(*module_graphs.base, per_page_module_graph)
1444-
.get_next_dynamic_imports_for_endpoint(*rsc_entry)
1445-
.await?;
1420+
let next_dynamic_imports = NextDynamicGraphs::new(module_graph, per_page_module_graph)
1421+
.get_next_dynamic_imports_for_endpoint(*rsc_entry)
1422+
.await?;
14461423

14471424
let is_production = project.next_mode().await?.is_production();
14481425

1449-
let client_references =
1450-
ClientReferencesGraphs::new(*module_graphs.base, per_page_module_graph)
1451-
.get_client_references_for_endpoint(
1452-
*rsc_entry,
1453-
matches!(this.ty, AppEndpointType::Page { .. }),
1454-
is_production,
1455-
is_production,
1456-
)
1457-
.to_resolved()
1458-
.await?;
1426+
let client_references = ClientReferencesGraphs::new(module_graph, per_page_module_graph)
1427+
.get_client_references_for_endpoint(
1428+
*rsc_entry,
1429+
matches!(this.ty, AppEndpointType::Page { .. }),
1430+
is_production,
1431+
is_production,
1432+
)
1433+
.to_resolved()
1434+
.await?;
14591435

14601436
let client_references_chunks = get_app_client_references_chunks(
14611437
*client_references,
1462-
*module_graphs.full,
1438+
module_graph,
14631439
*client_chunking_context,
14641440
availability_info,
14651441
ssr_chunking_context.map(|ctx| *ctx),
@@ -1541,7 +1517,7 @@ impl AppEndpoint {
15411517
.await?
15421518
{
15431519
let webpack_stats = generate_webpack_stats(
1544-
*module_graphs.base,
1520+
module_graph,
15451521
app_entry.original_name.clone(),
15461522
client_assets.await?.into_iter().copied(),
15471523
)
@@ -1595,7 +1571,7 @@ impl AppEndpoint {
15951571
node_root.clone(),
15961572
app_entry.original_name.clone(),
15971573
runtime,
1598-
*module_graphs.full,
1574+
module_graph,
15991575
this.app_project
16001576
.project()
16011577
.runtime_chunking_context(process_client_assets, runtime),
@@ -1610,7 +1586,7 @@ impl AppEndpoint {
16101586
*client_references,
16111587
server_path.clone(),
16121588
process_client_assets,
1613-
*module_graphs.full,
1589+
module_graph,
16141590
)
16151591
.to_resolved()
16161592
.await?;
@@ -1632,7 +1608,7 @@ impl AppEndpoint {
16321608
client_references_chunks,
16331609
client_chunking_context,
16341610
ssr_chunking_context,
1635-
async_module_info: module_graphs.full.async_module_info().to_resolved().await?,
1611+
async_module_info: module_graph.async_module_info().to_resolved().await?,
16361612
next_config: project.next_config().to_resolved().await?,
16371613
runtime,
16381614
mode: *project.next_mode().await?,
@@ -1715,7 +1691,7 @@ impl AppEndpoint {
17151691

17161692
if emit_manifests == EmitManifests::Full {
17171693
let dynamic_import_entries = collect_next_dynamic_chunks(
1718-
*module_graphs.full,
1694+
module_graph,
17191695
*client_chunking_context,
17201696
next_dynamic_imports,
17211697
NextDynamicChunkAvailability::ClientReferences(
@@ -1831,7 +1807,7 @@ impl AppEndpoint {
18311807
let loadable_manifest_output = if emit_manifests == EmitManifests::Full {
18321808
// create react-loadable-manifest for next/dynamic
18331809
let dynamic_import_entries = collect_next_dynamic_chunks(
1834-
*module_graphs.full,
1810+
module_graph,
18351811
*client_chunking_context,
18361812
next_dynamic_imports,
18371813
NextDynamicChunkAvailability::ClientReferences(
@@ -2203,17 +2179,17 @@ impl Endpoint for AppEndpoint {
22032179
let this = self.await?;
22042180
let app_entry = self.app_endpoint_entry().await?;
22052181
let server_action_loader = self.server_action_loader_module();
2206-
let module_graphs = this
2182+
let module_graph = this
22072183
.app_project
2208-
.app_module_graphs(
2209-
self,
2184+
.app_module_graph(
22102185
*app_entry.rsc_entry,
22112186
server_action_loader,
22122187
this.app_project.client_runtime_entries(),
22132188
matches!(this.ty, AppEndpointType::Page { .. }),
22142189
)
2190+
.to_resolved()
22152191
.await?;
2216-
Ok(Vc::cell(vec![module_graphs.full]))
2192+
Ok(Vc::cell(vec![module_graph]))
22172193
}
22182194

22192195
#[turbo_tasks::function]

crates/next-api/src/pages.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,18 +752,22 @@ impl PageEndpoint {
752752
.next_config()
753753
.turbopack_remove_unused_imports(next_mode)
754754
.await?;
755+
let remove_unused_exports = *project
756+
.next_config()
757+
.turbopack_remove_unused_exports(next_mode)
758+
.await?;
755759

756-
let graph = if remove_unused_imports {
760+
let graph = if remove_unused_imports || remove_unused_exports {
757761
let graph = ModuleGraph::from_graphs(graphs.clone(), None);
758-
let binding_usage_info = compute_binding_usage_info(graph, true);
762+
let binding_usage_info = compute_binding_usage_info(graph, remove_unused_imports);
759763
ModuleGraph::from_graphs(graphs, Some(binding_usage_info))
760764
} else {
761765
ModuleGraph::from_graphs(graphs, None)
762766
};
763767

764768
Ok(graph.connect())
765769
} else {
766-
Ok(*project.whole_app_module_graphs().await?.full)
770+
Ok(project.whole_app_module_graph())
767771
}
768772
}
769773

@@ -1733,6 +1737,7 @@ impl Endpoint for PageEndpoint {
17331737
async fn module_graphs(self: Vc<Self>) -> Result<Vc<ModuleGraphs>> {
17341738
let client_module_graph = self.client_module_graph().to_resolved().await?;
17351739
let ssr_module_graph = self.ssr_module_graph().to_resolved().await?;
1740+
// TODO refactor this so that there is a single module graph
17361741
Ok(Vc::cell(if client_module_graph != ssr_module_graph {
17371742
vec![client_module_graph, ssr_module_graph]
17381743
} else {

0 commit comments

Comments
 (0)