Skip to content

Commit 7f8b32d

Browse files
committed
Claude Sonnet 4 port to Rust
1 parent 7001dec commit 7f8b32d

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

crates/next-core/src/next_shared/webpack_rules/babel.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ use crate::{
2626
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
2727
struct ReactCompilerEnvironmentConfig {
2828
#[serde(skip_serializing_if = "Option::is_none")]
29+
#[serde(rename = "enableNameAnonymousFunctions")]
2930
enable_name_anonymous_functions: Option<bool>,
3031
}
3132

3233
// Wrapper struct to augment the configured React Compiler options with defaults determined by
3334
// Next.js
3435
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
35-
struct ParsedReactCompilerOptions<T: serde::Serialize + Clone> {
36+
struct ResolvedReactCompilerOptions<T: serde::Serialize + Clone> {
3637
#[serde(flatten)]
3738
inner: T,
3839
#[serde(skip_serializing_if = "Option::is_none")]
@@ -129,10 +130,19 @@ pub async fn get_babel_loader_rules(
129130
}
130131
}
131132

132-
let react_compiler_options = next_config.react_compiler_options().await?;
133+
let configured_react_compiler_options = next_config.react_compiler_options().await?;
133134

134-
// Enable environment.enableNameAnonymousFunctions in development
135-
let parsed_react_compiler_options = if let Some(ref opts) = react_compiler_options.as_ref() {
135+
// if there's no babel config and react-compiler shouldn't be enabled, bail out early
136+
if babel_config_path.is_none()
137+
&& (configured_react_compiler_options.is_none()
138+
|| !builtin_conditions.contains(&WebpackLoaderBuiltinCondition::Browser))
139+
{
140+
return Ok(Vec::new());
141+
}
142+
143+
// Create extended react compiler options with environment.enableNameAnonymousFunctions if in
144+
// development
145+
let react_compiler_options = if let Some(ref opts) = configured_react_compiler_options.as_ref() {
136146
let environment =
137147
if builtin_conditions.contains(&WebpackLoaderBuiltinCondition::Development) {
138148
Some(ReactCompilerEnvironmentConfig {
@@ -141,32 +151,15 @@ pub async fn get_babel_loader_rules(
141151
} else {
142152
None
143153
};
144-
Some(ParsedReactCompilerOptions {
154+
155+
Some(ResolvedReactCompilerOptions {
145156
inner: (**opts).clone(),
146157
environment,
147158
})
148159
} else {
149160
None
150161
};
151162

152-
// print serialized parsed_react_compiler_options
153-
if let Some(parsed_react_compiler_options) = &parsed_react_compiler_options {
154-
// debug print
155-
println!(
156-
"Parsed React Compiler Options: {}",
157-
serde_json::to_string_pretty(parsed_react_compiler_options)
158-
.expect("parsed react compiler options JSON serialization should never fail")
159-
);
160-
}
161-
162-
// if there's no babel config and react-compiler shouldn't be enabled, bail out early
163-
if babel_config_path.is_none()
164-
&& (react_compiler_options.is_none()
165-
|| !builtin_conditions.contains(&WebpackLoaderBuiltinCondition::Browser))
166-
{
167-
return Ok(Vec::new());
168-
}
169-
170163
// - See `packages/next/src/build/babel/loader/types.d.ts` for all the configuration options.
171164
// - See `packages/next/src/build/get-babel-loader-config.ts` for how we use this in webpack.
172165
let serde_json::Value::Object(mut loader_options) = serde_json::json!({
@@ -189,15 +182,15 @@ pub async fn get_babel_loader_rules(
189182
}
190183

191184
let mut loader_conditions = Vec::new();
192-
if let Some(parsed_opts) = parsed_react_compiler_options.as_ref()
185+
if let Some(react_compiler_plugin_options) = react_compiler_options.as_ref()
193186
&& let Some(babel_plugin_path) =
194187
resolve_babel_plugin_react_compiler(next_config, project_path).await?
195188
{
196189
let react_compiler_plugins =
197190
serde_json::Value::Array(vec![serde_json::Value::Array(vec![
198191
serde_json::Value::String(babel_plugin_path.into_owned()),
199-
serde_json::to_value(parsed_opts)
200-
.expect("parsed react compiler options JSON serialization should never fail"),
192+
serde_json::to_value(react_compiler_plugin_options)
193+
.expect("react compiler options JSON serialization should never fail"),
201194
])]);
202195

203196
loader_options.insert("reactCompilerPlugins".to_owned(), react_compiler_plugins);
@@ -208,7 +201,8 @@ pub async fn get_babel_loader_rules(
208201
//
209202
// NOTE: we already bail out at the earlier if `foreign` condition is set or if
210203
// `browser` is not set.
211-
match parsed_opts.inner.compilation_mode {
204+
let inner_opts = react_compiler_plugin_options.inner.await?;
205+
match inner_opts.compilation_mode {
212206
ReactCompilerCompilationMode::Annotation => {
213207
loader_conditions.push(ConditionItem::Base {
214208
path: None,

test/e2e/react-compiler/react-compiler.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ describe.each(['default', 'babelrc'] as const)(
9898
.elementByCss('[data-testid="call-frame"]')
9999
.text()
100100
const devFunctionName =
101-
variant === 'babelrc'
101+
variant === 'babelrc' && !isTurbopack
102102
? // next/babel transpiles away arrow functions defeating the React Compiler naming
103+
// TODO: Does Webpack or Turbopack get the Babel config right?
103104
'PageUseEffect'
104105
: // expected naming heuristic from React Compiler. This may change in future.
105106
// Just make sure this is the heuristic from the React Compiler not something else.

0 commit comments

Comments
 (0)