Skip to content

Commit

Permalink
next-upgrade: turbo to turbopack if supported
Browse files Browse the repository at this point in the history
  • Loading branch information
devjiwonchoi committed Oct 23, 2024
1 parent 0183e54 commit e4e5adf
Showing 1 changed file with 23 additions and 5 deletions.
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 e4e5adf

Please sign in to comment.