Visual Studio 如何从您的小型或组合javascript文件中删除所有console.log语句

q0qdq0h2  于 2023-02-16  发布在  Java
关注(0)|答案(4)|浏览(151)

我正在使用Visual Studio
我有javascript代码分散在不同的文件。
目前我正在使用以下链接的压缩程序
http://yuicompressor.codeplex.com/
这将在项目构建时生成作为组合+缩小文件的输出。
我正在寻找一些方法来删除所有console.log语句从我的代码在minified文件。请建议我如何做到这一点。

vptzau2j

vptzau2j1#

为什么不这样做:

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

当您不希望"console.log"语句执行任何操作时?

t5fffqht

t5fffqht2#

您可以手动使用编辑器(如Visual Studio),并使用RegEx的搜索和替换功能删除所有控制台日志。
使用如下正则表达式:

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

您将能够匹配带有"'** Package 字符串的所有console.logs。
然后用
空字符串替换then。
您必须选择
g标记、全局选项或全部替换按钮。
还要考虑
console**对象有多个方法,如 dir、table、group等。

rhfm7lfc

rhfm7lfc3#

这也让我很困扰,因为我想优化网页的CEO的目的,我正在使用three.js这是一个巨大的文件。我试图最小化,但仍然有太多的控制台语句文件。
所以我创建了python脚本,它用0替换了所有的控制台语句。它在three.js上进行了测试,工作起来很有魅力。

*617,8kB最小值为3.js
*580,3kB新的,最小值为三个js

这里有一个要点...
https://gist.github.com/urosjarc/aa531af1f18c804a8dd8953190c2bef7
以下是示例输出...

▶ 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)
qjp7pelc

qjp7pelc4#

如果您使用VSCode作为文本编辑器,那么使用Regex很容易做到这一点。
1.打开侧边栏菜单中的搜索选项(或者您可以使用ctrl + F命令搜索特定的文件。
1.输入console.log。$
1.选择图标指定的搜索面板右上角的使用正则表达式选项。

1.按“查找全部”查找console.log()的所有示例,然后按“全部替换”或console.log(([^)]+));用于多行
主条目链接:https://davidgarciasantes.medium.com/remove-all-console-log-using-regex-in-vscode-9fd76476fa0f

相关问题