Skip to content

Commit

Permalink
fix: use edit event
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jul 9, 2022
1 parent 5694bb0 commit 8725763
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions packages/vite/src/node/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ export const isWSL2 = (() => {
* returns `true` when it works, `false` when it doesn't, `undefined` when it failed to detect
*/
export const detectWhetherChokidarWithDefaultOptionWorks = (
root: string,
timeout = 100
root: string
): Promise<{ result: boolean | undefined; warning: string | undefined }> =>
new Promise((resolve) => {
const editWait = 100
const detectWaitTimeout = 500

const id = ('' + performance.now()).replace(/\./g, '')
const tempFileShort = `.chokidardetector.${id}.txt`
const tempFile = path.resolve(root, tempFileShort)
Expand All @@ -42,24 +44,29 @@ export const detectWhetherChokidarWithDefaultOptionWorks = (
disableGlobbing: true,
ignoreInitial: true
})
w.on('add', () => {
// add works with WSL2, but edit does not work
w.on('edit', () => {
resolveWithCleanup(true)
})
w.on('error', () => {
resolveWithCleanup(undefined)
})
w.on('ready', () => {
fs.promises.writeFile(tempFile, 'detector', 'utf-8').then(
() => {
;(async () => {
try {
await fs.promises.writeFile(tempFile, 'detector', 'utf-8')
wroteFile = true
},
() => {
await new Promise((resolve) => setTimeout(resolve, editWait))
// edit file
await fs.promises.writeFile(tempFile, 'detector2', 'utf-8')
} catch {
resolveWithCleanup(undefined)
}
)
})()

timeoutId = setTimeout(() => {
resolveWithCleanup(false)
}, timeout)
}, detectWaitTimeout)
})

let resolved = false
Expand Down

0 comments on commit 8725763

Please sign in to comment.