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

Do I still need this package to polyfill ? #60

Open
qiulang opened this issue Feb 9, 2021 · 6 comments
Open

Do I still need this package to polyfill ? #60

qiulang opened this issue Feb 9, 2021 · 6 comments

Comments

@qiulang
Copy link

qiulang commented Feb 9, 2021

Hi
I see Rethink polyfilling story was closed because of this project.
But I read here https://babeljs.io/docs/en/babel-polyfill/ "As of Babel 7.4.0, this package has been deprecated"

So my question is I do not need this package to do polyfill with the latest @babel/core (I use version "7.12.13" ), right ?

But since this package was deprecated what is latest document about polyfill using babel ? How much information in Rethink polyfilling story is invalid now ?

@nicolo-ribaudo
Copy link
Member

@babel/polyfill has nothing to do with this. This repository is a collection of Babel plugins to inject imports to other polyfills.

@qiulang
Copy link
Author

qiulang commented Feb 9, 2021

Oh thanks. I had thought @babel/polyfill was the result of this project. lol
So I am a library author and according to the readme I should use @babel/plugin-transform-runtime because @babel/preset-env's useBuiltIns: "usage" will pollute the global scope.

But I just need to polyfill a specific api (Promise.any to be exact) what is the problem of useBuiltIns: "usage" ? Even I use core-js's Promise.any function, what is the problem it will cause ? I still don't understand.

@qiulang
Copy link
Author

qiulang commented Feb 9, 2021

The readme also said "These packages are highly experimental and they have not been tested in production applications yet.”
I am rather confused with those words. So what is the "proper" way to polyfill in in production applications then ?

@ljharb
Copy link
Member

ljharb commented Feb 9, 2021

@qiulang imo the best practice for package authors is not to rely on polyfills when possible. For Promise.any, I'd suggest depending on https://npmjs.com/promise.any and using that directly. In environments where it's natively available, it will use the native one.

@agilgur5
Copy link
Contributor

agilgur5 commented Mar 2, 2022

Might be necro'ing, but as a long-time user, thought I'd answer too since this wasn't closed and OP didn't quite respond.

The readme also said "These packages are highly experimental and they have not been tested in production applications yet.”

I've used them in multiple production applications and libraries at this point 🙂

So I am a library author and according to the readme I should use @babel/plugin-transform-runtime because @babel/preset-env's useBuiltIns: "usage" will pollute the global scope.

But I just need to polyfill a specific api (Promise.any to be exact) what is the problem of useBuiltIns: "usage" ?

preset-env's useBuiltIns: "usage" is not pure, so for a library author that is not ideal as it will cause (potentially unintended and hard to detect) side-effects when your library is used.

transform-runtime was pure, so good for library authors, but didn't support targets like preset-env did.

The collection of plugins here allow you to get the best of both worlds as a library author, as written in the History and Motivation section of the README.

So what is the "proper" way to polyfill in in production applications then ?

As an application author, one could just use preset-env on its own as polluting scope isn't as much a potential problem, but the packages (and general methodology) here also allow you to use an alternative polyfill other than core-js as well, instead of forcing core-js usage. That's mentioned in the above section of the README as well.

As a library author, one could use targets with usage-pure here to get the "best of both worlds", such as:

{
  "targets": { "firefox": 42 },
  "presets": ["@babel/preset-env"],
  "plugins": [
    ["polyfill-corejs3", {
      "method": "usage-pure"
    }]
  ]
}

The Migration docs offer various examples of different use-cases when moving from preset-env's useBuiltIns to the plugins here.

@nicolo-ribaudo
Copy link
Member

These packages are used in preset-env too, so they have been battle-tested. We should probably remove the "highly experimental" notice!

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

No branches or pull requests

4 participants