Skip to content

Commit 55f83e0

Browse files
authored
[turbopack] Ensure React Compiler options are based dev vs prod (#84062)
This is restoring the forking of React Compiler options. We'll need this to enable naming of anonymous functions in dev. The plumbing stretches more files than for Turbopack so I filed this separately.
1 parent e0e6213 commit 55f83e0

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

packages/next/src/build/babel/loader/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { webpack } from 'next/dist/compiled/webpack/webpack'
2+
import type { JSONValue } from '../../../server/config-shared'
23
import type { Span } from '../../../trace'
34

45
export interface NextJsLoaderContext extends webpack.LoaderContext<{}> {
@@ -17,7 +18,7 @@ export interface NextBabelLoaderBaseOptions {
1718
/**
1819
* Custom plugins to be added to the generated babel options.
1920
*/
20-
reactCompilerPlugins?: Array<any>
21+
reactCompilerPlugins?: Array<JSONValue>
2122

2223
/**
2324
* Paths that the loader should not apply the react-compiler to.

packages/next/src/build/get-babel-loader-config.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path'
2-
import type { ReactCompilerOptions } from '../server/config-shared'
2+
import type { JSONValue, ReactCompilerOptions } from '../server/config-shared'
33
import type { NextBabelLoaderOptions } from './babel/loader/types'
44

55
function getReactCompiler() {
@@ -15,27 +15,26 @@ function getReactCompiler() {
1515
}
1616

1717
const getReactCompilerPlugins = (
18-
options: boolean | ReactCompilerOptions | undefined,
19-
isServer: boolean
20-
) => {
21-
if (!options || isServer) {
18+
maybeOptions: boolean | ReactCompilerOptions | undefined,
19+
isServer: boolean,
20+
isDev: boolean
21+
): undefined | JSONValue[] => {
22+
if (!maybeOptions || isServer) {
2223
return undefined
2324
}
2425

25-
const compilerOptions = typeof options === 'boolean' ? {} : options
26-
if (options) {
27-
return [
28-
[
29-
getReactCompiler(),
30-
{
31-
// https://react.dev/reference/react-compiler/panicThreshold
32-
panicThreshold: 'none',
33-
...compilerOptions,
34-
},
35-
],
36-
]
26+
const defaultOptions: ReactCompilerOptions = isDev
27+
? {
28+
// TODO: enable `environment.enableNameAnonymousFunctions`Ï
29+
}
30+
: {}
31+
const options: ReactCompilerOptions =
32+
typeof maybeOptions === 'boolean' ? {} : maybeOptions
33+
const compilerOptions: JSONValue = {
34+
...defaultOptions,
35+
...options,
3736
}
38-
return undefined
37+
return [[getReactCompiler(), compilerOptions]]
3938
}
4039

4140
const getBabelLoader = (
@@ -67,7 +66,8 @@ const getBabelLoader = (
6766
hasJsxRuntime: true,
6867
reactCompilerPlugins: getReactCompilerPlugins(
6968
reactCompilerOptions,
70-
isServer
69+
isServer,
70+
dev
7171
),
7272
reactCompilerExclude,
7373
}
@@ -90,11 +90,13 @@ const getReactCompilerLoader = (
9090
reactCompilerOptions: boolean | ReactCompilerOptions | undefined,
9191
cwd: string,
9292
isServer: boolean,
93-
reactCompilerExclude: ((excludePath: string) => boolean) | undefined
93+
reactCompilerExclude: ((excludePath: string) => boolean) | undefined,
94+
isDev: boolean
9495
) => {
9596
const reactCompilerPlugins = getReactCompilerPlugins(
9697
reactCompilerOptions,
97-
isServer
98+
isServer,
99+
isDev
98100
)
99101
if (!reactCompilerPlugins) {
100102
return undefined

packages/next/src/build/webpack-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,8 @@ export default async function getBaseWebpackConfig(
492492
config.experimental?.reactCompiler,
493493
dir,
494494
isNodeOrEdgeCompilation,
495-
codeCondition.exclude
495+
codeCondition.exclude,
496+
dev
496497
)
497498

498499
let swcTraceProfilingInitialized = false

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export interface StyledComponentsConfig {
8888
cssProp?: boolean
8989
}
9090

91-
type JSONValue =
91+
export type JSONValue =
9292
| string
9393
| number
9494
| boolean

0 commit comments

Comments
 (0)