From 70cf288df80df9f63ef0a242463a9e4e393df55f Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Thu, 18 Mar 2021 17:46:14 +0800 Subject: [PATCH] fix: use chokidar to watch files again --- src/index.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8c42e96..92b133d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,7 @@ import { removeFiles, } from './utils' import glob from 'globby' -import { PrettyError } from './errors' +import { handleError, PrettyError } from './errors' import { postcssPlugin } from './esbuild/postcss' import { externalPlugin } from './esbuild/external' import { sveltePlugin } from './esbuild/svelte' @@ -182,7 +182,7 @@ export async function runEsbuild( minifyIdentifiers: options.minifyIdentifiers, minifySyntax: options.minifySyntax, keepNames: options.keepNames, - watch: options.watch, + incremental: options.watch, }) } catch (error) { console.error(`${makeLabel(format, 'error')} Build failed`) @@ -362,10 +362,27 @@ export async function build(_options: Options) { } } + const startWatcher = async () => { + if (!options.watch) return + + const { watch } = await import('chokidar') + const watcher = watch('.', { + ignoreInitial: true, + ignorePermissionErrors: true, + ignored: ['**/{.git,node_modules}/**', options.outDir], + }) + watcher.on('all', async (type, file) => { + console.log(makeLabel('CLI', 'info'), `Change detected: ${type} ${file}`) + await buildAll().catch(handleError) + }) + } + console.log(makeLabel('CLI', 'info'), `Target: ${options.target}`) await buildAll() + startWatcher() + if (options.dts) { const hasTypescript = resolveFrom.silent(process.cwd(), 'typescript') if (!hasTypescript) {