Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding babel-polyfill to Webpack 4 configuration file

I want to add babel-polyfill to my webpack.config.js. Babel documentation shows how to do that:

module.exports = {
  entry: ["babel-polyfill", "./app/js"]
};

However, in my webpack.config.js I'm using more than only one entry point:

module.exports = {
    entry: {
    'js/bundle': './src/scripts/index', // generates bundle.js inside dist/js folder
    'js/another': './src/scripts/another', // generates another.js inside dist/js folder
  },
}

It generates two different bundles inside dist/js folder. But now, I don't know how to add the babel-polyfill to my entry.

I tried this:

  entry: [
    babel-polyfill,
    {
      'js/bundle': './src/scripts/index', // generates bundle.js inside dist/js folder
      'js/another': './src/scripts/another', // generates another.js inside dist/js folder
    },
  ]

but then when I try to build, I get this error: SyntaxError: Unexpected identifier

Any help?

like image 837
user1941537 Avatar asked Nov 30 '25 19:11

user1941537


1 Answers

I found this thread because array forEach wasn't being transpiled when I was using the Babel loader alone, IE 11 was complaining. All I did was npm install babel-polyfill, then in my main js file I require('babel-polyfill'). Actually I didn't need to modify my webpack config. After installing babel-polyfill and requiring it, successful transpilation of forEach

like image 99
Ted Fitzpatrick Avatar answered Dec 02 '25 12:12

Ted Fitzpatrick



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!