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

Code before:
async function bundle (nuxt: Nuxt) {
  try {
    const { bundle } = typeof nuxt.options.builder === 'string'
      ? await loadBuilder(nuxt, nuxt.options.builder)
      : nuxt.options.builder

    await bundle(nuxt)
  } catch (error: any) {
    await nuxt.callHook('build:error', error)

    if (error.toString().includes('Cannot find module \'@nuxt/webpack-builder\'')) {
      throw new Error('Could not load `@nuxt/webpack-builder`. You may need to add it to your project dependencies, following the steps in `https://github.com/nuxt/framework/pull/2812`.')
    }

    throw error
  }
}

async function loadBuilder (nuxt: Nuxt, builder: string): Promise<NuxtBuilder> {
  try {
    return await importModule(builder, { url: [directoryToURL(nuxt.options.rootDir), new URL(import.meta.url)] })
  } catch {
    throw new Error(`Loading \`${builder}\` builder failed. You can read more about the nuxt \`builder\` option at: \`https://nuxt.com/docs/api/nuxt-config#builder\``)
  }
}

function resolveDirectoriesToWatch (nuxt: Nuxt) {
  const pathsToWatch = new Set<string>()
  for (const layer of nuxt.options._layers) {
    const dir = layer.config.srcDir || layer.cwd
    if (!dir || isIgnored(dir)) { continue }

    pathsToWatch.add(dir.replace(/[^/]$/, '$&/'))
  }
  for (const pattern of nuxt.options.watch) {
    if (typeof pattern !== 'string') { continue }
    const path = dirname(resolve(nuxt.options.srcDir, pattern)) + '/'
    let shouldAdd = true
    for (const w of pathsToWatch) {
      if (w.startsWith(path)) {
        pathsToWatch.delete(w)
      }
      if (path.startsWith(w)) {
        shouldAdd = false
      }
    }
    if (shouldAdd) {
      pathsToWatch.add(path)
    }
  }
  return pathsToWatch

Review comment (refactor):
The new helper `resolveDirectoriesToWatch` lacks a JSDoc comment explaining its purpose, parameters, and return value. Adding a brief docblock will improve readability and maintainability.
```suggestion

/**
 * Resolves the set of directories to watch for changes based on the Nuxt configuration.
 * 
 * @param {Nuxt} nuxt - The Nuxt instance containing configuration and options.
 * @returns {Set<string>} A set of normalized directory paths to watch.
 */
```

Code after:
async function bundle (nuxt: Nuxt) {
  try {
    const { bundle } = typeof nuxt.options.builder === 'string'
      ? await loadBuilder(nuxt, nuxt.options.builder)
      : nuxt.options.builder

    await bundle(nuxt)
  } catch (error: any) {
    await nuxt.callHook('build:error', error)

    if (error.toString().includes('Cannot find module \'@nuxt/webpack-builder\'')) {
      throw new Error('Could not load `@nuxt/webpack-builder`. You may need to add it to your project dependencies, following the steps in `https://github.com/nuxt/framework/pull/2812`.')
    }

    throw error
  }
}

async function loadBuilder (nuxt: Nuxt, builder: string): Promise<NuxtBuilder> {
  try {
    return await importModule(builder, { url: [directoryToURL(nuxt.options.rootDir), new URL(import.meta.url)] })
  } catch {
    throw new Error(`Loading \`${builder}\` builder failed. You can read more about the nuxt \`builder\` option at: \`https://nuxt.com/docs/api/nuxt-config#builder\``)
  }
}

function resolvePathsToWatch (nuxt: Nuxt, opts: { parentDirectories?: boolean } = {}): Set<string> {
  const pathsToWatch = new Set<string>()
  for (const layer of nuxt.options._layers) {
    const dir = layer.config.srcDir || layer.cwd
    if (!dir || isIgnored(dir)) { continue }

    pathsToWatch.add(dir.replace(/[^/]$/, '$&/'))
  }
  for (const pattern of nuxt.options.watch) {
    if (typeof pattern !== 'string') { continue }
    const path = opts?.parentDirectories
      ? join(dirname(resolve(nuxt.options.srcDir, pattern)), '')
      : resolve(nuxt.options.srcDir, pattern)
    let shouldAdd = true
    for (const w of [...pathsToWatch]) {
      if (w.startsWith(path)) {
        pathsToWatch.delete(w)
      }
      if (path.startsWith(w)) {
        shouldAdd = false
      }
    }
    if (shouldAdd) {
      pathsToWatch.add(path)
    }