Webpack fails in production mode while running uglifyjs-webpack-plugin

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

What is the current behavior?
Execution fails in production mode while using minimiser (aka uglifyjs-webpack-plugin). If I use the default one (not specifying any option in optimization.minimizer), bundling fails with no error information:

ERROR in bundle-prod.js from UglifyJs
undefined

If I specify the latest version of uglifyjs-webpack-plugin like this:

optimization: {
    minimizer: [new UglifyJsPlugin()]
},

It fails with:

ERROR in bundle-prod.js from UglifyJs
TypeError: Cannot read property 'replace' of undefined
    at make_string (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:5645:19)
    at encode_string (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:5689:19)
    at Object.print_string (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6107:27)
    at eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6396:16)
    at doit (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6168:13)
    at AST_Directive.eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6175:13)
    at AST_Directive.AST_Node.print (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:5425:18)
    at eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6438:22)
    at Array.forEach (<anonymous>)
    at display_body (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6429:14)
    at eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6478:17)
    at eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:5923:19)
    at OutputStream.with_indent (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:5888:38)
    at Object.with_block (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:5922:9)
    at print_bracketed (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6477:20)
    at AST_Function.eval [as _do_print] (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6597:9)
    at eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6600:14)
    at doit (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6168:13)
    at AST_Function.eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6175:13)
    at AST_Function.AST_Node.print (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:5425:18)
    at eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:7111:21)
    at Array.forEach (<anonymous>)
    at eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:7109:15)
    at Object.with_square (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:5942:19)
    at eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:7106:16)
    at doit (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6168:13)
    at AST_Array.eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6175:13)
    at AST_Array.AST_Node.print (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:5425:18)
    at eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6992:22)
    at Array.forEach (<anonymous>)
    at eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6990:23)
    at Object.with_parens (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:5934:19)
    at eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6989:16)
    at doit (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6168:13)
    at AST_Call.eval (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:6175:13)
    at AST_Call.AST_Node.print (eval at <anonymous> (D:\natha\Documents\WebstormProjects\aziende-nostrane-ticino\ant-api\node_modules\uglify-es\tools\node.js:21:1), <anonymous>:5425:18)

If the current behavior is a bug, please provide the steps to reproduce.
Run webpack in production mode using the following configuration:

// webpack.common.js
module.exports = {
    entry: './src/bin/www.ts',
    target: 'node',
    node: {
        __filename: true,
        __dirname: true
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        modules: ['node_modules'],
        extensions: ['.tsx', '.ts', '.js']
    },
    externals: ['pg-hstore', 'tedious']
};
// webpack.prod.js
const merge = require('webpack-merge');
const common = require('./webpack.common');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const Dotenv = require('dotenv-webpack');
const path = require('path');

module.exports = merge(common, {
    mode: "production",
    devtool: false,
    plugins: [
        new Dotenv({
            path: './.env.prod'
        })
    ],
    optimization: {
        minimizer: [new UglifyJsPlugin({sourceMap: true})]
    },
    output: {
        filename: 'bundle-prod.js',
        path: path.resolve(__dirname, 'dist')
    }
});

Not sure if it has anything to do (maybe a problematic dependency…) but here is my package.json:

{
  "name": "ant-api",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./dist/bundle.js",
    "watch": "webpack --config webpack.dev.js",
    "build": "webpack --config webpack.prod.js",
    "test": "jasmine"
  },
  "dependencies": {
    "bcrypt": "^3.0.0",
    "bluebird": "^3.5.1",
    "cookie-parser": "~1.4.3",
    "cors": "^2.8.4",
    "debug": "^3.1.0",
    "dotenv": "^6.0.0",
    "dotenv-webpack": "^1.5.7",
    "express": "~4.16.0",
    "express-auth-parser": "^0.1.2",
    "express-fileupload": "^0.4.0",
    "express-jwt": "^5.3.1",
    "js-sha256": "^0.9.0",
    "jsonwebtoken": "^8.3.0",
    "moment": "latest",
    "morgan": "~1.9.0",
    "mysql2": "^1.6.1",
    "nyc": "^12.0.2",
    "sequelize": "^4.38.0",
    "winston": "^3.0.0"
  },
  "devDependencies": {
    "@types/bcrypt": "^2.0.0",
    "@types/chai": "^4.1.4",
    "@types/cookie-parser": "^1.4.1",
    "@types/cors": "^2.8.4",
    "@types/debug": "0.0.30",
    "@types/dotenv": "^4.0.3",
    "@types/express-fileupload": "^0.4.0",
    "@types/express-jwt": "0.0.40",
    "@types/jsonwebtoken": "^7.2.8",
    "@types/mocha": "^5.2.5",
    "@types/morgan": "^1.7.35",
    "@types/passport-http": "^0.3.6",
    "@types/sequelize": "^4.27.24",
    "@types/source-map-support": "^0.4.1",
    "@types/supertest": "^2.0.5",
    "aws-sdk": "^2.295.0",
    "chai": "^4.1.2",
    "chai-exclude": "^1.0.9",
    "chai-shallow-deep-equal": "^1.4.6",
    "mocha": "^5.2.0",
    "source-map-support": "^0.5.8",
    "sqlite3": "^4.0.2",
    "supertest": "^3.1.0",
    "ts-loader": "^4.4.2",
    "typescript": "^3.0.1",
    "uglifyjs-webpack-plugin": "^1.3.0",
    "webpack": "^4.16.5",
    "webpack-cli": "^3.1.0",
    "webpack-merge": "^4.1.4",
    "webpack-node-externals": "^1.7.2"
  }
}

Example repository
Clone the following repository:
https://github.com/nathanlepori/webpack-uglifyjs-test
Follow instruction in the README.md file.
What is the expected behavior?
Should create the bundle without failing (duh…)

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.

  • No browser involved
  • Node v8.11.4
  • Webpack v4.16.5
  • OS Windows 10

Author: Fantashit

2 thoughts on “Webpack fails in production mode while running uglifyjs-webpack-plugin

  1. The next major webpack release will use terser-webpack-plugin by default. We can’t “fix” it in v4 because this could be a breaking change. If you are affected by this bug, please specify terser-webpack-plugin in config.

Comments are closed.