Skip to content

Commit

Permalink
fix(vite): avoid creating server for production build (nuxt#5941)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Jul 17, 2022
1 parent 02598fb commit 4abcfb6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
36 changes: 19 additions & 17 deletions packages/vite/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
await ctx.nuxt.callHook('server:devMiddleware', viteMiddleware)

ctx.nuxt.hook('close', async () => {
await viteServer.close()
})
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)

if (!ctx.nuxt.options.dev) {
ctx.nuxt.hook('close', async () => {
await viteServer.close()
})
} else {
// Build
const start = Date.now()
await vite.build(clientConfig)
await ctx.nuxt.callHook('build:resources', wpfs)
Expand Down
10 changes: 1 addition & 9 deletions packages/vite/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

0 comments on commit 4abcfb6

Please sign in to comment.