17

After running my code through webpack it contians arrow functions. I need the code to work in ie11 so I need to get rid of the arrow functions.

I'm using babel-loader for all .js files.

I wrote a loader to check code for arrow functions and ran it after the babel-loader and didn't get any arrow functions, so I know the output from babel is good.

I've also tried babel-polyfill and the babel plugin for transforming arrow funtions.

As I know the babel-loader outputs good code I suspect it might be a plugin, but I can't just disable them to test as that breaks the build.

Webpack plugins used in dev:

  plugins: [
    new webpack.DefinePlugin({
      'process.env': require('../config/dev.env')
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
    new webpack.NoEmitOnErrorsPlugin(),
    // https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
      inject: true
    }),
    // copy custom static assets
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, '../static'),
        to: config.dev.assetsSubDirectory,
        ignore: ['.*']
      }
    ])
  ]

The problem also appears in prod as well, but fixing it in dev should tell me how to fix it in prod as well.

I don't know of anywhere else the arrow function could be coming from, so I expect to, in essence, get code that works on ie11, but there's arrow functions coming from somewhere so it doesn't work.

It's not my code, so I can't just post it all. I can, however, post relevant snippets, but I don't know where the problem is so I don't know what's relevant yet.

2 Answers 2

33

I had the same problem and found the cause and solution.

Cause

babel-loader converts the grammar of es6 and higher to es5. However, because the conversion is done by the loader, the conversion occurs only in each file before it is bundled. After the loader completes the conversion, webpack starts to bundle. However, webpack does not care about target version of babel-loader when it bundles files. It just bundles file with grammar of it's default ECMA version(which could be es6 or later). It was the reason why bundled result includes es6 grammar such as arrow function.

Initial Step

  • file1 (es6)
  • file2 (es6)
  • file3 (es6)

After loader works

  • file1' (es5)
  • file2' (es5)
  • file3' (es5)

After webpack bundles files

  • bundled file (es6)

Solution

You can just simply add target: "es5" in webpack.config.js to handle this. After that, webpack bundles file in grammar of es5

// .babelrc
{
  "presets": ["@babel/preset-env"]
}
// webpack.config.js
module: {
   ...
   target: "es5", // include this!!
   loaders: [
     {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
     }
  ]
}

In webpack 5:

module.exports = {
    target: ['web', 'es5']
}

target is positioned at the root of the config schema and needs to know whether it targets a node or web environment

References:

3
  • This was extremely helpful - I just spent about 6 hours trying to figure out why Babel was including arrow functions with @babel/preset-env, and this answer finally made me realize it was a Webpack problem, not a Babel problem. Thank you!
    – Joseph
    Feb 22, 2021 at 0:47
  • 1
    TL;DR ... babel is doing the right thing, webpack is using arrow functions. Set target: "es5" as config option to webpack to make it do the right thing. Apr 3, 2021 at 8:59
  • The target property is not valid. Here's what happens when you add it and try to run Webpack: [webpack-cli] Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema. - configuration.module has an unknown property 'target'. These properties are valid: object { defaultRules?, exprContextCritical?, exprContextRecursive?, exprContextRegExp?... Jul 27, 2021 at 20:31
1

You can use babel. Since arrow functions comes with es6 , you can use babel to convert es5. Also this link could help to Webpack not converting ES6 to ES5.

Given below webpack config is what I used for babel.

module: {
   loaders: [
     {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        options: {
           babelrc: false,
           presets: ["@babel/preset-env", "@babel/preset-react","es2015"]
         }
     }
  ]
}     
7
  • Maybe I should have been more explicit, but I'm using babel, and it outputs correct code without arrow functions. Jan 4, 2019 at 13:19
  • "webpack.js.org/concepts/loaders/#loader-features" as written in the webpacks documentation, "Loaders can be chained. Each loader in the chain applies transformations to the processed resource. A chain is executed in reverse order. The first loader passes its result (resource with applied transformations) to the next one, and so forth. Finally, webpack expects JavaScript to be returned by the last loader in the chain." So maybe you can add babel to every loader chain you created. Therefore, whatever each loader returns it will be translated by babel.
    – ysnfrk
    Jan 4, 2019 at 17:18
  • 1
    @BjørnMoholt does your node modules transpiled by babel ? Maybe this is the problem.
    – ysnfrk
    Jan 4, 2019 at 17:34
  • Try to refer link below may help you to configure Webpack properly may help to solve the issue. medium.com/daily-js-tips/… Jan 7, 2019 at 3:10
  • @ysnfrk Good thinking! Tried it, didn't work. Took a while to compile though. Jan 7, 2019 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.