Unverified Commit 4642591d authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Mark React Compiler integration as stable (#84220)

parent 3f75ff69
...@@ -91,6 +91,7 @@ pub struct NextConfig { ...@@ -91,6 +91,7 @@ pub struct NextConfig {
pub experimental: ExperimentalConfig, pub experimental: ExperimentalConfig,
pub images: ImageConfig, pub images: ImageConfig,
pub page_extensions: Vec<RcStr>, pub page_extensions: Vec<RcStr>,
pub react_compiler: Option<ReactCompilerOptionsOrBoolean>,
pub react_production_profiling: Option<bool>, pub react_production_profiling: Option<bool>,
pub react_strict_mode: Option<bool>, pub react_strict_mode: Option<bool>,
pub transpile_packages: Option<Vec<RcStr>>, pub transpile_packages: Option<Vec<RcStr>>,
...@@ -816,7 +817,6 @@ pub struct ExperimentalConfig { ...@@ -816,7 +817,6 @@ pub struct ExperimentalConfig {
web_vitals_attribution: Option<Vec<RcStr>>, web_vitals_attribution: Option<Vec<RcStr>>,
server_actions: Option<ServerActionsOrLegacyBool>, server_actions: Option<ServerActionsOrLegacyBool>,
sri: Option<SubResourceIntegrity>, sri: Option<SubResourceIntegrity>,
react_compiler: Option<ReactCompilerOptionsOrBoolean>,
cache_components: Option<bool>, cache_components: Option<bool>,
use_cache: Option<bool>, use_cache: Option<bool>,
root_params: Option<bool>, root_params: Option<bool>,
...@@ -1634,7 +1634,7 @@ impl NextConfig { ...@@ -1634,7 +1634,7 @@ impl NextConfig {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn react_compiler_options(&self) -> Vc<OptionalReactCompilerOptions> { pub fn react_compiler_options(&self) -> Vc<OptionalReactCompilerOptions> {
let options = &self.experimental.react_compiler; let options = &self.react_compiler;
let options = match options { let options = match options {
Some(ReactCompilerOptionsOrBoolean::Boolean(true)) => { Some(ReactCompilerOptionsOrBoolean::Boolean(true)) => {
......
--- ---
title: reactCompiler title: reactCompiler
description: Enable the React Compiler to automatically optimize component rendering. description: Enable the React Compiler to automatically optimize component rendering.
version: experimental
--- ---
Next.js includes support for the [React Compiler](https://react.dev/learn/react-compiler), a tool designed to improve performance by automatically optimizing component rendering. This reduces the need for manual memoization using `useMemo` and `useCallback`. Next.js includes support for the [React Compiler](https://react.dev/learn/react-compiler/introduction), a tool designed to improve performance by automatically optimizing component rendering. This reduces the need for manual memoization using `useMemo` and `useCallback`.
Next.js includes a custom performance optimization written in SWC that makes the React Compiler more efficient. Instead of running the compiler on every file, Next.js analyzes your project and only applies the React Compiler to relevant files. This avoids unnecessary work and leads to faster builds compared to using the Babel plugin on its own. Next.js includes a custom performance optimization written in SWC that makes the React Compiler more efficient. Instead of running the compiler on every file, Next.js analyzes your project and only applies the React Compiler to relevant files. This avoids unnecessary work and leads to faster builds compared to using the Babel plugin on its own.
...@@ -20,15 +19,13 @@ To use it, install the `babel-plugin-react-compiler`: ...@@ -20,15 +19,13 @@ To use it, install the `babel-plugin-react-compiler`:
npm install babel-plugin-react-compiler npm install babel-plugin-react-compiler
``` ```
Then, add `experimental.reactCompiler` option in `next.config.js`: Then, add `reactCompiler` option in `next.config.js`:
```ts filename="next.config.ts" switcher ```ts filename="next.config.ts" switcher
import type { NextConfig } from 'next' import type { NextConfig } from 'next'
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
experimental: {
reactCompiler: true, reactCompiler: true,
},
} }
export default nextConfig export default nextConfig
...@@ -37,9 +34,7 @@ export default nextConfig ...@@ -37,9 +34,7 @@ export default nextConfig
```js filename="next.config.js" switcher ```js filename="next.config.js" switcher
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
experimental: {
reactCompiler: true, reactCompiler: true,
},
} }
module.exports = nextConfig module.exports = nextConfig
...@@ -53,11 +48,9 @@ You can configure the compiler to run in "opt-in" mode as follows: ...@@ -53,11 +48,9 @@ You can configure the compiler to run in "opt-in" mode as follows:
import type { NextConfig } from 'next' import type { NextConfig } from 'next'
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
experimental: {
reactCompiler: { reactCompiler: {
compilationMode: 'annotation', compilationMode: 'annotation',
}, },
},
} }
export default nextConfig export default nextConfig
...@@ -66,11 +59,9 @@ export default nextConfig ...@@ -66,11 +59,9 @@ export default nextConfig
```js filename="next.config.js" switcher ```js filename="next.config.js" switcher
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
experimental: {
reactCompiler: { reactCompiler: {
compilationMode: 'annotation', compilationMode: 'annotation',
}, },
},
} }
module.exports = nextConfig module.exports = nextConfig
......
...@@ -482,14 +482,14 @@ export default async function getBaseWebpackConfig( ...@@ -482,14 +482,14 @@ export default async function getBaseWebpackConfig(
(appDir || pagesDir)!, (appDir || pagesDir)!,
dev, dev,
isClient, isClient,
config.experimental?.reactCompiler, config.reactCompiler,
codeCondition.exclude codeCondition.exclude
) )
const reactCompilerLoader = babelLoader const reactCompilerLoader = babelLoader
? undefined ? undefined
: getReactCompilerLoader( : getReactCompilerLoader(
config.experimental?.reactCompiler, config.reactCompiler,
dir, dir,
isNodeOrEdgeCompilation, isNodeOrEdgeCompilation,
codeCondition.exclude, codeCondition.exclude,
......
...@@ -458,19 +458,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -458,19 +458,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
testProxy: z.boolean().optional(), testProxy: z.boolean().optional(),
defaultTestRunner: z.enum(SUPPORTED_TEST_RUNNERS_LIST).optional(), defaultTestRunner: z.enum(SUPPORTED_TEST_RUNNERS_LIST).optional(),
allowDevelopmentBuild: z.literal(true).optional(), allowDevelopmentBuild: z.literal(true).optional(),
reactCompiler: z.union([
z.boolean(),
z
.object({
compilationMode: z
.enum(['infer', 'annotation', 'all'])
.optional(),
panicThreshold: z
.enum(['none', 'critical_errors', 'all_errors'])
.optional(),
})
.optional(),
]),
reactDebugChannel: z.boolean().optional(), reactDebugChannel: z.boolean().optional(),
staticGenerationRetryCount: z.number().int().optional(), staticGenerationRetryCount: z.number().int().optional(),
staticGenerationMaxConcurrency: z.number().int().optional(), staticGenerationMaxConcurrency: z.number().int().optional(),
...@@ -655,6 +643,17 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -655,6 +643,17 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
poweredByHeader: z.boolean().optional(), poweredByHeader: z.boolean().optional(),
productionBrowserSourceMaps: z.boolean().optional(), productionBrowserSourceMaps: z.boolean().optional(),
publicRuntimeConfig: z.record(z.string(), z.any()).optional(), publicRuntimeConfig: z.record(z.string(), z.any()).optional(),
reactCompiler: z.union([
z.boolean(),
z
.object({
compilationMode: z.enum(['infer', 'annotation', 'all']).optional(),
panicThreshold: z
.enum(['none', 'critical_errors', 'all_errors'])
.optional(),
})
.optional(),
]),
reactProductionProfiling: z.boolean().optional(), reactProductionProfiling: z.boolean().optional(),
reactStrictMode: z.boolean().nullable().optional(), reactStrictMode: z.boolean().nullable().optional(),
reactMaxHeadersLength: z.number().nonnegative().int().optional(), reactMaxHeadersLength: z.number().nonnegative().int().optional(),
......
...@@ -476,10 +476,10 @@ export interface ExperimentalConfig { ...@@ -476,10 +476,10 @@ export interface ExperimentalConfig {
* Set this to `false` to disable the automatic configuration of the babel loader when a Babel * Set this to `false` to disable the automatic configuration of the babel loader when a Babel
* configuration file is present. This option is enabled by default. * configuration file is present. This option is enabled by default.
* *
* If this is set to `false`, but `experimental.reactCompiler` is `true`, the built-in Babel will * If this is set to `false`, but `reactCompiler` is `true`, the built-in Babel will
* still be configured, but any Babel configuration files on disk will be ignored. If you wish to * still be configured, but any Babel configuration files on disk will be ignored. If you wish to
* use React Compiler with a different manually-configured `babel-loader`, you should disable both * use React Compiler with a different manually-configured `babel-loader`, you should disable both
* this and `experimental.reactCompiler`. * this and `reactCompiler`.
*/ */
turbopackUseBuiltinBabel?: boolean turbopackUseBuiltinBabel?: boolean
...@@ -678,12 +678,6 @@ export interface ExperimentalConfig { ...@@ -678,12 +678,6 @@ export interface ExperimentalConfig {
* *
*/ */
serverComponentsExternalPackages?: string[] serverComponentsExternalPackages?: string[]
/**
* Enable experimental React compiler optimization.
* Configuration accepts partial config object to the compiler, if provided
* compiler will be enabled.
*/
reactCompiler?: boolean | ReactCompilerOptions
/** /**
* When enabled, in dev mode, Next.js will send React's debug info through the * When enabled, in dev mode, Next.js will send React's debug info through the
...@@ -1099,6 +1093,13 @@ export interface NextConfig { ...@@ -1099,6 +1093,13 @@ export interface NextConfig {
*/ */
productionBrowserSourceMaps?: boolean productionBrowserSourceMaps?: boolean
/**
* Enable {@link https://nextjs.org/docs/app/api-reference/config/next-config-js/reactCompiler React Compiler in Next.js}.
* Configuration accepts partial config object of the Compiler.
* If provided, the Compiler will be enabled.
*/
reactCompiler?: boolean | ReactCompilerOptions
/** /**
* Enable react profiling in production * Enable react profiling in production
* *
...@@ -1374,6 +1375,7 @@ export const defaultConfig = Object.freeze({ ...@@ -1374,6 +1375,7 @@ export const defaultConfig = Object.freeze({
excludeDefaultMomentLocales: true, excludeDefaultMomentLocales: true,
serverRuntimeConfig: {}, serverRuntimeConfig: {},
publicRuntimeConfig: {}, publicRuntimeConfig: {},
reactCompiler: undefined,
reactProductionProfiling: false, reactProductionProfiling: false,
reactStrictMode: null, reactStrictMode: null,
reactMaxHeadersLength: 6000, reactMaxHeadersLength: 6000,
...@@ -1507,7 +1509,6 @@ export const defaultConfig = Object.freeze({ ...@@ -1507,7 +1509,6 @@ export const defaultConfig = Object.freeze({
static: 300, static: 300,
}, },
allowDevelopmentBuild: undefined, allowDevelopmentBuild: undefined,
reactCompiler: undefined,
reactDebugChannel: false, reactDebugChannel: false,
staticGenerationRetryCount: undefined, staticGenerationRetryCount: undefined,
serverComponentsHmrCache: true, serverComponentsHmrCache: true,
......
...@@ -664,6 +664,13 @@ function assignDefaultsAndValidate( ...@@ -664,6 +664,13 @@ function assignDefaultsAndValidate(
configFileName, configFileName,
silent silent
) )
warnOptionHasBeenMovedOutOfExperimental(
result,
'reactCompiler',
'reactCompiler',
configFileName,
silent
)
if ((result.experimental as any).outputStandalone) { if ((result.experimental as any).outputStandalone) {
if (!silent) { if (!silent) {
...@@ -1812,19 +1819,11 @@ function enforceExperimentalFeatures( ...@@ -1812,19 +1819,11 @@ function enforceExperimentalFeatures(
if ( if (
process.env.__NEXT_ENABLE_REACT_COMPILER === 'true' && process.env.__NEXT_ENABLE_REACT_COMPILER === 'true' &&
// We do respect an explicit value in the user config. // We do respect an explicit value in the user config.
(config.experimental.reactCompiler === undefined || (config.reactCompiler === undefined ||
(isDefaultConfig && !config.experimental.reactCompiler)) (isDefaultConfig && !config.reactCompiler))
) { ) {
config.experimental.reactCompiler = true config.reactCompiler = true
// TODO: Report if we enable non-experimental features via env
if (configuredExperimentalFeatures) {
addConfiguredExperimentalFeature(
configuredExperimentalFeatures,
'reactCompiler',
true,
'enabled by `__NEXT_ENABLE_REACT_COMPILER`'
)
}
} }
if ( if (
......
...@@ -123,14 +123,14 @@ export function eventCliSession( ...@@ -123,14 +123,14 @@ export function eventCliSession(
pagesDir: event.pagesDir, pagesDir: event.pagesDir,
staticStaleTime: nextConfig.experimental.staleTimes?.static ?? null, staticStaleTime: nextConfig.experimental.staleTimes?.static ?? null,
dynamicStaleTime: nextConfig.experimental.staleTimes?.dynamic ?? null, dynamicStaleTime: nextConfig.experimental.staleTimes?.dynamic ?? null,
reactCompiler: Boolean(nextConfig.experimental.reactCompiler), reactCompiler: Boolean(nextConfig.reactCompiler),
reactCompilerCompilationMode: reactCompilerCompilationMode:
typeof nextConfig.experimental.reactCompiler !== 'boolean' typeof nextConfig.reactCompiler !== 'boolean'
? (nextConfig.experimental.reactCompiler?.compilationMode ?? null) ? (nextConfig.reactCompiler?.compilationMode ?? null)
: null, : null,
reactCompilerPanicThreshold: reactCompilerPanicThreshold:
typeof nextConfig.experimental.reactCompiler !== 'boolean' typeof nextConfig.reactCompiler !== 'boolean'
? (nextConfig.experimental.reactCompiler?.panicThreshold ?? null) ? (nextConfig.reactCompiler?.panicThreshold ?? null)
: null, : null,
} }
return [{ eventName: EVENT_VERSION, payload }] return [{ eventName: EVENT_VERSION, payload }]
......
...@@ -2,9 +2,7 @@ ...@@ -2,9 +2,7 @@
* @type {import('next').NextConfig} * @type {import('next').NextConfig}
*/ */
const nextConfig = { const nextConfig = {
experimental: {
reactCompiler: true, reactCompiler: true,
},
reactProductionProfiling: true, reactProductionProfiling: true,
} }
......
import { nextTestSetup, FileRef } from 'e2e-utils' import { nextTestSetup, FileRef } from 'e2e-utils'
import { assertHasRedbox, retry } from 'next-test-utils' import { assertHasRedbox } from 'next-test-utils'
import { join } from 'path' import { join } from 'path'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
...@@ -46,13 +46,6 @@ describe.each(['default', 'babelrc'] as const)( ...@@ -46,13 +46,6 @@ describe.each(['default', 'babelrc'] as const)(
}, },
}) })
it('should show an experimental warning', async () => {
await retry(() => {
expect(next.cliOutput).toContain('Experiments (use with caution)')
expect(stripAnsi(next.cliOutput)).toContain('✓ reactCompiler')
})
})
it('should memoize Components', async () => { it('should memoize Components', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
......
module.exports = { module.exports = {
experimental: {
reactCompiler: true reactCompiler: true
}
} }
\ No newline at end of file
module.exports = { module.exports = {
experimental: {
reactCompiler: { reactCompiler: {
compilationMode: 'annotation', compilationMode: 'annotation',
panicThreshold: 'critical_errors' panicThreshold: 'critical_errors'
} }
}
} }
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