Skip to content

Commit a983e02

Browse files
committed
Named page chunks
1 parent 7dfa56c commit a983e02

File tree

9 files changed

+216
-206
lines changed

9 files changed

+216
-206
lines changed

packages/next-swc/crates/next-build/src/next_build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu
115115

116116
let page_chunks = get_page_chunks(
117117
pages_structure,
118+
next_router_root,
118119
project_root,
119120
execution_context,
120121
node_root,

packages/next-swc/crates/next-build/src/next_pages/node_context.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::{bail, Result};
22
use next_core::{next_client::RuntimeEntriesVc, turbopack::core::chunk::EvaluatableAssetsVc};
3+
use turbo_tasks::primitives::StringVc;
34
use turbopack_binding::{
45
turbo::{tasks::Value, tasks_fs::FileSystemPathVc},
56
turbopack::{
@@ -10,7 +11,7 @@ use turbopack_binding::{
1011
reference_type::{EntryReferenceSubType, ReferenceType},
1112
resolve::{parse::RequestVc, pattern::QueryMapVc},
1213
},
13-
ecmascript::EcmascriptModuleAssetVc,
14+
ecmascript::chunk::EcmascriptChunkPlaceableVc,
1415
},
1516
};
1617

@@ -84,17 +85,24 @@ impl PagesBuildNodeContextVc {
8485
pub async fn node_chunk(
8586
self,
8687
asset: AssetVc,
88+
pathname: StringVc,
8789
reference_type: Value<ReferenceType>,
8890
) -> Result<AssetVc> {
8991
let this = self.await?;
9092

9193
let node_asset_page = this.node_asset_context.process(asset, reference_type);
9294

93-
let Some(node_module_asset) = EcmascriptModuleAssetVc::resolve_from(node_asset_page).await? else {
95+
let Some(node_module_asset) = EcmascriptChunkPlaceableVc::resolve_from(node_asset_page).await? else {
9496
bail!("Expected an EcmaScript module asset");
9597
};
9698

99+
let pathname = pathname.await?;
100+
97101
let chunking_context = self.node_chunking_context();
98-
Ok(chunking_context.generate_exported_chunk(node_module_asset, this.node_runtime_entries))
102+
Ok(chunking_context.generate_entry_chunk(
103+
this.node_root.join(&format!("server/pages/{pathname}.js")),
104+
node_module_asset,
105+
this.node_runtime_entries,
106+
))
99107
}
100108
}

packages/next-swc/crates/next-build/src/next_pages/page_chunks.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use next_core::{
1414
get_server_resolve_options_context, ServerContextType,
1515
},
1616
pages_structure::{
17-
OptionPagesStructureVc, PagesDirectoryStructure, PagesDirectoryStructureVc, PagesStructure,
18-
PagesStructureItem, PagesStructureVc,
17+
PagesDirectoryStructure, PagesDirectoryStructureVc, PagesStructure, PagesStructureItem,
18+
PagesStructureVc,
1919
},
2020
pathname_for_path,
2121
turbopack::core::asset::AssetsVc,
@@ -57,7 +57,8 @@ impl PageChunksVc {
5757
/// Returns a list of page chunks.
5858
#[turbo_tasks::function]
5959
pub async fn get_page_chunks(
60-
pages_structure: OptionPagesStructureVc,
60+
pages_structure: PagesStructureVc,
61+
next_router_root: FileSystemPathVc,
6162
project_root: FileSystemPathVc,
6263
execution_context: ExecutionContextVc,
6364
node_root: FileSystemPathVc,
@@ -67,10 +68,11 @@ pub async fn get_page_chunks(
6768
next_config: NextConfigVc,
6869
node_addr: ServerAddrVc,
6970
) -> Result<PageChunksVc> {
70-
let Some(pages_structure) = *pages_structure.await? else {
71-
return Ok(PageChunksVc::empty());
71+
let pages_dir = if let Some(pages) = pages_structure.await?.pages {
72+
pages.project_path().resolve().await?
73+
} else {
74+
project_root.join("pages")
7275
};
73-
let pages_dir = pages_structure.project_path().resolve().await?;
7476

7577
let mode = NextMode::Build;
7678

@@ -175,6 +177,7 @@ pub async fn get_page_chunks(
175177
node_build_context,
176178
client_build_context,
177179
pages_structure,
180+
next_router_root,
178181
))
179182
}
180183

@@ -183,6 +186,7 @@ async fn get_page_chunks_for_root_directory(
183186
node_build_context: PagesBuildNodeContextVc,
184187
client_build_context: PagesBuildClientContextVc,
185188
pages_structure: PagesStructureVc,
189+
next_router_root: FileSystemPathVc,
186190
) -> Result<PageChunksVc> {
187191
let PagesStructure {
188192
app,
@@ -193,8 +197,6 @@ async fn get_page_chunks_for_root_directory(
193197
} = *pages_structure.await?;
194198
let mut chunks = vec![];
195199

196-
let next_router_root = pages.next_router_path();
197-
198200
// This only makes sense on both the client and the server, but they should map
199201
// to different assets (server can be an external module).
200202
let app = app.await?;
@@ -241,17 +243,19 @@ async fn get_page_chunks_for_root_directory(
241243
);
242244
}
243245

244-
chunks.extend(
245-
get_page_chunks_for_directory(
246-
node_build_context,
247-
client_build_context,
248-
pages,
249-
next_router_root,
250-
)
251-
.await?
252-
.iter()
253-
.copied(),
254-
);
246+
if let Some(pages) = pages {
247+
chunks.extend(
248+
get_page_chunks_for_directory(
249+
node_build_context,
250+
client_build_context,
251+
pages,
252+
next_router_root,
253+
)
254+
.await?
255+
.iter()
256+
.copied(),
257+
);
258+
}
255259

256260
Ok(PageChunksVc::cell(chunks))
257261
}
@@ -328,7 +332,7 @@ async fn get_page_chunk_for_file(
328332

329333
Ok(PageChunk {
330334
pathname,
331-
node_chunk: node_build_context.node_chunk(page_asset, reference_type.clone()),
335+
node_chunk: node_build_context.node_chunk(page_asset, pathname, reference_type.clone()),
332336
client_chunks: client_build_context.client_chunk(page_asset, pathname, reference_type),
333337
}
334338
.cell())

packages/next-swc/crates/next-core/js/src/build/client/bootstrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* This is the runtime entry point for Next.js client-side bundles.
2+
* This is the runtime entry point for Next.js Page Router client-side bundles.
33
*/
44

55
import './shims'

packages/next-swc/crates/next-core/src/app_source.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use turbopack_binding::{
1616
asset::{AssetVc, AssetsVc},
1717
chunk::{EvaluatableAssetVc, EvaluatableAssetsVc},
1818
compile_time_info::CompileTimeInfoVc,
19-
context::{AssetContext, AssetContextVc},
19+
context::AssetContext,
2020
environment::{EnvironmentIntention, ServerAddrVc},
2121
issue::{Issue, IssueSeverity, IssueSeverityVc, IssueVc},
2222
reference_type::{
@@ -360,7 +360,7 @@ fn app_context(
360360
next_config: NextConfigVc,
361361
server_addr: ServerAddrVc,
362362
output_path: FileSystemPathVc,
363-
) -> AssetContextVc {
363+
) -> ModuleAssetContextVc {
364364
let next_server_to_client_transition = NextServerToClientTransition { ssr }.cell().into();
365365
let mode = NextMode::Development;
366366

@@ -473,7 +473,6 @@ fn app_context(
473473
execution_context,
474474
),
475475
)
476-
.into()
477476
}
478477

479478
/// Create a content source serving the `app` or `src/app` directory as
@@ -651,8 +650,8 @@ async fn create_global_metadata_source(
651650
async fn create_app_page_source_for_route(
652651
pathname: &str,
653652
loader_tree: LoaderTreeVc,
654-
context_ssr: AssetContextVc,
655-
context: AssetContextVc,
653+
context_ssr: ModuleAssetContextVc,
654+
context: ModuleAssetContextVc,
656655
project_path: FileSystemPathVc,
657656
app_dir: FileSystemPathVc,
658657
env: ProcessEnvVc,
@@ -697,8 +696,8 @@ async fn create_app_page_source_for_route(
697696
#[turbo_tasks::function]
698697
async fn create_app_not_found_page_source(
699698
loader_tree: LoaderTreeVc,
700-
context_ssr: AssetContextVc,
701-
context: AssetContextVc,
699+
context_ssr: ModuleAssetContextVc,
700+
context: ModuleAssetContextVc,
702701
project_path: FileSystemPathVc,
703702
app_dir: FileSystemPathVc,
704703
env: ProcessEnvVc,
@@ -742,7 +741,7 @@ async fn create_app_not_found_page_source(
742741
async fn create_app_route_source_for_route(
743742
pathname: &str,
744743
entry_path: FileSystemPathVc,
745-
context_ssr: AssetContextVc,
744+
context_ssr: ModuleAssetContextVc,
746745
project_path: FileSystemPathVc,
747746
app_dir: FileSystemPathVc,
748747
env: ProcessEnvVc,
@@ -786,8 +785,8 @@ async fn create_app_route_source_for_route(
786785
struct AppRenderer {
787786
runtime_entries: AssetsVc,
788787
app_dir: FileSystemPathVc,
789-
context_ssr: AssetContextVc,
790-
context: AssetContextVc,
788+
context_ssr: ModuleAssetContextVc,
789+
context: ModuleAssetContextVc,
791790
project_path: FileSystemPathVc,
792791
server_root: FileSystemPathVc,
793792
intermediate_output_path: FileSystemPathVc,
@@ -815,7 +814,7 @@ impl AppRendererVc {
815814
(context_ssr, intermediate_output_path)
816815
};
817816

818-
let config = parse_segment_config_from_loader_tree(loader_tree, context);
817+
let config = parse_segment_config_from_loader_tree(loader_tree, context.into());
819818

820819
let runtime = config.await?.runtime;
821820
let rsc_transition = match runtime {
@@ -828,7 +827,7 @@ impl AppRendererVc {
828827
counter: usize,
829828
imports: Vec<String>,
830829
loader_tree_code: String,
831-
context: AssetContextVc,
830+
context: ModuleAssetContextVc,
832831
unsupported_metadata: Vec<FileSystemPathVc>,
833832
rsc_transition: &'static str,
834833
}
@@ -927,8 +926,11 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}";
927926
.push(format!("import {identifier} from \"{inner_module_id}\";"));
928927
state.inner_assets.insert(
929928
inner_module_id,
930-
StaticModuleAssetVc::new(SourceAssetVc::new(path).into(), state.context)
931-
.into(),
929+
StaticModuleAssetVc::new(
930+
SourceAssetVc::new(path).into(),
931+
state.context.into(),
932+
)
933+
.into(),
932934
);
933935
writeln!(state.loader_tree_code, " manifest: {identifier},")?;
934936
}
@@ -1156,7 +1158,7 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}";
11561158
runtime_entries
11571159
.await?
11581160
.iter()
1159-
.map(|entry| EvaluatableAssetVc::from_asset(*entry, context))
1161+
.map(|entry| EvaluatableAssetVc::from_asset(*entry, context.into()))
11601162
.collect(),
11611163
),
11621164
module,
@@ -1188,7 +1190,7 @@ impl NodeEntry for AppRenderer {
11881190
#[turbo_tasks::value]
11891191
struct AppRoute {
11901192
runtime_entries: AssetsVc,
1191-
context: AssetContextVc,
1193+
context: ModuleAssetContextVc,
11921194
entry_path: FileSystemPathVc,
11931195
intermediate_output_path: FileSystemPathVc,
11941196
project_path: FileSystemPathVc,
@@ -1232,7 +1234,7 @@ impl AppRouteVc {
12321234

12331235
route_bootstrap(
12341236
entry_asset,
1235-
this.context,
1237+
this.context.into(),
12361238
this.project_path,
12371239
bootstrap_asset,
12381240
BootstrapConfigVc::empty(),
@@ -1266,7 +1268,7 @@ impl AppRouteVc {
12661268
this.runtime_entries
12671269
.await?
12681270
.iter()
1269-
.map(|entry| EvaluatableAssetVc::from_asset(*entry, this.context))
1271+
.map(|entry| EvaluatableAssetVc::from_asset(*entry, this.context.into()))
12701272
.collect(),
12711273
),
12721274
module,

packages/next-swc/crates/next-core/src/next_image/module.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ use turbopack_binding::{
55
turbopack::{
66
core::{
77
asset::AssetVc,
8-
context::{AssetContext, AssetContextVc},
9-
plugin::{CustomModuleType, CustomModuleTypeVc},
8+
context::AssetContext,
109
reference_type::{InnerAssetsVc, ReferenceType},
1110
resolve::ModulePartVc,
1211
},
1312
r#static::StaticModuleAssetVc,
13+
turbopack::{
14+
module_options::{CustomModuleType, CustomModuleTypeVc},
15+
ModuleAssetContextVc,
16+
},
1417
},
1518
};
1619

@@ -43,9 +46,9 @@ impl StructuredImageModuleType {
4346
pub(crate) fn create_module(
4447
source: AssetVc,
4548
blur_placeholder_mode: BlurPlaceholderMode,
46-
context: AssetContextVc,
49+
context: ModuleAssetContextVc,
4750
) -> AssetVc {
48-
let static_asset = StaticModuleAssetVc::new(source, context);
51+
let static_asset = StaticModuleAssetVc::new(source, context.into());
4952
context.process(
5053
StructuredImageSourceAsset {
5154
image: source,
@@ -76,7 +79,7 @@ impl CustomModuleType for StructuredImageModuleType {
7679
fn create_module(
7780
&self,
7881
source: AssetVc,
79-
context: AssetContextVc,
82+
context: ModuleAssetContextVc,
8083
_part: Option<ModulePartVc>,
8184
) -> AssetVc {
8285
StructuredImageModuleType::create_module(source, self.blur_placeholder_mode, context)

0 commit comments

Comments
 (0)