webpack5在生产环境屏蔽掉控制台打印 失效处理

常规是使用

javascript 复制代码
const TerserPlugin = require('terser-webpack-plugin')

const terserUglifyPlugin = new TerserPlugin({
  exclude: ['/node_modules/'],
  terserOptions: {
    parse: {},
    compress: {
      warnings: false,
      drop_console: true,
      drop_debugger: true
    },
    output: {
      comments: false,
      beautify: false
    },
    warnings: false
  }
})
const minimizer = process.env.NODE_ENV === 'production' ? [terserUglifyPlugin] : []

module.exports = defineConfig({
  lintOnSave: false,
  transpileDependencies: true,
  devServer: {
    client: {
      overlay: false,
    },
  },
  configureWebpack: {
    plugins: [
    ],
    optimization: {
      minimize: true,
      minimizer: minimizer
    },
    resolve: {
    }
  }
})

但按上面配置,打包部署后,控制台依然存在console

解决方法:

javascript 复制代码
 // 在configureWebpack 同级加上
  chainWebpack: (config) => {
    if (process.env.NODE_ENV === 'production') {
      config.optimization.minimizers.delete('terser'); // 删除默认的 TerserPlugin
    }
  },
相关推荐
Nueuis1 小时前
微信小程序前端面经
前端·微信小程序·小程序
_r0bin_3 小时前
前端面试准备-7
开发语言·前端·javascript·fetch·跨域·class
IT瘾君3 小时前
JavaWeb:前端工程化-Vue
前端·javascript·vue.js
zhang98800003 小时前
JavaScript 核心原理深度解析-不停留于表面的VUE等的使用!
开发语言·javascript·vue.js
potender3 小时前
前端框架Vue
前端·vue.js·前端框架
站在风口的猪11084 小时前
《前端面试题:CSS预处理器(Sass、Less等)》
前端·css·html·less·css3·sass·html5
程序员的世界你不懂4 小时前
(9)-Fiddler抓包-Fiddler如何设置捕获Https会话
前端·https·fiddler
MoFe14 小时前
【.net core】天地图坐标转换为高德地图坐标(WGS84 坐标转 GCJ02 坐标)
java·前端·.netcore
去旅行、在路上5 小时前
chrome使用手机调试触屏web
前端·chrome
Aphasia3115 小时前
模式验证库——zod
前端·react.js