diff --git a/packages/vite/src/client.ts b/packages/vite/src/client.ts index 5ff1679549..dfafe8d1cb 100644 --- a/packages/vite/src/client.ts +++ b/packages/vite/src/client.ts @@ -139,6 +139,9 @@ export async function buildClient (ctx: ViteBuildContext) { ], appType: 'custom', server: { + warmup: { + clientFiles: [ctx.entry], + }, middlewareMode: true, }, } satisfies vite.InlineConfig, ctx.nuxt.options.vite.$client || {})) @@ -217,6 +220,7 @@ export async function buildClient (ctx: ViteBuildContext) { // Dev const viteServer = await vite.createServer(clientConfig) ctx.clientServer = viteServer + ctx.nuxt.hook('close', () => viteServer.close()) await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: true, isServer: false }) const transformHandler = viteServer.middlewares.stack.findIndex(m => m.handle instanceof Function && m.handle.name === 'viteTransformMiddleware') viteServer.middlewares.stack.splice(transformHandler, 0, { @@ -257,10 +261,6 @@ export async function buildClient (ctx: ViteBuildContext) { }) }) await ctx.nuxt.callHook('server:devHandler', viteMiddleware) - - ctx.nuxt.hook('close', async () => { - await viteServer.close() - }) } else { // Build logger.info('Building client...') diff --git a/packages/vite/src/server.ts b/packages/vite/src/server.ts index af87239aa2..cc13adeaf1 100644 --- a/packages/vite/src/server.ts +++ b/packages/vite/src/server.ts @@ -97,6 +97,9 @@ export async function buildServer (ctx: ViteBuildContext) { }, }, server: { + warmup: { + ssrFiles: [ctx.entry], + }, // https://github.com/vitest-dev/vitest/issues/229#issuecomment-1002685027 preTransformRequests: false, hmr: false, @@ -155,11 +158,11 @@ export async function buildServer (ctx: ViteBuildContext) { const viteServer = await vite.createServer(serverConfig) ctx.ssrServer = viteServer - await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: false, isServer: true }) - // Close server on exit ctx.nuxt.hook('close', () => viteServer.close()) + await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: false, isServer: true }) + // Initialize plugins await viteServer.pluginContainer.buildStart({}) diff --git a/packages/vite/src/vite.ts b/packages/vite/src/vite.ts index 14526e828e..a5ddb53984 100644 --- a/packages/vite/src/vite.ts +++ b/packages/vite/src/vite.ts @@ -2,7 +2,7 @@ import { existsSync } from 'node:fs' import * as vite from 'vite' import { dirname, join, normalize, resolve } from 'pathe' import type { Nuxt, NuxtBuilder, ViteConfig } from '@nuxt/schema' -import { addVitePlugin, isIgnored, logger, resolvePath } from '@nuxt/kit' +import { addVitePlugin, isIgnored, logger, resolvePath, useNitro } from '@nuxt/kit' import replace from '@rollup/plugin-replace' import type { RollupReplaceOptions } from '@rollup/plugin-replace' import { sanitizeFilePath } from 'mlly' @@ -218,15 +218,27 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => { }) if (nuxt.options.vite.warmupEntry !== false) { - const start = Date.now() - warmupViteServer(server, [ctx.entry], env.isServer) - .then(() => logger.info(`Vite ${env.isClient ? 'client' : 'server'} warmed up in ${Date.now() - start}ms`)) - .catch(logger.error) + // Don't delay nitro build for warmup + useNitro().hooks.hookOnce('compiled', () => { + const start = Date.now() + warmupViteServer(server, [ctx.entry], env.isServer) + .then(() => logger.info(`Vite ${env.isClient ? 'client' : 'server'} warmed up in ${Date.now() - start}ms`)) + .catch(logger.error) + }) } }) - await buildClient(ctx) - await buildServer(ctx) + await withLogs(() => buildClient(ctx), 'Vite client built', ctx.nuxt.options.dev) + await withLogs(() => buildServer(ctx), 'Vite server built', ctx.nuxt.options.dev) } const globalThisReplacements = Object.fromEntries([';', '(', '{', '}', ' ', '\t', '\n'].map(d => [`${d}global.`, `${d}globalThis.`])) + +async function withLogs (fn: () => Promise, message: string, enabled = true) { + if (!enabled) { return fn() } + + const start = performance.now() + await fn() + const duration = performance.now() - start + logger.success(`${message} in ${Math.round(duration)}ms`) +}