Skip to content

Commit 13c276f

Browse files
authored
fix: handle function in watchOptions.ignored (#600)
1 parent 5b7c333 commit 13c276f

File tree

4 files changed

+4053
-11
lines changed

4 files changed

+4053
-11
lines changed

src/watch/InclusiveNodeWatchFileSystem.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ function createIsIgnored(
1212
ignored: WatchFileSystemOptions['ignored'] | undefined
1313
): (path: string) => boolean {
1414
const ignoredPatterns = ignored ? (Array.isArray(ignored) ? ignored : [ignored]) : [];
15-
const ignoredFunctions = ignoredPatterns
16-
// sanitize patterns - see https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/issues/594
17-
.filter((pattern) => typeof pattern === 'string' || pattern instanceof RegExp)
18-
.map((pattern) =>
19-
pattern instanceof RegExp
20-
? (path: string) => pattern.test(path)
21-
: (path: string) => minimatch(path, pattern)
22-
);
15+
const ignoredFunctions = ignoredPatterns.map((pattern) => {
16+
// ensure patterns are valid - see https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/issues/594
17+
if (typeof pattern === 'string') {
18+
return (path: string) => minimatch(path, pattern);
19+
} else if (typeof pattern === 'function') {
20+
return pattern;
21+
} else if (pattern instanceof RegExp) {
22+
return (path: string) => pattern.test(path);
23+
} else {
24+
// fallback to no-ignore function
25+
return () => false;
26+
}
27+
});
2328
ignoredFunctions.push((path: string) =>
2429
BUILTIN_IGNORED_DIRS.some((ignoredDir) => path.includes(`/${ignoredDir}/`))
2530
);

src/watch/WatchFileSystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ interface WatchFileSystemOptions {
44
aggregateTimeout: number;
55
poll: boolean;
66
followSymlinks: boolean;
7-
ignored: string | RegExp | (string | RegExp)[];
7+
ignored: string | RegExp | Function | (string | RegExp | Function)[];
88
}
99

1010
// watchpack v1 and v2 internal interface

test/e2e/TypeScriptWatchApi.spec.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,12 @@ describe('TypeScript Watch API', () => {
414414
{ webpack: '4.0.0', async: false, ignored: '[path.resolve(__dirname, "src/model/**")]' },
415415
{ webpack: '^4.0.0', async: true, ignored: '"**/src/model/**"' },
416416
{ webpack: '^5.0.0', async: false, ignored: '/src\\/model/' },
417+
{
418+
webpack: '^4.0.0',
419+
async: true,
420+
ignored:
421+
'(file) => forwardSlash(file).includes(forwardSlash(path.resolve(__dirname, "src/model/")))',
422+
},
417423
])('ignores directories from watch with %p', async ({ webpack, async, ignored }) => {
418424
await sandbox.load([
419425
await readFixture(join(__dirname, 'fixtures/environment/typescript-basic.fixture'), {
@@ -430,6 +436,16 @@ describe('TypeScript Watch API', () => {
430436
await readFixture(join(__dirname, 'fixtures/implementation/typescript-basic.fixture')),
431437
]);
432438

439+
await sandbox.patch(
440+
'webpack.config.js',
441+
'module.exports = {',
442+
[
443+
'function forwardSlash(input) {',
444+
" return path.normalize(input).replace(/\\\\+/g, '/');",
445+
'}',
446+
'module.exports = {',
447+
].join('\n')
448+
);
433449
await sandbox.patch(
434450
'webpack.config.js',
435451
" entry: './src/index.ts',",
@@ -453,7 +469,11 @@ describe('TypeScript Watch API', () => {
453469
await sandbox.write('src/model/Group.ts', '// TODO: to implement');
454470

455471
// there should be no re-build
456-
await expect(driver.waitForErrors(3000)).rejects.toBeDefined();
457-
await expect(driver.waitForNoErrors(3000)).rejects.toBeDefined();
472+
await expect(driver.waitForNoErrors(3000)).rejects.toEqual(
473+
new Error('Exceeded time on waiting for no errors message to appear.')
474+
);
475+
await expect(driver.waitForErrors(3000)).rejects.toEqual(
476+
new Error('Exceeded time on waiting for errors to appear.')
477+
);
458478
});
459479
});

0 commit comments

Comments
 (0)