jayphelps / core-decorators

Library of stage-0 JavaScript decorators (aka ES2016/ES7 decorators but not accurate) inspired by languages that come with built-ins like @​override, @​deprecate, @​autobind, @​mixin and more. Popular with React/Angular, but is framework agnostic.
MIT License
4.51k stars 263 forks source link

Not working in IE when used with TypeScript #81

Closed maxdeviant closed 8 years ago

maxdeviant commented 8 years ago

I am using @autobind from core-decorators in a TypeScript codebase, and I am seeing errors when trying to execute my code in IE. It works in Chrome, and I have only tried reproducing the error in IE11, so far.

image

The issue appears to stem from the spread operator in autobind.js:

// original source code
function handle(args) {
  if (args.length === 1) {
    return autobindClass(...args);
  } else {
    return autobindMethod(...args);
  }
}

// the generated output, which I am consuming from npm
function handle(args) {
  if (args.length === 1) {
    return autobindClass.apply(undefined, _toConsumableArray(args));
  } else {
    return autobindMethod.apply(undefined, _toConsumableArray(args));
  }
}

// auto-generated function to handle spread operator
function _toConsumableArray(arr) {
  if (Array.isArray(arr)) {
    for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2;
  } else {
    // the offending line
    return Array.from(arr);
  }
}

The Array.from is the issue, since IE does not have support for it.

Thoughts?

maxdeviant commented 8 years ago

The main issue here is that the emitted lib files have ES6-specific functions, and I am not running through any additional transpiling on my end, aside from my TypeScript code, which is being compiled to ES5.

This is also semi-related to #35.

jayphelps commented 8 years ago

I don't believe this is something I'll be able to accommodate since IMO people should be including an ES6 polyfill library if you're using ES6 and above syntax. Babel users include babel-polyfill which itself includes the core-js polyfill lib. I'm not sure what TypeScript users use. If I include the polyfill, it will add bloat that a vast majority of users don't need since they're already polyfilling ES6. core-decorators actually doesn't officially support TypeScript, but I try to fix a majority of the issues that get reported, as long as it doesn't notably impact the more common ES6 users via babel et al.

That said, it certainly doesn't hurt to warn users in the README that this library assumes you've polyfilled to ES6.

Thoughts?

maxdeviant commented 8 years ago

Yea, I think you are right on that one.

I generally like to avoid global polyfills, in favor of ponyfills, but in this case I suppose I'll just polyfill the necessary libs. No biggie :smile:

Warning in the README would be a nice heads up, especially since the issue is not noticeable in Chrome, since it has more complete ES6 support.

jayphelps commented 8 years ago

@maxdeviant thanks for understanding and of course for letting me know, since I did not in fact realize the spread operator transpiles to that. I'll add that warning.

mrvijaycode commented 7 years ago

@jayphelps thanks buddy. Its resolved with your help for me. Its working in IE11 now by just installing

npm install --save babel-polyfill

and i have added below imports in my *.ts file.

import "es6-promise/auto"; import "babel-polyfill";

Thank you very much for your help