vue-cli 3.0 build后 如何在vue.config.js怎么配置去掉console.log

阅读 11.6k
6 个回答

楼主试试这个

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

 configureWebpack: config => {
        if (process.env.NODE_ENV === "production") {
            // 为生产环境修改配置...
            config.mode = "production";
            // 这里修改下 
-            config.optimization.minimizer[
+             config.optimization.minimizer = [
                new UglifyJsPlugin({
                    uglifyOptions: {
                        compress: {
                            warnings: false,
                            drop_console: true, //console
                            drop_debugger: true,
                            pure_funcs: ['console.log'] //移除console
                        }
                    }
                })
            ]

        } else {
            // 为开发环境修改配置...
            config.mode = "development";
        }
 }
  config.optimization.minimizer([
    new UglifyJsPlugin({
      uglifyOptions: {
        compress: {
          warnings: false,
          drop_console: true, // console
          pure_funcs: ['console.log'] // 移除console
        }
      }
    })
  ])

参考https://github.com/vuejs/vue-...
尝试将你的代码new UglifyJsPlugin(...)部分改成下面的

const TerserPlugin = require('terser-webpack-plugin')
module.exports = {
    configureWebpack({
        optimization: {
            minimizer: [
                new TerserPlugin({
                    terserOptions: {
                        compress: { drop_console: true }
                    }
                })
            ]
        }
    })
}

module.exports = {
  // Project deployment base
  // By default we assume your app will be deployed at the root of a domain,
  // e.g. https://www.my-app.com/
  // If your app is deployed at a sub-path, you will need to specify that
  // sub-path here. For example, if your app is deployed at
  // https://www.foobar.com/my-app/
  // then change this to '/my-app/'
  //baseUrl: BASE_URL,
  //baseUrl写法过期了
  publicPath: BASE_URL,
  // tweak internal webpack configuration.
  // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
  // 如果你不需要使用eslint,把lintOnSave设为false即可
  lintOnSave: false,
  chainWebpack: config => {
    config.resolve.alias
      .set('@', resolve('src')) // key,value自行定义,比如.set('@@', resolve('src/components'))
      .set('_c', resolve('src/components'))
      .set('vue$','vue/dist/vue.esm.js')
    if (process.env.NODE_ENV === 'production') {
      // 为生产环境修改配置...
      // 用来打包删除所有config以及debugger,true打开
      config.optimization
        .minimizer[
          new UglifyJsPlugin({
            uglifyOptions: {
              compress: {
                warnings: false,
                drop_console: true, // console
                drop_debugger: false,
                pure_funcs: ['console.log']// 移除console
              }
            }
          })
        ]
    } else {
      // 为开发环境修改配置...
    }
  },
  // 打包时不生成.map文件
  productionSourceMap: false
  // 这里写你调用接口的基础路径,来解决跨域,如果设置了代理,那你本地开发环境的axios的baseUrl要写为 '' ,即空字符串
  // devServer: {
  //   proxy: 'localhost:3000'
  // }

}

各位大神 我是在vue.config.js 这么写的:

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

 configureWebpack: config => {
        if (process.env.NODE_ENV === "production") {
            // 为生产环境修改配置...
            config.mode = "production";
            config.optimization.minimizer[
                new UglifyJsPlugin({
                    uglifyOptions: {
                        compress: {
                            warnings: false,
                            drop_console: true, //console
                            drop_debugger: true,
                            pure_funcs: ['console.log'] //移除console
                        }
                    }
                })
            ]

        } else {
            // 为开发环境修改配置...
            config.mode = "development";
        }
 }

打包之后不生效啊 。。。

configureWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
    }
  }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏