Skip to content

Commit e337cb5

Browse files
committed
[turbopack] Ensure React Compiler options are based dev vs prod
1 parent b82f1db commit e337cb5

File tree

5 files changed

+48
-30
lines changed

5 files changed

+48
-30
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: 25 additions & 20 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,25 +15,27 @@ 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+
if (maybeOptions) {
27+
const defaultOptions: ReactCompilerOptions = isDev
28+
? {
29+
// TODO: enable `environment.enableNameAnonymousFunctions`Ï
30+
}
31+
: {}
32+
const options: ReactCompilerOptions =
33+
typeof maybeOptions === 'boolean' ? {} : maybeOptions
34+
const compilerOptions: JSONValue = {
35+
...defaultOptions,
36+
...options,
37+
}
38+
return [[getReactCompiler(), compilerOptions]]
3739
}
3840
return undefined
3941
}
@@ -67,7 +69,8 @@ const getBabelLoader = (
6769
hasJsxRuntime: true,
6870
reactCompilerPlugins: getReactCompilerPlugins(
6971
reactCompilerOptions,
70-
isServer
72+
isServer,
73+
dev
7174
),
7275
reactCompilerExclude,
7376
}
@@ -90,11 +93,13 @@ const getReactCompilerLoader = (
9093
reactCompilerOptions: boolean | ReactCompilerOptions | undefined,
9194
cwd: string,
9295
isServer: boolean,
93-
reactCompilerExclude: ((excludePath: string) => boolean) | undefined
96+
reactCompilerExclude: ((excludePath: string) => boolean) | undefined,
97+
isDev: boolean
9498
) => {
9599
const reactCompilerPlugins = getReactCompilerPlugins(
96100
reactCompilerOptions,
97-
isServer
101+
isServer,
102+
isDev
98103
)
99104
if (!reactCompilerPlugins) {
100105
return undefined

packages/next/src/build/swc/index.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -621,38 +621,47 @@ function bindingToApi(
621621
...options,
622622
nextConfig: await serializeNextConfig(
623623
options.nextConfig,
624-
path.join(options.rootPath, options.projectPath)
624+
path.join(options.rootPath, options.projectPath),
625+
options.dev
625626
),
626627
env: rustifyEnv(options.env),
627628
}
628629
}
629630

630631
async function rustifyPartialProjectOptions(
631-
options: Partial<ProjectOptions>
632+
options: Partial<ProjectOptions>,
633+
dev: boolean
632634
): Promise<NapiPartialProjectOptions> {
633635
return {
634636
...options,
635637
nextConfig:
636638
options.nextConfig &&
637639
(await serializeNextConfig(
638640
options.nextConfig,
639-
path.join(options.rootPath!, options.projectPath!)
641+
path.join(options.rootPath!, options.projectPath!),
642+
dev
640643
)),
641644
env: options.env && rustifyEnv(options.env),
642645
}
643646
}
644647

645648
class ProjectImpl implements Project {
646649
private readonly _nativeProject: { __napiType: 'Project' }
650+
private dev: boolean
647651

648-
constructor(nativeProject: { __napiType: 'Project' }) {
652+
constructor(nativeProject: { __napiType: 'Project' }, dev: boolean) {
649653
this._nativeProject = nativeProject
654+
this.dev = dev
650655
}
651656

652657
async update(options: Partial<ProjectOptions>) {
658+
if (options.dev !== undefined) {
659+
// TODO: Can we actually switch between dev and prod or is that just the types?
660+
this.dev = options.dev
661+
}
653662
await binding.projectUpdate(
654663
this._nativeProject,
655-
await rustifyPartialProjectOptions(options)
664+
await rustifyPartialProjectOptions(options, this.dev)
656665
)
657666
}
658667

@@ -795,7 +804,8 @@ function bindingToApi(
795804

796805
async function serializeNextConfig(
797806
nextConfig: NextConfigComplete,
798-
projectPath: string
807+
projectPath: string,
808+
dev: boolean
799809
): Promise<string> {
800810
// Avoid mutating the existing `nextConfig` object. NOTE: This is only a shallow clone.
801811
let nextConfigSerializable: Record<string, any> = { ...nextConfig }
@@ -1075,7 +1085,8 @@ function bindingToApi(
10751085
{
10761086
throwTurbopackInternalError,
10771087
}
1078-
)
1088+
),
1089+
options.dev
10791090
)
10801091
}
10811092
}

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
@@ -89,7 +89,7 @@ export interface StyledComponentsConfig {
8989
cssProp?: boolean
9090
}
9191

92-
type JSONValue =
92+
export type JSONValue =
9393
| string
9494
| number
9595
| boolean

0 commit comments

Comments
 (0)