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

fix: restart optimize #7004

Merged
merged 3 commits into from Mar 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 21 additions & 5 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -400,7 +400,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(() => {
Expand Down Expand Up @@ -565,7 +565,7 @@ export async function createServer(
try {
server._optimizeDepsMetadata = await optimizeDeps(
config,
config.server.force || server._forceOptimizeOnRestart
config.server.force
)
} finally {
server._isRunningOptimizer = false
Expand Down Expand Up @@ -734,9 +734,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
Expand All @@ -745,7 +754,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]
}
Expand All @@ -765,4 +778,7 @@ async function restartServer(server: ViteDevServer) {
} else {
logger.info('server restarted.', { timestamp: true })
}

// new server (the current server) can restart now
newServer._restartPromise = null
}