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

Way to exclude some polyfills completely. #11425

Closed
pacifier007 opened this issue Apr 15, 2020 · 5 comments
Closed

Way to exclude some polyfills completely. #11425

pacifier007 opened this issue Apr 15, 2020 · 5 comments
Labels
i: enhancement i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@pacifier007
Copy link

Feature Request

When using useBuiltIns: "usage", a lot of polyfills added may not be needed at all for my target browsers (ES7, I tried with Edge > 16). For example, when I use array.includes('string'), the following get added:

import "core-js/modules/es.number.to-fixed";
import "core-js/modules/es.regexp.to-string";
import "core-js/modules/es.string.match";
import "core-js/modules/es.string.replace";
import "core-js/modules/es.string.search";
import "core-js/modules/es.string.split";
import "core-js/modules/web.dom-collections.iterator";

This adds in a lot of code that's unnecessary for my usage.

In babel/env options, I can add these all to the exclude but these may be relevant to another polyfill.

Suggestion

Please add a way to exclude specific methods or features. For instance, it would be quite useful to be able to exclude array.includes, string.includes or even all .includes() entirely.

@babel-bot
Copy link
Collaborator

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

@nicolo-ribaudo
Copy link
Member

Doesn't exclude: ["es.array.includes", "es.string.includes"] work for you?

Also, those polyfills you have in your output are not related to .includes(). When only using array.includes('string') and targeting Edge 16, I get this output:

import "core-js/modules/es.string.includes";
array.includes('string');

This is what I get when targeting an older browser (ie 7):

import "core-js/modules/es.array.includes";
import "core-js/modules/es.string.includes";
array.includes('string');

@pacifier007
Copy link
Author

Whoops. Sorry about that. My example wasn't correct as I had a lot of ES5 code in there too - that I didn't think would trigger any polyfills at all.

I can confirm exclude does work.

That brings me to a similar query. Is there any way to ignore a whole chunk of ES5 code (or any code), to not be polyfilled. I couldn't find any.

Something like autoprefixer or eslint /* babel-core-js: off */ . That would be great to have. This way we could filter out false positives like array.slice, array.indexOf, string.match etc. getting polyfilled when they're not using one of the new es features.

@JMarkoski
Copy link
Contributor

Related: #9170 , zloirock/core-js#518.

About the suggestion to have an option to disable polyfilling a block of code: Suppose you have two browsers as your targets and you disable polyfilling for a block of code that needs polyfills on the first browser, but doesn't on the second. This will leave you with a bundle that will run on the browser that supports the features natively, failing in the other browser, because you disabled polyfilling in your code. This is why@babel/preset-env does what it does: you give it a list of targets, it generates a set of polyfills that will make the code run on all of them. Yes, the polyfills will be present on the browsers that support the features natively too, but that is the trade-off when supporting multiple targets. What you actually want is feature detection when polyfilling features, hence the related issues.

@nicolo-ribaudo
Copy link
Member

I'm closing this issue as partially answered, and partially moved to babel/babel-polyfills#11 (which is the repo where we'll implement polyfills innovations)

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 18, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: enhancement i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

4 participants