在不弹出的情况下在Create React App中删除日志语句 [英] Removing log statements in Create React App without ejecting

查看:87
本文介绍了在不弹出的情况下在Create React App中删除日志语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发Apps时,我使用了许多 console.log()语句(我的意思是很多)。我如何才能从
创建React应用
中不弹出 删除它们完全实现:

I use lots of console.log() statements while developing Apps (and I mean LOTS). How exactly can I remove them without "ejecting" from a Create React App What I considered but not sure how exactly to implement:

在我的package.json中:

In my package.json:

 "build": "react-scripts build && uglifyjs --compress drop_console build/static/js/main.xxxxx.js  -o build/static/js/main.xxxxx.js

但是,我怎么确切知道上的 hash 后缀main.js 文件,所以我可以调用此命令并以相同的文件名保存输出js文件

However How exactly can I know the hash suffix on the main.js file so I can call this command and save the output js file with same filename

推荐答案

如果只想禁止日志输出,则可以包装 console.log 并使用它

If you just want to suppress log output you could wrap console.log and use that instead

const log = (...msgs) => {
  if (process.env.NODE_ENV === 'development') console.log(...msgs)
}

您可以导入/导出此文件,但这听起来很痛苦。添加到 global

You can import / export this, but that sounds like a pain. Seems like a good thing to add to global

global.log = log

global.log('will only log in dev')

这篇关于在不弹出的情况下在Create React App中删除日志语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆