vue项目打包时去除所有console(webpack)

vue项目在打包时去除所有console,需在vue.config.js加上以下配置

vite项目打包删除console.log

第一步:由于vite是没有集成terser插件的 需要先安装

// 安装
npm install terser --save-dev

第二步:在vite.config.js中编写

// 安装
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue'
export default defineConfig({
   plugins: [vue()],
   build:{
      minify:"terser",
      terserOptions:{
        compress:{
          drop_console:true,
          drop_debugger:true
        }
      }
    }
});
   

第三部:npm run build

vue3

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
    devServer: {
        port: 8085,
    },
    // 打包时去除console
    terser: {
        terserOptions: {
            compress: {
                drop_console: true,
                drop_debugger:true
            }
        }
    }
})

vue2项目

使用之前先检查node_modules目录中是否有terser-webpack-plugin,有就不需安装了

webpack4的安装方式

npm install terser-webpack-plugin --save-dev@4.2.3

webpack5的安装方式

npm install terser-webpack-plugin --save-dev
const TerserPlugin = require('terser-webpack-plugin')
module.exports = {
    configureWebpack: {
        // 去除页面所有console
        optimization: {
            minimizer: [new TerserPlugin(
                {terserOptions:{
                    compress:{warnings: false, drop_console: true, drop_debugger: true, pure_funcs: ["console.log"]}}
                }
            )],
        },
    },
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值