diff --git a/packages/next/errors.json b/packages/next/errors.json index 6da4b883bac3d..cd3f62c8209f2 100644 --- a/packages/next/errors.json +++ b/packages/next/errors.json @@ -848,5 +848,7 @@ "847": "Route %s with \\`dynamic = \"error\"\\` couldn't be rendered statically because it used \\`connection()\\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering", "848": "%sused %s. \\`searchParams\\` is a Promise and must be unwrapped with \\`await\\` or \\`React.use()\\` before accessing its properties. Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis", "849": "Route %s with \\`dynamic = \"error\"\\` couldn't be rendered statically because it used \\`cookies()\\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering", - "850": "metadataBase is not a valid URL: %s" + "850": "metadataBase is not a valid URL: %s", + "851": "Pass either `webpack` or `turbopack`, not both.", + "852": "Only custom servers can pass `webpack`, `turbo`, or `turbopack`." } diff --git a/packages/next/src/server/next.ts b/packages/next/src/server/next.ts index 2ca7d7b9bffb9..9f479bc0996f3 100644 --- a/packages/next/src/server/next.ts +++ b/packages/next/src/server/next.ts @@ -52,6 +52,15 @@ export type NextServerOptions = Omit< > & Partial> +export type NextBundlerOptions = { + /** @deprecated Use `turbopack` instead */ + turbo?: boolean + /** Selects Turbopack as the bundler */ + turbopack?: boolean + /** Selects Webpack as the bundler */ + webpack?: boolean +} + export type RequestHandler = ( req: IncomingMessage, res: ServerResponse, @@ -531,18 +540,30 @@ class NextCustomServer implements NextWrapperServer { // This file is used for when users run `require('next')` function createServer( - options: NextServerOptions & { - turbo?: boolean - turbopack?: boolean - } + options: NextServerOptions & NextBundlerOptions ): NextWrapperServer { - if ( - options && - (options.turbo || options.turbopack || process.env.IS_TURBOPACK_TEST) - ) { - // Configure TURBOPACK if it isn't already set - process.env.TURBOPACK ??= '1' + // next sets customServer to false when calling this function, in that case we don't want to modify the environment variables + const isCustomServer = options?.customServer ?? true + if (isCustomServer) { + const selectTurbopack = + options && + (options.turbo || options.turbopack || process.env.IS_TURBOPACK_TEST) + const selectWebpack = + options && (options.webpack || process.env.IS_WEBPACK_TEST) + if (selectTurbopack && selectWebpack) { + throw new Error('Pass either `webpack` or `turbopack`, not both.') + } + if (selectTurbopack || !selectWebpack) { + process.env.TURBOPACK ??= selectTurbopack ? '1' : 'auto' + } + } else { + if (options && (options.webpack || options.turbo || options.turbopack)) { + throw new Error( + 'Only custom servers can pass `webpack`, `turbo`, or `turbopack`.' + ) + } } + // The package is used as a TypeScript plugin. if ( options &&