Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Babel 6.0.20 Modules feature not work in IE8

I am trying to export a es6 module in header.js:

export default {
    setHeaderHighlight: function (index) {
        // do somethings
    }
};

And import it in index.js:

import header from "./header.js"

$(function () {
    header.setHeaderHighlight(0);
});

Then transformation comes out in index.bundle.js:

var _header = __webpack_require__(129);

var _header2 = _interopRequireDefault(_header);

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : { default: obj }; // crash here
}

So here is the problem, ie8 will rise a Expected identifier Exception at { default: obj }, but everythings is ok >=ie9.

Is there something i can do with this?

like image 546
Bruce Avatar asked Nov 04 '15 02:11

Bruce


3 Answers

By default, Babel 6.x requires you to enable an explicit set of transformations. The standard es2015 preset converts ES6 to ES5, however IE8 is not ES5-compatible. In this case, if you look at the plugins list, you will see

  • transform-es3-member-expression-literals
  • transform-es3-property-literals

These will convert your properties to be compatible with IE8. Generally in Babel 6.x you'd do this by passing those names as part of your plugins array, alongside the presets array and install the transforms via

npm install --save-dev babel-plugin-transform-es3-member-expression-literals babel-plugin-transform-es3-property-literals
like image 118
loganfsmyth Avatar answered Oct 16 '22 10:10

loganfsmyth


I use webpack + es3ify-loader as workaround.

loaders: {
  {
    test: /\.jsx?$/,
    exclude: /node_modules/,
    loaders: ['es3ify', `babel?${JSON.stringify(babelQuery)}`],
  },
}
like image 44
sorrycc Avatar answered Oct 16 '22 09:10

sorrycc


I also have the problem, and I wrote a webpack plugin to resolve it. I didn't really know if there is a nicer way to handle it, but it works.

The module in node_modules also works well.

like image 31
brycehq Avatar answered Oct 16 '22 10:10

brycehq