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

This commit is contained in:
Daniel Roe 2024-11-17 16:22:59 -05:00
parent 7284241808
commit 8e0f7314aa
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
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 { checkForExternalConfigurationFiles } from './external-config-files'
import { cleanupCaches, getVueHash } from './cache' import { cleanupCaches, getVueHash } from './cache'
const IS_RESTART_PATH_RE = /^(?:app\.|error\.|plugins\/|middleware\/|layouts\/)/i
export async function build (nuxt: Nuxt) { export async function build (nuxt: Nuxt) {
const app = createApp(nuxt) const app = createApp(nuxt)
nuxt.apps.default = app nuxt.apps.default = app
@ -21,19 +20,24 @@ export async function build (nuxt: Nuxt) {
if (nuxt.options.dev) { if (nuxt.options.dev) {
watch(nuxt) watch(nuxt)
nuxt.hook('builder:watch', async (event, relativePath) => { nuxt.hook('builder:watch', async (event, relativePath) => {
if (event === 'change') { return } // Unset mainComponent and errorComponent if app or error component is changed
const path = resolve(nuxt.options.srcDir, relativePath) if (event === 'add' || event === 'unlink') {
const relativePaths = nuxt.options._layers.map(l => relative(l.config.srcDir || l.cwd, path)) const path = resolve(nuxt.options.srcDir, relativePath)
const restartPath = relativePaths.find(relativePath => IS_RESTART_PATH_RE.test(relativePath)) for (const layer of nuxt.options._layers) {
if (restartPath) { const relativePath = relative(layer.config.srcDir || layer.cwd, path)
if (restartPath.startsWith('app')) { if (relativePath.match(/^app\./i)) {
app.mainComponent = undefined 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) => { nuxt.hook('builder:generateApp', (options) => {
// Bypass debounce if we are selectively invalidating templates // 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 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) { function isImportsTemplate (template: ResolvedNuxtTemplate) {
return [ return IMPORTS_TEMPLATE_RE.test(template.filename)
'/types/imports.d.ts',
'/imports.d.ts',
'/imports.mjs',
].some(i => template.filename.endsWith(i))
} }
const regenerateImports = async () => { const regenerateImports = async () => {