Skip to content

Babel includes polyfill 'core-js/modules/web.dom.iterable' to output if script contains forEach() method call. #9449

@zhaparoff

Description

@zhaparoff

Bug Report

Current Behavior
Babel includes import "core-js/modules/web.dom.iterable"; in any output which input contains forEach() method call. It can be Array.prototype.forEach() or forEach() method defined in custom class or interface:
https://github.com/zhaparoff/ts-babel-tests/blob/master/src/js/babelTest2.js -> https://github.com/zhaparoff/ts-babel-tests/blob/master/dist/js/babelTest2.js

https://github.com/zhaparoff/ts-babel-tests/blob/master/src/ts/babelTest1.ts ->
https://github.com/zhaparoff/ts-babel-tests/blob/master/dist/ts/babelTest1.js

The only exception is calling forEach() for an array defined in the same visibility scope.
https://github.com/zhaparoff/ts-babel-tests/blob/master/src/js/babelTest1.js -> https://github.com/zhaparoff/ts-babel-tests/blob/master/dist/js/babelTest1.js

Also targeting Node instead of browsers omits that polyfill, which is obviously expected.
But targeting any browser, even the latest version of Chrome, includes that polyfill.

Input Code
https://github.com/zhaparoff/ts-babel-tests

Expected behavior/code
From my point of view, Babel shouldn't polyfill something that does not require polyfilling for sure.
I understand that JS has no type signatures and polyfilling suggestions could be made only from the context where the call is found. But developer should have ability to precisely control such kind of polyfilling, am I right? Probably there is a setting or plugin to tur off or a workaround, that I'm not aware of?

Babel Configuration (.babelrc, package.json, cli command)
https://github.com/zhaparoff/ts-babel-tests/blob/master/package.json
https://github.com/zhaparoff/ts-babel-tests/blob/master/.babelrc
npm run build

Environment

  • Babel version(s): 7.2.2
  • Node/npm version: Node 11.8.0/npm 6.7.0
  • OS: Windows 10, 1803
  • Monorepo: ?
  • How you are using Babel: cli through npm run

Possible Solution
Include the option for DOM polyfills into env preset, allowing to control it separately of browser/node target settings. Or move this polyfilling to a separate plugin, which can be disabled.

Additional context/Screenshots
I even won't use DOM manipulations in my project, they will be handled by existing application platform API. I believe, there should be the feasible way to control it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    i: needs triageoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions