5

Iam using Visual studio

Iam having javascript codes scattered in different files.

Currently iam using below link's compressor

http://yuicompressor.codeplex.com/

this generates output as combined+minified files on project build.

i am looking for some way to remove all console.log statements from my codes in minified files. Please suggest me how to do this.

Praveen Prasad
  • 31,561
  • 18
  • 73
  • 106
  • That won't help you, but ideally you would have a build script that only includes `console.log` in development mode. This could be done with e.g. ant and the C preprocessor (cpp). – Felix Kling Jan 06 '11 at 15:38
  • See also https://stackoverflow.com/questions/20092466/can-uglify-js-remove-the-console-log-statements – Radek Svítil Feb 15 '18 at 17:14

4 Answers4

1

Why not just do this:

window['console'] = { log: function() {} };

when you don't want the "console.log" statements to do anything?

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • iam looking for some way in visual studio, too get rid of unnecessary code. Rather than doing this. A build event that would merge all files and get rid of console thing. – Praveen Prasad Jan 06 '11 at 15:56
  • 1
    OK, well I'm not a Visual Studio expert, so I regret that I cannot help with that. – Pointy Jan 06 '11 at 15:59
0

Manually, you can use your editor, such as Visual Studio and with Search and Replace functionality use RegEx to remove all of your console logs.

With regex such as this:

console\.log(\((\").*\"\);+|\((\').*\'\);+)

You will be able to match all console.logs with " or ' wrapping strings.

Then you replace then with empty string.

You have to select g flag, or GLOBAL option or Replace All button.

Also consider, that console object has multiple methods, such as dir, table, group, etc...

Jakub Kriz
  • 1,501
  • 2
  • 21
  • 29
0

This buged me also because I wanted to optimize web page for CEO purposes, and I was using three.js which is huge file. I tried to minimized but still there was to much console statements in file.

So I created python script which replaces all console statements with 0. It was tested on three.js and its working like a charm.

  • 617,8kB three.js.min
  • 580,3kB new.three.js.min

Here is a gist...

https://gist.github.com/urosjarc/aa531af1f18c804a8dd8953190c2bef7

Here is example output...

▶ python3 rm_console_statements.py
Enter input js file: three.min.js
Number of consoles: 424
Removed: console.warn("THREE.MathUtils: .setQuaternionFromProperEuler() encountered an unknown order: "+r)
Removed: console.warn("THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.")
Removed: console.warn("THREE.Vector2: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.")
Removed: console.warn("THREE.Vector2: offset has been removed from .fromBufferAttribute().")
Removed: console.error("THREE.Matrix3: the constructor no longer reads arguments. use .set() instead.")
Removed: console.warn("THREE.ImageUtils.getDataURL: Image converted to jpg for performance reasons",t)
Removed: console.warn("THREE.Texture: Unable to serialize Texture.")
Removed: console.warn("THREE.Vector4: .add() now only accepts one argument. Use .addVectors( a, b ) instead.")
Removed: console.warn("THREE.Vector4: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.")
Removed: console.warn("THREE.Vector4: offset has been removed from .fromBufferAttribute().")
Removed: console.warn("THREE.Quaternion: Static .slerp() has been deprecated. Use qm.slerpQuaternions( qa, qb, t ) instead.")
Removed: console.warn("THREE.Quaternion: .setFromEuler() encountered an unknown order: "+s)
Removed: console.warn("THREE.Quaternion: .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead.")
Removed: console.warn("THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead.")
Removed: console.warn("THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.")
Removed: console.warn("THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.")
Removed: console.error("THREE.Vector3: .applyEuler() now expects an Euler rotation rather than a Vector3 and order.")
Removed: console.warn("THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.")
Removed: console.warn("THREE.Vector3: offset has been removed from .fromBufferAttribute().")
Removed: console.error("THREE.Matrix4: the constructor no longer reads arguments. use .set() instead.")
Removed: console.error("THREE.Matrix4: .makeRotationFromEuler() now expects a Euler rotation rather than a Vector3 and order.")
Removed: console.warn("THREE.Matrix4: .multiply() now only accepts one argument. Use .multiplyMatrices( a, b ) instead.")
Removed: console.warn("THREE.Matrix4: .makePerspective() has been redefined and has a new signature. Please check the docs.")
Removed: console.warn("THREE.Euler: .setFromRotationMatrix() encountered an unknown order: "+e)
Removed: console.error("THREE.Object3D.add: object can't be added as a child of itself.",t)
Removed: console.error("THREE.Object3D.add: object not an instance of THREE.Object3D.",t)
Removed: console.warn("THREE.Material: '"+e+"' parameter is undefined.")
Removed: console.warn("THREE."+this.type+": .shading has been removed. Use the boolean .flatShading instead.")
Removed: console.warn("THREE."+this.type+": '"+e+"' is not a property of this material.")
Removed: console.warn("THREE.Color: Alpha component of "+t+" will be ignored.")
Removed: console.warn("THREE.Color: Unknown color "+t)
Removed: console.warn("THREE.BufferAttribute.copyColorsArray(): color is undefined",i)
Removed: console.warn("THREE.BufferAttribute.copyVector2sArray(): vector is undefined",i)
Removed: console.warn("THREE.BufferAttribute.copyVector3sArray(): vector is undefined",i)
Removed: console.warn("THREE.BufferAttribute.copyVector4sArray(): vector is undefined",i)
0

If you’re using VSCode as your text editor, it’s easy to do this using Regex.

  1. Open the search option in the sidebar menu (or you can search through the specific files using the ctrl + F command.
  2. Type in console.log.*$
  3. Select the Use Regex option found in the right corner of the search panel that’s designated by the icon .*
  4. Press Find All to find all instances of console.log() and then press Replace All or console.log(([^)]+)); for multiple lines

Main article link: https://davidgarciasantes.medium.com/remove-all-console-log-using-regex-in-vscode-9fd76476fa0f