Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 692
Alex MacArthur Avatar asked Oct 21 '25 17:10

Alex MacArthur


1 Answers

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.

like image 195
kingdaro Avatar answered Oct 23 '25 07:10

kingdaro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!