javascript - uglify-js 可以删除 console.log 语句吗?

标签 javascript node.js uglifyjs2

我正在使用 uglify-js缩小源代码。我想删除原始源代码的 console.log 语句。可能吗?或者有没有其他压缩工具支持这个?

我在 Node.js 中使用如下代码。

var uglify = require('uglify-js');
var originalSourceCode = 'var name = function(){var str = "test"; return str}; console.log("log data");';
var minifiedCode = uglify.minify(originalSourceCode, {
                fromString : true,
                mangle: {},
                warnings: true
            });
console.log(minifiedCode);

输出是:

$node m.js
{ code: 'var name=function(){var a="test";return a};console.log("log data");',
  map: 'null' }

在缩小的代码中,console.log 没有被删除。

最佳答案

最近(2013 年末)添加了另一个名为 drop_console 的选项

drop_console -- default false. Pass true to discard calls to console.* functions

这是像这样添加到 grunt init 配置中的:

grunt.initConfig({
  uglify: {
    options: {
      compress: {
        drop_console: true // <-
      }
    },
    my_target: {
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

取自 grunt-contrib-uglify github documents

关于javascript - uglify-js 可以删除 console.log 语句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20092466/

相关文章:

javascript - jQuery datepicker 用一位数字解析年份

javascript - 到达词缀底部并滚动回顶部后,Affix Bootstrap 会闪烁

javascript - Nodejs变量作用域问题

javascript - Webpack 在使用继承缩小/丑化 ES6 代码时删除了类名

node.js - 在整个 Node 项目上使用 UglifyJs?

javascript - 确保表单不会在输入时提交,而是运行函数

javascript - 将 Data-test-id 属性添加到我的 Jquery 中的 <td>

javascript - 如何测试函数是否调用特定的方法/函数?

javascript - -- Node v4.x 中的 prof?

javascript - 为什么 UglifyJS 将 `someFunction` 转换为 `(0, v.someFunction)` ?