Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 20 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/02-app/02-api-reference/06-create-next-app.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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 customize the default import alias (@/*)? No / Yes
Would you like to use Turbopack for next dev? (RC) No / Yes
```

Once you've answered the prompts, a new project will be created with the correct configuration depending on your answers.
Expand Down Expand Up @@ -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 "@/*").
Expand Down
4 changes: 4 additions & 0 deletions packages/create-next-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 "@/*").
Expand Down
12 changes: 12 additions & 0 deletions packages/create-next-app/create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function createApp({
importAlias,
skipInstall,
empty,
turbo,
}: {
appPath: string
packageManager: PackageManager
Expand All @@ -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'
Expand Down Expand Up @@ -237,6 +239,16 @@ export async function createApp({
console.log()
}

if (hasPackageJson) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
if (turbo) {
packageJson.scripts.dev = 'next dev --turbo'
} else {
packageJson.scripts.dev = 'next dev'
}
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
}

let cdpath: string
if (path.join(originalDirectory, appName) === appPath) {
cdpath = appName
Expand Down
39 changes: 38 additions & 1 deletion packages/create-next-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ async function run(): Promise<void> {
importAlias: '@/*',
customizeImportAlias: false,
empty: false,
turbo: false,
}
const getPrefOrDefault = (field: string) =>
preferences[field] ?? defaults[field]
Expand Down Expand Up @@ -395,7 +396,24 @@ async function run(): Promise<void> {
program.app = Boolean(appRouter)
}
}

if (!process.argv.includes('--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? (RC)`,
initial: getPrefOrDefault('turbo'),
active: 'Yes',
inactive: 'No',
})
program.turbo = Boolean(turbo)
preferences.turbo = Boolean(turbo)
}
}
const importAliasPattern = /^[^*"]+\/\*\s*$/
if (
typeof program.importAlias !== 'string' ||
Expand Down Expand Up @@ -439,6 +457,24 @@ async function run(): Promise<void> {
}
}
}
if (!process.argv.includes('--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? (RC)`,
initial: getPrefOrDefault('turbo'),
active: 'Yes',
inactive: 'No',
})
program.turbo = Boolean(turbo)
preferences.turbo = Boolean(turbo)
}
}
}

try {
Expand All @@ -455,6 +491,7 @@ async function run(): Promise<void> {
importAlias: program.importAlias,
skipInstall: program.skipInstall,
empty: program.empty,
turbo: program.turbo,
})
} catch (reason) {
if (!(reason instanceof DownloadError)) {
Expand Down
Loading