Rename experimentalReact (#49046)

This PR renames `experimental.experimentalReact` as
`experimental.serverActions` and makes it a hard compilation error if
it's not set but detected server actions.
This commit is contained in:
Shu Ding 2023-05-01 20:35:52 +02:00 committed by GitHub
parent 1628260b88
commit 905cb5a56b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 31 additions and 29 deletions

View file

@ -1143,7 +1143,7 @@ export default async function build(
...process.env, ...process.env,
__NEXT_PRIVATE_PREBUNDLED_REACT: __NEXT_PRIVATE_PREBUNDLED_REACT:
type === 'app' type === 'app'
? config.experimental.experimentalReact ? config.experimental.serverActions
? 'experimental' ? 'experimental'
: 'next' : 'next'
: '', : '',

View file

@ -1952,7 +1952,7 @@ ${
// to ensure the correctness of the version for app. // to ensure the correctness of the version for app.
`\ `\
if (nextConfig && nextConfig.experimental && nextConfig.experimental.appDir) { if (nextConfig && nextConfig.experimental && nextConfig.experimental.appDir) {
process.env.__NEXT_PRIVATE_PREBUNDLED_REACT = nextConfig.experimental.experimentalReact ? 'experimental' : 'next' process.env.__NEXT_PRIVATE_PREBUNDLED_REACT = nextConfig.experimental.serverActions ? 'experimental' : 'next'
} }
` `
} }

View file

@ -704,8 +704,8 @@ export default async function getBaseWebpackConfig(
const hasServerComponents = hasAppDir const hasServerComponents = hasAppDir
const disableOptimizedLoading = true const disableOptimizedLoading = true
const enableTypedRoutes = !!config.experimental.typedRoutes && hasAppDir const enableTypedRoutes = !!config.experimental.typedRoutes && hasAppDir
const experimentalReact = !!config.experimental.experimentalReact && hasAppDir const serverActions = !!config.experimental.serverActions && hasAppDir
const bundledReactChannel = experimentalReact ? '-experimental' : '' const bundledReactChannel = serverActions ? '-experimental' : ''
if (isClient) { if (isClient) {
if ( if (
@ -2307,7 +2307,7 @@ export default async function getBaseWebpackConfig(
appDir, appDir,
dev, dev,
isEdgeServer, isEdgeServer,
useExperimentalReact: experimentalReact, useServerActions: serverActions,
})), })),
hasAppDir && hasAppDir &&
!isClient && !isClient &&

View file

@ -32,13 +32,12 @@ import {
import { traverseModules, forEachEntryModule } from '../utils' import { traverseModules, forEachEntryModule } from '../utils'
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep' import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
import { getProxiedPluginState } from '../../build-context' import { getProxiedPluginState } from '../../build-context'
import { warnOnce } from '../../../shared/lib/utils/warn-once'
interface Options { interface Options {
dev: boolean dev: boolean
appDir: string appDir: string
isEdgeServer: boolean isEdgeServer: boolean
useExperimentalReact: boolean useServerActions: boolean
} }
const PLUGIN_NAME = 'ClientEntryPlugin' const PLUGIN_NAME = 'ClientEntryPlugin'
@ -80,14 +79,14 @@ export class ClientReferenceEntryPlugin {
dev: boolean dev: boolean
appDir: string appDir: string
isEdgeServer: boolean isEdgeServer: boolean
useExperimentalReact: boolean useServerActions: boolean
assetPrefix: string assetPrefix: string
constructor(options: Options) { constructor(options: Options) {
this.dev = options.dev this.dev = options.dev
this.appDir = options.appDir this.appDir = options.appDir
this.isEdgeServer = options.isEdgeServer this.isEdgeServer = options.isEdgeServer
this.useExperimentalReact = options.useExperimentalReact this.useServerActions = options.useServerActions
this.assetPrefix = !this.dev && !this.isEdgeServer ? '../' : '' this.assetPrefix = !this.dev && !this.isEdgeServer ? '../' : ''
} }
@ -159,7 +158,7 @@ export class ClientReferenceEntryPlugin {
name: PLUGIN_NAME, name: PLUGIN_NAME,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH, stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH,
}, },
(assets) => this.createAsset(compilation, assets) (assets) => this.createActionAssets(compilation, assets)
) )
}) })
} }
@ -241,20 +240,23 @@ export class ClientReferenceEntryPlugin {
) )
if (actionEntryImports.size > 0) { if (actionEntryImports.size > 0) {
if (!this.useExperimentalReact) { if (!this.useServerActions) {
warnOnce( compilation.errors.push(
'\nServer Actions require `experimental.experimentalReact` option to be enabled in your Next.js config.\n' new Error(
'Server Actions require `experimental.serverActions` option to be enabled in your Next.js config.'
)
)
} else {
addActionEntryList.push(
this.injectActionEntry({
compiler,
compilation,
actions: actionEntryImports,
entryName: name,
bundlePath: name,
})
) )
} }
addActionEntryList.push(
this.injectActionEntry({
compiler,
compilation,
actions: actionEntryImports,
entryName: name,
bundlePath: name,
})
)
} }
}) })
@ -780,7 +782,7 @@ export class ClientReferenceEntryPlugin {
}) })
} }
createAsset( createActionAssets(
compilation: webpack.Compilation, compilation: webpack.Compilation,
assets: webpack.Compilation['assets'] assets: webpack.Compilation['assets']
) { ) {

View file

@ -286,7 +286,7 @@ const configSchema = {
appDir: { appDir: {
type: 'boolean', type: 'boolean',
}, },
experimentalReact: { serverActions: {
type: 'boolean', type: 'boolean',
}, },
extensionAlias: { extensionAlias: {

View file

@ -275,7 +275,7 @@ export interface ExperimentalConfig {
/** /**
* Enable `react@experimental` channel for the `app` directory. * Enable `react@experimental` channel for the `app` directory.
*/ */
experimentalReact?: boolean serverActions?: boolean
} }
export type ExportPathMap = { export type ExportPathMap = {

View file

@ -69,7 +69,7 @@ export const createWorker = (
ipcPort: number, ipcPort: number,
isNodeDebugging: boolean | 'brk' | undefined, isNodeDebugging: boolean | 'brk' | undefined,
type: 'pages' | 'app', type: 'pages' | 'app',
useExperimentalReact?: boolean useServerActions?: boolean
) => { ) => {
const { initialEnv } = require('@next/env') as typeof import('@next/env') const { initialEnv } = require('@next/env') as typeof import('@next/env')
const { Worker } = require('next/dist/compiled/jest-worker') const { Worker } = require('next/dist/compiled/jest-worker')
@ -91,7 +91,7 @@ export const createWorker = (
NODE_ENV: process.env.NODE_ENV, NODE_ENV: process.env.NODE_ENV,
...(type === 'app' ...(type === 'app'
? { ? {
__NEXT_PRIVATE_PREBUNDLED_REACT: useExperimentalReact __NEXT_PRIVATE_PREBUNDLED_REACT: useServerActions
? 'experimental' ? 'experimental'
: 'next', : 'next',
} }

View file

@ -274,7 +274,7 @@ export default class NextNodeServer extends BaseServer {
ipcPort, ipcPort,
options.isNodeDebugging, options.isNodeDebugging,
'app', 'app',
this.nextConfig.experimental.experimentalReact this.nextConfig.experimental.serverActions
) )
} }
this.renderWorkers.pages = createWorker( this.renderWorkers.pages = createWorker(

View file

@ -2,6 +2,6 @@
module.exports = { module.exports = {
experimental: { experimental: {
appDir: true, appDir: true,
experimentalReact: true, serverActions: true,
}, },
} }