diff --git a/packages/nuxt/src/core/builder.ts b/packages/nuxt/src/core/builder.ts index e3c9b3ab6f..441cc3885f 100644 --- a/packages/nuxt/src/core/builder.ts +++ b/packages/nuxt/src/core/builder.ts @@ -108,6 +108,18 @@ function createWatcher () { ignored: [isIgnored, /[\\/]node_modules[\\/]/], }) + const restartPaths = new Set() + 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') { diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 6c11c3384b..29ec359988 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -412,7 +412,7 @@ async function initNuxt (nuxt: Nuxt) { await nuxt.callHook('modules:before') const modulesToInstall = new Map>() - const watchedPaths = new Set() + const modulePaths = new Set() const specifiedModules = new Set() 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 }) }