Search code examples
javascriptpluginsbabeljspolyfills

Should you polyfill plugins or leave it up to the implementation?


I've had an issue raised on a Vue.js plugin of mine asking to bundle a polyfill for Object.assign in the plugin for IE10. I'm wondering if this is generally best practice or not for such things.

It's easy enough, but would add unnecessary overhead in the plugin for browsers that have native support. Is it better to leave this up to the implementation to require such polyfills?


Solution

  • In my opinion, you shouldn't ship a plugin with anything more than it needs to run. Just because one user's app requires IE10 support, doesn't mean every user's does.

    The only case I could see for including a polyfill would be if Vue itself took a hard stance on supporting IE10, creating the expectation that Vue plugins also follow that. In most cases, though, if the user of your plugin needs a polyfill, there shouldn't be any reason why they can't add it to their project themselves.

    Regarding your comment about polyfills in browsers that already natively support it, it's generally best-practice to check if the browser has native support before applying a polyfill, so the plugin just would do nothing unless it's required. Example:

    if (typeof Object.assign !== 'function') {
      Object.assign = myPolyfill
    }