Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Babel loses code formatting

Tags:

gulp

babeljs

I'm trying to integrate babel through gulp.

var babel = require('gulp-babel');
var es6 = require('babel-preset-es2015');
...
return gulp.src('path/to/my/source/file/js')
       .pipe(babel({presets:es6}))
...

When I run the compile task, my linter (JSHint) says that the line is too long and that I'm missing a line end.

Let's say that my source file is as following (please note the last empty line) :

(function(){
var myApp = angular.module('first-dependence',[
    'another-dependence',
    'and-another-dependence']
}();       
// Empty line here

Babel outputs it like this:

(function(){
var myApp = angular.module('first-dependence',['another-dependence','and-another-dependence']
}();

For me, he is ignoring the line returns inside the instructions and removes the last empty line.

Is it possible to tell babel to keep the formatting as it's and to only transcompile ?

Regards

like image 556
Doej Avatar asked Sep 29 '16 09:09

Doej


1 Answers

You can't. But you can try

  • the retainLines option

    .pipe(babel({presets:es6, retainLines:true}))
    
  • or to rely on source maps (see gulp-babel).

But neither will preserve your white-space exactly as it is right now.

like image 182
flob Avatar answered Nov 17 '22 13:11

flob