Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print a message when the watcher of babel-cli is ready. #14748

Merged
merged 1 commit into from Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/babel-cli/src/babel/dir.ts
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
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
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
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
@@ -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
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
@@ -1,2 +1,3 @@
Successfully compiled 4 files with Babel (123ms).
The watcher is ready.
Successfully compiled 1 file with Babel (123ms).
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
@@ -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