【问题标题】:How to remove all console.log statements from your minified or combined javascript file如何从缩小或组合的 javascript 文件中删除所有 console.log 语句
【发布时间】:2021-09-16 17:32:55
【问题描述】:

我正在使用 Visual Studio

我的 javascript 代码分散在不同的文件中。

目前我正在使用以下链接的压缩器

http://yuicompressor.codeplex.com/

这会在项目构建时以组合+缩小文件的形式生成输出。

我正在寻找某种方法从我的缩小文件中的代码中删除所有 console.log 语句。 请建议我如何做到这一点。

【问题讨论】:

  • 这对您没有帮助,但理想情况下,您将拥有一个在开发模式下仅包含 console.log 的构建脚本。这可以通过例如来完成。 ant 和 C 预处理器 (cpp)。

标签: javascript visual-studio performance


【解决方案1】:

为什么不这样做:

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

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

【讨论】:

  • 我在 Visual Studio 中寻找某种方式,也摆脱了不必要的代码。而不是这样做。一个将合并所有文件并摆脱控制台的构建事件。
  • 好吧,我不是 Visual Studio 专家,所以很遗憾我无法提供帮助。
【解决方案2】:

您可以手动使用编辑器(例如 Visual Studio)并通过搜索和替换功能使用 RegEx 来删除所有控制台日志。

使用这样的正则表达式:

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

您将能够使用 "' 包装字符串匹配所有 console.logs。

然后将 then 替换为 空字符串

您必须选择 g 标志,或 GLOBAL 选项或 全部替换 按钮。

同时考虑,console对象有多种方法,如dir、table、group等...

【讨论】:

    【解决方案3】:

    这也困扰着我,因为我想为 CEO 目的优化网页,而我使用的是三个巨大的文件。我试图最小化,但文件中仍然有很多控制台语句。

    所以我创建了 python 脚本,它将所有控制台语句替换为 0。 它在 three.js 上进行了测试,它的工作就像一个魅力。

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

    这里有一个要点...

    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)
    

    【讨论】:

      猜你喜欢
      • 2018-06-30
      • 2019-05-10
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      相关资源
      最近更新 更多