Skip to content

Commit ab3e892

Browse files
committed
Change to control chunking with a single bit and infer whole app module graph as part of that
1 parent bb9d85e commit ab3e892

File tree

10 files changed

+155
-82
lines changed

10 files changed

+155
-82
lines changed

crates/next-api/src/project.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ impl Project {
906906
.get_all_endpoints(false)
907907
.await?
908908
.iter()
909-
.map(|endpoint| endpoint.entries().owned())
909+
.map(async |endpoint| endpoint.entries().owned().await)
910910
.try_flat_join()
911911
.await?;
912912
modules.extend(self.client_main_modules().await?.iter().cloned());
@@ -922,7 +922,7 @@ impl Project {
922922
.get_all_endpoints(false)
923923
.await?
924924
.iter()
925-
.map(async |endpoint| Ok(endpoint.additional_entries(graphs).owned().await?))
925+
.map(async |endpoint| endpoint.additional_entries(graphs).owned().await)
926926
.try_flat_join()
927927
.await?;
928928
Ok(Vc::cell(modules))
@@ -1021,20 +1021,23 @@ impl Project {
10211021
pub(super) async fn client_chunking_context(
10221022
self: Vc<Self>,
10231023
) -> Result<Vc<Box<dyn ChunkingContext>>> {
1024+
let next_mode = self.next_mode();
1025+
let next_config = self.next_config();
10241026
Ok(get_client_chunking_context(ClientChunkingContextOptions {
1025-
mode: self.next_mode(),
1027+
mode: next_mode,
1028+
chunking_style: next_config.turbo_chunking_style(next_mode),
10261029
root_path: self.project_root_path().owned().await?,
10271030
client_root: self.client_relative_path().owned().await?,
10281031
client_root_to_root_path: rcstr!("/ROOT"),
1029-
asset_prefix: self.next_config().computed_asset_prefix(),
1030-
chunk_suffix_path: self.next_config().chunk_suffix_path(),
1032+
asset_prefix: next_config.computed_asset_prefix(),
1033+
chunk_suffix_path: next_config.chunk_suffix_path(),
10311034
environment: self.client_compile_time_info().environment(),
10321035
module_id_strategy: self.module_ids(),
10331036
export_usage: self.export_usage(),
1034-
minify: self.next_config().turbo_minify(self.next_mode()),
1035-
source_maps: self.next_config().client_source_maps(self.next_mode()),
1037+
minify: next_config.turbo_minify(next_mode),
1038+
source_maps: next_config.client_source_maps(next_mode),
10361039
no_mangling: self.no_mangling(),
1037-
scope_hoisting: self.next_config().turbo_scope_hoisting(self.next_mode()),
1040+
scope_hoisting: next_config.turbo_scope_hoisting(next_mode),
10381041
}))
10391042
}
10401043

@@ -1043,8 +1046,12 @@ impl Project {
10431046
self: Vc<Self>,
10441047
client_assets: bool,
10451048
) -> Result<Vc<NodeJsChunkingContext>> {
1049+
let next_mode = self.next_mode();
1050+
let next_config = self.next_config();
10461051
let options = ServerChunkingContextOptions {
1047-
mode: self.next_mode(),
1052+
mode: next_mode,
1053+
1054+
chunking_style: next_config.turbo_chunking_style(next_mode),
10481055
root_path: self.project_root_path().owned().await?,
10491056
node_root: self.node_root().owned().await?,
10501057
node_root_to_root_path: self.node_root_to_root_path().owned().await?,

crates/next-core/src/next_client/context.rs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use turbopack_browser::{
1919
};
2020
use turbopack_core::{
2121
chunk::{
22-
ChunkingConfig, ChunkingContext, MangleType, MinifyType, SourceMapsType,
22+
ChunkingConfig, ChunkingContext, ChunkingStyle, MangleType, MinifyType, SourceMapsType,
2323
module_id_strategies::ModuleIdStrategy,
2424
},
2525
compile_time_info::{CompileTimeDefines, CompileTimeInfo, FreeVarReference, FreeVarReferences},
@@ -401,6 +401,7 @@ pub async fn get_client_module_options_context(
401401
#[derive(Clone, Debug, PartialEq, Eq, Hash, TaskInput, TraceRawVcs, Serialize, Deserialize)]
402402
pub struct ClientChunkingContextOptions {
403403
pub mode: Vc<NextMode>,
404+
pub chunking_style: Vc<ChunkingStyle>,
404405
pub root_path: FileSystemPath,
405406
pub client_root: FileSystemPath,
406407
pub client_root_to_root_path: RcStr,
@@ -421,6 +422,7 @@ pub async fn get_client_chunking_context(
421422
) -> Result<Vc<Box<dyn ChunkingContext>>> {
422423
let ClientChunkingContextOptions {
423424
mode,
425+
chunking_style,
424426
root_path,
425427
client_root,
426428
client_root_to_root_path,
@@ -470,29 +472,35 @@ pub async fn get_client_chunking_context(
470472
if next_mode.is_development() {
471473
builder = builder
472474
.hot_module_replacement()
473-
.use_file_source_map_uris()
474475
.dynamic_chunk_content_loading(true);
475-
} else {
476-
builder = builder
477-
.chunking_config(
478-
Vc::<EcmascriptChunkType>::default().to_resolved().await?,
479-
ChunkingConfig {
480-
min_chunk_size: 50_000,
481-
max_chunk_count_per_group: 40,
482-
max_merge_chunk_size: 200_000,
483-
..Default::default()
484-
},
485-
)
486-
.chunking_config(
487-
Vc::<CssChunkType>::default().to_resolved().await?,
488-
ChunkingConfig {
489-
max_merge_chunk_size: 100_000,
490-
..Default::default()
491-
},
492-
)
493-
.use_content_hashing(ContentHashing::Direct { length: 16 })
494-
.module_merging(*scope_hoisting.await?);
495476
}
477+
match *chunking_style.await? {
478+
ChunkingStyle::Production => {
479+
builder = builder
480+
.chunking_config(
481+
Vc::<EcmascriptChunkType>::default().to_resolved().await?,
482+
ChunkingConfig {
483+
min_chunk_size: 50_000,
484+
max_chunk_count_per_group: 40,
485+
max_merge_chunk_size: 200_000,
486+
..Default::default()
487+
},
488+
)
489+
.chunking_config(
490+
Vc::<CssChunkType>::default().to_resolved().await?,
491+
ChunkingConfig {
492+
max_merge_chunk_size: 100_000,
493+
..Default::default()
494+
},
495+
)
496+
.use_content_hashing(ContentHashing::Direct { length: 16 });
497+
}
498+
ChunkingStyle::Development => {
499+
builder = builder.use_file_source_map_uris();
500+
}
501+
}
502+
503+
builder = builder.module_merging(*scope_hoisting.await?);
496504

497505
Ok(Vc::upcast(builder.build()))
498506
}

crates/next-core/src/next_config.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use turbopack::module_options::{
1717
module_options_context::MdxTransformOptions,
1818
};
1919
use turbopack_core::{
20+
chunk::ChunkingStyle,
2021
issue::{Issue, IssueExt, IssueStage, OptionStyledString, StyledString},
2122
resolve::ResolveAliasMap,
2223
};
@@ -875,7 +876,7 @@ pub struct ExperimentalConfig {
875876
turbopack_tree_shaking: Option<bool>,
876877
turbopack_scope_hoisting: Option<bool>,
877878
turbopack_use_system_tls_certs: Option<bool>,
878-
turbopack_use_whole_app_module_graph_in_dev: Option<bool>,
879+
turbopack_use_production_chunking_in_dev: Option<bool>,
879880
/// Disable automatic configuration of the sass loader.
880881
#[serde(default)]
881882
turbopack_use_builtin_sass: Option<bool>,
@@ -1818,13 +1819,34 @@ impl NextConfig {
18181819
#[turbo_tasks::function]
18191820
pub async fn turbo_use_whole_app_module_graph(&self, mode: Vc<NextMode>) -> Result<Vc<bool>> {
18201821
Ok(Vc::cell(match *mode.await? {
1822+
// If we are using production chunking in development mode, we need to use the whole app
1823+
// module graph as well.
18211824
NextMode::Development => self
18221825
.experimental
1823-
.turbopack_use_whole_app_module_graph_in_dev
1826+
.turbopack_use_production_chunking_in_dev
18241827
.unwrap_or(false),
18251828
NextMode::Build => true,
18261829
}))
18271830
}
1831+
1832+
#[turbo_tasks::function]
1833+
pub async fn turbo_chunking_style(&self, mode: Vc<NextMode>) -> Result<Vc<ChunkingStyle>> {
1834+
Ok(match *mode.await? {
1835+
NextMode::Development => {
1836+
if self
1837+
.experimental
1838+
.turbopack_use_production_chunking_in_dev
1839+
.unwrap_or(false)
1840+
{
1841+
ChunkingStyle::production()
1842+
} else {
1843+
ChunkingStyle::development()
1844+
}
1845+
}
1846+
NextMode::Build => ChunkingStyle::production(),
1847+
})
1848+
}
1849+
18281850
#[turbo_tasks::function]
18291851
pub async fn client_source_maps(&self, mode: Vc<NextMode>) -> Result<Vc<bool>> {
18301852
let source_maps = self.experimental.turbopack_source_maps;

crates/next-core/src/next_server/context.rs

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use turbopack::{
1616
};
1717
use turbopack_core::{
1818
chunk::{
19-
ChunkingConfig, MangleType, MinifyType, SourceMapsType,
19+
ChunkingConfig, ChunkingStyle, MangleType, MinifyType, SourceMapsType,
2020
module_id_strategies::ModuleIdStrategy,
2121
},
2222
compile_time_defines,
@@ -1032,6 +1032,7 @@ pub fn get_server_runtime_entries(
10321032
#[derive(Clone, Debug, PartialEq, Eq, Hash, TaskInput, TraceRawVcs, Serialize, Deserialize)]
10331033
pub struct ServerChunkingContextOptions {
10341034
pub mode: Vc<NextMode>,
1035+
pub chunking_style: Vc<ChunkingStyle>,
10351036
pub root_path: FileSystemPath,
10361037
pub node_root: FileSystemPath,
10371038
pub node_root_to_root_path: RcStr,
@@ -1052,6 +1053,7 @@ pub async fn get_server_chunking_context_with_client_assets(
10521053
) -> Result<Vc<NodeJsChunkingContext>> {
10531054
let ServerChunkingContextOptions {
10541055
mode,
1056+
chunking_style,
10551057
root_path,
10561058
node_root,
10571059
node_root_to_root_path,
@@ -1096,28 +1098,31 @@ pub async fn get_server_chunking_context_with_client_assets(
10961098
.export_usage(*export_usage.await?)
10971099
.file_tracing(next_mode.is_production());
10981100

1099-
if next_mode.is_development() {
1100-
builder = builder.use_file_source_map_uris();
1101-
} else {
1102-
builder = builder
1103-
.chunking_config(
1104-
Vc::<EcmascriptChunkType>::default().to_resolved().await?,
1105-
ChunkingConfig {
1106-
min_chunk_size: 20_000,
1107-
max_chunk_count_per_group: 100,
1108-
max_merge_chunk_size: 100_000,
1109-
..Default::default()
1110-
},
1111-
)
1112-
.chunking_config(
1113-
Vc::<CssChunkType>::default().to_resolved().await?,
1114-
ChunkingConfig {
1115-
max_merge_chunk_size: 100_000,
1116-
..Default::default()
1117-
},
1118-
)
1119-
.module_merging(*scope_hoisting.await?);
1101+
match *chunking_style.await? {
1102+
ChunkingStyle::Development => {
1103+
builder = builder.use_file_source_map_uris();
1104+
}
1105+
ChunkingStyle::Production => {
1106+
builder = builder
1107+
.chunking_config(
1108+
Vc::<EcmascriptChunkType>::default().to_resolved().await?,
1109+
ChunkingConfig {
1110+
min_chunk_size: 20_000,
1111+
max_chunk_count_per_group: 100,
1112+
max_merge_chunk_size: 100_000,
1113+
..Default::default()
1114+
},
1115+
)
1116+
.chunking_config(
1117+
Vc::<CssChunkType>::default().to_resolved().await?,
1118+
ChunkingConfig {
1119+
max_merge_chunk_size: 100_000,
1120+
..Default::default()
1121+
},
1122+
)
1123+
}
11201124
}
1125+
builder = builder.module_merging(*scope_hoisting.await?);
11211126

11221127
Ok(builder.build())
11231128
}
@@ -1128,6 +1133,7 @@ pub async fn get_server_chunking_context(
11281133
) -> Result<Vc<NodeJsChunkingContext>> {
11291134
let ServerChunkingContextOptions {
11301135
mode,
1136+
chunking_style,
11311137
root_path,
11321138
node_root,
11331139
node_root_to_root_path,
@@ -1169,28 +1175,31 @@ pub async fn get_server_chunking_context(
11691175
.export_usage(*export_usage.await?)
11701176
.file_tracing(next_mode.is_production());
11711177

1172-
if next_mode.is_development() {
1173-
builder = builder.use_file_source_map_uris()
1174-
} else {
1175-
builder = builder
1176-
.chunking_config(
1177-
Vc::<EcmascriptChunkType>::default().to_resolved().await?,
1178-
ChunkingConfig {
1179-
min_chunk_size: 20_000,
1180-
max_chunk_count_per_group: 100,
1181-
max_merge_chunk_size: 100_000,
1182-
..Default::default()
1183-
},
1184-
)
1185-
.chunking_config(
1186-
Vc::<CssChunkType>::default().to_resolved().await?,
1187-
ChunkingConfig {
1188-
max_merge_chunk_size: 100_000,
1189-
..Default::default()
1190-
},
1191-
)
1192-
.module_merging(*scope_hoisting.await?);
1178+
match *chunking_style.await? {
1179+
ChunkingStyle::Development => {
1180+
builder = builder.use_file_source_map_uris();
1181+
}
1182+
ChunkingStyle::Production => {
1183+
builder = builder
1184+
.chunking_config(
1185+
Vc::<EcmascriptChunkType>::default().to_resolved().await?,
1186+
ChunkingConfig {
1187+
min_chunk_size: 20_000,
1188+
max_chunk_count_per_group: 100,
1189+
max_merge_chunk_size: 100_000,
1190+
..Default::default()
1191+
},
1192+
)
1193+
.chunking_config(
1194+
Vc::<CssChunkType>::default().to_resolved().await?,
1195+
ChunkingConfig {
1196+
max_merge_chunk_size: 100_000,
1197+
..Default::default()
1198+
},
1199+
)
1200+
}
11931201
}
1202+
builder = builder.module_merging(*scope_hoisting.await?);
11941203

11951204
Ok(builder.build())
11961205
}

packages/next/src/server/config-schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
475475
turbopackUseSystemTlsCerts: z.boolean().optional(),
476476
turbopackUseBuiltinBabel: z.boolean().optional(),
477477
turbopackUseBuiltinSass: z.boolean().optional(),
478-
turbopackUseWholeAppModuleGraphInDev: z.boolean().optional(),
478+
turbopackUseProductionChunkingInDev: z.boolean().optional(),
479479
optimizePackageImports: z.array(z.string()).optional(),
480480
optimizeServerReact: z.boolean().optional(),
481481
clientTraceMetadata: z.array(z.string()).optional(),

packages/next/src/server/config-shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,9 @@ export interface ExperimentalConfig {
654654
turbopackUseBuiltinSass?: boolean
655655

656656
/**
657-
* Whether to use the whole app module graph in development mode. This makes development mode chunking more like production but may come with an HMR performance regression.
657+
* Whether to use production chunking in development mode. This makes development mode chunking more like production but may come with an HMR performance regression.
658658
*/
659-
turbopackUseWholeAppModuleGraphInDev?: boolean
659+
turbopackUseProductionChunkingInDev?: boolean
660660

661661
/**
662662
* For use with `@next/mdx`. Compile MDX files using the new Rust compiler.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
module.exports = {}
1+
module.exports = {
2+
experimental: {
3+
turbopackUseProductionChunkingInDev: true,
4+
},
5+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
module.exports = {}
1+
module.exports = {
2+
experimental: {
3+
turbopackUseProductionChunkingInDev: true,
4+
},
5+
}

turbopack/crates/turbopack-core/src/chunk/chunking_context.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ pub struct EntryChunkGroupResult {
9898
pub availability_info: AvailabilityInfo,
9999
}
100100

101+
#[turbo_tasks::value]
102+
pub enum ChunkingStyle {
103+
Production,
104+
Development,
105+
}
106+
#[turbo_tasks::value_impl]
107+
impl ChunkingStyle {
108+
#[turbo_tasks::function]
109+
pub fn production() -> Vc<Self> {
110+
ChunkingStyle::Production.cell()
111+
}
112+
113+
#[turbo_tasks::function]
114+
pub fn development() -> Vc<Self> {
115+
ChunkingStyle::Development.cell()
116+
}
117+
}
118+
101119
#[derive(
102120
Default,
103121
Debug,

0 commit comments

Comments
 (0)