Skip to content

Commit 34772a6

Browse files
committed
Turbopack: Merge babel-loader and react-compiler configuration logic to avoid running babel twice
1 parent 2f2f6c1 commit 34772a6

File tree

10 files changed

+308
-145
lines changed

10 files changed

+308
-145
lines changed

crates/next-api/src/app.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,9 +1337,7 @@ impl AppEndpoint {
13371337

13381338
// polyfill-nomodule.js is a pre-compiled asset distributed as part of next,
13391339
// load it as a RawModule.
1340-
let next_package = get_next_package(project.project_path().owned().await?)
1341-
.owned()
1342-
.await?;
1340+
let next_package = get_next_package(project.project_path().owned().await?).await?;
13431341
let polyfill_source =
13441342
FileSource::new(next_package.join("dist/build/polyfills/polyfill-nomodule.js")?);
13451343
let polyfill_output_path = client_chunking_context

crates/next-core/src/next_build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub async fn get_postcss_package_mapping(
1616
.resolved_cell(),
1717
ImportMapping::PrimaryAlternative(
1818
rcstr!("postcss"),
19-
Some(get_next_package(project_path.clone()).owned().await?),
19+
Some(get_next_package(project_path.clone()).await?),
2020
)
2121
.resolved_cell(),
2222
])

crates/next-core/src/next_config.rs

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -736,21 +736,42 @@ pub enum MdxRsOptions {
736736
#[turbo_tasks::value(shared, operation)]
737737
#[derive(Clone, Debug)]
738738
#[serde(rename_all = "camelCase")]
739-
pub enum ReactCompilerMode {
739+
pub enum ReactCompilerCompilationMode {
740740
Infer,
741741
Annotation,
742742
All,
743743
}
744744

745-
/// Subset of react compiler options
745+
impl Default for ReactCompilerCompilationMode {
746+
fn default() -> Self {
747+
Self::Infer
748+
}
749+
}
750+
746751
#[turbo_tasks::value(shared, operation)]
747752
#[derive(Clone, Debug)]
753+
#[serde(rename_all = "snake_case")]
754+
pub enum ReactCompilerPanicThreshold {
755+
None,
756+
CriticalErrors,
757+
AllErrors,
758+
}
759+
760+
impl Default for ReactCompilerPanicThreshold {
761+
fn default() -> Self {
762+
Self::None
763+
}
764+
}
765+
766+
/// Subset of react compiler options
767+
#[turbo_tasks::value(shared, operation)]
768+
#[derive(Clone, Debug, Default)]
748769
#[serde(rename_all = "camelCase")]
749770
pub struct ReactCompilerOptions {
750-
#[serde(skip_serializing_if = "Option::is_none")]
751-
pub compilation_mode: Option<ReactCompilerMode>,
752-
#[serde(skip_serializing_if = "Option::is_none")]
753-
pub panic_threshold: Option<RcStr>,
771+
#[serde(default)]
772+
pub compilation_mode: ReactCompilerCompilationMode,
773+
#[serde(default)]
774+
pub panic_threshold: ReactCompilerPanicThreshold,
754775
}
755776

756777
#[derive(
@@ -1303,6 +1324,14 @@ impl NextConfig {
13031324
Ok(config.cell())
13041325
}
13051326

1327+
#[turbo_tasks::function]
1328+
pub async fn config_file_path(
1329+
&self,
1330+
project_path: FileSystemPath,
1331+
) -> Result<Vc<FileSystemPath>> {
1332+
Ok(project_path.join(&self.config_file_name)?.cell())
1333+
}
1334+
13061335
#[turbo_tasks::function]
13071336
pub fn bundle_pages_router_dependencies(&self) -> Vc<bool> {
13081337
Vc::cell(self.bundle_pages_router_dependencies.unwrap_or_default())
@@ -1387,8 +1416,12 @@ impl NextConfig {
13871416
}
13881417

13891418
#[turbo_tasks::function]
1390-
pub async fn webpack_rules(&self, project_path: FileSystemPath) -> Result<Vc<WebpackRules>> {
1391-
let Some(turbo_rules) = self.turbopack.as_ref().and_then(|t| t.rules.as_ref()) else {
1419+
pub async fn webpack_rules(
1420+
self: Vc<Self>,
1421+
project_path: FileSystemPath,
1422+
) -> Result<Vc<WebpackRules>> {
1423+
let this = self.await?;
1424+
let Some(turbo_rules) = this.turbopack.as_ref().and_then(|t| t.rules.as_ref()) else {
13921425
return Ok(Vc::cell(Vec::new()));
13931426
};
13941427
if turbo_rules.is_empty() {
@@ -1411,7 +1444,6 @@ impl NextConfig {
14111444
.collect(),
14121445
)
14131446
}
1414-
let config_file_path = || project_path.join(&self.config_file_name);
14151447
for item in &rule_collection.0 {
14161448
match item {
14171449
RuleConfigCollectionItem::Shorthand(loaders) => {
@@ -1438,7 +1470,10 @@ impl NextConfig {
14381470
{
14391471
InvalidLoaderRuleRenameAsIssue {
14401472
glob: glob.clone(),
1441-
config_file_path: config_file_path()?,
1473+
config_file_path: self
1474+
.config_file_path(project_path.clone())
1475+
.owned()
1476+
.await?,
14421477
rename_as: rename_as.clone(),
14431478
}
14441479
.resolved_cell()
@@ -1453,7 +1488,10 @@ impl NextConfig {
14531488
} else {
14541489
InvalidLoaderRuleConditionIssue {
14551490
condition: condition.clone(),
1456-
config_file_path: config_file_path()?,
1491+
config_file_path: self
1492+
.config_file_path(project_path.clone())
1493+
.owned()
1494+
.await?,
14571495
}
14581496
.resolved_cell()
14591497
.emit();
@@ -1604,18 +1642,12 @@ impl NextConfig {
16041642
}
16051643

16061644
#[turbo_tasks::function]
1607-
pub fn react_compiler(&self) -> Vc<OptionalReactCompilerOptions> {
1645+
pub fn react_compiler_options(&self) -> Vc<OptionalReactCompilerOptions> {
16081646
let options = &self.experimental.react_compiler;
16091647

16101648
let options = match options {
16111649
Some(ReactCompilerOptionsOrBoolean::Boolean(true)) => {
1612-
OptionalReactCompilerOptions(Some(
1613-
ReactCompilerOptions {
1614-
compilation_mode: None,
1615-
panic_threshold: None,
1616-
}
1617-
.resolved_cell(),
1618-
))
1650+
OptionalReactCompilerOptions(Some(ReactCompilerOptions::default().resolved_cell()))
16191651
}
16201652
Some(ReactCompilerOptionsOrBoolean::Option(options)) => OptionalReactCompilerOptions(
16211653
Some(ReactCompilerOptions { ..options.clone() }.resolved_cell()),

crates/next-core/src/next_import_map.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
embed_js::{VIRTUAL_PACKAGE_NAME, next_js_fs},
2626
mode::NextMode,
2727
next_client::context::ClientContextType,
28-
next_config::NextConfig,
28+
next_config::{NextConfig, OptionFileSystemPath},
2929
next_edge::unsupported::NextEdgeUnsupportedModuleReplacer,
3030
next_font::google::{
3131
GOOGLE_FONTS_INTERNAL_PREFIX, NextFontGoogleCssModuleReplacer,
@@ -710,7 +710,7 @@ async fn insert_next_server_special_aliases(
710710
ServerContextType::AppSSR { app_dir }
711711
| ServerContextType::AppRSC { app_dir, .. }
712712
| ServerContextType::AppRoute { app_dir, .. } => {
713-
let next_package = get_next_package(app_dir.clone()).owned().await?;
713+
let next_package = get_next_package(app_dir.clone()).await?;
714714
import_map.insert_exact_alias(
715715
rcstr!("styled-jsx"),
716716
request_to_import_mapping(next_package.clone(), rcstr!("styled-jsx")),
@@ -1142,7 +1142,7 @@ async fn insert_next_shared_aliases(
11421142
.resolved_cell(),
11431143
);
11441144

1145-
let next_package = get_next_package(project_path.clone()).owned().await?;
1145+
let next_package = get_next_package(project_path.clone()).await?;
11461146
import_map.insert_singleton_alias(rcstr!("@swc/helpers"), next_package.clone());
11471147
import_map.insert_singleton_alias(rcstr!("styled-jsx"), next_package.clone());
11481148
import_map.insert_singleton_alias(rcstr!("next"), project_path.clone());
@@ -1241,19 +1241,28 @@ async fn insert_next_shared_aliases(
12411241
Ok(())
12421242
}
12431243

1244+
pub async fn get_next_package(context_directory: FileSystemPath) -> Result<FileSystemPath> {
1245+
try_get_next_package(context_directory)
1246+
.owned()
1247+
.await?
1248+
.context("Next.js package not found")
1249+
}
1250+
12441251
#[turbo_tasks::function]
1245-
pub async fn get_next_package(context_directory: FileSystemPath) -> Result<Vc<FileSystemPath>> {
1252+
pub async fn try_get_next_package(
1253+
context_directory: FileSystemPath,
1254+
) -> Result<Vc<OptionFileSystemPath>> {
12461255
let result = resolve(
12471256
context_directory.clone(),
12481257
ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined),
12491258
Request::parse(Pattern::Constant(rcstr!("next/package.json"))),
12501259
node_cjs_resolve_options(context_directory.root().owned().await?),
12511260
);
1252-
let source = result
1253-
.first_source()
1254-
.await?
1255-
.context("Next.js package not found")?;
1256-
Ok(source.ident().path().await?.parent().cell())
1261+
if let Some(source) = &*result.first_source().await? {
1262+
Ok(Vc::cell(Some(source.ident().path().await?.parent())))
1263+
} else {
1264+
Ok(Vc::cell(None))
1265+
}
12571266
}
12581267

12591268
pub async fn insert_alias_option<const N: usize>(

0 commit comments

Comments
 (0)