fix(vite): invalidate virtual modules when templates are regenerated (#2725)

This commit is contained in:
Daniel Roe 2022-01-17 10:49:53 +00:00 committed by GitHub
parent 4111038aa7
commit 4728fd545e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -92,6 +92,15 @@ export async function buildServer (ctx: ViteBuildContext) {
const viteServer = await vite.createServer(serverConfig) const viteServer = await vite.createServer(serverConfig)
ctx.nuxt.hook('close', () => viteServer.close()) ctx.nuxt.hook('close', () => viteServer.close())
// 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)
}
}
})
// Initialize plugins // Initialize plugins
await viteServer.pluginContainer.buildStart({}) await viteServer.pluginContainer.buildStart({})

View File

@ -101,6 +101,15 @@ export async function buildServer (ctx: ViteBuildContext) {
// Start development server // Start development server
const viteServer = await vite.createServer(serverConfig) const viteServer = await vite.createServer(serverConfig)
// 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())