tweak file watching (#112)

master
Brad Cornes 2020-05-03 14:45:36 +01:00
parent 9b136f448e
commit 2232f18d33
1 changed files with 13 additions and 7 deletions

View File

@ -114,27 +114,34 @@ export default async function getClassNames(
let watcher
function watch(files = []) {
if (watcher) watcher.close()
unwatch()
watcher = chokidar
.watch([CONFIG_GLOB, ...files], { cwd })
.watch(files, { cwd })
.on('change', handleChange)
.on('unlink', handleChange)
}
function unwatch() {
if (watcher) {
watcher.close()
}
}
async function handleChange() {
const prevDeps = result ? result.dependencies : []
const prevDeps = result ? [result.configPath, ...result.dependencies] : []
try {
result = await run()
} catch (error) {
if (error instanceof TailwindConfigError) {
onChange({ error })
} else {
unwatch()
onChange(null)
}
return
}
if (!arraysEqual(prevDeps, result.dependencies)) {
watch(result.dependencies)
const newDeps = [result.configPath, ...result.dependencies]
if (!arraysEqual(prevDeps, newDeps)) {
watch(newDeps)
}
onChange(result)
}
@ -143,11 +150,10 @@ export default async function getClassNames(
try {
result = await run()
} catch (_) {
watch()
return null
}
watch(result.dependencies)
watch([result.configPath, ...result.dependencies])
return result
}