Repository: vercel/next.js
Pull request: Turbopack: Add --turbopack for next start

Adds `--turbopack` for `next start` to match `next dev --turbopack` and `next build --turbopack`. This is required to boot Turbopack builds while webpack is still the default because there's some environment variable differences.

Initially wanted to automatically detect Turbopack builds but the problem is that we can write a marker during builds that will tell it's a Turbopack build but then the environment is not available during bootup. So it's a bit of a chicken-egg problem with getting the environment to know it used Turbopack during build.

Why is this info needed at runtime? React server components has an integration for every bundler and it needs to load the right version of React for the bundler, that's why it's needed.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the PR.
- Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes PACK-4201

Changed files:
filename: packages/next/src/bin/next.ts
patch: @@ -129,9 +129,8 @@ program
   .option('--no-mangling', 'Disables mangling.')
   .option('--profile', 'Enables production profiling for React.')
   .option('--experimental-app-only', 'Builds only App Router routes.')
-  .addOption(new Option('--experimental-turbo').hideHelp())
-  .addOption(new Option('--turbo').hideHelp())
-  .addOption(new Option('--turbopack').hideHelp())
+  .option('--turbo', 'Starts development mode using Turbopack.')
+  .option('--turbopack', 'Starts development mode using Turbopack.')
   .addOption(
     new Option(
       '--experimental-build-mode [mode]',
@@ -352,6 +351,8 @@ program
       'Specify the maximum amount of milliseconds to wait before closing inactive connections.'
     ).argParser(parseValidPositiveInteger)
   )
+  .addOption(new Option('--turbo').hideHelp())
+  .option('--turbopack', 'Starts development mode using Turbopack.')
   .action((directory: string, options: NextStartOptions) =>
     import('../cli/next-start.js').then((mod) =>
       mod.nextStart(options, directory)

filename: packages/next/src/cli/next-build.ts
patch: @@ -40,9 +40,6 @@ const nextBuild = (options: NextBuildOptions, directory?: string) => {
     experimentalUploadTrace,
   } = options
 
-  const useTurbopack =
-    options.experimentalTurbo || options.turbo || options.turbopack
-
   let traceUploadUrl: string | undefined
   if (experimentalUploadTrace && !process.env.NEXT_TRACE_UPLOAD_DISABLED) {
     traceUploadUrl = experimentalUploadTrace
@@ -75,7 +72,7 @@ const nextBuild = (options: NextBuildOptions, directory?: string) => {
     printAndExit(`> No such directory exists as the project root: ${dir}`)
   }
 
-  if (useTurbopack) {
+  if (options.turbo || options.turbopack) {
     process.env.TURBOPACK = '1'
   }
 

filename: packages/next/src/cli/next-start.ts
patch: @@ -13,6 +13,8 @@ export type NextStartOptions = {
   port: number
   hostname?: string
   keepAliveTimeout?: number
+  turbo?: boolean
+  turbopack?: boolean
 }
 
 /**
@@ -31,6 +33,10 @@ const nextStart = async (options: NextStartOptions, directory?: string) => {
     printAndExit(getReservedPortExplanation(port), 1)
   }
 
+  if (options.turbo || options.turbopack) {
+    process.env.TURBOPACK = '1'
+  }
+
   await startServer({
     dir,
     isDev: false,