Skip to content

Commit

Permalink
next-upgrade: change --turbo to --turbopack if applicable (#71737)
Browse files Browse the repository at this point in the history
  • Loading branch information
devjiwonchoi authored Oct 24, 2024
1 parent dfa35c4 commit dc1901a
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change `--turbo` to `--turbopack` in `next dev` script.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "change-turbo-to-turbopack",
"scripts": {
"dev": "next dev --turbo"
},
"dependencies": {
"next": "15.0.0-canary.0",
"react": "19.0.0-rc.0",
"react-dom": "19.0.0-rc.0"
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Suggest adding `--turbopack` to `next dev` script.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "suggest-turbopack",
"scripts": {
"dev": "next dev"
},
"dependencies": {
"next": "15.0.0-canary.0",
"react": "19.0.0-rc.0",
"react-dom": "19.0.0-rc.0"
}
}
Empty file.
28 changes: 23 additions & 5 deletions packages/next-codemod/bin/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export async function runUpgrade(
)

if (compareVersions(targetNextVersion, '15.0.0-canary') >= 0) {
await suggestTurbopack(appPackageJson)
await suggestTurbopack(appPackageJson, targetNextVersion)
}

const codemods = await suggestCodemods(
Expand Down Expand Up @@ -410,19 +410,37 @@ function isUsingAppDir(projectPath: string): boolean {
* 3. Otherwise, we ask the user to manually add `--turbopack` to their dev command,
* showing the current dev command as the initial value.
*/
async function suggestTurbopack(packageJson: any): Promise<void> {
async function suggestTurbopack(
packageJson: any,
targetNextVersion: string
): Promise<void> {
const devScript: string = packageJson.scripts['dev']
// Turbopack flag was changed from `--turbo` to `--turbopack` in v15.0.1-canary.3
// PR: https://github.com/vercel/next.js/pull/71657
// Release: https://github.com/vercel/next.js/releases/tag/v15.0.1-canary.3
const isAfterTurbopackFlagChange =
compareVersions(targetNextVersion, '15.0.1-canary.3') >= 0
const turboPackFlag = isAfterTurbopackFlagChange ? '--turbopack' : '--turbo'

if (!devScript) {
console.log(
`${pc.red('⨯')} Could not find a "dev" script in your package.json.`
`${pc.yellow('⚠')} No "dev" script found in your package.json. Skipping Turbopack suggestion.`
)
return
}

if (devScript.includes('next dev')) {
// covers "--turbopack" as well
if (devScript.includes('--turbo')) {
if (isAfterTurbopackFlagChange && !devScript.includes('--turbopack')) {
console.log() // new line
console.log(
`${pc.green('✔')} Replaced "--turbo" with "--turbopack" in your dev script.`
)
console.log() // new line
packageJson.scripts['dev'] = devScript.replace('--turbo', '--turbopack')
return
}
return
}

Expand All @@ -442,7 +460,7 @@ async function suggestTurbopack(packageJson: any): Promise<void> {

packageJson.scripts['dev'] = devScript.replace(
'next dev',
'next dev --turbopack'
`next dev ${turboPackFlag}`
)
return
}
Expand All @@ -455,7 +473,7 @@ async function suggestTurbopack(packageJson: any): Promise<void> {
{
type: 'text',
name: 'customDevScript',
message: 'Please manually add "--turbopack" to your dev command.',
message: `Please manually add "${turboPackFlag}" to your dev command.`,
initial: devScript,
},
{ onCancel }
Expand Down

0 comments on commit dc1901a

Please sign in to comment.