question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Array destructuring transpiles into code that is incompatible with IE11

See original GitHub issue

Bug Report

  • I would like to work on a fix!

Current Behavior Transpiling the code below generates code that uses Symbol, which does not run on IE11, even though I have @babel/env set to ie: 11.

Input Code

const oneTwoTree = () => [1, 2, 3];
const [a, b, c] = oneTwoTree();

Output Code

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var oneTwoTree = function oneTwoTree() {
  return [1, 2, 3];
};

var _oneTwoTree = oneTwoTree(),
    _oneTwoTree2 = _slicedToArray(_oneTwoTree, 3),
    a = _oneTwoTree2[0],
    b = _oneTwoTree2[1],
    c = _oneTwoTree2[2];

Expected behavior/code Transpiled code that runs on IE11.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
module.exports = function(api) {                                                                                       
  return {                                                                                                             
    presets: [                                                                                                         
      [                                                                                                                
        '@babel/env',                                                                                                  
        {                                                                                                              
          modules: 'amd',                                                                                              
          targets: {                                                                                                   
            ie: '11',                                                                                                  
            safari: '11'                                                                                               
          }                                                                                                            
        }                                                                                                              
      ],                                                                                                               
      ['@babel/react']                                                                                                 
    ],                                                                                                                 
    sourceMaps: 'both',                                                                                                
    sourceType: 'unambiguous'                                                                                          
  };                                                                                                                   
};                                                                                                                     

Environment

  • Babel version(s): v7.8.4
  • How you are using Babel: cli

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
luwescommented, Apr 8, 2021

Leaving this snippet here for anyone getting an error in IE11, hope it can help anyone. It took pretty long for me to debug…

Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.
function iterator() {
    var selfThis = this;
    var index = 0;
    var done;

    function Iterator() {}

    Iterator.prototype.next = function() {
        if (index > selfThis.length - 1) {
            done = true;
        }
        if (done) {
            return { value: undefined, done: true };
        }
        return { value: [index, selfThis[index++]], done: false };
    };
	
	// Many small polyfills leave out this method which Babel requires and otherwise throws the above error
    Iterator.prototype[Symbol.iterator] = function() {
        return this;
    };

    return new Iterator();
}

// Needed for: IE 11
if (!Array.prototype.entries) {
    Array.prototype.entries = iterator;
}

// Needed for: IE 11
if (!Array.prototype[Symbol.iterator]) {
    Array.prototype[Symbol.iterator] = iterator;
}
0reactions
nicolo-ribaudocommented, Feb 1, 2021

The problem was that document.body.childNodes is not iterable in old IE versions.

You can now enable the allowArrayLike option of the destructuring transform, or the new arrayLikeIsIterable assumption that will be released in Babel 7.13.0 (https://github.com/babel/rfcs/pull/5).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Destructuring assignment not working in IE 11 even after ...
I have a situation where I am adding a middleware which contain destructing params When opened in google chrome ,its working fine ....
Read more >
IE11 and the Missing Polyfills - DEV Community ‍ ‍
Our startup was in the stealth mode. We had no traffic, no customers, and, obviously, no worries. Everything was perfect - the code...
Read more >
Polyfills, transpilation, and browser support | by Dominic Fraser
Array.includes unsupported on IE11 https://caniuse.com/array-. It's important to note that while we use IE11 is as the most common example, ...
Read more >
ECMAScript 6 compatibility table
Feature name▻ Current browser 98% ES6 Trans‑ piler 25% Trace... Optimisation Optimisation Optimisation Optimi... §proper tail calls (tail call optimisation)▻ 0/2 0/2 0/2 §direct recursionc No...
Read more >
Destructuring assignment - JavaScript - MDN Web Docs
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found