mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-28 16:42:04 +00:00
perf(vite): start warmups after nitro build (#27963)
This commit is contained in:
parent
b91ec5d6dd
commit
a8eb2a46e1
@ -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...')
|
||||
|
@ -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({})
|
||||
|
||||
|
@ -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'
|
||||
@ -217,15 +217,27 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => {
|
||||
})
|
||||
|
||||
if (nuxt.options.vite.warmupEntry !== false) {
|
||||
// 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<void>, 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`)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user