From dc1901a2c2184c66247eae7e1e5ea2b0cc9629aa Mon Sep 17 00:00:00 2001 From: Jiwon Choi Date: Thu, 24 Oct 2024 18:36:56 +0900 Subject: [PATCH] next-upgrade: change `--turbo` to `--turbopack` if applicable (#71737) --- .../change-turbo-to-turbopack/README.md | 1 + .../change-turbo-to-turbopack/package.json | 11 ++++++++ .../pnpm-workspace.yaml | 0 .../suggest-turbopack/README.md | 1 + .../suggest-turbopack/package.json | 11 ++++++++ .../suggest-turbopack/pnpm-workspace.yaml | 0 packages/next-codemod/bin/upgrade.ts | 28 +++++++++++++++---- 7 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 packages/next-codemod/bin/__testfixtures__/change-turbo-to-turbopack/README.md create mode 100644 packages/next-codemod/bin/__testfixtures__/change-turbo-to-turbopack/package.json create mode 100644 packages/next-codemod/bin/__testfixtures__/change-turbo-to-turbopack/pnpm-workspace.yaml create mode 100644 packages/next-codemod/bin/__testfixtures__/suggest-turbopack/README.md create mode 100644 packages/next-codemod/bin/__testfixtures__/suggest-turbopack/package.json create mode 100644 packages/next-codemod/bin/__testfixtures__/suggest-turbopack/pnpm-workspace.yaml diff --git a/packages/next-codemod/bin/__testfixtures__/change-turbo-to-turbopack/README.md b/packages/next-codemod/bin/__testfixtures__/change-turbo-to-turbopack/README.md new file mode 100644 index 0000000000000..30f21e141d2fc --- /dev/null +++ b/packages/next-codemod/bin/__testfixtures__/change-turbo-to-turbopack/README.md @@ -0,0 +1 @@ +Change `--turbo` to `--turbopack` in `next dev` script. diff --git a/packages/next-codemod/bin/__testfixtures__/change-turbo-to-turbopack/package.json b/packages/next-codemod/bin/__testfixtures__/change-turbo-to-turbopack/package.json new file mode 100644 index 0000000000000..55b8ea31385a8 --- /dev/null +++ b/packages/next-codemod/bin/__testfixtures__/change-turbo-to-turbopack/package.json @@ -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" + } +} diff --git a/packages/next-codemod/bin/__testfixtures__/change-turbo-to-turbopack/pnpm-workspace.yaml b/packages/next-codemod/bin/__testfixtures__/change-turbo-to-turbopack/pnpm-workspace.yaml new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/packages/next-codemod/bin/__testfixtures__/suggest-turbopack/README.md b/packages/next-codemod/bin/__testfixtures__/suggest-turbopack/README.md new file mode 100644 index 0000000000000..05d6f0fcb9a45 --- /dev/null +++ b/packages/next-codemod/bin/__testfixtures__/suggest-turbopack/README.md @@ -0,0 +1 @@ +Suggest adding `--turbopack` to `next dev` script. diff --git a/packages/next-codemod/bin/__testfixtures__/suggest-turbopack/package.json b/packages/next-codemod/bin/__testfixtures__/suggest-turbopack/package.json new file mode 100644 index 0000000000000..132aed31cd42f --- /dev/null +++ b/packages/next-codemod/bin/__testfixtures__/suggest-turbopack/package.json @@ -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" + } +} diff --git a/packages/next-codemod/bin/__testfixtures__/suggest-turbopack/pnpm-workspace.yaml b/packages/next-codemod/bin/__testfixtures__/suggest-turbopack/pnpm-workspace.yaml new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/packages/next-codemod/bin/upgrade.ts b/packages/next-codemod/bin/upgrade.ts index e8425129137e2..36f3629cc1775 100644 --- a/packages/next-codemod/bin/upgrade.ts +++ b/packages/next-codemod/bin/upgrade.ts @@ -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( @@ -410,12 +410,21 @@ 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 { +async function suggestTurbopack( + packageJson: any, + targetNextVersion: string +): Promise { 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 } @@ -423,6 +432,15 @@ async function suggestTurbopack(packageJson: any): Promise { 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 } @@ -442,7 +460,7 @@ async function suggestTurbopack(packageJson: any): Promise { packageJson.scripts['dev'] = devScript.replace( 'next dev', - 'next dev --turbopack' + `next dev ${turboPackFlag}` ) return } @@ -455,7 +473,7 @@ async function suggestTurbopack(packageJson: any): Promise { { 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 }