Tag Archives: UglifyJsPlugin

vue.config.js build Package UglifyJsPlugin to clear console and print console.log Error: `warnings` is not a supported option

We want to log out all the printing functions. It is certainly unrealistic to log out one by one. It is a waste of energy and time, and there will be omissions

Here we implement it with the help of uglifyjs-webpack-plugin

install

cnpm i uglifyjs-webpack-plugin --save-dev

vue.config.js

// vue.config.js

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
...
configureWebpack: config => {
    if (process.env.NODE_ENV == 'production') {
    
      config.mode = 'production'
      config.optimization.minimizer = [
        new UglifyJsPlugin({
          uglifyOptions: {
			output: {
				comments: false
			},
			// deleteconsole debugger
			compress: {     
				warnings: false,	//	error
				drop_console: true, // console
				drop_debugger: false,
				pure_funcs: ['console.log']// 移除console
			}
          }
        })
      ]
    } else {
      config.mode = 'development'
    }
   }


...

}

warnings is not a supported option

Here is a knowledge point. Warnings will report errors.

Some UglifyJsPlugin versions do not support setting the warnings parameter in the compress of the uglifyOptions object, but directly set warnings as a property in the uglifyOptions object.

If an error is reported. Then write warnings as the uglifyOptions attribute in the following way

// vue.config.js

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
...
configureWebpack: config => {
    if (process.env.NODE_ENV == 'production') {
    
      config.mode = 'production'
      config.optimization.minimizer = [
        new UglifyJsPlugin({
          uglifyOptions: {
			output: {
				comments: false
			},
			// delete console debugger
			compress: {     
				drop_console: true, // console
				drop_debugger: false,
				pure_funcs: ['console.log']// remove console
			}, 
			warnings: false,	//	error
          }
        })
      ]
    } else {
      config.mode = 'development'
    }
   }


...

}