diff --git a/packages/vite/src/client.ts b/packages/vite/src/client.ts index 2cb3f03ebb..6434bd463f 100644 --- a/packages/vite/src/client.ts +++ b/packages/vite/src/client.ts @@ -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) diff --git a/packages/vite/src/vite.ts b/packages/vite/src/vite.ts index 342db7d0b1..84a399ed00 100644 --- a/packages/vite/src/vite.ts +++ b/packages/vite/src/vite.ts @@ -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) }