我正在使用 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']
}
}
}
});
关于javascript - uglify-js 可以删除 console.log 语句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20092466/