Unverified Commit 60ad8aff authored by Arindam Majumder's avatar Arindam Majumder Committed by GitHub

feat: Add option to enable Turbopack with create-next-app (#65926)

This PR addresses an issue where the `dev` script in `package.json` was
not correctly updated to use the `--turbo` option when specified. The
changes ensure that the `dev` script is set to `next dev --turbo` if the
`turbo` option is enabled.

### Changes:
1. **Function Modification**:
- Updated the `modifyPackageJson` function to include a check for the
`turbo` parameter.
- If `turbo` is true, the `dev` script in `package.json` is set to `next
dev --turbo`.
   - If `turbo` is false, the `dev` script remains `next dev`.

2. **Integration in App Creation**:
- Integrated the `modifyPackageJson` function into the app creation
process to ensure the `dev` script is correctly set based on the `turbo`
parameter.

### Testing:
- Verified that the `dev` script in `package.json` is correctly updated
to `next dev --turbo` when the `turbo` option is enabled.
- Ensured that the `dev` script remains `next dev` when the `turbo`
option is not enabled.

### Related Issues:
- Fixes #65924

### Notes:
- This change ensures that developers opting to use Turbopack for
development will have the correct script set up automatically.

---------
Signed-off-by: 's avatarArindam Majumder <arindammajumder2020@gmail.com>
Co-authored-by: 's avatarLee Robinson <me@leerob.io>
Co-authored-by: 's avatardevjiwonchoi <devjiwonchoi@gmail.com>
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
Co-authored-by: 's avatarSam Ko <sam@vercel.com>
parent 0eedddbf
......@@ -30,6 +30,7 @@ Would you like to use ESLint? No / Yes
Would you like to use Tailwind CSS? No / Yes
Would you like to use `src/` directory? No / Yes
Would you like to use App Router? (recommended) No / Yes
Would you like to use Turbopack for `next dev`? No / Yes
Would you like to customize the default import alias (@/*)? No / Yes
What import alias would you like configured? @/*
```
......
......@@ -38,6 +38,7 @@ Would you like to use ESLint? No / Yes
Would you like to use Tailwind CSS? No / Yes
Would you like to use `src/` directory? No / Yes
Would you like to use App Router? (recommended) No / Yes
Would you like to use Turbopack for `next dev`? No / Yes
Would you like to customize the default import alias (@/*)? No / Yes
```
......@@ -80,6 +81,10 @@ Options:
Initialize inside a `src/` directory.
--turbo
Enable Turbopack by default for development.
--import-alias <alias-to-configure>
Specify import alias to use (default "@/*").
......
......@@ -59,6 +59,10 @@ Options:
Initialize inside a `src/` directory.
--turbo
Enable Turbopack by default for development.
--import-alias <alias-to-configure>
Specify import alias to use (default "@/*").
......
......@@ -36,6 +36,7 @@ export async function createApp({
importAlias,
skipInstall,
empty,
turbo,
}: {
appPath: string
packageManager: PackageManager
......@@ -49,6 +50,7 @@ export async function createApp({
importAlias: string
skipInstall: boolean
empty: boolean
turbo: boolean
}): Promise<void> {
let repoInfo: RepoInfo | undefined
const mode: TemplateMode = typescript ? 'ts' : 'js'
......@@ -229,6 +231,7 @@ export async function createApp({
srcDir,
importAlias,
skipInstall,
turbo,
})
}
......
......@@ -83,6 +83,13 @@ const program = new Commander.Command(packageJson.name)
`
Initialize inside a \`src/\` directory.
`
)
.option(
'--turbo',
`
Enable Turbopack by default for development.
`
)
.option(
......@@ -272,6 +279,7 @@ async function run(): Promise<void> {
importAlias: '@/*',
customizeImportAlias: false,
empty: false,
turbo: false,
}
const getPrefOrDefault = (field: string) =>
preferences[field] ?? defaults[field]
......@@ -396,6 +404,25 @@ async function run(): Promise<void> {
}
}
if (!program.turbo && !process.argv.includes('--no-turbo')) {
if (ciInfo.isCI) {
program.turbo = getPrefOrDefault('turbo')
} else {
const styledTurbo = blue('Turbopack')
const { turbo } = await prompts({
onState: onPromptState,
type: 'toggle',
name: 'turbo',
message: `Would you like to use ${styledTurbo} for ${`next dev`}?`,
initial: getPrefOrDefault('turbo'),
active: 'Yes',
inactive: 'No',
})
program.turbo = Boolean(turbo)
preferences.turbo = Boolean(turbo)
}
}
const importAliasPattern = /^[^*"]+\/\*\s*$/
if (
typeof program.importAlias !== 'string' ||
......@@ -455,6 +482,7 @@ async function run(): Promise<void> {
importAlias: program.importAlias,
skipInstall: program.skipInstall,
empty: program.empty,
turbo: program.turbo,
})
} catch (reason) {
if (!(reason instanceof DownloadError)) {
......@@ -485,6 +513,7 @@ async function run(): Promise<void> {
importAlias: program.importAlias,
skipInstall: program.skipInstall,
empty: program.empty,
turbo: program.turbo,
})
}
conf.set('preferences', preferences)
......
......@@ -39,6 +39,7 @@ export const installTemplate = async ({
srcDir,
importAlias,
skipInstall,
turbo,
}: InstallTemplateArgs) => {
console.log(bold(`Using ${packageManager}.`));
......@@ -174,7 +175,7 @@ export const installTemplate = async ({
version: "0.1.0",
private: true,
scripts: {
dev: "next dev",
dev: `next dev${turbo ? " --turbo" : ""}`,
build: "next build",
start: "next start",
lint: "next lint",
......
......@@ -29,4 +29,5 @@ export interface InstallTemplateArgs {
srcDir: boolean;
importAlias: string;
skipInstall: boolean;
turbo: boolean;
}
......@@ -30,6 +30,7 @@ describe.skip('create-next-app', () => {
projectName,
'--ts',
'--app',
'--no-turbo',
'--no-eslint',
'--no-tailwind',
'--no-src-dir',
......@@ -99,6 +100,7 @@ describe.skip('create-next-app', () => {
projectName,
'--ts',
'--app',
'--no-turbo',
'--no-eslint',
'--no-tailwind',
'--no-src-dir',
......
......@@ -36,6 +36,7 @@ describe.skip('create-next-app with package manager bun', () => {
'--ts',
'--app',
'--use-bun',
'--no-turbo',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
......@@ -55,72 +56,73 @@ describe.skip('create-next-app with package manager bun', () => {
})
})
})
})
it('should use bun when user-agent is bun', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-bun'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'bun' },
}
)
it('should use bun when user-agent is bun', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-bun'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-turbo',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'bun' },
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
it('should use bun for --use-bun flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-bun-with-example'
const res = await run(
[projectName, '--use-bun', '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{ cwd }
)
it('should use bun for --use-bun flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-bun-with-example'
const res = await run(
[projectName, '--use-bun', '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{ cwd }
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
it('should use bun when user-agent is bun with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-bun-with-example'
const res = await run(
[projectName, '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'bun' },
}
)
it('should use bun when user-agent is bun with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-bun-with-example'
const res = await run(
[projectName, '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'bun' },
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
......@@ -29,6 +29,7 @@ describe.skip('create-next-app with package manager npm', () => {
'--ts',
'--app',
'--use-npm',
'--no-turbo',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
......@@ -48,72 +49,73 @@ describe.skip('create-next-app with package manager npm', () => {
})
})
})
})
it('should use npm when user-agent is npm', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-npm'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'npm' },
}
)
it('should use npm when user-agent is npm', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-npm'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-turbo',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'npm' },
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
it('should use npm for --use-npm flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-npm-with-example'
const res = await run(
[projectName, '--use-npm', '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{ cwd }
)
it('should use npm for --use-npm flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-npm-with-example'
const res = await run(
[projectName, '--use-npm', '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{ cwd }
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
it('should use npm when user-agent is npm with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-npm-with-example'
const res = await run(
[projectName, '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'npm' },
}
)
it('should use npm when user-agent is npm with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-npm-with-example'
const res = await run(
[projectName, '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'npm' },
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
......@@ -37,6 +37,7 @@ describe.skip('create-next-app with package manager pnpm', () => {
'--ts',
'--app',
'--use-pnpm',
'--no-turbo',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
......@@ -56,72 +57,73 @@ describe.skip('create-next-app with package manager pnpm', () => {
})
})
})
})
it('should use pnpm when user-agent is pnpm', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-pnpm'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'pnpm' },
}
)
it('should use pnpm when user-agent is pnpm', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-pnpm'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-turbo',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'pnpm' },
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
it('should use pnpm for --use-pnpm flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-pnpm-with-example'
const res = await run(
[projectName, '--use-pnpm', '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{ cwd }
)
it('should use pnpm for --use-pnpm flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-pnpm-with-example'
const res = await run(
[projectName, '--use-pnpm', '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{ cwd }
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
it('should use pnpm when user-agent is pnpm with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-pnpm-with-example'
const res = await run(
[projectName, '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'pnpm' },
}
)
it('should use pnpm when user-agent is pnpm with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-pnpm-with-example'
const res = await run(
[projectName, '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'pnpm' },
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
......@@ -30,6 +30,7 @@ describe.skip('create-next-app with package manager yarn', () => {
'--ts',
'--app',
'--use-yarn',
'--no-turbo',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
......@@ -49,72 +50,73 @@ describe.skip('create-next-app with package manager yarn', () => {
})
})
})
})
it('should use yarn when user-agent is yarn', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-yarn'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
'canary',
{
cwd,
env: { npm_config_user_agent: 'yarn' },
}
)
it('should use yarn when user-agent is yarn', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-yarn'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-turbo',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
'canary',
{
cwd,
env: { npm_config_user_agent: 'yarn' },
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
it('should use yarn for --use-yarn flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-yarn-with-example'
const res = await run(
[projectName, '--use-yarn', '--example', FULL_EXAMPLE_PATH],
'canary',
{ cwd }
)
it('should use yarn for --use-yarn flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-yarn-with-example'
const res = await run(
[projectName, '--use-yarn', '--example', FULL_EXAMPLE_PATH],
'canary',
{ cwd }
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
it('should use yarn when user-agent is yarn with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-yarn-with-example'
const res = await run(
[projectName, '--example', FULL_EXAMPLE_PATH],
'canary',
{
cwd,
env: { npm_config_user_agent: 'yarn' },
}
)
it('should use yarn when user-agent is yarn with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-yarn-with-example'
const res = await run(
[projectName, '--example', FULL_EXAMPLE_PATH],
'canary',
{
cwd,
env: { npm_config_user_agent: 'yarn' },
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
......@@ -22,6 +22,7 @@ describe.skip('create-next-app prompts', () => {
'--ts',
'--app',
'--eslint',
'--no-turbo',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
......@@ -59,6 +60,7 @@ describe.skip('create-next-app prompts', () => {
projectName,
'--app',
'--eslint',
'--no-turbo',
'--no-tailwind',
'--no-src-dir',
'--no-import-alias',
......@@ -95,6 +97,7 @@ describe.skip('create-next-app prompts', () => {
'--ts',
'--app',
'--eslint',
'--no-turbo',
'--no-src-dir',
'--no-import-alias',
],
......@@ -130,6 +133,7 @@ describe.skip('create-next-app prompts', () => {
'--ts',
'--app',
'--eslint',
'--no-turbo',
'--no-tailwind',
'--no-src-dir',
],
......
import { join } from 'node:path'
import {
createNextApp,
projectShouldHaveNoGitChanges,
......@@ -7,7 +8,7 @@ import {
useTempDir,
} from '../utils'
let testVersion
let testVersion: string
beforeAll(async () => {
if (testVersion) return
// TODO: investigate moving this post publish or create deployed GH#57025
......@@ -222,4 +223,32 @@ describe.skip('create-next-app --app (App Router)', () => {
})
})
})
it('should enable turbopack dev with --turbo flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'app-turbo'
const childProcess = createNextApp(
[
projectName,
'--ts',
'--app',
'--eslint',
'--turbo',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
},
testVersion
)
const exitCode = await spawnExitPromise(childProcess)
expect(exitCode).toBe(0)
const projectRoot = join(cwd, projectName)
const pkgJson = require(join(projectRoot, 'package.json'))
expect(pkgJson.scripts.dev).toBe('next dev --turbo')
})
})
})
import { join } from 'node:path'
import {
createNextApp,
projectShouldHaveNoGitChanges,
......@@ -7,7 +8,7 @@ import {
useTempDir,
} from '../utils'
let testVersion
let testVersion: string
beforeAll(async () => {
// TODO: investigate moving this post publish or create deployed GH#57025
// tarballs to avoid these failing while a publish is in progress
......@@ -232,4 +233,32 @@ describe.skip('create-next-app --no-app (Pages Router)', () => {
})
})
})
it('should enable turbopack dev with --turbo flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'pages-turbo'
const childProcess = createNextApp(
[
projectName,
'--ts',
'--no-app',
'--eslint',
'--turbo',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
},
testVersion
)
const exitCode = await spawnExitPromise(childProcess)
expect(exitCode).toBe(0)
const projectRoot = join(cwd, projectName)
const pkgJson = require(join(projectRoot, 'package.json'))
expect(pkgJson.scripts.dev).toBe('next dev --turbo')
})
})
})
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