标签:
code:
1 var fs = require(‘fs‘); 2 var jsp = require("./UglifyJS-master/uglify-js").parser; 3 var pro = require("./UglifyJS-master/uglify-js").uglify; 4 var finalCode=‘‘; 5 function jsMinifier(flieIn) { 6 var flieIn=Array.isArray(flieIn)? flieIn : [flieIn]; 7 var origCode,ast; 8 9 var getAllFiles = function (dir) { 10 var filesArr = fs.readdirSync(dir); 11 for(var j = 0; j < filesArr.length; j++) { 12 var isDir = fs.lstatSync(dir+‘/‘+filesArr[j]); 13 if(isDir.isDirectory()){ 14 getAllFiles(dir+‘/‘+filesArr[j]); 15 }else { 16 readFile(dir+‘/‘+filesArr[j]); 17 } 18 } 19 } 20 function readFile(filename){ 21 origCode = fs.readFileSync(filename, ‘utf8‘); 22 ast = jsp.parse(origCode); 23 ast = pro.ast_mangle(ast); 24 ast= pro.ast_squeeze(ast); 25 finalCode +=‘;‘+ pro.gen_code(ast); 26 } 27 for(var i=0; i<flieIn.length; i++) { 28 var isDir = fs.lstatSync(flieIn[i]); 29 if(isDir.isDirectory()) { 30 //为目录,获取目录下所有的文件。 31 getAllFiles(flieIn[i]); 32 }else { 33 readFile(flieIn[i]); 34 } 35 } 36 } 37 jsMinifier(process.argv[2]); 38 fs.writeFileSync(process.argv[3], finalCode, ‘utf8‘);
运行:
node jsMinifier /data/samba/iyy_dev/index/assets/models/wirelesswebim need.js
标签:
原文地址:http://www.cnblogs.com/liuyinlei/p/5104767.html