Zach Lysobey
Zach Lysobey

Reputation: 15734

Babel is not processing Array.from or 'for ... of' loops

I'm using babel with gulp (.pipe($.babel())), and it seems to work for most things but is not working for Array.from.

It works fine when running babel-node:

$ ./node_modules/babel/bin/babel-node.js
> Array.from
[Function: from]

But when the following code is processed with gulp:

var foo = () => { console.log(Array.from) }

The transpiled source is:

var foo = function foo() {
    console.log(Array.from);
};

And the console output when I execute foo is:

undefined

Upvotes: 18

Views: 9306

Answers (1)

Zach Lysobey
Zach Lysobey

Reputation: 15734

Ended up solving this myself while writing my question, but figured I'd finish and answer myself to help future searchers:

There are certain features of babel that require a polyfill loaded in the browser due to limitations of ES5. This is loaded automatically in babel-node or you can include with with babel-polyfill.

Some of the features requiring the polyfill:

  • Abstract References
  • Array destructuring
  • Async functions
  • Comprehensions
  • For of
  • Array.from
  • spread

Upvotes: 15

Related Questions