Search code examples
create-react-appuglifyjs

Removing log statements in Create React App without ejecting


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


Solution

  • 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')