terser-webpack-plugin的使用:删除注释和console

场景:扫描漏洞的时候,nodel_modules中有的插件的版本注释会视为漏洞,需要删除这些注释

注意:webpack4中用到terser-webpack-plugin压缩插件,不能使用最新的版本,而是terser-webpack-plugin@4.x版本;webpack5对应的使用terser-webpack-plugin@5.x版本,否则会报错误:Error: TypeError: Cannot read property ‘javascript‘ of undefined。如下图所示:

 terser-webpack-plugin的使用

①安装插件(我用的webpack4

npm i terser-webpack-plugin@4.2.3 -D

②vue.config.js中的配置


const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
  configureWebpack: {
    optimization: {
      minimize: process.env.NODE_ENV === 'production'?true:false,//为了不影响dev时的构建速度
      minimizer: [
        new TerserPlugin({
          parallel: true,//使用多进程并发运行以提高构建速度 Boolean|Number 默认值: true  
          terserOptions: {
            compress: {
              drop_console: true,//移除所有console相关代码;
              drop_debugger: true,//移除自动断点功能;
              pure_funcs: ["console.log", "console.error"],//配置移除指定的指令,如console.log,alert等
            },
            format: {
              comments: false,//删除注释
            },
          },
          extractComments: false,//是否将注释剥离到单独的文件中
        })
      ]
    }
  },
 
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值