Skip to content

Commit 3e483d4

Browse files
authored
fix: append star to improve ignore patterns (#23)
1 parent 82bb4e4 commit 3e483d4

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/watch/inclusive-node-watch-file-system.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { extname } from 'path';
1+
import path, { extname } from 'path';
22

33
import type { FSWatcher } from 'chokidar';
44
import chokidar from 'chokidar';
@@ -21,10 +21,17 @@ export function createIsIgnored(
2121
): (path: string) => boolean {
2222
const ignoredPatterns = ignored ? (Array.isArray(ignored) ? ignored : [ignored]) : [];
2323

24-
const filteredExcluded = excluded.filter((path) => {
24+
const filteredExcluded = excluded.filter((pattern) => {
2525
// Use `minimatch` to check if the path is a glob pattern.
26-
if (isGlob(path)) {
27-
ignoredPatterns.push(path);
26+
if (isGlob(pattern)) {
27+
// Append `/**` to the pattern to align minimatch's behavior
28+
// with the `exclude` option in tsconfig.json.
29+
const shouldAppendStar = pattern.includes('**') && !pattern.endsWith('*');
30+
if (shouldAppendStar) {
31+
ignoredPatterns.push(pattern.endsWith('/') ? `${pattern}**` : `${pattern}/**`);
32+
} else {
33+
ignoredPatterns.push(pattern);
34+
}
2835
return false;
2936
}
3037
return true;

test/unit/watch/inclusive-node-watch-file-system.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ describe('createIsIgnored', () => {
66
const distModulePath = '/path/to/dist/foo.js';
77
const srcModulePath = '/path/to/src/foo.ts';
88

9+
it('should handle built-in ignored directories', () => {
10+
const customPath = '/path/without/git/or/node_modules';
11+
const isIgnored = createIsIgnored([], []);
12+
expect(isIgnored(gitPath)).toBe(true);
13+
expect(isIgnored(customPath)).toBe(false);
14+
expect(isIgnored('/path/to/.github/foo')).toBe(false);
15+
});
16+
917
it('should allow to passing RegExp to the first argument', () => {
1018
const isIgnored = createIsIgnored([/[\\/](?:\.git|node_modules)[\\/]/], []);
1119
expect(isIgnored(gitPath)).toBe(true);
@@ -52,5 +60,34 @@ describe('createIsIgnored', () => {
5260
expect(isIgnored3(nodeModulesPath)).toBe(true);
5361
expect(isIgnored3(distModulePath)).toBe(true);
5462
expect(isIgnored3(srcModulePath)).toBe(false);
63+
64+
// exclude: ["**/dist"] in tsconfig.json
65+
const isIgnored4 = createIsIgnored([], ['/path/to/**/dist']);
66+
expect(isIgnored4(gitPath)).toBe(true);
67+
expect(isIgnored4(nodeModulesPath)).toBe(true);
68+
expect(isIgnored4(distModulePath)).toBe(true);
69+
expect(isIgnored4(srcModulePath)).toBe(false);
70+
71+
// exclude: ["**/dist/"] in tsconfig.json
72+
const isIgnored5 = createIsIgnored([], ['/path/to/**/dist/']);
73+
expect(isIgnored5(gitPath)).toBe(true);
74+
expect(isIgnored5(nodeModulesPath)).toBe(true);
75+
expect(isIgnored5(distModulePath)).toBe(true);
76+
expect(isIgnored5(srcModulePath)).toBe(false);
77+
78+
// exclude: ["dist/**/*"] in tsconfig.json
79+
const isIgnored6 = createIsIgnored([], ['/path/to/dist/**/*']);
80+
expect(isIgnored6(gitPath)).toBe(true);
81+
expect(isIgnored6(nodeModulesPath)).toBe(false);
82+
expect(isIgnored6(distModulePath)).toBe(true);
83+
expect(isIgnored6(srcModulePath)).toBe(false);
84+
});
85+
86+
it('should combine multiple ignore patterns', () => {
87+
const isIgnored = createIsIgnored([/[\\/]node_modules[\\/]/], ['/path/to/dist']);
88+
expect(isIgnored(gitPath)).toBe(true);
89+
expect(isIgnored(nodeModulesPath)).toBe(true);
90+
expect(isIgnored(distModulePath)).toBe(true);
91+
expect(isIgnored(srcModulePath)).toBe(false);
5592
});
5693
});

0 commit comments

Comments
 (0)