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