Skip to content

Commit

Permalink
feat: Add option to enable Turbopack with create-next-app (#65926)
Browse files Browse the repository at this point in the history
This PR addresses an issue where the `dev` script in `package.json` was
not correctly updated to use the `--turbo` option when specified. The
changes ensure that the `dev` script is set to `next dev --turbo` if the
`turbo` option is enabled.

### Changes:
1. **Function Modification**:
- Updated the `modifyPackageJson` function to include a check for the
`turbo` parameter.
- If `turbo` is true, the `dev` script in `package.json` is set to `next
dev --turbo`.
   - If `turbo` is false, the `dev` script remains `next dev`.

2. **Integration in App Creation**:
- Integrated the `modifyPackageJson` function into the app creation
process to ensure the `dev` script is correctly set based on the `turbo`
parameter.

### Testing:
- Verified that the `dev` script in `package.json` is correctly updated
to `next dev --turbo` when the `turbo` option is enabled.
- Ensured that the `dev` script remains `next dev` when the `turbo`
option is not enabled.

### Related Issues:
- Fixes #65924

### Notes:
- This change ensures that developers opting to use Turbopack for
development will have the correct script set up automatically.

---------

Signed-off-by: Arindam Majumder <[email protected]>
Co-authored-by: Lee Robinson <[email protected]>
Co-authored-by: devjiwonchoi <[email protected]>
Co-authored-by: Jiachi Liu <[email protected]>
Co-authored-by: Sam Ko <[email protected]>
  • Loading branch information
5 people authored May 20, 2024
1 parent 0eedddb commit 60ad8af
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 227 deletions.
1 change: 1 addition & 0 deletions docs/01-getting-started/01-installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Would you like to use ESLint? No / Yes
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 use Turbopack for `next dev`? No / Yes
Would you like to customize the default import alias (@/*)? No / Yes
What import alias would you like configured? @/*
```
Expand Down
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 @@ -38,6 +38,7 @@ Would you like to use ESLint? No / Yes
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 use Turbopack for `next dev`? No / Yes
Would you like to customize the default import alias (@/*)? No / Yes
```

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
3 changes: 3 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 @@ -229,6 +231,7 @@ export async function createApp({
srcDir,
importAlias,
skipInstall,
turbo,
})
}

Expand Down
29 changes: 29 additions & 0 deletions packages/create-next-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ const program = new Commander.Command(packageJson.name)
`
Initialize inside a \`src/\` directory.
`
)
.option(
'--turbo',
`
Enable Turbopack by default for development.
`
)
.option(
Expand Down Expand Up @@ -272,6 +279,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 @@ -396,6 +404,25 @@ async function run(): Promise<void> {
}
}

if (!program.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`}?`,
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 @@ -455,6 +482,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 Expand Up @@ -485,6 +513,7 @@ async function run(): Promise<void> {
importAlias: program.importAlias,
skipInstall: program.skipInstall,
empty: program.empty,
turbo: program.turbo,
})
}
conf.set('preferences', preferences)
Expand Down
3 changes: 2 additions & 1 deletion packages/create-next-app/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const installTemplate = async ({
srcDir,
importAlias,
skipInstall,
turbo,
}: InstallTemplateArgs) => {
console.log(bold(`Using ${packageManager}.`));

Expand Down Expand Up @@ -174,7 +175,7 @@ export const installTemplate = async ({
version: "0.1.0",
private: true,
scripts: {
dev: "next dev",
dev: `next dev${turbo ? " --turbo" : ""}`,
build: "next build",
start: "next start",
lint: "next lint",
Expand Down
1 change: 1 addition & 0 deletions packages/create-next-app/templates/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export interface InstallTemplateArgs {
srcDir: boolean;
importAlias: string;
skipInstall: boolean;
turbo: boolean;
}
2 changes: 2 additions & 0 deletions test/integration/create-next-app/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe.skip('create-next-app', () => {
projectName,
'--ts',
'--app',
'--no-turbo',
'--no-eslint',
'--no-tailwind',
'--no-src-dir',
Expand Down Expand Up @@ -99,6 +100,7 @@ describe.skip('create-next-app', () => {
projectName,
'--ts',
'--app',
'--no-turbo',
'--no-eslint',
'--no-tailwind',
'--no-src-dir',
Expand Down
114 changes: 58 additions & 56 deletions test/integration/create-next-app/package-manager/bun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe.skip('create-next-app with package manager bun', () => {
'--ts',
'--app',
'--use-bun',
'--no-turbo',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
Expand All @@ -55,72 +56,73 @@ describe.skip('create-next-app with package manager bun', () => {
})
})
})
})

it('should use bun when user-agent is bun', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-bun'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'bun' },
}
)
it('should use bun when user-agent is bun', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-bun'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-turbo',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'bun' },
}
)

expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})

it('should use bun for --use-bun flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-bun-with-example'
const res = await run(
[projectName, '--use-bun', '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{ cwd }
)
it('should use bun for --use-bun flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-bun-with-example'
const res = await run(
[projectName, '--use-bun', '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{ cwd }
)

expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})

it('should use bun when user-agent is bun with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-bun-with-example'
const res = await run(
[projectName, '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'bun' },
}
)
it('should use bun when user-agent is bun with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-bun-with-example'
const res = await run(
[projectName, '--example', FULL_EXAMPLE_PATH],
nextInstall.installDir,
{
cwd,
env: { npm_config_user_agent: 'bun' },
}
)

expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
Loading

0 comments on commit 60ad8af

Please sign in to comment.