website icon indicating copy to clipboard operation
website copied to clipboard

Need an example babel.config.js w/ how to achieve pollution-free polyfills via core-js3

Open JessicaSachs opened this issue 6 years ago • 1 comments

I was very excited to use core-js3 when I found out that it supported pollution-free polyfills for String.prototype.includes + other instance methods.

According to the babel documentation and release notes, pollution-free polyfills are one of the major value-adds that core-js3 brings. We need to link from the docs to a howto page that explains the feature.

Things that were not clear when trying to achieve pollution-free polyfills:

  • They are not the default for core-js3
  • What the minimal packages required are
  • What a minimal babel config looks like

When I posted on Babel's Slack, someone sent me the release notes for core-js@3, which mentioned 3-4 distinct packages (one of which doesn't exist).

Eventually, with 2 days of googling and searching for "pollution" and "pollute" throughout core-js and babel docs, I came up with this answer:

module.exports = {
    "presets": [
        "@babel/preset-env"
    ],
    /**
     * To add non-polluting polyfills for instance method transforms (String.prototype.includes, etc)
     * `npm i -D @babel/plugin-transform-runtime`
     * and pass in { corejs: 3 } to the options below...
     */
    "plugins": [
        ["@babel/plugin-transform-runtime", {
            "corejs": 3
        }]
    ]
}

I'm still very grateful for the ability to transform instance methods without polluting the global scope. It works seamlessly once it's configured. Just want to make the information on how to get it done more accessible :-)

JessicaSachs avatar Oct 30 '19 21:10 JessicaSachs

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

babel-bot avatar Oct 30 '19 21:10 babel-bot