diff --git a/package-lock.json b/package-lock.json index fb919b20..1b05568c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "glob": "^10.3.10", "icss-replace-symbols": "^1.1.0", "is-there": "^4.4.2", + "minimatch": "^9.0.3", "mkdirp": "^3.0.0", "postcss": "^8.0.0", "postcss-modules-extract-imports": "^3.0.0", diff --git a/package.json b/package.json index 67c2950d..36249545 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "glob": "^10.3.10", "icss-replace-symbols": "^1.1.0", "is-there": "^4.4.2", + "minimatch": "^9.0.3", "mkdirp": "^3.0.0", "postcss": "^8.0.0", "postcss-modules-extract-imports": "^3.0.0", diff --git a/src/run.ts b/src/run.ts index 05bd19ea..bb03c5d2 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,6 +1,8 @@ import chalk from 'chalk'; import chokidar from 'chokidar'; import { glob } from 'glob'; +import { minimatch } from 'minimatch'; + import { DtsCreator } from './dts-creator'; import { DtsContent } from './dts-content'; @@ -41,6 +43,21 @@ export async function run(searchDir: string, options: RunOptions = {}): Promise< const writeFile = async (f: string): Promise => { try { + // If we're watching recheck the file against the pattern since + // chokidar does not filter files inside symlinks and we don't + // know (without checking every parent) if the file is inside a + // symlink. + // + // Chokidar issue: + // + // https://github.com/paulmillr/chokidar/issues/967 + // + // When that's fixed this can be removed (from deleteFile too), + // but the issue is 2 years old already (reported 2020). + if (!!options.watch && !minimatch(f, filesPattern)) { + return; + } + const content: DtsContent = await creator.create(f, undefined, !!options.watch); await content.writeFile(); @@ -54,6 +71,11 @@ export async function run(searchDir: string, options: RunOptions = {}): Promise< const deleteFile = async (f: string): Promise => { try { + // Recheck patterh, see writeFile for explanation. + if (!!options.watch && !minimatch(f, filesPattern)) { + return; + } + const content: DtsContent = await creator.create(f, undefined, !!options.watch, true); await content.deleteFile();