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 isRestart for environment #18262

Closed
Closed
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 packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export function defineConfig(config: UserConfigExport): UserConfigExport {

export interface CreateDevEnvironmentContext {
ws: WebSocketServer
isRestart?: boolean
}

export interface DevEnvironmentOptions {
Expand Down
7 changes: 5 additions & 2 deletions packages/vite/src/node/server/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ export class DevEnvironment extends BaseEnvironment {
}
}

async init(options?: { watcher?: FSWatcher }): Promise<void> {
async init(options?: {
watcher?: FSWatcher
isRestart?: boolean
}): Promise<void> {
if (this._initiated) {
return
}
Expand Down Expand Up @@ -202,7 +205,7 @@ export class DevEnvironment extends BaseEnvironment {
}
}

async close(): Promise<void> {
async close(_options?: { isRestart?: boolean }): Promise<void> {
this._closing = true

this._crawlEndFinder?.cancel()
Expand Down
19 changes: 10 additions & 9 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export interface ViteDevServer {
/**
* Stop the server.
*/
close(): Promise<void>
close(options?: { isRestart?: boolean }): Promise<void>
/**
* Print server urls
*/
Expand Down Expand Up @@ -418,12 +418,12 @@ export interface ResolvedServerUrls {
export function createServer(
inlineConfig: InlineConfig = {},
): Promise<ViteDevServer> {
return _createServer(inlineConfig, { hotListen: true })
return _createServer(inlineConfig, { isRestart: false })
}

export async function _createServer(
inlineConfig: InlineConfig = {},
options: { hotListen: boolean },
options: { isRestart: boolean },
): Promise<ViteDevServer> {
const config = await resolveConfig(inlineConfig, 'serve')

Expand Down Expand Up @@ -494,12 +494,13 @@ export async function _createServer(
config,
{
ws,
isRestart: options.isRestart,
},
)
}

for (const environment of Object.values(environments)) {
await environment.init({ watcher })
await environment.init({ watcher, isRestart: options.isRestart })
}

// Backward compatibility
Expand Down Expand Up @@ -656,7 +657,7 @@ export async function _createServer(
server.config.logger.warn('No URL available to open in browser')
}
},
async close() {
async close(options) {
if (!middlewareMode) {
teardownSIGTERMListener(closeServerAndExit)
}
Expand All @@ -666,7 +667,7 @@ export async function _createServer(
ws.close(),
Promise.allSettled(
Object.values(server.environments).map((environment) =>
environment.close(),
environment.close({ isRestart: options?.isRestart }),
),
),
closeHttpServer(),
Expand Down Expand Up @@ -941,7 +942,7 @@ export async function _createServer(
return listen(port, ...args)
}) as any
} else {
if (options.hotListen) {
if (!options.isRestart) {
Object.values(environments).forEach((e) => e.hot.listen())
}
await initServer()
Expand Down Expand Up @@ -1115,7 +1116,7 @@ async function restartServer(server: ViteDevServer) {
let newServer: ViteDevServer | null = null
try {
// delay ws server listen
newServer = await _createServer(inlineConfig, { hotListen: false })
newServer = await _createServer(inlineConfig, { isRestart: true })
} catch (err: any) {
server.config.logger.error(err.message, {
timestamp: true,
Expand Down Expand Up @@ -1148,7 +1149,7 @@ async function restartServer(server: ViteDevServer) {
if (!middlewareMode) {
await server.listen(port, true)
} else {
server.ws.listen()
Object.values(server.environments).forEach((e) => e.hot.listen())
}
logger.info('server restarted.', { timestamp: true })

Expand Down
Loading