Unverified Commit 55f83e0f authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[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.
parent e0e6213f
import type { webpack } from 'next/dist/compiled/webpack/webpack' import type { webpack } from 'next/dist/compiled/webpack/webpack'
import type { JSONValue } from '../../../server/config-shared'
import type { Span } from '../../../trace' import type { Span } from '../../../trace'
export interface NextJsLoaderContext extends webpack.LoaderContext<{}> { export interface NextJsLoaderContext extends webpack.LoaderContext<{}> {
...@@ -17,7 +18,7 @@ export interface NextBabelLoaderBaseOptions { ...@@ -17,7 +18,7 @@ export interface NextBabelLoaderBaseOptions {
/** /**
* Custom plugins to be added to the generated babel options. * Custom plugins to be added to the generated babel options.
*/ */
reactCompilerPlugins?: Array<any> reactCompilerPlugins?: Array<JSONValue>
/** /**
* Paths that the loader should not apply the react-compiler to. * Paths that the loader should not apply the react-compiler to.
......
import path from 'path' import path from 'path'
import type { ReactCompilerOptions } from '../server/config-shared' import type { JSONValue, ReactCompilerOptions } from '../server/config-shared'
import type { NextBabelLoaderOptions } from './babel/loader/types' import type { NextBabelLoaderOptions } from './babel/loader/types'
function getReactCompiler() { function getReactCompiler() {
...@@ -15,27 +15,26 @@ function getReactCompiler() { ...@@ -15,27 +15,26 @@ function getReactCompiler() {
} }
const getReactCompilerPlugins = ( const getReactCompilerPlugins = (
options: boolean | ReactCompilerOptions | undefined, maybeOptions: boolean | ReactCompilerOptions | undefined,
isServer: boolean isServer: boolean,
) => { isDev: boolean
if (!options || isServer) { ): undefined | JSONValue[] => {
if (!maybeOptions || isServer) {
return undefined return undefined
} }
const compilerOptions = typeof options === 'boolean' ? {} : options const defaultOptions: ReactCompilerOptions = isDev
if (options) { ? {
return [ // TODO: enable `environment.enableNameAnonymousFunctions`Ï
[ }
getReactCompiler(), : {}
{ const options: ReactCompilerOptions =
// https://react.dev/reference/react-compiler/panicThreshold typeof maybeOptions === 'boolean' ? {} : maybeOptions
panicThreshold: 'none', const compilerOptions: JSONValue = {
...compilerOptions, ...defaultOptions,
}, ...options,
],
]
} }
return undefined return [[getReactCompiler(), compilerOptions]]
} }
const getBabelLoader = ( const getBabelLoader = (
...@@ -67,7 +66,8 @@ const getBabelLoader = ( ...@@ -67,7 +66,8 @@ const getBabelLoader = (
hasJsxRuntime: true, hasJsxRuntime: true,
reactCompilerPlugins: getReactCompilerPlugins( reactCompilerPlugins: getReactCompilerPlugins(
reactCompilerOptions, reactCompilerOptions,
isServer isServer,
dev
), ),
reactCompilerExclude, reactCompilerExclude,
} }
...@@ -90,11 +90,13 @@ const getReactCompilerLoader = ( ...@@ -90,11 +90,13 @@ const getReactCompilerLoader = (
reactCompilerOptions: boolean | ReactCompilerOptions | undefined, reactCompilerOptions: boolean | ReactCompilerOptions | undefined,
cwd: string, cwd: string,
isServer: boolean, isServer: boolean,
reactCompilerExclude: ((excludePath: string) => boolean) | undefined reactCompilerExclude: ((excludePath: string) => boolean) | undefined,
isDev: boolean
) => { ) => {
const reactCompilerPlugins = getReactCompilerPlugins( const reactCompilerPlugins = getReactCompilerPlugins(
reactCompilerOptions, reactCompilerOptions,
isServer isServer,
isDev
) )
if (!reactCompilerPlugins) { if (!reactCompilerPlugins) {
return undefined return undefined
......
...@@ -492,7 +492,8 @@ export default async function getBaseWebpackConfig( ...@@ -492,7 +492,8 @@ export default async function getBaseWebpackConfig(
config.experimental?.reactCompiler, config.experimental?.reactCompiler,
dir, dir,
isNodeOrEdgeCompilation, isNodeOrEdgeCompilation,
codeCondition.exclude codeCondition.exclude,
dev
) )
let swcTraceProfilingInitialized = false let swcTraceProfilingInitialized = false
......
...@@ -88,7 +88,7 @@ export interface StyledComponentsConfig { ...@@ -88,7 +88,7 @@ export interface StyledComponentsConfig {
cssProp?: boolean cssProp?: boolean
} }
type JSONValue = export type JSONValue =
| string | string
| number | number
| boolean | boolean
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment