Skip to content

Commit 2f330b5

Browse files
authored
fix: use external store for files change to not rely on wfs (#546)
Some plugins can wrap WFS which makes storing changed and removed files in WFS unreliable
1 parent 8978049 commit 2f330b5

File tree

7 files changed

+28
-73
lines changed

7 files changed

+28
-73
lines changed

src/hooks/getChangedFiles.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/hooks/getDeletedFiles.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/hooks/getWatcher.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/hooks/tapAfterEnvironmentToPatchWatching.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function tapAfterEnvironmentToPatchWatching(
1313
// wrap original watch file system
1414
(compiler as CompilerWithWatchFileSystem).watchFileSystem = new InclusiveNodeWatchFileSystem(
1515
watchFileSystem,
16+
compiler,
1617
state
1718
);
1819
}

src/hooks/tapStartToConnectAndRunReporter.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import webpack from 'webpack';
22
import { ForkTsCheckerWebpackPluginConfiguration } from '../ForkTsCheckerWebpackPluginConfiguration';
33
import { ForkTsCheckerWebpackPluginState } from '../ForkTsCheckerWebpackPluginState';
44
import { getForkTsCheckerWebpackPluginHooks } from './pluginHooks';
5-
import { getDeletedFiles } from './getDeletedFiles';
6-
import { Dependencies, FilesChange, ReporterRpcClient } from '../reporter';
7-
import { getChangedFiles } from './getChangedFiles';
5+
import { Dependencies, FilesChange, getFilesChange, ReporterRpcClient } from '../reporter';
86
import { OperationCanceledError } from '../error/OperationCanceledError';
97
import { tapDoneToAsyncGetIssues } from './tapDoneToAsyncGetIssues';
108
import { tapAfterCompileToGetIssues } from './tapAfterCompileToGetIssues';
@@ -51,10 +49,7 @@ function tapStartToConnectAndRunReporter(
5149
let change: FilesChange = {};
5250

5351
if (state.watching) {
54-
change = {
55-
changedFiles: getChangedFiles(compilation.compiler),
56-
deletedFiles: getDeletedFiles(compilation.compiler),
57-
};
52+
change = getFilesChange(compiler);
5853

5954
configuration.logger.infrastructure.info(
6055
[

src/reporter/FilesChange.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
import subtract from '../utils/array/substract';
22
import unique from '../utils/array/unique';
3+
import { Compiler } from 'webpack';
34

45
interface FilesChange {
56
changedFiles?: string[];
67
deletedFiles?: string[];
78
}
89

10+
const compilerFilesChangeMap = new WeakMap<Compiler, FilesChange>();
11+
12+
function getFilesChange(compiler: Compiler): FilesChange {
13+
return compilerFilesChangeMap.get(compiler) || {};
14+
}
15+
16+
function updateFilesChange(compiler: Compiler, change: FilesChange): void {
17+
compilerFilesChangeMap.set(compiler, aggregateFilesChanges([getFilesChange(compiler), change]));
18+
}
19+
20+
function clearFilesChange(compiler: Compiler): void {
21+
compilerFilesChangeMap.delete(compiler);
22+
}
23+
924
/**
1025
* Computes aggregated files change based on the subsequent files changes.
1126
*
@@ -31,4 +46,4 @@ function aggregateFilesChanges(changes: FilesChange[]): FilesChange {
3146
};
3247
}
3348

34-
export { FilesChange, aggregateFilesChanges };
49+
export { FilesChange, getFilesChange, updateFilesChange, clearFilesChange, aggregateFilesChanges };

src/watch/InclusiveNodeWatchFileSystem.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { ForkTsCheckerWebpackPluginState } from '../ForkTsCheckerWebpackPluginSt
22
import chokidar, { FSWatcher } from 'chokidar';
33
import { extname } from 'path';
44
import { Watcher, WatchFileSystem, WatchFileSystemOptions } from './WatchFileSystem';
5+
import { Compiler } from 'webpack';
6+
import { clearFilesChange, updateFilesChange } from '../reporter';
57

68
const IGNORED_DIRS = ['node_modules', '.git', '.yarn', '.pnp'];
79

@@ -13,17 +15,13 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
1315
get watcher() {
1416
return this.watchFileSystem.watcher || this.watchFileSystem.wfs?.watcher;
1517
}
16-
17-
readonly changedFiles: Set<string>;
18-
readonly removedFiles: Set<string>;
1918
readonly dirsWatchers: Map<string, FSWatcher | undefined>;
2019

2120
constructor(
2221
private watchFileSystem: WatchFileSystem,
22+
private compiler: Compiler,
2323
private pluginState: ForkTsCheckerWebpackPluginState
2424
) {
25-
this.changedFiles = new Set();
26-
this.removedFiles = new Set();
2725
this.dirsWatchers = new Map();
2826
}
2927

@@ -38,8 +36,7 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
3836
callback?: Function,
3937
callbackUndelayed?: Function
4038
): Watcher {
41-
this.changedFiles.clear();
42-
this.removedFiles.clear();
39+
clearFilesChange(this.compiler);
4340

4441
// use standard watch file system for files and missing
4542
const standardWatcher = this.watchFileSystem.watch(
@@ -54,14 +51,12 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
5451

5552
this.watcher?.on('change', (file: string) => {
5653
if (typeof file === 'string' && !isIgnored(file)) {
57-
this.changedFiles.add(file);
58-
this.removedFiles.delete(file);
54+
updateFilesChange(this.compiler, { changedFiles: [file] });
5955
}
6056
});
6157
this.watcher?.on('remove', (file: string) => {
6258
if (typeof file === 'string' && !isIgnored(file)) {
63-
this.removedFiles.add(file);
64-
this.changedFiles.delete(file);
59+
updateFilesChange(this.compiler, { deletedFiles: [file] });
6560
}
6661
});
6762

@@ -105,8 +100,7 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
105100
return;
106101
}
107102

108-
this.changedFiles.add(file);
109-
this.removedFiles.delete(file);
103+
updateFilesChange(this.compiler, { changedFiles: [file] });
110104

111105
const mtime = stats?.mtimeMs || stats?.ctimeMs || 1;
112106

@@ -124,8 +118,7 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
124118
return;
125119
}
126120

127-
this.removedFiles.add(file);
128-
this.changedFiles.delete(file);
121+
updateFilesChange(this.compiler, { deletedFiles: [file] });
129122

130123
this.watcher?._onRemove(dirToWatch, file, 'rename');
131124
});
@@ -137,8 +130,7 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
137130
return {
138131
...standardWatcher,
139132
close: () => {
140-
this.changedFiles.clear();
141-
this.removedFiles.clear();
133+
clearFilesChange(this.compiler);
142134

143135
if (standardWatcher) {
144136
standardWatcher.close();

0 commit comments

Comments
 (0)