Skip to content

Commit

Permalink
Print a message when the watcher of babel-cli is ready. (babel#14748)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu authored Jul 10, 2022
1 parent 41582ee commit ea99182
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/babel-cli/src/babel/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ export default async function ({
watcher.watch(filenameOrDir);
});

watcher.startWatcher();

watcher.onFilesChange(async filenames => {
processing++;
if (startTime === null) startTime = process.hrtime();
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-cli/src/babel/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ export default async function ({
if (cliOptions.watch) {
filenames.forEach(watcher.watch);

watcher.startWatcher();

watcher.onFilesChange((changes, event, cause) => {
const actionableChange = changes.some(
filename =>
Expand Down
33 changes: 30 additions & 3 deletions packages/babel-cli/src/babel/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const depToFiles = new Map<string, Set<string>>();

let isWatchMode = false;
let watcher;
const watchQueue = new Set<string>();
let hasStarted = false;

export function enable({ enableGlobbing }: { enableGlobbing: boolean }) {
isWatchMode = true;
Expand All @@ -25,14 +27,31 @@ export function enable({ enableGlobbing }: { enableGlobbing: boolean }) {
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.",
);
}

watcher.add(path.resolve(filename));
if (!hasStarted) {
watchQueue.add(path.resolve(filename));
} else {
watcher.add(path.resolve(filename));
}
}

/**
Expand Down Expand Up @@ -86,7 +105,11 @@ export function updateExternalDependencies(
if (!depToFiles.has(dep)) {
depToFiles.set(dep, new Set());

watcher.add(dep);
if (!hasStarted) {
watchQueue.add(dep);
} else {
watcher.add(dep);
}
}
depToFiles.get(dep).add(absFilename);
}
Expand All @@ -100,7 +123,11 @@ function removeFileDependency(filename: string, dep: string) {
if (depToFiles.get(dep).size === 0) {
depToFiles.delete(dep);

watcher.unwatch(dep);
if (!hasStarted) {
watchQueue.delete(dep);
} else {
watcher.unwatch(dep);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ const run = (async function* () {
assert.match(files[1], /src[\\/]main.js -> lib[\\/]main.js/);
assert.match(yield, /Successfully compiled 2 files with Babel \(\d+ms\)\./);

assert.equal(yield, "The watcher is ready.");

logFile("lib/index.js");
logFile("lib/main.js");
// wait 2s for watcher setup
await new Promise(resolve => setTimeout(resolve, 2000));

fs.writeFileSync("./file.txt", "Updated!");

files = [yield, yield].sort();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
BATCHED(0) src/index.js -> lib/index.js
BATCHED(0) src/main.js -> lib/main.js
Successfully compiled 2 files with Babel (123ms).
The watcher is ready.
EXECUTOR lib/index.js "let str = /"Hi :)/";"
EXECUTOR lib/main.js "console.log(/"Hi :)/");"
BATCHED(1) src/index.js -> lib/index.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ if (!assert.match) assert.match = (val, re) => assert(re.test(val));
const run = (async function* () {
assert.match(yield, /Successfully compiled 4 files with Babel \(\d+ms\)\./);

// wait 2s for watcher setup
await new Promise(resolve => setTimeout(resolve, 2000));
assert.equal(yield, "The watcher is ready.");

// update ./module1/src/index.js
fs.writeFileSync(
"./module1/src/index.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Successfully compiled 4 files with Babel (123ms).
The watcher is ready.
Successfully compiled 1 file with Babel (123ms).
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ if (!assert.match) assert.match = (val, re) => assert(re.test(val));
const run = (async function* () {
assert.match(yield, /Successfully compiled 2 files with Babel \(\d+ms\)\./);

assert.equal(yield, "The watcher is ready.");

logFile("lib/index.js");
logFile("lib/main.js");
// wait 2s for watcher setup
await new Promise(resolve => setTimeout(resolve, 2000));

fs.writeFileSync("./file.txt", "Updated!");

assert.match(yield, /Successfully compiled 2 files with Babel \(\d+ms\)\./);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Successfully compiled 2 files with Babel (123ms).
The watcher is ready.
EXECUTOR lib/index.js "let str = /"Hi :)/";"
EXECUTOR lib/main.js "console.log(/"Hi :)/");"
Successfully compiled 2 files with Babel (123ms).
Expand Down

0 comments on commit ea99182

Please sign in to comment.