vue 移除 console.log

js yekong 675℃
npm install uglifyjs-webpack-plugin --save-dev

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


config.plugins.push(
  new UglifyJsPlugin({
    uglifyOptions: {
      compress: {
        drop_debugger: true, // 注释console
        drop_console: true,
        pure_funcs: ['console.log'] // 移除console
      },
    },
    sourceMap: false,
    parallel: true,
  }),
)



完整代码
const _Ip = 'http://new.wanjunshijie.com/'
const webpack = require('webpack')
const isProduction = process.env.NODE_ENV === 'production'
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const cdn = {
  css: [],
  js: [
    'https://cdn.bootcss.com/vue/2.5.17/vue.runtime.min.js',
    'https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js',
    'https://cdn.bootcss.com/vuex/3.0.1/vuex.min.js',
    'https://cdn.bootcss.com/axios/0.18.0/axios.min.js',
  ]
}


const CompressionWebpackPlugin = require('compression-webpack-plugin')
module.exports = {
  productionSourceMap: false,
  chainWebpack: config => {
    config.module.rules.delete('eslint')
    // 生产环境配置
    if (isProduction) {
      // 生产环境注入cdn
      config.plugin('html')
        .tap(args => {
          args[0].cdn = cdn
          return args
        })
    }
  },
  configureWebpack: config => {
    if (isProduction) {
      config.plugins.push(
        new UglifyJsPlugin({
          uglifyOptions: {
            compress: {
              drop_debugger: true, // 注释console
              drop_console: true,
              pure_funcs: ['console.log'] // 移除console
            },
          },
          sourceMap: false,
          parallel: true,
        }),
      )
      // 用cdn方式引入
      config.externals = {
        'vue': 'Vue',
        'vuex': 'Vuex',
        'vue-router': 'VueRouter',
        'axios': 'axios'
      }
      return {
        plugins: [new CompressionWebpackPlugin({
          test: /\.(js|css)(\?.*)?$/i,
          threshold: 10240,
          deleteOriginalAssets: false
        })]
      }
    }
  },
  devServer: {
    disableHostCheck: true,
    port: 8082,
    proxy: {
      '/ajax': {
        target: _Ip,
        changeOrigin: true,
        pathRewrite: {
          '^/ajax': '/ajax' //代理的路径
        }
      },
      '/ajaxtools': {
        target: _Ip,
        changeOrigin: true,
        pathRewrite: {
          '^/ajaxtools': '/ajaxtools' //代理的路径
        }
      },
      '/Admin': {
        target: _Ip,
        changeOrigin: true,
        pathRewrite: {
          '^/Admin': '/Admin' //代理的路径
        }
      },
    },
  },
}
喜欢 (0)