Skip to content

Commit

Permalink
fix #111 and log errors during watch
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdesl committed Jun 14, 2018
1 parent 818a7d5 commit d786f03
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
30 changes: 3 additions & 27 deletions src/cli.js
@@ -1,8 +1,8 @@
#!/usr/bin/env node
import chalk from 'chalk';
import microbundle from '.';
import prog from './prog';
import { stdout, stderr } from './utils';
import { stdout } from './utils';
import logError from './log-error';

const run = opts => {
microbundle(opts)
Expand All @@ -12,31 +12,7 @@ const run = opts => {
})
.catch(err => {
process.exitCode = (typeof err.code === 'number' && err.code) || 1;

const error = err.error || err;
const description = `${
error.name ? error.name + ': ' : ''
}${error.message || error}`;
const message = error.plugin
? `(${error.plugin} plugin) ${description}`
: description;

stderr(chalk.bold.red(message));

if (error.loc) {
stderr();
stderr(`at ${error.loc.file}:${error.loc.line}:${error.loc.column}`);
}

if (error.frame) {
stderr();
stderr(chalk.dim(error.frame));
} else if (err.stack) {
const headlessStack = error.stack.replace(message, '');
stderr(chalk.dim(headlessStack));
}

stderr();
logError(err);
process.exit();
});
};
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Expand Up @@ -19,6 +19,7 @@ import prettyBytes from 'pretty-bytes';
import shebangPlugin from 'rollup-plugin-preserve-shebang';
import typescript from 'rollup-plugin-typescript2';
import flow from './lib/flow-plugin';
import logError from './log-error';
import { readFile, isDir, isFile, stdout, stderr } from './utils';
import camelCase from 'camelcase';

Expand Down Expand Up @@ -162,8 +163,10 @@ export default async function microbundle(options) {
options.inputOptions,
),
).on('event', e => {
if (e.code === 'ERROR' || e.code === 'FATAL') {
return reject(e);
if (e.code === 'FATAL') {
return reject(e.error);
} else if (e.code === 'ERROR') {
logError(e.error);
}
if (e.code === 'END') {
getSizeInfo(options._code, options.outputOptions.file).then(
Expand Down
28 changes: 28 additions & 0 deletions src/log-error.js
@@ -0,0 +1,28 @@
import chalk from 'chalk';
import { stderr } from './utils';

export default function(err) {
const error = err.error || err;
const description = `${error.name ? error.name + ': ' : ''}${error.message ||
error}`;
const message = error.plugin
? `(${error.plugin} plugin) ${description}`
: description;

stderr(chalk.bold.red(message));

if (error.loc) {
stderr();
stderr(`at ${error.loc.file}:${error.loc.line}:${error.loc.column}`);
}

if (error.frame) {
stderr();
stderr(chalk.dim(error.frame));
} else if (err.stack) {
const headlessStack = error.stack.replace(message, '');
stderr(chalk.dim(headlessStack));
}

stderr();
}

0 comments on commit d786f03

Please sign in to comment.