Skip to content

Commit

Permalink
use plugin names in CLI logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jan 6, 2017
1 parent 7831164 commit be7c90f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
41 changes: 17 additions & 24 deletions bin/src/logging.js
Expand Up @@ -8,40 +8,33 @@ const errorSymbol = process.stderr.isTTY ? `🚨 ` : `Error: `;
// log to stderr to keep `rollup main.js > bundle.js` from breaking
export const stderr = console.error.bind( console ); // eslint-disable-line no-console

export function handleWarning ( warning ) {
stderr( `${warnSymbol}${chalk.bold( warning.message )}` );
function log ( object, symbol ) {
const message = object.plugin ? `(${object.plugin} plugin) ${object.message}` : object.message;

stderr( `${symbol}${chalk.bold( message )}` );

if ( warning.url ) {
stderr( chalk.cyan( warning.url ) );
if ( object.url ) {
stderr( chalk.cyan( object.url ) );
}

if ( warning.loc ) {
stderr( `${relativeId( warning.loc.file )} (${warning.loc.line}:${warning.loc.column})` );
if ( object.loc ) {
stderr( `${relativeId( object.loc.file )} (${object.loc.line}:${object.loc.column})` );
} else if ( object.id ) {
stderr( relativeId( object.id ) );
}

if ( warning.frame ) {
stderr( chalk.dim( warning.frame ) );
if ( object.frame ) {
stderr( chalk.dim( object.frame ) );
}

stderr( '' );
}

export function handleError ( err, recover ) {
stderr( `${errorSymbol}${chalk.bold( err.message )}` );

if ( err.url ) {
stderr( chalk.cyan( err.url ) );
}

if ( err.loc ) {
stderr( `${relativeId( err.loc.file )} (${err.loc.line}:${err.loc.column})` );
}

if ( err.frame ) {
stderr( chalk.dim( err.frame ) );
}

stderr( '' );
export function handleWarning ( warning ) {
log( warning, warnSymbol );
}

export function handleError ( err, recover ) {
log( err, errorSymbol );
if ( !recover ) process.exit( 1 );
}
10 changes: 6 additions & 4 deletions src/Bundle.js
Expand Up @@ -619,11 +619,13 @@ export default class Bundle {

warn ( warning ) {
warning.toString = () => {
if ( warning.loc ) {
return `${relativeId( warning.loc.file )} (${warning.loc.line}:${warning.loc.column}) ${warning.message}`;
}
let str = '';

if ( warning.plugin ) str += `(${warning.plugin} plugin) `;
if ( warning.loc ) str += `${relativeId( warning.loc.file )} (${warning.loc.line}:${warning.loc.column}) `;
str += warning.message;

return warning.message;
return str;
};

this.onwarn( warning );
Expand Down
1 change: 1 addition & 0 deletions src/utils/transform.js
Expand Up @@ -44,6 +44,7 @@ export default function transform ( bundle, source, id, plugins ) {
warn: ( warning, pos ) => {
warning = augment( warning, pos, 'PLUGIN_WARNING' );
warning.plugin = plugin.name;
warning.id = id;
bundle.warn( warning );
},

Expand Down
2 changes: 2 additions & 0 deletions test/function/plugin-warn/_config.js
Expand Up @@ -15,11 +15,13 @@ module.exports = {
warnings: [
{
code: 'PLUGIN_WARNING',
id: path.resolve( __dirname, 'main.js' ),
plugin: 'test',
message: 'foo'
},
{
code: 'PLUGIN_WARNING',
id: path.resolve( __dirname, 'main.js' ),
plugin: 'test',
message: 'bar',
pos: 22,
Expand Down

0 comments on commit be7c90f

Please sign in to comment.