『壹』 如何使用nodejs 的 uglifyjs 压缩js文件
[js压缩]uglifyjs source.js -mc -o target.min.js
以压缩angular.js为例,可以压缩局部变量名,去掉无用的局部变量,空白符等,压缩比例接近80%,效率不错
[user_00@ ~]$ uglifyjs angular.js -mc -o angular.min.js
WARN: Dropping unused function isBoolean [angular.js:416,9]
WARN: Dropping unused function makeMap [angular.js:446,9]
WARN: Dropping unused function size [angular.js:488,9]
WARN: Dropping unused function isLeafNode [angular.js:523,9]
WARN: Dropping unused function argument boundTranscludeFn [angular.js:4682,27]
WARN: Dropping unused function argument rootElement [angular.js:4681,16]
WARN: Dropping unused function argument nodeList [angular.js:4680,17]
WARN: Dropping unused function argument scope [angular.js:4679,22]
WARN: Dropping unused function nodesetLinkingFn [angular.js:4678,9]
WARN: Dropping unused function argument boundTranscludeFn [angular.js:4690,27]
WARN: Dropping unused function argument rootElement [angular.js:4689,16]
WARN: Dropping unused function argument node [angular.js:4688,13]
WARN: Dropping unused function argument scope [angular.js:4687,22]
WARN: Dropping unused function argument nodesetLinkingFn [angular.js:4686,25]
WARN: Dropping unused function directiveLinkingFn [angular.js:4685,9]
WARN: Dropping unused function stripHash [angular.js:5009,9]
WARN: Dropping unused variable NG_SWITCH [angular.js:13857,4]
WARN: Dropping unused variable fns [angular.js:1536,8]
WARN: Dropping unused variable events [angular.js:1536,13]
WARN: Dropping unused function wrongMode [angular.js:3879,13]
WARN: Dropping unused function argument cause [angular.js:4799,31]
WARN: Dropping unused function argument exception [angular.js:4799,20]
WARN: Dropping unused function argument args [angular.js:8165,28]
WARN: Dropping unused function argument args [angular.js:8231,33]
WARN: Dropping unused function argument names [angular.js:9018,32]
WARN: Dropping unused function argument name [angular.js:9030,40]
WARN: Dropping unused variable value [angular.js:9230,14]
WARN: Dropping unused function argument value [angular.js:12475,36]
[user_00@localhost ~]$ ls -lh angular.*
-rw-rw-r-- 1 user_00 user_00 479K Apr 5 01:53 angular.js
-rw-rw-r-- 1 user_00 user_00 79K May 4 16:09 angular.min.js
『贰』 npm安装完uglifyjs怎么对js代码进行混淆
如果你是全局安装的,可以直接在需要混淆文件的目录下,
按住shift键右击鼠标选择在此处打开命令窗口如图
新建test.js开始测试
varUglifyJS=require('uglify-js');
//代码压缩
varresult=UglifyJS.minify("varb=function(){};",{fromString:true});
console.log("
===========================");
console.log(result);
//文件压缩
result=UglifyJS.minify(["demo.js"]);
console.log("
===========================");
console.log(result.code);
//多文件压缩,指定sourcemap和网站来源
result=UglifyJS.minify(["main.js","demo.js"],{
outSourceMap:"out.js.map",
sourceRoot:"http://onbook.me",
mangle:true
});
console.log("
===========================");
console.log(result.code);
console.log(result.map);
http://blog.fens.me/nodejs-uglifyjs2-js/