code:
var fs = require('fs'); var jsp = require("./UglifyJS-master/uglify-js").parser; var pro = require("./UglifyJS-master/uglify-js").uglify; var finalCode=''; function jsMinifier(flieIn) { var flieIn=Array.isArray(flieIn)? flieIn : [flieIn]; var origCode,ast; var getAllFiles = function (dir) { var filesArr = fs.readdirSync(dir); for(var j = 0; j < filesArr.length; j++) { var isDir = fs.lstatSync(dir+'/'+filesArr[j]); if(isDir.isDirectory()){ getAllFiles(dir+'/'+filesArr[j]); }else { readFile(dir+'/'+filesArr[j]); } } } function readFile(filename){ origCode = fs.readFileSync(filename, 'utf8'); ast = jsp.parse(origCode); ast = pro.ast_mangle(ast); ast= pro.ast_squeeze(ast); finalCode +=';'+ pro.gen_code(ast); } for(var i=0; i<flieIn.length; i++) { var isDir = fs.lstatSync(flieIn[i]); if(isDir.isDirectory()) { //为目录,获取目录下所有的文件。 getAllFiles(flieIn[i]); }else { readFile(flieIn[i]); } } } jsMinifier(process.argv[2]); fs.writeFileSync(process.argv[3], finalCode, 'utf8');
运行:
node jsMinifier /data/samba/iyy_dev/index/assets/models/wirelesswebim need.js