babel-plugin-transform-for-of-without-iterator
Why?
By default, Babel transpiles for-or syntax to use Symbol.iterator
. Pretty ironic from a tool that's supposed to make your code backwards compatible. And now you have to import ~100kB Symbol
polyfill.
Well not anymore.
Installation
npm install babel-plugin-transform-for-of-without-iterator --save-dev
and add it to the plugin list
{
"plugins": ["babel-plugin-transform-for-of-without-iterator"]
}
What it does
Wraps the right side of the "of" into Array.from
Supports:
- arrays and array-like (
NodeList
) Set
Map
map.entries()
and friends
in
for let key val of map console
out
var _arrayified = map;if !Array _arrayified = Array;for var _i = 0; _i < _arrayifiedlength; _i++ var _arrayified$_i = _arrayified_i; var key = _arrayified$_i0; var val = _arrayified$_i1; console;
instead of babel's default
var _iteratorNormalCompletion = true;var _didIteratorError = false;var _iteratorError = undefined; try for var _iterator = mapSymboliterator _step; !_iteratorNormalCompletion = _step = _iteratornextdone; _iteratorNormalCompletion = true var _ref2 = _stepvalue; var _key = _ref20; var _val = _ref21; console; catch err _didIteratorError = true; _iteratorError = err; finally try if !_iteratorNormalCompletion && _iteratorreturn != null _iterator; finally if _didIteratorError throw _iteratorError;