likes
comments
collection
share

vue打包时去除console信息

作者站长头像
站长
· 阅读数 51
  1. 相关网址(1)npm官网地址:https://www.npmjs.com/package...(2)webpack官网地址:https://webpack.html.cn/plugi...
  2. npm下载uglifyjs-webpack-plugin

    npm install uglifyjs-webpack-plugin --save-dev
  3. 在build/webpack.prod.conf.js中添加以下代码

    const UglifyJsPlugin = require("uglifyjs-webpack-plugin")
    module.exports = {
    // ...
    configureWebpack: (config) => {
      if (process.env.NODE_ENV === 'production') {
     // 为生产环境修改配置...
     config.mode = 'production'~~~~
     let optimization = {
      // ...
       minimizer: [
         new UglifyJsPlugin({
           uglifyOptions: {
             warnings: false,
             compress: {
               drop_console: true, // 放弃对`console.*`函数的调用(删除console)
               drop_debugger: false, // 删除debugger
               pure_funcs: ['console.log'] 
             }
           }
         })
       ]
     }
     Object.assign(config, {
       optimization
     })
      } else {
     // 为开发环境修改配置...
     config.mode = 'development'
      }
     // ...
    },
    // ...
    }
转载自:https://segmentfault.com/a/1190000039792657
评论
请登录