fix(vite): avoid creating server for production build (#5941)

This commit is contained in:
Anthony Fu 2022-07-17 22:17:07 +08:00 committed by GitHub
parent 02598fba56
commit 4abcfb6bf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 26 deletions

View File

@ -60,25 +60,27 @@ export async function buildClient (ctx: ViteBuildContext) {
await ctx.nuxt.callHook('vite:extendConfig', clientConfig, { isClient: true, isServer: false })
const viteServer = await vite.createServer(clientConfig)
ctx.clientServer = viteServer
await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: true, isServer: false })
if (ctx.nuxt.options.dev) {
// Dev
const viteServer = await vite.createServer(clientConfig)
ctx.clientServer = viteServer
await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: true, isServer: false })
const viteMiddleware: Connect.NextHandleFunction = (req, res, next) => {
// Workaround: vite devmiddleware modifies req.url
const originalURL = req.url
viteServer.middlewares.handle(req, res, (err) => {
req.url = originalURL
next(err)
const viteMiddleware: Connect.NextHandleFunction = (req, res, next) => {
// Workaround: vite devmiddleware modifies req.url
const originalURL = req.url
viteServer.middlewares.handle(req, res, (err) => {
req.url = originalURL
next(err)
})
}
await ctx.nuxt.callHook('server:devMiddleware', viteMiddleware)
ctx.nuxt.hook('close', async () => {
await viteServer.close()
})
}
await ctx.nuxt.callHook('server:devMiddleware', viteMiddleware)
ctx.nuxt.hook('close', async () => {
await viteServer.close()
})
if (!ctx.nuxt.options.dev) {
} else {
// Build
const start = Date.now()
await vite.build(clientConfig)
await ctx.nuxt.callHook('build:resources', wpfs)

View File

@ -101,15 +101,6 @@ export async function bundle (nuxt: Nuxt) {
if (!nuxt.options.dev) {
ctx.config.server.hmr = false
ctx.config.server.watch = undefined
// TODO: Workaround for vite watching tsconfig changes
// https://github.com/nuxt/framework/pull/5875
ctx.config.plugins.push({
name: 'nuxt:close-vite-watcher',
configureServer (server) {
return server?.watcher?.close()
}
})
}
await nuxt.callHook('vite:extend', ctx)
@ -129,6 +120,7 @@ export async function bundle (nuxt: Nuxt) {
.then(() => logger.info(`Vite ${env.isClient ? 'client' : 'server'} warmed up in ${Date.now() - start}ms`))
.catch(logger.error)
})
await buildClient(ctx)
await buildServer(ctx)
}