Repository: nuxt/nuxt
File: packages/nuxt/src/core/builder.ts (TypeScript)

Code before:
  watcher.on('all', (event, path) => {
    if (event === 'all' || event === 'ready' || event === 'error' || event === 'raw') {
      return
    }
    nuxt.callHook('builder:watch', event, normalize(path))
  })
  nuxt.hook('close', () => watcher?.close())
}

function createGranularWatcher () {
  const nuxt = useNuxt()
  const isIgnored = createIsIgnored(nuxt)

  if (nuxt.options.debug && nuxt.options.debug.watchers) {
    // eslint-disable-next-line no-console
    console.time('[nuxt] builder:chokidar:watch')
  }

  let pending = 0

  const ignoredDirs = new Set([...nuxt.options.modulesDir, nuxt.options.buildDir])
  const pathsToWatch = new Set(nuxt.options._layers.map(layer => layer.config.srcDir || layer.cwd).filter(d => d && !isIgnored(d)))
  for (const pattern of nuxt.options.watch) {
    if (typeof pattern !== 'string') { continue }
    const path = resolve(nuxt.options.srcDir, pattern)
    if ([...pathsToWatch].some(w => path.startsWith(w.replace(/[^/]$/, '$&/')))) { continue }
    pathsToWatch.add(path)
  }
  for (const dir of pathsToWatch) {
    pending++
    const watcher = chokidarWatch(dir, { ...nuxt.options.watchers.chokidar, ignoreInitial: false, depth: 0, ignored: [isIgnored, /[\\/]node_modules[\\/]/] })
    const watchers: Record<string, FSWatcher> = {}

    watcher.on('all', (event, path) => {
      if (event === 'all' || event === 'ready' || event === 'error' || event === 'raw') {
        return
      }
      path = normalize(path)
      if (!pending) {
        nuxt.callHook('builder:watch', event, path)
      }
      if (event === 'unlinkDir' && path in watchers) {
        watchers[path]?.close()
        delete watchers[path]
      }
      if (event === 'addDir' && path !== dir && !ignoredDirs.has(path) && !pathsToWatch.has(path) && !(path in watchers) && !isIgnored(path)) {
        const pathWatcher = watchers[path] = chokidarWatch(path, { ...nuxt.options.watchers.chokidar, ignored: [isIgnored] })
        pathWatcher.on('all', (event, p) => {
          if (event === 'all' || event === 'ready' || event === 'error' || event === 'raw') {
            return
          }

Review comment (performance):
Spreading `pathsToWatch` into an array for `.some()` creates an intermediate array on each iteration; consider iterating over the `Set` directly or using a helper function to avoid unnecessary allocations.

Code after:
  watcher.on('all', (event, path) => {
    if (event === 'all' || event === 'ready' || event === 'error' || event === 'raw') {
      return
    }
    nuxt.callHook('builder:watch', event, normalize(path))
  })
  nuxt.hook('close', () => watcher?.close())
}

function createGranularWatcher () {
  const nuxt = useNuxt()
  const isIgnored = createIsIgnored(nuxt)

  if (nuxt.options.debug && nuxt.options.debug.watchers) {
    // eslint-disable-next-line no-console
    console.time('[nuxt] builder:chokidar:watch')
  }

  let pending = 0

  const ignoredDirs = new Set([...nuxt.options.modulesDir, nuxt.options.buildDir])
  const pathsToWatch = new Set(nuxt.options._layers.map(layer => layer.config.srcDir || layer.cwd).filter(d => d && !isIgnored(d)))
  for (const pattern of nuxt.options.watch) {
    if (typeof pattern !== 'string') { continue }
    const path = resolve(nuxt.options.srcDir, pattern)
    let shouldAdd = true
    for (const w of pathsToWatch) {
      if (path.startsWith(w.replace(/[^/]$/, '$&/'))) {
        shouldAdd = false
        break
      }
    }
    if (shouldAdd) {
      pathsToWatch.add(path)
    }
  }
  for (const dir of pathsToWatch) {
    pending++
    const watcher = chokidarWatch(dir, { ...nuxt.options.watchers.chokidar, ignoreInitial: false, depth: 0, ignored: [isIgnored, /[\\/]node_modules[\\/]/] })
    const watchers: Record<string, FSWatcher> = {}

    watcher.on('all', (event, path) => {
      if (event === 'all' || event === 'ready' || event === 'error' || event === 'raw') {
        return
      }
      path = normalize(path)
      if (!pending) {
        nuxt.callHook('builder:watch', event, path)
      }
      if (event === 'unlinkDir' && path in watchers) {
        watchers[path]?.close()