Skip to content

Commit

Permalink
Fix watcher behaviour (#344)
Browse files Browse the repository at this point in the history
This fixes the watcher regression in #342, and includes a full end-to-end test of the watching behaviour to avoid any further regressions in future.
  • Loading branch information
guybedford authored and styfle committed Apr 3, 2019
1 parent 6cbe067 commit 0760377
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ Options:
let api = false;
if (require.main === module) {
runCmd(process.argv.slice(2), process.stdout, process.stderr)
.then(() => {
process.exit()
.then((watching) => {
if (!watching)
process.exit();
})
.catch(e => {
if (!e.silent)
Expand Down Expand Up @@ -315,6 +316,7 @@ async function runCmd (argv, stdout, stderr) {
startTime = Date.now();
stdout.write('File change, rebuilding...\n');
});
return true;
} else {
return ncc.then(handler);
}
Expand Down
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,17 @@ module.exports = (
else {
let cachedResult;
watcher = compiler.watch({}, (err, stats) => {
if (err) return reject(err);
if (err)
return watchHandler({ err });
if (stats.hasErrors())
return watchHandler({ err: stats.toString() });
const { code, map, assets } = finalizeHandler();
const returnValue = finalizeHandler();
// clear output file system
mfs.data = {};
if (watchHandler)
watchHandler({ code, map, assets, err: null });
watchHandler(returnValue);
else
cachedResult = { code, map, assets};
cachedResult = returnValue;
});
let closed = false;
return {
Expand Down
5 changes: 5 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@
{
args: ["run", "--watch", "test/integration/test.ts"],
expect: { code: 2 }
},
{
args: ["build", "-o", "tmp", "--watch", "test/fixtures/no-dep.js"],
timeout: 500,
expect: { timeout: true }
}
]
1 change: 1 addition & 0 deletions test/fixtures/no-dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('asdf');
16 changes: 13 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ for (const cliTest of eval(fs.readFileSync(__dirname + "/cli.js").toString())) {
ps.stderr.on("data", chunk => stderr += chunk.toString());
ps.stdout.on("data", chunk => stdout += chunk.toString());
const expected = cliTest.expect || { code: 0 };
let timedOut = false;
if (cliTest.timeout)
setTimeout(() => {
timedOut = true;
ps.kill();
}, cliTest.timeout);
const code = await new Promise(resolve => ps.on("close", resolve));
if (typeof expected === "function")
expect(expected(code, stdout, stderr)).toBe(true);
else if ("code" in expected)
expect(code).toBe(expected.code);
expect(expected(code, stdout, stderr, timedOut)).toBe(true);
else {
if ("code" in expected)
expect(code).toBe(expected.code);
if ("timeout" in expected)
expect(timedOut).toBe(true);
}
});
}

Expand Down

0 comments on commit 0760377

Please sign in to comment.