fix(nuxt): recompile templates on change events (#29954)

This commit is contained in:
Daniel Roe 2024-11-17 16:22:59 -05:00 committed by GitHub
parent e9f7cb7f89
commit 4924e26329
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 17 deletions

View File

@ -10,7 +10,6 @@ import { generateApp as _generateApp, createApp } from './app'
import { checkForExternalConfigurationFiles } from './external-config-files'
import { cleanupCaches, getVueHash } from './cache'
const IS_RESTART_PATH_RE = /^(?:app\.|error\.|plugins\/|middleware\/|layouts\/)/i
export async function build (nuxt: Nuxt) {
const app = createApp(nuxt)
nuxt.apps.default = app
@ -21,19 +20,24 @@ export async function build (nuxt: Nuxt) {
if (nuxt.options.dev) {
watch(nuxt)
nuxt.hook('builder:watch', async (event, relativePath) => {
if (event === 'change') { return }
const path = resolve(nuxt.options.srcDir, relativePath)
const relativePaths = nuxt.options._layers.map(l => relative(l.config.srcDir || l.cwd, path))
const restartPath = relativePaths.find(relativePath => IS_RESTART_PATH_RE.test(relativePath))
if (restartPath) {
if (restartPath.startsWith('app')) {
app.mainComponent = undefined
// Unset mainComponent and errorComponent if app or error component is changed
if (event === 'add' || event === 'unlink') {
const path = resolve(nuxt.options.srcDir, relativePath)
for (const layer of nuxt.options._layers) {
const relativePath = relative(layer.config.srcDir || layer.cwd, path)
if (relativePath.match(/^app\./i)) {
app.mainComponent = undefined
break
}
if (relativePath.match(/^error\./i)) {
app.errorComponent = undefined
break
}
}
if (restartPath.startsWith('error')) {
app.errorComponent = undefined
}
await generateApp()
}
// Recompile app templates
await generateApp()
})
nuxt.hook('builder:generateApp', (options) => {
// Bypass debounce if we are selectively invalidating templates

View File

@ -107,12 +107,9 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
const priorities = nuxt.options._layers.map((layer, i) => [layer.config.srcDir, -i] as const).sort(([a], [b]) => b.length - a.length)
const IMPORTS_TEMPLATE_RE = /\/imports\.(?:d\.ts|mjs)$/
function isImportsTemplate (template: ResolvedNuxtTemplate) {
return [
'/types/imports.d.ts',
'/imports.d.ts',
'/imports.mjs',
].some(i => template.filename.endsWith(i))
return IMPORTS_TEMPLATE_RE.test(template.filename)
}
const regenerateImports = async () => {