# terser-webpack-plugin- JS 压缩

# 1. 介绍

webpack 生产环境,默认会压缩 JS 文件。

但如果需要设置额外的压缩配置(如 删除 console.log 语句),则需要显式配置 JS 压缩插件。

# 2. 使用

安装:

npm i -D terser-webpack-plugin

使用:

const TerserPlugin = require('terser-webpack-plugin');

const IS_PROD = process.env.NODE_ENV === 'production';

module.exports = {
  // ...
  
  optimization: {
    minimize: IS_PROD,
    minimizer: [
      new CssMinimizerPlugin(),
      new TerserPlugin({
        terserOptions: {
          compress: {
            // false - 删除所有 console.* 语句
            // ['log', 'info'] - 删除 console.log 、console.info 语句             
            drop_console: ['log', 'info'],
          },
        },
      }),
    ],
  },
}

# 3. 参考

本章目录