Skip to content

Commit c7d0ce1

Browse files
committed
Defer check and imports
1 parent 5af946c commit c7d0ce1

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

packages/next/cli/next-dev.ts

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { printAndExit } from '../server/lib/utils'
77
import * as Log from '../build/output/log'
88
import { startedDevelopmentServer } from '../build/output'
99
import { cliCommand } from '../bin/next'
10-
import semver from 'next/dist/compiled/semver'
11-
import { getPackageVersion } from '../lib/get-package-version'
1210

1311
const nextDev: cliCommand = (argv) => {
1412
const validArgs: arg.Spec = {
@@ -60,6 +58,11 @@ const nextDev: cliCommand = (argv) => {
6058
}
6159

6260
async function preflight() {
61+
const { getPackageVersion } = await import('../lib/get-package-version')
62+
const semver = await import('next/dist/compiled/semver').then(
63+
(res) => res.default
64+
)
65+
6366
const reactVersion: string | null = await getPackageVersion({
6467
cwd: dir,
6568
name: 'react',
@@ -84,46 +87,44 @@ const nextDev: cliCommand = (argv) => {
8487
const port = args['--port'] || 3000
8588
const appUrl = `http://${args['--hostname'] || 'localhost'}:${port}`
8689

87-
preflight()
88-
// Ignore preflight errors:
89-
.catch(() => {})
90-
.then(() => {
91-
startedDevelopmentServer(appUrl)
90+
startedDevelopmentServer(appUrl)
9291

93-
startServer(
94-
{ dir, dev: true, isNextDevCommand: true },
95-
port,
96-
args['--hostname']
97-
)
98-
.then(async (app) => {
99-
await app.prepare()
100-
})
101-
.catch((err) => {
102-
if (err.code === 'EADDRINUSE') {
103-
let errorMessage = `Port ${port} is already in use.`
104-
const pkgAppPath = require('next/dist/compiled/find-up').sync(
105-
'package.json',
106-
{
107-
cwd: dir,
108-
}
109-
)
110-
const appPackage = require(pkgAppPath)
111-
if (appPackage.scripts) {
112-
const nextScript = Object.entries(appPackage.scripts).find(
113-
(scriptLine) => scriptLine[1] === 'next'
114-
)
115-
if (nextScript) {
116-
errorMessage += `\nUse \`npm run ${nextScript[0]} -- -p <some other port>\`.`
117-
}
118-
}
119-
// tslint:disable-next-line
120-
console.error(errorMessage)
121-
} else {
122-
// tslint:disable-next-line
123-
console.error(err)
92+
startServer(
93+
{ dir, dev: true, isNextDevCommand: true },
94+
port,
95+
args['--hostname']
96+
)
97+
.then(async (app) => {
98+
// Start preflight after server is listening and ignore errors:
99+
preflight().catch(() => {})
100+
// Finalize server bootup:
101+
await app.prepare()
102+
})
103+
.catch((err) => {
104+
if (err.code === 'EADDRINUSE') {
105+
let errorMessage = `Port ${port} is already in use.`
106+
const pkgAppPath = require('next/dist/compiled/find-up').sync(
107+
'package.json',
108+
{
109+
cwd: dir,
124110
}
125-
process.nextTick(() => process.exit(1))
126-
})
111+
)
112+
const appPackage = require(pkgAppPath)
113+
if (appPackage.scripts) {
114+
const nextScript = Object.entries(appPackage.scripts).find(
115+
(scriptLine) => scriptLine[1] === 'next'
116+
)
117+
if (nextScript) {
118+
errorMessage += `\nUse \`npm run ${nextScript[0]} -- -p <some other port>\`.`
119+
}
120+
}
121+
// tslint:disable-next-line
122+
console.error(errorMessage)
123+
} else {
124+
// tslint:disable-next-line
125+
console.error(err)
126+
}
127+
process.nextTick(() => process.exit(1))
127128
})
128129
}
129130

0 commit comments

Comments
 (0)