uglify-js可以删除console.log语句吗? [英] Can uglify-js remove the console.log statements?

查看:1043
本文介绍了uglify-js可以删除console.log语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using uglify-js to minify the source code. I want to remove the console.log statements of the original source code. Is it possible? Or is there any other compressor tool supports this?

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

I use the code as below in 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不是删除。

In the minified code the console.log isn't removed.

推荐答案

在最新的uglify-js(v2.4.3)中,添加了一个新的压缩选项'pure_funcs'。如果我将console.log函数添加到此数组,它将在缩小的js文件中删除。下面的测试代码显示了此选项的工作原理。这正是我想要的。

In the lastest uglify-js ( v2.4.3), a new compress option ‘pure_funcs’ is added. If I add the console.log functions to this array, it will be removed in the minified js file. The test code below shows how this option works. This is exactly what I want.

// file: m.js
var uglify = require('uglify-js');
var originalSourceCode = 'var name = function(){var str = "test"; return str}; console.log("log data" + name());';
var minifiedCode = uglify.minify(originalSourceCode, {
                fromString : true,
                mangle: {},
                warnings: true,
                compress:{
                    pure_funcs: [ 'console.log' ]
                }
            });
console.log(minifiedCode);

$node m.js
WARN: Dropping side-effect-free statement [?:1,53]
{ code: 'var name=function(){var n="test";return n};',
  map: 'null' }

来自 https://github.com/mishoo/UglifyJS2 的报价


pure_funcs - 默认为null。您可以传递一组名称,UglifyJS将假定这些函数不会产生副作用。
危险:不会检查名称是否在范围内重新定义。这里有一个示例
的例子,例如var q = Math.floor(a / b)。如果变量q不是其他地方使用的
,则UglifyJS会丢弃它,但仍会保留
Math.floor(a / b),而不知道它的作用。你可以传递pure_funcs:[
'Math.floor']让它知道这个函数不会产生任何
的副作用,在这种情况下整个语句都会被丢弃。
当前实现增加了一些开销(压缩将慢了
)。

pure_funcs -- default null. You can pass an array of names and UglifyJS will assume that those functions do not produce side effects. DANGER: will not check if the name is redefined in scope. An example case here, for instance var q = Math.floor(a/b). If variable q is not used elsewhere, UglifyJS will drop it, but will still keep the Math.floor(a/b), not knowing what it does. You can pass pure_funcs: [ 'Math.floor' ] to let it know that this function won't produce any side effect, in which case the whole statement would get discarded. The current implementation adds some overhead (compression will be slower).

这篇关于uglify-js可以删除console.log语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆