-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Need option to remove console.* #28
Comments
This is possible today with the DEBUG && console.log('debug-only log'); With a debug build using console.log('debug-only log'); With a release build using I will consider adding a feature lets you remove calls to global functions. It looks like other bundlers handle this by marking certain functions as "pure", which means they don't have any side effects. Then the optimizer will remove them. Any side effects that the arguments have will still happen, however. |
So it means i have to update my code to add a DEBUG test before each console.* ? |
@evanw must it be that precise syntax or do you allow for something like |
Yes, they all work. DEBUG && console.log(1)
if (DEBUG) console.log(2)
if (DEBUG && Foo) console.log(3) All three statements above will be removed with console.log(1), console.log(2), Foo && console.log(3); What happens is that |
That's... that's so simple and brilliant! General feature flagging... this is legitimately inspiring me right now. It would also eliminate optional chains like |
Yes, you could also do that. I suppose that means you could write I just checked what esbuild does and it looks like esbuild's dead code elimination for optional chains only works if you set |
Evan just a quick follow up - what happens when there's a following 'else' statement? For example: |
Constructs such as |
With version 0.5.22 you can now do |
Hello. I am puzzled as to why the minifier is not stripping this code: const DEBUG = window.DEBUG;
if (DEBUG) {
console.log('Strip me');
} when built with the options: {
define: { 'window.DEBUG': false },
minify: true
} The odd thing is those same options will happily strip this: if (window.DEBUG) {
console.log('Strip me');
} The reason I want to do this is I like to write code that will run in the browser before and after building, and it is useful to be able to do something like this at the top of a file: const DEBUG = window.DEBUG !== false; Which allows pre-build debug code to run where window.DEBUG has not been set. I could put that into every condition... if (window.DEBUG !== false) {
console.log('Strip me');
} ... but it would be nice not to have to do that everywhere. |
No description provided.
The text was updated successfully, but these errors were encountered: