diff --git a/packages/cli/src/commands/plugin/build.ts b/packages/cli/src/commands/plugin/build.ts index aa9c68c494..9be71c5e5b 100644 --- a/packages/cli/src/commands/plugin/build.ts +++ b/packages/cli/src/commands/plugin/build.ts @@ -18,6 +18,43 @@ import { rollup, watch, OutputOptions } from 'rollup'; import conf from './rollup.config'; import { Command } from 'commander'; import { withCache, getDefaultCacheOptions } from 'commands/build-cache'; +import { paths } from 'helpers/paths'; +import chalk from 'chalk'; + +function logError(error: any) { + console.log(''); + + if (error.code === 'PLUGIN_ERROR') { + // typescript2 plugin has a complete message with all codeframes + if (error.plugin === 'rpt2') { + console.log(error.message); + } else { + // Log which plugin is causing errors to make it easier to identity. + // If we see these in logs we likely want to provide some custom error + // output for those plugins too. + console.log(`(plugin ${error.plugin}) ${error}`); + } + } else { + // Generic rollup errors, log what's available + if (error.loc) { + const file = `${paths.resolveTarget((error.loc.file || error.id)!)}`; + const pos = `${error.loc.line}:${error.loc.column}`; + console.log(`${file} [${pos}]`); + } else if (error.id) { + console.log(paths.resolveTarget(error.id)); + } + + console.log(String(error)); + + if (error.url) { + console.log(chalk.cyan(error.url)); + } + + if (error.frame) { + console.log(chalk.dim(error.frame)); + } + } +} export default async (cmd: Command) => { if (cmd.watch) { @@ -33,20 +70,29 @@ export default async (cmd: Command) => { // ERROR — encountered an error while bundling if (event.code === 'ERROR') { - console.log(event.error); - } else { - console.log(event.code); + logError(event.error); + } else if (event.code === 'BUNDLE_START') { + console.log(chalk.dim(`building ${event.input}`)); + } else if (event.code === 'BUNDLE_END') { + const { input, duration } = event; + const s = (duration / 1000).toFixed(1); + console.log(chalk.green(`built ${input} in ${s}s`)); } }); }); } await withCache(getDefaultCacheOptions(), async () => { - const bundle = await rollup({ - input: conf.input, - plugins: conf.plugins, - }); - await bundle.generate(conf.output as OutputOptions); - await bundle.write(conf.output as OutputOptions); + try { + const bundle = await rollup({ + input: conf.input, + plugins: conf.plugins, + }); + await bundle.generate(conf.output as OutputOptions); + await bundle.write(conf.output as OutputOptions); + } catch (error) { + logError(error); + process.exit(1); + } }); };