fix(vite): invalidate client modules too (#3116)

This commit is contained in:
Daniel Roe 2022-02-08 00:10:42 +00:00 committed by GitHub
parent dabb72ed7c
commit 0f9bcbf68f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 9 deletions

View File

@ -131,15 +131,7 @@ export async function buildServer (ctx: ViteBuildContext) {
// Start development server // Start development server
const viteServer = await vite.createServer(serverConfig) const viteServer = await vite.createServer(serverConfig)
await ctx.nuxt.callHook('vite:serverCreated', viteServer)
// Invalidate virtual modules when templates are re-generated
ctx.nuxt.hook('app:templatesGenerated', () => {
for (const [id, mod] of viteServer.moduleGraph.idToModuleMap) {
if (id.startsWith('\x00virtual:')) {
viteServer.moduleGraph.invalidateModule(mod)
}
}
})
// Close server on exit // Close server on exit
ctx.nuxt.hook('close', () => viteServer.close()) ctx.nuxt.hook('close', () => viteServer.close())

View File

@ -104,6 +104,15 @@ export async function bundle (nuxt: Nuxt) {
await nuxt.callHook('vite:extend', ctx) await nuxt.callHook('vite:extend', ctx)
nuxt.hook('vite:serverCreated', (server: vite.ViteDevServer) => { nuxt.hook('vite:serverCreated', (server: vite.ViteDevServer) => {
// Invalidate virtual modules when templates are re-generated
ctx.nuxt.hook('app:templatesGenerated', () => {
for (const [id, mod] of server.moduleGraph.idToModuleMap) {
if (id.startsWith('\x00virtual:')) {
server.moduleGraph.invalidateModule(mod)
}
}
})
const start = Date.now() const start = Date.now()
warmupViteServer(server, ['/entry.mjs']).then(() => { warmupViteServer(server, ['/entry.mjs']).then(() => {
consola.info(`Vite warmed up in ${Date.now() - start}ms`) consola.info(`Vite warmed up in ${Date.now() - start}ms`)