Skip to content

Commit

Permalink
chore: extract DEFAULT_DEV_PORT and DEFAULT_PREVIEW_PORT (#11669)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenfan0 authored Jan 17, 2023
1 parent 5865f39 commit c9f009d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,7 @@ export const wildcardHosts = new Set([
'::',
'0000:0000:0000:0000:0000:0000:0000:0000',
])

export const DEFAULT_DEV_PORT = 5173

export const DEFAULT_PREVIEW_PORT = 4173
3 changes: 2 additions & 1 deletion packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import compression from './server/middlewares/compression'
import { proxyMiddleware } from './server/middlewares/proxy'
import { resolveHostname, resolveServerUrls, shouldServeFile } from './utils'
import { printServerUrls } from './logger'
import { DEFAULT_PREVIEW_PORT } from './constants'
import { resolveConfig } from '.'
import type { InlineConfig, ResolvedConfig } from '.'

Expand Down Expand Up @@ -139,7 +140,7 @@ export async function preview(

const options = config.preview
const hostname = await resolveHostname(options.host)
const port = options.port ?? 4173
const port = options.port ?? DEFAULT_PREVIEW_PORT
const protocol = options.https ? 'https' : 'http'
const logger = config.logger

Expand Down
9 changes: 6 additions & 3 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
} from '../optimizer'
import { bindShortcuts } from '../shortcuts'
import type { BindShortcutsOptions } from '../shortcuts'
import { CLIENT_DIR } from '../constants'
import { CLIENT_DIR, DEFAULT_DEV_PORT } from '../constants'
import type { Logger } from '../logger'
import { printServerUrls } from '../logger'
import { invalidatePackageData } from '../packages'
Expand Down Expand Up @@ -661,7 +661,7 @@ async function startServer(
}

const options = server.config.server
const port = inlinePort ?? options.port ?? 5173
const port = inlinePort ?? options.port ?? DEFAULT_DEV_PORT
const hostname = await resolveHostname(options.host)

const protocol = options.https ? 'https' : 'http'
Expand Down Expand Up @@ -809,7 +809,10 @@ async function restartServer(server: ViteDevServer) {
if (!middlewareMode) {
await server.listen(port, true)
logger.info('server restarted.', { timestamp: true })
if ((port ?? 5173) !== (prevPort ?? 5173) || host !== prevHost) {
if (
(port ?? DEFAULT_DEV_PORT) !== (prevPort ?? DEFAULT_DEV_PORT) ||
host !== prevHost
) {
logger.info('')
server.printUrls()
}
Expand Down

0 comments on commit c9f009d

Please sign in to comment.