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

feat: Add option to enable Turbopack with create-next-app #65926

Merged
merged 20 commits into from
May 20, 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
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',
`
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved

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
Loading