Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for of on HTMLCollection (getElementsBy) throws error when targets to ie11 #12068

Open
1 task
max-hk opened this issue Sep 17, 2020 · 9 comments
Open
1 task

Comments

@max-hk
Copy link

max-hk commented Sep 17, 2020

Bug Report

  • I would like to work on a fix!

Current behavior

The output of @babel/preset-env and core-js failed to run on browsers.

Demo Project: https://github.com/maxloh/babel-failed

Open index.html and you will see the following error in console.

image

Input Code

[0, 1, 2].indexOf(0);
for (const node of document.getElementsByClassName('h1')) {
  console.log(node.innerText);
}

Expected behavior

The output run in browser with no errors.

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

  • Filename: babel.config.js
module.exports = {
  entry: './src/demo-1.js',
  mode: 'production',
  output: {
    filename: './demo-1.js',
  },
  module: {
    rules: [{
      test: /\.js$/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: [
            ["@babel/preset-env", { "useBuiltIns": "usage", "targets": { "ie": "11" }, "corejs": 3 }]
          ]
        }
      }
    }]
  }
};

Environment

  • Babel version(s): 7.11.6
  • Node/npm version:Node 14.7.0/npm 6.14.7
  • OS: Windows 10
  • Monorepo: [e.g. yes/no/Lerna]
  • How you are using Babel: loader

Possible Solution

Additional context
Add any other context about the problem here. Or a screenshot if applicable

@max-hk max-hk changed the title Output of @babel/preset-env failed to run in the browser Output of @babel/preset-env failed to run on browsers Sep 17, 2020
@babel-bot
Copy link
Collaborator

Hey @MaxLOh! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@existentialism
Copy link
Member

existentialism commented Sep 17, 2020

@MaxLOh

Couple things...

(a) you'll want to exclude node_modules from your babel-loader config, otherwise core-js will get run through Babel and is the cause of the error in demo-1 and demo-2.

module: {
    rules: [{
      test: /\.js$/,
+     exclude: /node_modules/,
      use: {
        loader: 'babel-loader',

(b) you'll also want to enable the allowArrayLike option (https://babeljs.io/docs/en/babel-plugin-transform-for-of#allowarraylike), to get the for-of loop on a HTMLCollection inside demo-2 to work.

      use: {
        loader: 'babel-loader',
        options: {
          presets: [
            ["@babel/preset-env", { "useBuiltIns": "usage", "targets": { "ie": "11" }, "corejs": 3 }]
-         ]
+         ],
+         "plugins": [
+           ["@babel/plugin-transform-for-of", {
+             "allowArrayLike": true
+           }]
+         ]
        }

@JLHwung
Copy link
Contributor

JLHwung commented Sep 17, 2020

Duplicate of #11763

@JLHwung JLHwung marked this as a duplicate of #11763 Sep 17, 2020
@JLHwung JLHwung changed the title Output of @babel/preset-env failed to run on browsers for of on HTMLCollection (getElementsBy) throws error when targets to ie11 Sep 17, 2020
@max-hk
Copy link
Author

max-hk commented Sep 17, 2020

@existentialism Thx.

Shouldn't (a) be documented in @babel/preset-env documentations?

@nicolo-ribaudo
Copy link
Member

Probably it should be included in core-js's documentation, since it's specifically core-js that should not be transpiled because it's not compatible with Babel's transforms.

@JLHwung
Copy link
Contributor

JLHwung commented Sep 17, 2020

Can we add allowArrayLike: true to the for-of transform when targets contain ie?

@existentialism
Copy link
Member

@JLHwung I'm down for that

@nicolo-ribaudo
Copy link
Member

I think that we can only do that in loose mode, since that allows doing different uncompliant things.
Or we could add an arrowArrayLike option to preset-env, so that we don't add the helper by default.

@max-hk
Copy link
Author

max-hk commented Sep 18, 2020

Probably it should be included in core-js's documentation, since it's specifically core-js that should not be transpiled because it's not compatible with Babel's transforms.

@nicolo-ribaudo But @babel/preset-env uses core-js by default.

New comers may get confused as a basic/minimal configuration not resulting in a working output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants