-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
watcher.ts
147 lines (122 loc) 路 3.49 KB
/
watcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { createRequire } from "module";
import path from "path";
const fileToDeps = new Map<string, Set<string>>();
const depToFiles = new Map<string, Set<string>>();
let isWatchMode = false;
let watcher;
export function enable({ enableGlobbing }: { enableGlobbing: boolean }) {
isWatchMode = true;
const { FSWatcher } = requireChokidar();
watcher = new FSWatcher({
disableGlobbing: !enableGlobbing,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 10,
},
});
watcher.on("unlink", unwatchFile);
}
export function watch(filename: string): void {
if (!isWatchMode) {
throw new Error(
"Internal Babel error: .watch called when not in watch mode.",
);
}
watcher.add(path.resolve(filename));
}
/**
* Call @param callback whenever a dependency (source file)/
* external dependency (non-source file) changes.
*
* Handles mapping external dependencies to their corresponding
* dependencies.
*/
export function onFilesChange(
callback: (filenames: string[], event: string, cause: string) => void,
): void {
if (!isWatchMode) {
throw new Error(
"Internal Babel error: .onFilesChange called when not in watch mode.",
);
}
const listenerFactory = event => filename => {
const absoluteFile = path.resolve(filename);
callback(
[absoluteFile, ...(depToFiles.get(absoluteFile) ?? [])],
event,
absoluteFile,
);
};
watcher.on("add", listenerFactory("add"));
watcher.on("change", listenerFactory("change"));
}
/**
* Call @param callback when watcher is ready for changes.
*/
export function onReady(callback: () => void): void {
if (!isWatchMode) {
throw new Error(
"Internal Babel error: .onReady called when not in watch mode.",
);
}
watcher.on("ready", callback);
}
export function updateExternalDependencies(
filename: string,
dependencies: Set<string>,
) {
if (!isWatchMode) return;
// Use absolute paths
const absFilename = path.resolve(filename);
const absDependencies = new Set(
Array.from(dependencies, dep => path.resolve(dep)),
);
if (fileToDeps.has(absFilename)) {
for (const dep of fileToDeps.get(absFilename)) {
if (!absDependencies.has(dep)) {
removeFileDependency(absFilename, dep);
}
}
}
for (const dep of absDependencies) {
if (!depToFiles.has(dep)) {
depToFiles.set(dep, new Set());
watcher.add(dep);
}
depToFiles.get(dep).add(absFilename);
}
fileToDeps.set(absFilename, absDependencies);
}
function removeFileDependency(filename: string, dep: string) {
depToFiles.get(dep).delete(filename);
if (depToFiles.get(dep).size === 0) {
depToFiles.delete(dep);
watcher.unwatch(dep);
}
}
function unwatchFile(filename: string) {
if (!fileToDeps.has(filename)) return;
for (const dep of fileToDeps.get(filename)) {
removeFileDependency(filename, dep);
}
fileToDeps.delete(filename);
}
function requireChokidar(): any {
// @ts-expect-error - TS is not configured to support import.meta.
const require = createRequire(import.meta.url);
try {
return process.env.BABEL_8_BREAKING
? require("chokidar")
: parseInt(process.versions.node) >= 8
? require("chokidar")
: require("@nicolo-ribaudo/chokidar-2");
} catch (err) {
console.error(
"The optional dependency chokidar failed to install and is required for " +
"--watch. Chokidar is likely not supported on your platform.",
);
throw err;
}
}