Debian Codegraph @

packages/babel-generator/test/fixtures/sourcemaps/real-world-babel-file1/input.ts

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