Improve formatting of errors when building (#11456)
* Improve formatting of errors when building * Remove undefined from the header when error.plugin is undefined * Add babel-code-frame and syntax highlighting in error message * Run yarn prettier and fix code format
Soo Jae Hwang committed
Nov 19, 2017 at 23:23 UTC
962042f82713b7d8fc61165cf4480801cbbd1f67
3 files changed
+25
-4
package.json
+1
@@ -8,6 +8,7 @@
8
"art": "^0.10.1",
9
"async": "^1.5.0",
10
"babel-cli": "^6.6.5",
11
+ "babel-code-frame": "^6.26.0",
12
"babel-core": "^6.0.0",
13
"babel-eslint": "^7.1.0",
14
"babel-jest": "^21.3.0-beta.4",
scripts/rollup/build.js
+15
-3
@@ -25,6 +25,7 @@ const syncReactNative = require('./sync').syncReactNative;
25
const syncReactNativeRT = require('./sync').syncReactNativeRT;
26
const syncReactNativeCS = require('./sync').syncReactNativeCS;
27
const Packaging = require('./packaging');
28
+const codeFrame = require('babel-code-frame');
29
const Wrappers = require('./wrappers');
30
31
const UMD_DEV = Bundles.bundleTypes.UMD_DEV;
@@ -407,10 +408,21 @@ function createBundle(bundle, bundleType) {
408
})
409
.catch(error => {
410
if (error.code) {
410
- console.error(`\x1b[31m-- ${error.code} (${error.plugin}) --`);
411
+ console.error(
412
+ `\x1b[31m-- ${error.code}${
413
+ error.plugin ? ` (${error.plugin})` : ''
414
+ } --`
415
+ );
416
console.error(error.message);
412
- console.error(error.loc);
413
- console.error(error.codeFrame);
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);
426
} else {
427
console.error(error);
428
}
yarn.lock
+9
-1
@@ -274,6 +274,14 @@ babel-code-frame@^6.16.0, babel-code-frame@^6.22.0:
274
esutils "^2.0.2"
275
js-tokens "^3.0.0"
276
277
+babel-code-frame@^6.26.0:
278
+ version "6.26.0"
279
+ resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
280
+ dependencies:
281
+ chalk "^1.1.3"
282
+ esutils "^2.0.2"
283
+ js-tokens "^3.0.2"
284
+
285
babel-core@6, babel-core@^6.0.0, babel-core@^6.24.1:
286
version "6.24.1"
287
resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.24.1.tgz#8c428564dce1e1f41fb337ec34f4c3b022b5ad83"
@@ -2930,7 +2938,7 @@ js-tokens@1.0.1:
2938
version "1.0.1"
2939
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-1.0.1.tgz#cc435a5c8b94ad15acb7983140fc80182c89aeae"
2940
2933
-js-tokens@^3.0.0:
2941
+js-tokens@^3.0.0, js-tokens@^3.0.2:
2942
version "3.0.2"
2943
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
2944