Skip to content

Commit

Permalink
polish up watcher errors
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Dec 18, 2018
1 parent 7386a02 commit 99b03ca
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ When `watch: true` is set, the build object is not a promise, but has the follow
```js
{
// handler re-run on each build completion
handler (({ code, map, assets }) => { ... })
// watch errors are reported on "err"
handler (({ err, code, map, assets }) => { ... })
// handler re-run on each rebuild start
rebuild (() => {})
// close the watcher
Expand Down
14 changes: 12 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ switch (args._[0]) {
errTooManyArguments("build");

let startTime = Date.now();
let ps;
const ncc = require("./index.js")(
eval("require.resolve")(resolve(args._[1] || ".")),
{
Expand All @@ -175,7 +176,14 @@ switch (args._[0]) {
}
);

async function handler ({ code, map, assets }) {
async function handler ({ err, code, map, assets }) {
// handle watch errors
if (err) {
console.error(err);
console.log('Watching for changes...');
return;
}

outDir = outDir || resolve("dist");
mkdirp.sync(outDir);
// remove all existing ".js" files in the out directory
Expand Down Expand Up @@ -210,7 +218,7 @@ switch (args._[0]) {
}

if (run) {
const ps = require("child_process").fork(outDir + "/index.js", {
ps = require("child_process").fork(outDir + "/index.js", {
execArgv: map
? ["-r", resolve(__dirname, "sourcemap-register")]
: []
Expand All @@ -221,6 +229,8 @@ switch (args._[0]) {
if (args["--watch"]) {
ncc.handler(handler);
ncc.rebuild(() => {
if (ps)
ps.kill();
startTime = Date.now();
console.log('File change, rebuilding...');
});
Expand Down
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,8 @@ module.exports = (
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) return reject(err);
if (stats.hasErrors()) {
if (stats.hasErrors())
return reject(new Error(stats.toString()));
}
resolve();
});
})
Expand All @@ -240,14 +239,15 @@ module.exports = (
let cachedResult;
watcher = compiler.watch({}, (err, stats) => {
if (err) return reject(err);
if (stats.hasErrors()) {
return reject(new Error(stats.toString()));
}
if (err)
return watchHandler({ err });
if (stats.hasErrors())
return watchHandler({ err: stats.toString() });
const { code, map, assets } = finalizeHandler();
// clear output file system
mfs.data = {};
if (watchHandler)
watchHandler({ code, map, assets });
watchHandler({ code, map, assets, err: null });
else
cachedResult = { code, map, assets};
});
Expand Down

0 comments on commit 99b03ca

Please sign in to comment.