Last active
September 16, 2021 09:19
-
-
Save urosjarc/aa531af1f18c804a8dd8953190c2bef7 to your computer and use it in GitHub Desktop.
This script will remove all console statements from js file and replace them with 0 (zero number).This script was tested on three.js.min and its working...This is my effort to minimize final output of minified js file even further for CEO purposes... (three.js.min => 617,8 kB new.three.js.min => 580,3 kB)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
This script will remove all console statements from js file and replace them with 0 (zero number). | |
This script was tested on three.js.min and its working... | |
This is my effort to minimize final output of minified js file even further for CEO purposes... | |
three.js.min => 617,8 kB | |
new.three.js.min => 580,3 kB | |
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) | |
''' | |
input_js_path = input("Enter input js file: ") | |
with open(input_js_path) as f: | |
text = f.read() | |
newText = '' | |
print("Number of consoles:", text.count('console.')) | |
searchWord = 'console.' | |
i=0 | |
count = 0 | |
run = True | |
removed = 0 | |
while True: | |
while searchWord != text[i:i+len(searchWord)]: | |
newText += text[i] | |
i+=1 | |
if i >= len(text): | |
run = False | |
break | |
if not run: | |
break | |
oklepaj=0 | |
j=i | |
word = '' | |
while True: | |
if '(' == text[j]: | |
oklepaj+=1 | |
elif ')' == text[j]: | |
oklepaj-=1 | |
if oklepaj == 0: | |
count+=1 | |
removed+=len(text[i:j+1]) | |
print('Removed:', text[i:j+1]) | |
newText+='0' | |
i=j+1 | |
break | |
j+=1 | |
with open(f'new.{input_js_path}.js', 'w') as f: | |
f.write(newText) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment