From b7f245c66b5ccdd0c372eabeb2776595e8ddc7ad Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 14 Jun 2025 15:16:37 +0200 Subject: [PATCH] perf(dev): use native `fs.watch` to watch `dist`, config and `.env` --- knip.json | 1 - packages/nuxi/package.json | 1 - packages/nuxi/src/dev/utils.ts | 32 ++++++++++++-------------------- packages/nuxt-cli/package.json | 1 - pnpm-lock.yaml | 6 ------ 5 files changed, 12 insertions(+), 29 deletions(-) diff --git a/knip.json b/knip.json index 6d45d0a9d..3a30755b9 100644 --- a/knip.json +++ b/knip.json @@ -18,7 +18,6 @@ "packages/nuxt-cli": { "ignoreDependencies": [ "c12", - "chokidar", "clipboardy", "confbox", "consola", diff --git a/packages/nuxi/package.json b/packages/nuxi/package.json index 4dc5db84f..bd4723dc7 100644 --- a/packages/nuxi/package.json +++ b/packages/nuxi/package.json @@ -45,7 +45,6 @@ "@types/node": "^22.15.31", "@types/semver": "^7.7.0", "c12": "^3.0.4", - "chokidar": "^4.0.3", "citty": "^0.1.6", "clipboardy": "^4.0.0", "confbox": "^0.2.2", diff --git a/packages/nuxi/src/dev/utils.ts b/packages/nuxi/src/dev/utils.ts index ada92bba1..fbbf8cee7 100644 --- a/packages/nuxi/src/dev/utils.ts +++ b/packages/nuxi/src/dev/utils.ts @@ -1,19 +1,19 @@ import type { Nuxt, NuxtConfig } from '@nuxt/schema' import type { DotenvOptions } from 'c12' -import type { FSWatcher } from 'chokidar' import type { HTTPSOptions, Listener, ListenOptions, ListenURL } from 'listhen' import type { NitroDevServer } from 'nitropack' +import type { FSWatcher } from 'node:fs' import type { IncomingMessage, RequestListener, ServerResponse } from 'node:http' import type { AddressInfo } from 'node:net' import EventEmitter from 'node:events' +import { watch } from 'node:fs' import process from 'node:process' -import chokidar from 'chokidar' import defu from 'defu' import { toNodeListener } from 'h3' import { listen } from 'listhen' -import { join, relative, resolve } from 'pathe' +import { resolve } from 'pathe' import { debounce } from 'perfect-debounce' import { provider } from 'std-env' import { joinURL } from 'ufo' @@ -108,6 +108,7 @@ interface DevServerEventMap { export class NuxtDevServer extends EventEmitter { private _handler?: RequestListener private _distWatcher?: FSWatcher + private _configWatcher?: FSWatcher private _currentNuxt?: NuxtWithServer private _loadingMessage?: string private _loadingError?: Error @@ -198,12 +199,11 @@ export class NuxtDevServer extends EventEmitter { } async close() { + this._distWatcher?.close() + this._configWatcher?.close() if (this._currentNuxt) { await this._currentNuxt.close() } - if (this._distWatcher) { - await this._distWatcher.close() - } } async _load(reload?: boolean, reason?: string) { @@ -334,11 +334,8 @@ export class NuxtDevServer extends EventEmitter { } // Watch dist directory - this._distWatcher = chokidar.watch(resolve(this._currentNuxt.options.buildDir, 'dist'), { - ignoreInitial: true, - depth: 0, - }) - this._distWatcher.on('unlinkDir', () => { + this._distWatcher = watch(resolve(this._currentNuxt.options.buildDir, 'dist')) + this._distWatcher.on('change', () => { this.loadDebounced(true, '.nuxt/dist directory has been removed') }) @@ -347,18 +344,13 @@ export class NuxtDevServer extends EventEmitter { } async _watchConfig() { - const configWatcher = chokidar.watch([this.options.cwd, join(this.options.cwd, '.config')], { - ignoreInitial: true, - depth: 0, - }) - configWatcher.on('all', (event, _file) => { - if (event === 'all' || event === 'ready' || event === 'error' || event === 'raw') { - return - } - const file = relative(this.options.cwd, _file) + this._configWatcher = watch(this.options.cwd, { recursive: true }) + + this._configWatcher.on('change', (_event, file: string) => { if (file === (this.options.dotenv.fileName || '.env')) { this.emit('restart') } + if (RESTART_RE.test(file)) { this.loadDebounced(true, `${file} updated`) } diff --git a/packages/nuxt-cli/package.json b/packages/nuxt-cli/package.json index 06e35983a..098c4d740 100644 --- a/packages/nuxt-cli/package.json +++ b/packages/nuxt-cli/package.json @@ -34,7 +34,6 @@ }, "dependencies": { "c12": "^3.0.4", - "chokidar": "^4.0.3", "citty": "^0.1.6", "clipboardy": "^4.0.0", "confbox": "^0.2.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 187f4e52a..cdd81f7f4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -118,9 +118,6 @@ importers: c12: specifier: ^3.0.4 version: 3.0.4(magicast@0.3.5) - chokidar: - specifier: ^4.0.3 - version: 4.0.3 citty: specifier: ^0.1.6 version: 0.1.6 @@ -223,9 +220,6 @@ importers: c12: specifier: ^3.0.4 version: 3.0.4(magicast@0.3.5) - chokidar: - specifier: ^4.0.3 - version: 4.0.3 citty: specifier: ^0.1.6 version: 0.1.6