0

How Babel helps me for support old browsers with ES3 (e.g. IE7-8)? For example. I have following simple code written in ES6 standard

'use strict';

class Alert {
    constructor() {
        let a = [1, 2, 3];
        a.forEach((value) => {
            console.log(value);
        });
    };
}

console.log(new Alert());

Babel transpiles this code to ES5

'use strict';

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Alert = function Alert() {
    _classCallCheck(this, Alert);

    var a = [1, 2, 3];
    a.forEach(function (value) {
        console.log(value);
    });
};

console.log(new Alert());

As we know, ES3 browsers does not support Array.prototype.forEach() method... How should I organize my project using Babel with ES3 browsers support? And how to build it to one single production .js file?

  • Seeing that the mentioned browser isn't even supported anymore by Microsoft, why would you still support it? Drop the support so you can move forward. The last OS using that browser was Windows XP, and for that, the support dropped in 2015 if I am not mistaking. No need to keep dragging this baggage with you – Icepickle Dec 10 '17 at 12:11
  • `forEach` (and other ES5 builtin methods) cannot be transpiled - it's not syntax! -, you need to polyfill it. – Bergi Dec 10 '17 at 13:37
  • @Bergi OK, I understood that forEach is not a syntax, but what polyfill libraries should I use for support it in old platforms? There are two most popular polyfill libraries: babel-polyfill and core-js... What difference between them and what you recommend to use in web applications? – Mikhail Matveev Dec 10 '17 at 14:31
  • @Icepickle Some web browsers in 2012 model year Smart-TV platforms are not supporting ES5 standard (like old IE browsers). – Mikhail Matveev Dec 10 '17 at 14:34
  • @MikhailMatveev Yes, use either of them (there are others as well). I cannot recommend a specific one though. – Bergi Dec 10 '17 at 14:36
  • @vicbyte If I understood correctly, first of all I need to transpile my code from ES6 to ES5 and then use ES5 pollyfills? – Mikhail Matveev Dec 10 '17 at 14:38
  • @Bergi, are any possibilities to build my app with transpilling ES6 code to ES5 with pollyfills using only Babel tools? Or maybe I need to use Grunt or Gulp? – Mikhail Matveev Dec 10 '17 at 14:46

0 Answers0