UglifyJS fails with Unexpected Token when using babel-preset-env

Do you want to request a feature or report a bug?
Report a bug

What is the current behavior?
I was using webpack 3.8.1 with the UglifyJS plugin and babel-preset-es2015.
Then migrated to babel-preset-env but when the UglifyJS plugin is processing I get this error:

internal/streams/legacy.js:59
      throw er; // Unhandled stream error in pipe.
      ^
Error: game.min.js from UglifyJs
Unexpected token: name (is_electron) [game.min.js:30772,4]

If the current behavior is a bug, please provide the steps to reproduce.
Install the mentioned library versions and try to apply the webpack UglifyJS plugin with minimize: true (or false, it does not matter).

What is the expected behavior?
To work out of the box with babel presets. To not have to add extra configuration to webpack.
Adding a query to the module solves the issue, but a not advanced user will not know this:

      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        query: {
          presets: [
            require.resolve('babel-preset-env')
          ]
        }
      }

If this is a feature request, what is motivation or use case for changing the behavior?

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.
webpack 3.8.1
babel-cli 6.26.0
babel-loader 7.1.2
babel-preset-env 1.6.1
gulp 3.9.1
webpack-stream 4.0.0
.babelrc:

{
  "presets": [
    [
      "env",
      {
        "targets": {
          "chrome": "60"
        }
      }
    ]
  ]
}

Author: Fantashit

1 thought on “UglifyJS fails with Unexpected Token when using babel-preset-env

  1. @itsjavi The current version of uglifyjs-webpack-plugin (v0.4.6) supports ES5 only, so in case your targets for babel-preset-env allow some ES2015+ features, minification may fail. Install the latest version of uglifyjs-webpack-plugin which fully supports ES2015+ manually until webpack 4 ships with it by default.

    npm i -D uglifyjs-webpack-plugin
    

    webpack.config.js

    const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
    
    const config = {
       plugins: [
         new UglifyJSPlugin({ ...options, ...uglifyOptions })
       ]
    }

    ⚠️ See the README for options

Comments are closed.