Search code examples
javascriptecmascript-6babeljsrollup

Trouble Transpiling startsWith() Using Babel and Rollup


I'm having unexpected trouble transpiling ES2015's startsWith using Rollup and Babel. I'm using babel-preset-env and have the following in my .babelrc:

{
   "presets": [
     ["env", {
       "targets": {
         "browsers": ["last 2 versions", "not ie >= 10"]
       }, 
       "debug": true
     }]
   ]
 }

My rollup.config.js is set up to respect my .babelrc, and I'm seeing that it's correctly outputting in my debugging information that it's respecting my browser targets. Still, in my bundled JS, I'm seeing startsWith untouched, with no sight of a polyfill anywhere.

What might I be doing wrong?


Solution

  • Babel only transpiles syntax (like let/const, arrow functions, classes, etc.) and not API methods (like .startsWith() or .includes()).

    For that, you'll need a polyfill, like babel-polyfill. However, if you only need the .startsWith function, a simpler polyfill will suffice. That, or you could simply make your own startsWith() function and use that.