fix(nuxt): don't debounce watcher and include layers (#5002)

This commit is contained in:
Daniel Roe 2022-06-10 15:50:47 +01:00 committed by GitHub
parent 11a7340883
commit fbcbac01d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 8 deletions

View File

@ -13,11 +13,11 @@ export async function build (nuxt: Nuxt) {
if (nuxt.options.dev) { if (nuxt.options.dev) {
watch(nuxt) watch(nuxt)
nuxt.hook('builder:watch', async (event, path) => { nuxt.hook('builder:watch', async (event, path) => {
if (event !== 'change' && /app|error|plugins/i.test(path)) { if (event !== 'change' && /^(app\.|error\.|plugins\/|layouts\/)/i.test(path)) {
if (path.match(/app/i)) { if (path.startsWith('app')) {
app.mainComponent = null app.mainComponent = null
} }
if (path.match(/error/i)) { if (path.startsWith('error')) {
app.errorComponent = null app.errorComponent = null
} }
await generateApp() await generateApp()
@ -38,7 +38,7 @@ export async function build (nuxt: Nuxt) {
} }
function watch (nuxt: Nuxt) { function watch (nuxt: Nuxt) {
const watcher = chokidar.watch(nuxt.options.srcDir, { const watcher = chokidar.watch(nuxt.options._layers.map(i => i.config.srcDir), {
...nuxt.options.watchers.chokidar, ...nuxt.options.watchers.chokidar,
cwd: nuxt.options.srcDir, cwd: nuxt.options.srcDir,
ignoreInitial: true, ignoreInitial: true,
@ -49,8 +49,7 @@ function watch (nuxt: Nuxt) {
] ]
}) })
const watchHook = debounce((event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir', path: string) => nuxt.callHook('builder:watch', event, normalize(path))) watcher.on('all', (event, path) => nuxt.callHook('builder:watch', event, normalize(path)))
watcher.on('all', watchHook)
nuxt.hook('close', () => watcher.close()) nuxt.hook('close', () => watcher.close())
return watcher return watcher
} }

View File

@ -33,11 +33,10 @@ export default defineNuxtModule({
nuxt.hook('builder:watch', async (event, path) => { nuxt.hook('builder:watch', async (event, path) => {
const dirs = [ const dirs = [
nuxt.options.dir.pages, nuxt.options.dir.pages,
nuxt.options.dir.layouts,
nuxt.options.dir.middleware nuxt.options.dir.middleware
].filter(Boolean) ].filter(Boolean)
const pathPattern = new RegExp(`^(${dirs.map(escapeRE).join('|')})/`) const pathPattern = new RegExp(`(^|\\/)(${dirs.map(escapeRE).join('|')})/`)
if (event !== 'change' && path.match(pathPattern)) { if (event !== 'change' && path.match(pathPattern)) {
await nuxt.callHook('builder:generateApp') await nuxt.callHook('builder:generateApp')
} }