Skip to content

Commit

Permalink
feat(server): console hold address(fixes vitejs#10527)
Browse files Browse the repository at this point in the history
  • Loading branch information
alqmc committed Oct 22, 2022
1 parent de6323f commit b6fb575
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
21 changes: 1 addition & 20 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { performance } from 'node:perf_hooks'
import { cac } from 'cac'
import colors from 'picocolors'
import type { BuildOptions } from './build'
Expand Down Expand Up @@ -105,25 +104,7 @@ cli

await server.listen()

const info = server.config.logger.info

// @ts-ignore
const viteStartTime = global.__vite_start_time ?? false
const startupDurationString = viteStartTime
? colors.dim(
`ready in ${colors.reset(
colors.bold(Math.ceil(performance.now() - viteStartTime))
)} ms`
)
: ''

info(
`\n ${colors.green(
`${colors.bold('VITE')} v${VERSION}`
)} ${startupDurationString}\n`,
{ clear: !server.config.logger.hasWarned }
)

server.printReadyInfo()
server.printUrls()
} catch (e) {
createLogger(options.logLevel).error(
Expand Down
19 changes: 18 additions & 1 deletion packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export async function handleHMRUpdate(
const isEnv =
config.inlineConfig.envFile !== false &&
(fileName === '.env' || fileName.startsWith('.env.'))

const keepAddress = config.server.keepAddress
if (isConfig || isConfigDependency || isEnv) {
// auto restart server
debugHmr(`[config change] ${colors.dim(shortFile)}`)
Expand Down Expand Up @@ -108,6 +110,11 @@ export async function handleHMRUpdate(
clear: true,
timestamp: true
})
if (keepAddress) {
server.printReadyInfo()
server.printUrls()
}

ws.send({
type: 'full-reload',
path: config.server.middlewareMode
Expand All @@ -128,12 +135,14 @@ export function updateModules(
file: string,
modules: ModuleNode[],
timestamp: number,
{ config, ws }: ViteDevServer
{ config, ws, printReadyInfo, printUrls }: ViteDevServer
): void {
const updates: Update[] = []
const invalidatedModules = new Set<ModuleNode>()
let needFullReload = false

const keepAddress = config.server.keepAddress

for (const mod of modules) {
invalidate(mod, timestamp, invalidatedModules)
if (needFullReload) {
Expand Down Expand Up @@ -169,6 +178,10 @@ export function updateModules(
clear: true,
timestamp: true
})
if (keepAddress) {
printReadyInfo()
printUrls()
}
ws.send({
type: 'full-reload'
})
Expand All @@ -186,6 +199,10 @@ export function updateModules(
.join('\n'),
{ clear: true, timestamp: true }
)
if (keepAddress) {
printReadyInfo()
printUrls()
}
ws.send({
type: 'update',
updates
Expand Down
31 changes: 30 additions & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
initDepsOptimizer,
initDevSsrDepsOptimizer
} from '../optimizer'
import { CLIENT_DIR } from '../constants'
import { CLIENT_DIR, VERSION } from '../constants'
import type { Logger } from '../logger'
import { printServerUrls } from '../logger'
import { invalidatePackageData } from '../packages'
Expand Down Expand Up @@ -123,6 +123,12 @@ export interface ServerOptions extends CommonServerOptions {
* in a future minor version without following semver
*/
force?: boolean

/**
* Always keep address
* @default false
*/
keepAddress?: boolean
}

export interface ResolvedServerOptions extends ServerOptions {
Expand Down Expand Up @@ -263,6 +269,11 @@ export interface ViteDevServer {
* Print server urls
*/
printUrls(): void

/**
* Print server start ready info
*/
printReadyInfo(): void
/**
* Restart the server.
*
Expand Down Expand Up @@ -436,6 +447,24 @@ export async function createServer(
)
}
},
printReadyInfo() {
const info = server.config.logger.info
// @ts-ignore
const viteStartTime = global.__vite_start_time ?? false
const startupDurationString = viteStartTime
? colors.dim(
`ready in ${colors.reset(
colors.bold(Math.ceil(performance.now() - viteStartTime))
)} ms`
)
: ''
info(
`\n ${colors.green(
`${colors.bold('VITE')} v${VERSION}`
)} ${startupDurationString}\n`,
{ clear: !server.config.logger.hasWarned }
)
},
async restart(forceOptimize?: boolean) {
if (!server._restartPromise) {
server._forceOptimizeOnRestart = !!forceOptimize
Expand Down

0 comments on commit b6fb575

Please sign in to comment.