js项目生产环境中移除 console

1、terser-webpack-plugin

webpack 构建的项目中安装使用

安装:
npm install terser-webpack-plugin --save-dev

配置

在webpack.config.js文件中

复制代码
new TerserPlugin({
  terserOptions: {
    output: {
      comments: false, // 去除注释
    },
    warnings: false, // 去除黄色警告,
    compress: {
      drop_console: true,
      drop_debugger: true, // 特定情况需要利用debugger防止调试
      pure_funcs: ['console.log'], // 移除console.log 避免console.error
    },
  },
}),

2、

babel-plugin-transform-remove-console

安装
npm install babel-plugin-transform-remove-console --save-dev

在babel.config.js文件中加入配置

复制代码
module.exports = {
  plugins: [
    'transform-remove-console',
  ],
};

如果只想在生产环境中使用,可以改成:

复制代码
const prodPlugins = [];
if (process.en.NODE_ENV === 'production') {
	prodPlugins.push('transform-remove-console');
}
module.exports = {
  plugins: [
     ...prodPlugins
  ],
};
相关推荐
星哥说事3 分钟前
跨平台开源笔记神器,用DeepSeek写笔记 , 效率翻倍
前端
喜欢你,还有大家31 分钟前
FTP文件传输服务
linux·运维·服务器·前端
该用户已不存在35 分钟前
你没有听说过的7个Windows开发必备工具
前端·windows·后端
Bi1 小时前
Dokploy安装和部署项目流程
运维·前端
普通网友1 小时前
前端安全攻防:XSS, CSRF 等防范与检测
前端·安全·xss
携欢1 小时前
PortSwigger靶场之Reflected XSS into attribute with angle brackets HTML-encoded通关秘籍
前端·xss
小爱同学_1 小时前
React知识:useState和useRef的使用
前端·react.js
再学一点就睡1 小时前
双 Token 认证机制:从原理到实践的完整实现
前端·javascript·后端
wallflower20201 小时前
滑动窗口算法在前端开发中的探索与应用
前端·算法
蚂蚁绊大象1 小时前
flutter第二话题-布局约束
前端