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