From 3c684af3c12202ec4ae93cc523bdacb2246c93bc Mon Sep 17 00:00:00 2001 From: markshawn2020 Date: Mon, 23 Dec 2024 05:43:00 +0800 Subject: [PATCH] fix: watch mode not working with external outDir When using tsup's watch mode with an outDir that points to a location outside the current directory (e.g., ../../dist), file changes are not detected and the rebuild is not triggered. This commit fixes the issue by: - Converting paths to absolute paths before comparison - Replacing glob-based ignore check with direct path comparison - Ensuring consistent path handling for both local and external directories --- src/index.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7b60cf8e..04be733b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -376,9 +376,12 @@ export async function build(_options: Options) { : [options.ignoreWatch] : [] + // Convert outDir to absolute path for reliable path comparison + const absoluteOutDir = path.resolve(process.cwd(), options.outDir) + const ignored = [ '**/{.git,node_modules}/**', - options.outDir, + absoluteOutDir, ...customIgnores, ] @@ -399,15 +402,22 @@ export async function build(_options: Options) { ) logger.info( 'CLI', - `Ignoring changes in ${ignored - .map((v) => `"${v}"`) - .join(' | ')}`, + `Ignoring paths: ${ignored.map((v) => `"${v}"`).join(' | ')}`, ) const watcher = watch(await glob(watchPaths), { ignoreInitial: true, ignorePermissionErrors: true, - ignored: (p) => globSync(p, { ignore: ignored }).length === 0, + ignored: (p: string) => { + const absolutePath = path.resolve(process.cwd(), p) + // Check if path should be ignored using absolute path comparison + return ignored.some(ignore => { + const absoluteIgnore = path.isAbsolute(ignore) + ? ignore + : path.resolve(process.cwd(), ignore) + return absolutePath.startsWith(absoluteIgnore) + }) + }, }) watcher.on('all', async (type, file) => { file = slash(file)