Skip to content

Commit baa3a37

Browse files
authored
fix: take into account watchOptions.ignored webpack setting (#586)
Our custom implementation of WatchFileSystem should take into account watchOptions.ignored setting ✅ Closes: #583
1 parent dca599a commit baa3a37

5 files changed

+11349
-5
lines changed

src/watch/InclusiveNodeWatchFileSystem.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,26 @@ import { extname } from 'path';
44
import { Watcher, WatchFileSystem, WatchFileSystemOptions } from './WatchFileSystem';
55
import { Compiler } from 'webpack';
66
import { clearFilesChange, updateFilesChange } from '../reporter';
7-
8-
const IGNORED_DIRS = ['node_modules', '.git', '.yarn', '.pnp'];
9-
10-
function isIgnored(path: string) {
11-
return IGNORED_DIRS.some((ignoredDir) => path.includes(`/${ignoredDir}/`));
7+
import minimatch from 'minimatch';
8+
9+
const BUILTIN_IGNORED_DIRS = ['node_modules', '.git', '.yarn', '.pnp'];
10+
11+
function createIsIgnored(
12+
ignored: WatchFileSystemOptions['ignored'] | undefined
13+
): (path: string) => boolean {
14+
const ignoredPatterns = ignored ? (Array.isArray(ignored) ? ignored : [ignored]) : [];
15+
const ignoredFunctions = ignoredPatterns.map((pattern) =>
16+
pattern instanceof RegExp
17+
? (path: string) => pattern.test(path)
18+
: (path: string) => minimatch(path, pattern)
19+
);
20+
ignoredFunctions.push((path: string) =>
21+
BUILTIN_IGNORED_DIRS.some((ignoredDir) => path.includes(`/${ignoredDir}/`))
22+
);
23+
24+
return function isIgnored(path: string) {
25+
return ignoredFunctions.some((ignoredFunction) => ignoredFunction(path));
26+
};
1227
}
1328

1429
class InclusiveNodeWatchFileSystem implements WatchFileSystem {
@@ -37,6 +52,7 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
3752
callbackUndelayed?: Function
3853
): Watcher {
3954
clearFilesChange(this.compiler);
55+
const isIgnored = createIsIgnored(options?.ignored);
4056

4157
// use standard watch file system for files and missing
4258
const standardWatcher = this.watchFileSystem.watch(

test/e2e/TypeScriptWatchApi.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,51 @@ describe('TypeScript Watch API', () => {
409409
].join('\n'),
410410
]);
411411
});
412+
413+
it.each([
414+
{ webpack: '4.0.0', async: false, ignored: '[path.resolve(__dirname, "src/model/**")]' },
415+
{ webpack: '^4.0.0', async: true, ignored: '"**/src/model/**"' },
416+
{ webpack: '^5.0.0', async: false, ignored: '/src\\/model/' },
417+
])('ignores directories from watch with %p', async ({ webpack, async, ignored }) => {
418+
await sandbox.load([
419+
await readFixture(join(__dirname, 'fixtures/environment/typescript-basic.fixture'), {
420+
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION: JSON.stringify(
421+
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION
422+
),
423+
TS_LOADER_VERSION: JSON.stringify('^5.0.0'),
424+
TYPESCRIPT_VERSION: JSON.stringify('~3.8.0'),
425+
WEBPACK_VERSION: JSON.stringify(webpack),
426+
WEBPACK_CLI_VERSION: JSON.stringify(WEBPACK_CLI_VERSION),
427+
WEBPACK_DEV_SERVER_VERSION: JSON.stringify(WEBPACK_DEV_SERVER_VERSION),
428+
ASYNC: JSON.stringify(async),
429+
}),
430+
await readFixture(join(__dirname, 'fixtures/implementation/typescript-basic.fixture')),
431+
]);
432+
433+
await sandbox.patch(
434+
'webpack.config.js',
435+
" entry: './src/index.ts',",
436+
[" entry: './src/index.ts',", ' watchOptions: {', ` ignored: ${ignored}`, ' },'].join(
437+
'\n'
438+
)
439+
);
440+
441+
const driver = createWebpackDevServerDriver(sandbox.spawn('npm run webpack-dev-server'), async);
442+
443+
// first compilation is successful
444+
await driver.waitForNoErrors();
445+
446+
// then we introduce semantic error by removing "firstName" and "lastName" from the User model
447+
await sandbox.patch(
448+
'src/model/User.ts',
449+
[' firstName?: string;', ' lastName?: string;'].join('\n'),
450+
''
451+
);
452+
// then we add a new file in this directory
453+
await sandbox.write('src/model/Group.ts', '// TODO: to implement');
454+
455+
// there should be no re-build
456+
await expect(driver.waitForErrors(3000)).rejects.toBeDefined();
457+
await expect(driver.waitForNoErrors(3000)).rejects.toBeDefined();
458+
});
412459
});

0 commit comments

Comments
 (0)