fix(nuxt): allow restarting nuxt on paths outside srcDir (#30771)

This commit is contained in:
Daniel Roe 2025-01-28 22:36:36 +01:00
parent ca2d91f8e0
commit 7a1e5c818b
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
2 changed files with 17 additions and 3 deletions

View File

@ -108,6 +108,18 @@ function createWatcher () {
ignored: [isIgnored, /[\\/]node_modules[\\/]/],
})
const restartPaths = new Set<string>()
const srcDir = nuxt.options.srcDir.replace(/\/?$/, '/')
for (const pattern of nuxt.options.watch) {
if (typeof pattern !== 'string') { continue }
const path = resolve(nuxt.options.srcDir, pattern)
if (!path.startsWith(srcDir)) {
restartPaths.add(path)
}
}
watcher.add([...restartPaths])
// TODO: consider moving to emit absolute path in 3.8 or 4.0
watcher.on('all', (event, path) => {
if (event === 'all' || event === 'ready' || event === 'error' || event === 'raw') {

View File

@ -412,7 +412,7 @@ async function initNuxt (nuxt: Nuxt) {
await nuxt.callHook('modules:before')
const modulesToInstall = new Map<string | NuxtModule, Record<string, any>>()
const watchedPaths = new Set<string>()
const modulePaths = new Set<string>()
const specifiedModules = new Set<string>()
for (const _mod of nuxt.options.modules) {
@ -430,13 +430,15 @@ async function initNuxt (nuxt: Nuxt) {
`${modulesDir}/*/index{${nuxt.options.extensions.join(',')}}`,
])
for (const mod of layerModules) {
watchedPaths.add(mod)
modulePaths.add(mod)
if (specifiedModules.has(mod)) { continue }
specifiedModules.add(mod)
modulesToInstall.set(mod, {})
}
}
nuxt.options.watch.push(...modulePaths)
// Register user and then ad-hoc modules
for (const key of ['modules', '_modules'] as const) {
for (const item of nuxt.options[key as 'modules']) {
@ -659,7 +661,7 @@ export default defineNuxtPlugin({
nuxt.hooks.hook('builder:watch', (event, relativePath) => {
const path = resolve(nuxt.options.srcDir, relativePath)
// Local module patterns
if (watchedPaths.has(path)) {
if (modulePaths.has(path)) {
return nuxt.callHook('restart', { hard: true })
}