Search code examples
polyfillsbabel-polyfill

Does core-js polyfill or does it always replace the function?


I have a react project that needs to support older browsers and we're using things like array.find() and array.map(). I see that our project already has core-js in the node_modules folder, so it would be nice to use it rather than adding another package to the project.

If I import like this:

import 'core-js/fn/array/find';

as was suggested in this response: http://discuss.babeljs.io/t/why-does-array-prototype-find-not-work-with-babel-preset-es2015/46/4 does that polyfill or will it replace the array.find function that may already exist? If it will replace the existing function is there any performance issues or other reasons why you wouldn't want to use this approach?


Solution

  • core-js always replaces the functions you import.

    There can be problems with that, that one I ran in to was trying to import the full library in code loaded by a framework that loads zone.js, which breaks zone.js by replacing the Promises that zone.js has already replaced.

    So now I use core-js to polyfill individual functions by doing things like this:

    if( ! Array.prototype.includes ) { require( "core-js/fn/array/includes" ); }