Skip to content

Commit

Permalink
fix: don't call fs.watch if watch is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Oct 30, 2024
1 parent 67e3e95 commit 71a7055
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
37 changes: 17 additions & 20 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import type { Logger } from '../logger'
import { printServerUrls } from '../logger'
import { warnFutureDeprecation } from '../deprecations'
import {
createNoopWatcher,
getResolvedOutDirs,
resolveChokidarOptions,
resolveEmptyOutDir,
Expand Down Expand Up @@ -467,26 +468,22 @@ export async function _createServer(
setClientErrorHandler(httpServer, config.logger)
}

const watcher = chokidar.watch(
// config file dependencies and env file might be outside of root
[
root,
...config.configFileDependencies,
...getEnvFilesForMode(config.mode, config.envDir),
// Watch the public directory explicitly because it might be outside
// of the root directory.
...(publicDir && publicFiles ? [publicDir] : []),
],
resolvedWatchOptions,
)
// If watch is turned off, patch `.add()` as a noop to prevent programmatically
// watching additional files and to keep it fast.
// eslint-disable-next-line eqeqeq -- null means disabled
if (serverConfig.watch === null) {
watcher.add = function () {
return this
}
}
const watcher =
// eslint-disable-next-line eqeqeq -- null means disabled
serverConfig.watch === null
? createNoopWatcher()
: chokidar.watch(
// config file dependencies and env file might be outside of root
[
root,
...config.configFileDependencies,
...getEnvFilesForMode(config.mode, config.envDir),
// Watch the public directory explicitly because it might be outside
// of the root directory.
...(publicDir && publicFiles ? [publicDir] : []),
],
resolvedWatchOptions,
)

const environments: Record<string, DevEnvironment> = {}

Expand Down
43 changes: 42 additions & 1 deletion packages/vite/src/node/watch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import path from 'node:path'
import type { WatchOptions } from 'dep-types/chokidar'
import EventEmitter from 'node:events'
import type {
FSWInstanceOptions,
FSWatcher,
WatchOptions,
} from 'dep-types/chokidar'
import picomatch from 'picomatch'
import type { OutputOptions } from 'rollup'
import colors from 'picocolors'
Expand Down Expand Up @@ -95,3 +100,39 @@ export function resolveChokidarOptions(

return resolvedWatchOptions
}

class NoopWatcher extends EventEmitter implements FSWatcher {
closed = true
options: FSWInstanceOptions = {
persistent: false,
ignoreInitial: false,
followSymlinks: false,
usePolling: false,
interval: 0,
binaryInterval: 0,
ignorePermissionErrors: false,
atomic: false,
ignored: [],
awaitWriteFinish: false,
}

add() {
return this
}
unwatch() {
return this
}
async close() {
// noop
}
getWatched(): Record<string, string[]> {
return {}
}
emitWithAll() {
// noop
}
}

export function createNoopWatcher(): NoopWatcher {
return new NoopWatcher()
}

0 comments on commit 71a7055

Please sign in to comment.