Skip to content

Commit 0182769

Browse files
committed
Show nicer message on syntax errors
1 parent d445cd6 commit 0182769

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

scripts/rollup/build.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -416,13 +416,23 @@ function createBundle(bundle, bundleType) {
416416
console.error(error.message);
417417

418418
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+
}
426436
} else {
427437
console.error(error);
428438
}

0 commit comments

Comments
 (0)