每天学习一个vue插件(26)——uglifyjs-webpack-plugin

1,331 阅读2分钟

和你们这些少爷不同,我们光是活着就竭尽全力了

前言

1.介绍

options 选项

uglifyOptions 压缩器

{
  // 不显示警告信息
  warnings: false,
  compress: {
    // 移除 debugger
    drop_debugger: true,
    // 移除console.*函数
    drop_console: true,
    // 移除console.log的引用
    // 例如 log = console.log, 移除log,同时移除console.log
    pure_funcs: ['console.log']
  }
}

parallel 多进程

// 使用多进程压缩
parallel:true

cache 缓存

// 使用缓存
cache:true

extractComments 注释

// 保留注释
// 单独抽取一个文件 [name].[hash].[ext].LICENSE
extractComments: true

使用

安装

npm i uglifyjs-webpack-plugin -D

配置

config
.plugin('uglifyJs')
.use(UglifyJsPlugin, [
  {
    uglifyOptions: {
      warnings: false,
      compress: {
        // 移除 debugger
        drop_debugger: true,
        // 移除console.*函数
        drop_console: true,
        // 移除console.log的引用
        // 例如 log = console.log, 移除log,同时移除console.log
        pure_funcs: ['console.log']
      }
    },
    // 多进程并行运行
    parallel: true,
    // 启用缓存
    cache: true,
    // 抽取注释
    extractComments: true
  }
])

3.注意

1.dev环境依然保留console,prod环境没有console
2.warnings配置项与compress同级

尾声

今天有好几次,我差点以为遇见了你,想你了~

晚安 ^_^

参考链接

往期回顾