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

@babel/preset-env adding unnecessary core-js polyfill #10758

Open
serenader2014 opened this issue Nov 25, 2019 · 4 comments
Open

@babel/preset-env adding unnecessary core-js polyfill #10758

serenader2014 opened this issue Nov 25, 2019 · 4 comments

Comments

@serenader2014
Copy link

@serenader2014 serenader2014 commented Nov 25, 2019

Bug Report

Current Behavior
@babel/preset-env is adding unnecessary core-js polyfill, when setting @babel/preset-env 's target options to chrome 49, for a quite simple one line code :window.location.search, babel will produce a useless core-js polyfill:

"use strict";

require("core-js/modules/es6.regexp.search");

window.location.search;

The weird thing is, when changing the target browsers to chrome 50, the polyfill is gone. So why babel will produce this es6.regexp.search polyfill for this code under chrome 50 version?

Input Code

REPL link: https://babeljs.io/repl#?babili=false&browsers=chrome%2049&build=&builtIns=usage&spec=false&loose=false&code_lz=O4SwdgJg9sB0A2UDGBDALiKZYGcCmKATkgBYDcQA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env&prettier=false&targets=&version=7.7.4&externalPlugins=

Expected behavior/code

Base on the input from the REPL link, the result should not contain core-js polyfill.

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

  • Filename: babel.config.js
{
  "your": { "config": "here" }
}

Environment


  • Babel version(s): [e.g. v6.0.0, v7.0.0-beta.34]
  • Node/npm version: [e.g. Node 8/npm 5]
  • OS: [e.g. OSX 10.13.4, Windows 10]
  • Monorepo: [e.g. yes/no/Lerna]
  • How you are using Babel: [e.g. cli, register, loader]

Possible Solution

Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Nov 25, 2019

Hey @serenader2014! 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."

@JLHwung

This comment has been minimized.

Copy link
Contributor

@JLHwung JLHwung commented Nov 25, 2019

There are two questions here. The first one is why regexp.search polyfill is applied. The regexp.search polyfill kicks in because window.location could be overwritten as RegExp:

(function (window) {
  window.location = /foo/;
  window.location.search("foo");
})({})

so when Babel sees a pattern like foo.bar.search, it would assume that foo.bar may be a RegExp and inject the regexp.search polyfill if the browser does not support regexp.search.

The other question is why it is applied under chrome 50. Babel uses data in compat-table to determine when the polyfill should apply. However I do notice that chrome 49 is marked as supported on browser-compat-data, which is conflicted with compat-table.

If you have chrome 49 test environment and confirm it works under chrome 49, could you submit a PR to compat-table? Then preset-env will benefit from this change.

@zloirock

This comment has been minimized.

Copy link
Member

@zloirock zloirock commented Nov 25, 2019

However I do notice that chrome 49 is marked as supported on browser-compat-data, which is conflicted with compat-table.

I wrote many times that compat-table, caniuse and mdn/browser-compat-data are bad data sources for polyfilling. Chrome 49:

image

@serenader2014

This comment has been minimized.

Copy link
Author

@serenader2014 serenader2014 commented Nov 26, 2019

so when Babel sees a pattern like foo.bar.search, it would assume that foo.bar may be a RegExp and inject the regexp.search polyfill if the browser does not support regexp.search.

That sounds weird to me. So how can I use window.location.search without having a polyfill? Is there anyway to tell babel not to polyfill a specific file?

@JLHwung JLHwung mentioned this issue Nov 29, 2019
3 of 5 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.