问题
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:
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
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
回答1:
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)
}
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')
回答2:
Add this to index.js
if (process.env.NODE_ENV !== 'development') {
console.log = () => {}
}
Note that this will only suppress the messages, it will not strip them from your deployed code.
References: How to quickly and conveniently disable all console.log statements in my code?
来源:https://stackoverflow.com/questions/47839311/removing-log-statements-in-create-react-app-without-ejecting