fix(nuxt): test watch paths against all layer srcDirs (#22307)

This commit is contained in:
Joaquín Sánchez 2023-07-30 20:47:29 +02:00 committed by GitHub
parent 9b09b4d112
commit 2df9a4b9db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import { join, normalize, resolve } from 'pathe' import { join, normalize, relative, resolve } from 'pathe'
import { createDebugger, createHooks } from 'hookable' import { createDebugger, createHooks } from 'hookable'
import type { LoadNuxtOptions } from '@nuxt/kit' import type { LoadNuxtOptions } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit' import { addBuildPlugin, addComponent, addPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
@ -349,12 +349,17 @@ async function initNuxt (nuxt: Nuxt) {
} }
// User provided patterns // User provided patterns
const layerRelativePaths = nuxt.options._layers.map(l => relative(l.config.srcDir || l.cwd, path))
for (const pattern of nuxt.options.watch) { for (const pattern of nuxt.options.watch) {
if (typeof pattern === 'string') { if (typeof pattern === 'string') {
if (pattern === path) { return nuxt.callHook('restart') } // Test (normalised) strings against absolute path and relative path to any layer `srcDir`
if (pattern === path || layerRelativePaths.includes(pattern)) { return nuxt.callHook('restart') }
continue continue
} }
if (pattern.test(path)) { return nuxt.callHook('restart') } // Test regular expressions against path to _any_ layer `srcDir`
if (layerRelativePaths.some(p => pattern.test(p))) {
return nuxt.callHook('restart')
}
} }
// Core Nuxt files: app.vue, error.vue and app.config.ts // Core Nuxt files: app.vue, error.vue and app.config.ts

View File

@ -365,8 +365,9 @@ export default defineUntypedSchema({
/** /**
* The watch property lets you define patterns that will restart the Nuxt dev server when changed. * The watch property lets you define patterns that will restart the Nuxt dev server when changed.
* *
* It is an array of strings or regular expressions, which will be matched against the file path * It is an array of strings or regular expressions. Strings should be either absolute paths or
* relative to the project `srcDir`. * relative to the `srcDir` (and the `srcDir` of any layers). Regular expressions will be matched
* against the path relative to the project `srcDir` (and the `srcDir` of any layers).
* *
* @type {Array<string | RegExp>} * @type {Array<string | RegExp>}
*/ */