forked from babel/babel
/
watcher.ts
166 lines (139 loc) 路 3.74 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { createRequire } from "module";
import path from "path";
import type { WatchOptions, FSWatcher } from "chokidar";
const fileToDeps = new Map<string, Set<string>>();
const depToFiles = new Map<string, Set<string>>();
let isWatchMode = false;
let watcher: FSWatcher;
const watchQueue = new Set<string>();
let hasStarted = false;
export function enable({ enableGlobbing }: { enableGlobbing: boolean }) {
isWatchMode = true;
const { FSWatcher } = requireChokidar();
const options: WatchOptions = {
disableGlobbing: !enableGlobbing,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 10,
},
};
watcher = new FSWatcher(options);
watcher.on("unlink", unwatchFile);
}
export function startWatcher() {
hasStarted = true;
for (const dep of watchQueue) {
watcher.add(dep);
}
watchQueue.clear();
watcher.on("ready", () => {
console.log("The watcher is ready.");
});
}
export function watch(filename: string): void {
if (!isWatchMode) {
throw new Error(
"Internal Babel error: .watch called when not in watch mode.",
);
}
if (!hasStarted) {
watchQueue.add(path.resolve(filename));
} else {
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.",
);
}
watcher.on("all", (event, filename) => {
if (event !== "change" && event !== "add") return;
const absoluteFile = path.resolve(filename);
callback(
[absoluteFile, ...(depToFiles.get(absoluteFile) ?? [])],
event,
absoluteFile,
);
});
}
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)),
);
const deps = fileToDeps.get(absFilename);
if (deps) {
for (const dep of deps) {
if (!absDependencies.has(dep)) {
removeFileDependency(absFilename, dep);
}
}
}
for (const dep of absDependencies) {
let deps = depToFiles.get(dep);
if (!deps) {
depToFiles.set(dep, (deps = new Set()));
if (!hasStarted) {
watchQueue.add(dep);
} else {
watcher.add(dep);
}
}
deps.add(absFilename);
}
fileToDeps.set(absFilename, absDependencies);
}
function removeFileDependency(filename: string, dep: string) {
const deps = depToFiles.get(dep) as Set<string>;
deps.delete(filename);
if (deps.size === 0) {
depToFiles.delete(dep);
if (!hasStarted) {
watchQueue.delete(dep);
} else {
watcher.unwatch(dep);
}
}
}
function unwatchFile(filename: string) {
const deps = fileToDeps.get(filename);
if (!deps) return;
for (const dep of deps) {
removeFileDependency(filename, dep);
}
fileToDeps.delete(filename);
}
function requireChokidar(): any {
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;
}
}