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

next-upgrade: change --turbo to --turbopack if applicable #71737

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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"
}
}
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"
}
}
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.`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels more like output I'd expect to see with a --debug flag rather being in the common case. Not sure if others agree but I'd probably just remove it all together.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, it's rare for an existing app to be missing scripts.dev, and shouldn't we not silence it when it isn't applicable? I agree that it's a bit verbose, but it would be better to inform users correctly that something wasn't as expected!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's just the language. I'm viewing it as someone who doesn't know what Turbopack is. The warning would make me think "what is a Turbopack suggestion? Why should I care?"

Maybe the language should be more about suggesting the user add a dev script to help with local development?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on omitting skipping turbopack suggestion as it's not super relevant in the context

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context should include Turbopack somehow because without it we don't even need to inform that "dev" script is missing.

Copy link
Member

@eps1lon eps1lon Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can already see the feedback: "Why didn't it ask me to switch to turbopack?".

How about

Suggested change
`${pc.yellow('⚠')} No "dev" script found in your package.json. Skipping Turbopack suggestion.`
`No "dev" script found in your package.json. If you're not already using Turbopack, consider enabling it by using `next dev --turbopack`: https://nextjs.org/docs/architecture/turbopack.`

?

)
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
Loading