//js var fs = require('fs'); var jsp = require("uglify-js").parser; var pro = require("uglify-js").uglify; function jsMinifier(flieIn, fileOut) { var flieIn=Array.isArray(flieIn)? flieIn : [flieIn]; var origCode,ast,finalCode=''; for(var i=0; i<flieIn.length; i++) { origCode = fs.readFileSync(flieIn[i], 'utf8'); ast = jsp.parse(origCode); ast = pro.ast_mangle(ast); ast= pro.ast_squeeze(ast); finalCode +=';'+ pro.gen_code(ast); } fs.writeFileSync(fileOut, finalCode, 'utf8'); } //jsMinifier('./file-src/test2.js', './file-smin/test-min.js'); //单个文件压缩 jsMinifier(['./file-src/test.js','./file-src/test2.js'], './file-smin/test-min.js'); //合并压缩命令翻译
//css var cleanCSS = require('clean-css'); function cssMinifier(flieIn, fileOut) { var flieIn=Array.isArray(flieIn)? flieIn : [flieIn]; var origCode,finalCode=''; for(var i=0; i<flieIn.length; i++) { origCode = fs.readFileSync(flieIn[i], 'utf8'); finalCode += cleanCSS.process(origCode); } fs.writeFileSync(fileOut, finalCode, 'utf8'); } //cssMinifier('./file-src/indexw_20120913.css', './file-smin/index.css'); //单个文件压缩 cssMinifier(['./file-src/index_20120913.css','./file-src/indexw_20120913.css'], './file-smin/index.css'); //图片 var imgMinifier = require('node-smushit'); //imgMinifier.smushit('./file-src/images', {recursive: true}); //递归 imgMinifier.smushit('./file-src/images');
以上为API的JS调用方式。
尝试的玩了下:1、UglifyJS 安装的时候要注意做以下操作:
## clone the repository
mkdir -p /where/you/wanna/put/it
cd /where/you/wanna/put/it
git clone git://github.com/mishoo/UglifyJS.git
## make the module available to Node
mkdir -p ~/.node_libraries/
cd ~/.node_libraries/
ln -s /where/you/wanna/put/it/UglifyJS/uglify-js.js
## and if you want the CLI script too:
mkdir -p ~/bin
cd ~/bin
ln -s /where/you/wanna/put/it/UglifyJS/bin/uglifyjs
# (then add ~/bin to your $PATH if it's not there already)
命令行直接这样玩:uglifyjs [ options... ] [ filename ]
只能压缩单个文件?api的测试是可以压缩多个js文件的。
2、clean-css
命令行玩了下:cat one.css two.css three.css | cleancss -o merged-and-minified.css
可行。但api的调用没成功,一直报process方法找不到未定义。可能上面例子有问题?没细究。
3、node-smushit
命令行:smushit file1 file2 file3
玩了下,提示process 3,save 0.
理论上来讲是替换原图片的,实际上我这个没保存成功,不知道为啥。
所以api的也没有测试。
已有0条评论