Show nicer message on syntax errors
Dan Abramov committed
Nov 27, 2017 at 15:55 UTC
018276976c0c47aa1d17cb889034899146f47cb7
1 file changed
+17
-7
scripts/rollup/build.js
+17
-7
@@ -416,13 +416,23 @@ function createBundle(bundle, bundleType) {
416
console.error(error.message);
417
418
const {file, line, column} = error.loc;
419
- const rawLines = fs.readFileSync(file, 'utf-8');
420
- // column + 1 is required due to rollup counting column start position from 0
421
- // whereas babel-code-frame counts from 1
422
- const frame = codeFrame(rawLines, line, column + 1, {
423
- highlightCode: true,
424
- });
425
- console.error(frame);
419
+ if (file) {
420
+ // This looks like an error from Rollup, e.g. missing export.
421
+ // We'll use the accurate line numbers provided by Rollup but
422
+ // use Babel code frame because it looks nicer.
423
+ const rawLines = fs.readFileSync(file, 'utf-8');
424
+ // column + 1 is required due to rollup counting column start position from 0
425
+ // whereas babel-code-frame counts from 1
426
+ const frame = codeFrame(rawLines, line, column + 1, {
427
+ highlightCode: true,
428
+ });
429
+ console.error(frame);
430
+ } else {
431
+ // This looks like an error from a plugin (e.g. Babel).
432
+ // In this case we'll resort to displaying the provided code frame
433
+ // because we can't be sure the reported location is accurate.
434
+ console.error(error.codeFrame);
435
+ }
436
} else {
437
console.error(error);
438
}