From 2df9a4b9db44ae831562c7fc2f7753fe2764731f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Sun, 30 Jul 2023 20:47:29 +0200 Subject: [PATCH] fix(nuxt): test `watch` paths against all layer `srcDir`s (#22307) --- packages/nuxt/src/core/nuxt.ts | 11 ++++++++--- packages/schema/src/config/common.ts | 5 +++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 9aa9a65dfc..5654f56572 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -1,4 +1,4 @@ -import { join, normalize, resolve } from 'pathe' +import { join, normalize, relative, resolve } from 'pathe' import { createDebugger, createHooks } from 'hookable' import type { LoadNuxtOptions } 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 + const layerRelativePaths = nuxt.options._layers.map(l => relative(l.config.srcDir || l.cwd, path)) for (const pattern of nuxt.options.watch) { 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 } - 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 diff --git a/packages/schema/src/config/common.ts b/packages/schema/src/config/common.ts index d5801081f2..80df36c583 100644 --- a/packages/schema/src/config/common.ts +++ b/packages/schema/src/config/common.ts @@ -365,8 +365,9 @@ export default defineUntypedSchema({ /** * 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 - * relative to the project `srcDir`. + * It is an array of strings or regular expressions. Strings should be either absolute paths or + * 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} */