7

I am new to babel.

I set it up like this:

.babelrc:

{
    "presets": ["es2015", "es2017"]
}

gulpfile:

gulp.task('default', function() {
    return gulp.src(['src/**/*.js', '!src/**/3rd/*'])
        .pipe(babel())
        .pipe(gulp.dest('dist'));
});

However this seems to be compiling to es5 which is not fully supported in Safari.

Is there a way to set target? So it can compile to ES3?

Noitidart
  • 35,443
  • 37
  • 154
  • 323

1 Answers1

12

No, Babel does not support transpiling to ES3.

Your options are:

  1. Compile to ES5 and then use ES5 shim.

  2. Use a different transpiler. Google Closure Compiler and TypeScript both support ES6 as input and ES3 as output.

Note: The "ES5" code that Babel produces may not work in all browsers, as it may include features not present in ES5 (see caveats for more info). In other words, since you're targeting even older browsers, you'll also need Babel polyfill as well as ES5 shim.

fstanis
  • 5,234
  • 1
  • 23
  • 42
  • I did `npm install --save-dev babel-polyfill` then in my js file I did: `import 'babel/polyfill';` however in the transpiled file I see: `'use strict'; require('babel/polyfill');` this will obviously not work when I paste it to jsfiddle.net - I get `require is not defiend` - can you please help me understand why its not properly transpiling? – Noitidart Oct 23 '16 at 17:24
  • 1
    This is a bit of a separate issue - in short, you need a module loader. I'd recommend [this question](http://stackoverflow.com/questions/31593694/do-i-need-require-js-when-i-use-babel) as it gives a **very** thorough overview of what your options are in this regard. – fstanis Oct 23 '16 at 17:27
  • Thanks fastanis! But I thought babel provided module loader no? – Noitidart Oct 23 '16 at 17:38
  • 1
    Sadly, no. Babel can only transform ES6 modules (`import` keyword) into another module system (such as AMD or SystemJS). – fstanis Oct 23 '16 at 18:06