| 1 |
|
| 2 |
|
| 3 |
import { createRequire } from "module"; |
| 4 |
import path from "path"; |
| 5 |
import type { WatchOptions, FSWatcher } from "chokidar"; |
| 6 |
const fileToDeps = new Map<string, Set<string>>(); |
| 7 |
const depToFiles = new Map<string, Set<string>>(); |
| 8 |
let isWatchMode = false; |
| 9 |
let watcher: FSWatcher; |
| 10 |
const watchQueue = new Set<string>(); |
| 11 |
let hasStarted = false; |
| 12 |
export function enable({ |
| 13 |
enableGlobbing |
| 14 |
}: { |
| 15 |
enableGlobbing: boolean; |
| 16 |
}) { |
| 17 |
isWatchMode = true; |
| 18 |
const { |
| 19 |
FSWatcher |
| 20 |
} = requireChokidar(); |
| 21 |
const options: WatchOptions = { |
| 22 |
disableGlobbing: !enableGlobbing, |
| 23 |
persistent: true, |
| 24 |
ignoreInitial: true, |
| 25 |
awaitWriteFinish: { |
| 26 |
stabilityThreshold: 50, |
| 27 |
pollInterval: 10 |
| 28 |
} |
| 29 |
}; |
| 30 |
watcher = new FSWatcher(options); |
| 31 |
watcher.on("unlink", unwatchFile); |
| 32 |
} |
| 33 |
export function startWatcher() { |
| 34 |
hasStarted = true; |
| 35 |
for (const dep of watchQueue) { |
| 36 |
watcher.add(dep); |
| 37 |
} |
| 38 |
watchQueue.clear(); |
| 39 |
watcher.on("ready", () => { |
| 40 |
console.log("The watcher is ready."); |
| 41 |
}); |
| 42 |
} |
| 43 |
export function watch(filename: string): void { |
| 44 |
if (!isWatchMode) { |
| 45 |
throw new Error("Internal Babel error: .watch called when not in watch mode."); |
| 46 |
} |
| 47 |
if (!hasStarted) { |
| 48 |
watchQueue.add(path.resolve(filename)); |
| 49 |
} else { |
| 50 |
watcher.add(path.resolve(filename)); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
export function onFilesChange(callback: (filenames: string[], event: string, cause: string) => void): void { |
| 62 |
if (!isWatchMode) { |
| 63 |
throw new Error("Internal Babel error: .onFilesChange called when not in watch mode."); |
| 64 |
} |
| 65 |
watcher.on("all", (event, filename) => { |
| 66 |
if (event !== "change" && event !== "add") return; |
| 67 |
const absoluteFile = path.resolve(filename); |
| 68 |
callback([absoluteFile, ...(depToFiles.get(absoluteFile) ?? [])], event, absoluteFile); |
| 69 |
}); |
| 70 |
} |
| 71 |
export function updateExternalDependencies(filename: string, dependencies: Set<string>) { |
| 72 |
if (!isWatchMode) return; |
| 73 |
|
| 74 |
|
| 75 |
const absFilename = path.resolve(filename); |
| 76 |
const absDependencies = new Set(Array.from(dependencies, dep => path.resolve(dep))); |
| 77 |
const deps = fileToDeps.get(absFilename); |
| 78 |
if (deps) { |
| 79 |
for (const dep of deps) { |
| 80 |
if (!absDependencies.has(dep)) { |
| 81 |
removeFileDependency(absFilename, dep); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
for (const dep of absDependencies) { |
| 86 |
let deps = depToFiles.get(dep); |
| 87 |
if (!deps) { |
| 88 |
depToFiles.set(dep, deps = new Set()); |
| 89 |
if (!hasStarted) { |
| 90 |
watchQueue.add(dep); |
| 91 |
} else { |
| 92 |
watcher.add(dep); |
| 93 |
} |
| 94 |
} |
| 95 |
deps.add(absFilename); |
| 96 |
} |
| 97 |
fileToDeps.set(absFilename, absDependencies); |
| 98 |
} |
| 99 |
function removeFileDependency(filename: string, dep: string) { |
| 100 |
const deps = (depToFiles.get(dep) as Set<string>); |
| 101 |
deps.delete(filename); |
| 102 |
if (deps.size === 0) { |
| 103 |
depToFiles.delete(dep); |
| 104 |
if (!hasStarted) { |
| 105 |
watchQueue.delete(dep); |
| 106 |
} else { |
| 107 |
watcher.unwatch(dep); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
function unwatchFile(filename: string) { |
| 112 |
const deps = fileToDeps.get(filename); |
| 113 |
if (!deps) return; |
| 114 |
for (const dep of deps) { |
| 115 |
removeFileDependency(filename, dep); |
| 116 |
} |
| 117 |
fileToDeps.delete(filename); |
| 118 |
} |
| 119 |
function requireChokidar(): any { |
| 120 |
const require = createRequire(import.meta.url); |
| 121 |
try { |
| 122 |
return process.env.BABEL_8_BREAKING ? require("chokidar") : parseInt(process.versions.node) >= 8 ? require("chokidar") : require("@nicolo-ribaudo/chokidar-2"); |
| 123 |
} catch (err) { |
| 124 |
console.error("The optional dependency chokidar failed to install and is required for " + "--watch. Chokidar is likely not supported on your platform."); |
| 125 |
throw err; |
| 126 |
} |
| 127 |
} |