Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove console.log calls from production build #1491

Closed
edevil opened this issue Feb 6, 2017 · 6 comments
Closed

Remove console.log calls from production build #1491

edevil opened this issue Feb 6, 2017 · 6 comments

Comments

@edevil
Copy link

edevil commented Feb 6, 2017

Is there an option to remove all "console.log" calls when building the production artifact?

I know there are some webpack plugins that do this but I'm not sure I can use them without ejecting the project from this framework.

@gaearon
Copy link
Contributor

gaearon commented Feb 6, 2017

No, it’s not supported. You’d need to eject to do this automatically.

@edevil edevil closed this as completed Feb 6, 2017
@lany9527
Copy link

@glocore
Copy link

glocore commented Jan 29, 2018

I know this is not what this issue is about, but if you're only looking for a way to prevent the console messages from showing up in the production build, but don't mind that the code staying as is (meaning it won't be stripped out of your production code), then you could just monkey-patch console.log() so that it only works in development.

Or a more elegant way of doing it would be using the debug module, which lets you display messages based on the name of the module calling it. This requires you to add a field to your localStorage as described here. You can simply not add this field for production and the debug messages won't show. I ended up creating a utility class to generate color-coded messages of different levels such as 'trace', 'info', 'warn', 'error', etc. The module name I passed was simply the name of my app, so I just needed to add this in my index.js:

if(env.process.NODE_ENV !== 'production') localStorage.set('debug', 'name_of_app:*')

@parkerdan
Copy link

parkerdan commented Feb 7, 2018

I have disabled in prod by adding this at the root index.
Is this a bad approach??

function noop() {}

if (process.env.NODE_ENV !== 'development') {
  console.log = noop;
  console.warn = noop;
  console.error = noop;
}

@barbagallo
Copy link

barbagallo commented Feb 27, 2018

@parkerdan that's not a bad solution tbh, but it also probably depends on what you're doing.

If someone wanted to be malicious and scan your code for key pieces of data that you use to debug with (i.e. user data, account data..), that'll still leak into the final output and may be found by searching for "console.".

You could probably obfuscate usage of console by renaming it, and the methods, and then hooking them up to noop functions. Again, it prob depends on what you're trying to protect / hide.

Or you could use something like this: https://www.npmjs.com/package/webpack-strip

@behnammodi
Copy link

@gaearon Do you plan to add this item?

@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants