fix: restart optimize (#7004)

This commit is contained in:
Bjorn Lu 2022-03-03 16:31:46 +08:00 committed by GitHub
parent ce07a0ad6a
commit 47fbe29a97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,7 +10,7 @@ import chokidar from 'chokidar'
import type { CommonServerOptions } from '../http'
import { resolveHttpsConfig, resolveHttpServer, httpServerStart } from '../http'
import type { InlineConfig, ResolvedConfig } from '../config'
import { resolveConfig } from '../config'
import { mergeConfig, resolveConfig } from '../config'
import type { PluginContainer } from './pluginContainer'
import { createPluginContainer } from './pluginContainer'
import type { FSWatcher, WatchOptions } from 'types/chokidar'
@ -409,7 +409,7 @@ export async function createServer(
throw new Error('cannot print server URLs in middleware mode.')
}
},
async restart(forceOptimize: boolean) {
async restart(forceOptimize?: boolean) {
if (!server._restartPromise) {
server._forceOptimizeOnRestart = !!forceOptimize
server._restartPromise = restartServer(server).finally(() => {
@ -574,7 +574,7 @@ export async function createServer(
try {
server._optimizeDepsMetadata = await optimizeDeps(
config,
config.server.force || server._forceOptimizeOnRestart
config.server.force
)
} finally {
server._isRunningOptimizer = false
@ -743,9 +743,18 @@ async function restartServer(server: ViteDevServer) {
await server.close()
let inlineConfig = server.config.inlineConfig
if (server._forceOptimizeOnRestart) {
inlineConfig = mergeConfig(inlineConfig, {
server: {
force: true
}
})
}
let newServer = null
try {
newServer = await createServer(server.config.inlineConfig)
newServer = await createServer(inlineConfig)
} catch (err: any) {
server.config.logger.error(err.message, {
timestamp: true
@ -754,7 +763,11 @@ async function restartServer(server: ViteDevServer) {
}
for (const key in newServer) {
if (key !== 'app') {
if (key === '_restartPromise') {
// prevent new server `restart` function from calling
// @ts-ignore
newServer[key] = server[key]
} else if (key !== 'app') {
// @ts-ignore
server[key] = newServer[key]
}
@ -774,4 +787,7 @@ async function restartServer(server: ViteDevServer) {
} else {
logger.info('server restarted.', { timestamp: true })
}
// new server (the current server) can restart now
newServer._restartPromise = null
}