开发者

How to remove all console.log statements from your minified or combined javascript file

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 al开发者_JAVA技巧l console.log statements from my codes in minified files. Please suggest me how to do this.


Why not just do this:

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

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


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...


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜