mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-13 09:33:54 +00:00
perf(vite): start warmups after nitro build (#27963)
This commit is contained in:
parent
bbb755084b
commit
90900c6c8a
@ -139,6 +139,9 @@ export async function buildClient (ctx: ViteBuildContext) {
|
|||||||
],
|
],
|
||||||
appType: 'custom',
|
appType: 'custom',
|
||||||
server: {
|
server: {
|
||||||
|
warmup: {
|
||||||
|
clientFiles: [ctx.entry],
|
||||||
|
},
|
||||||
middlewareMode: true,
|
middlewareMode: true,
|
||||||
},
|
},
|
||||||
} satisfies vite.InlineConfig, ctx.nuxt.options.vite.$client || {}))
|
} satisfies vite.InlineConfig, ctx.nuxt.options.vite.$client || {}))
|
||||||
@ -217,6 +220,7 @@ export async function buildClient (ctx: ViteBuildContext) {
|
|||||||
// Dev
|
// Dev
|
||||||
const viteServer = await vite.createServer(clientConfig)
|
const viteServer = await vite.createServer(clientConfig)
|
||||||
ctx.clientServer = viteServer
|
ctx.clientServer = viteServer
|
||||||
|
ctx.nuxt.hook('close', () => viteServer.close())
|
||||||
await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: true, isServer: false })
|
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')
|
const transformHandler = viteServer.middlewares.stack.findIndex(m => m.handle instanceof Function && m.handle.name === 'viteTransformMiddleware')
|
||||||
viteServer.middlewares.stack.splice(transformHandler, 0, {
|
viteServer.middlewares.stack.splice(transformHandler, 0, {
|
||||||
@ -257,10 +261,6 @@ export async function buildClient (ctx: ViteBuildContext) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
await ctx.nuxt.callHook('server:devHandler', viteMiddleware)
|
await ctx.nuxt.callHook('server:devHandler', viteMiddleware)
|
||||||
|
|
||||||
ctx.nuxt.hook('close', async () => {
|
|
||||||
await viteServer.close()
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
// Build
|
// Build
|
||||||
logger.info('Building client...')
|
logger.info('Building client...')
|
||||||
|
@ -97,6 +97,9 @@ export async function buildServer (ctx: ViteBuildContext) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
server: {
|
server: {
|
||||||
|
warmup: {
|
||||||
|
ssrFiles: [ctx.entry],
|
||||||
|
},
|
||||||
// https://github.com/vitest-dev/vitest/issues/229#issuecomment-1002685027
|
// https://github.com/vitest-dev/vitest/issues/229#issuecomment-1002685027
|
||||||
preTransformRequests: false,
|
preTransformRequests: false,
|
||||||
hmr: false,
|
hmr: false,
|
||||||
@ -155,11 +158,11 @@ export async function buildServer (ctx: ViteBuildContext) {
|
|||||||
const viteServer = await vite.createServer(serverConfig)
|
const viteServer = await vite.createServer(serverConfig)
|
||||||
ctx.ssrServer = viteServer
|
ctx.ssrServer = viteServer
|
||||||
|
|
||||||
await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: false, isServer: true })
|
|
||||||
|
|
||||||
// Close server on exit
|
// Close server on exit
|
||||||
ctx.nuxt.hook('close', () => viteServer.close())
|
ctx.nuxt.hook('close', () => viteServer.close())
|
||||||
|
|
||||||
|
await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: false, isServer: true })
|
||||||
|
|
||||||
// Initialize plugins
|
// Initialize plugins
|
||||||
await viteServer.pluginContainer.buildStart({})
|
await viteServer.pluginContainer.buildStart({})
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ import { existsSync } from 'node:fs'
|
|||||||
import * as vite from 'vite'
|
import * as vite from 'vite'
|
||||||
import { dirname, join, normalize, resolve } from 'pathe'
|
import { dirname, join, normalize, resolve } from 'pathe'
|
||||||
import type { Nuxt, NuxtBuilder, ViteConfig } from '@nuxt/schema'
|
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 replace from '@rollup/plugin-replace'
|
||||||
import type { RollupReplaceOptions } from '@rollup/plugin-replace'
|
import type { RollupReplaceOptions } from '@rollup/plugin-replace'
|
||||||
import { sanitizeFilePath } from 'mlly'
|
import { sanitizeFilePath } from 'mlly'
|
||||||
@ -218,15 +218,27 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (nuxt.options.vite.warmupEntry !== false) {
|
if (nuxt.options.vite.warmupEntry !== false) {
|
||||||
const start = Date.now()
|
// Don't delay nitro build for warmup
|
||||||
warmupViteServer(server, [ctx.entry], env.isServer)
|
useNitro().hooks.hookOnce('compiled', () => {
|
||||||
.then(() => logger.info(`Vite ${env.isClient ? 'client' : 'server'} warmed up in ${Date.now() - start}ms`))
|
const start = Date.now()
|
||||||
.catch(logger.error)
|
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 withLogs(() => buildClient(ctx), 'Vite client built', ctx.nuxt.options.dev)
|
||||||
await buildServer(ctx)
|
await withLogs(() => buildServer(ctx), 'Vite server built', ctx.nuxt.options.dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
const globalThisReplacements = Object.fromEntries([';', '(', '{', '}', ' ', '\t', '\n'].map(d => [`${d}global.`, `${d}globalThis.`]))
|
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