日期:2014-05-16 浏览次数:20353 次
首先先问下坛子里的筒子:uglifyjs的sources多个file,target运行后只压缩第一个文件,是不是有bug?不能一次多个文件批量压缩么?我都是先一个个压缩然后concat的。
?
贴代码如下:
/* 运行命令: cms:>/groovy js cms:>/groovy js /myjsdir cms:>/groovy css cms:>/groovy css /mycssdir */ def ant = new AntBuilder() String base_dir = '../' String app_name = 'app' String dist_dir = "${base_dir}/dist" String war_dir = "${base_dir}/webroot" String dist_war_dir = "${dist_dir}/${app_name}.war" String encoding = 'utf-8' // 把js/css文件先转成ascii(如果**gbk),uglifyjs貌似只能outputEncoding utf-8 def nativeascii = {path_src, path_dest -> String includes_str = new File(path_src).listFiles().grep{it.isFile() && it.name ==~ /.*(css|js)$/}.collect{it.name}.join(',') ant.native2ascii encoding: encoding, src: path_src, dest: path_dest, includes: includes_str } if(args && 'js' == args[0]){ // compress js sample ant.taskdef name: 'uglify', classname: 'uglify.ant.UglifyTask' // change dir here String dir = '/js' if(args.size() > 1) dir = args[1] dir = dist_war_dir + dir def js_dir = new File(dir) def ascii_dir = new File(js_dir, 'ascii') def output_dir = new File(js_dir, 'output') ascii_dir.delete() ascii_dir.mkdir() output_dir.delete() output_dir.mkdir() def cmp = {js -> println 'Start compile js file : ' + js ant.uglify verbose: 'true', maxLineLen: 300, output: new File(output_dir, js).absolutePath, { sources dir: ascii_dir.absolutePath, { file name: js } } } nativeascii(js_dir.absolutePath, ascii_dir.absolutePath) // jscalendar.js压缩总报错,使用其他压缩工具 Set skip_js_ll = ['all.js', 'calendar.js'] ascii_dir.eachFile{ if(it.name.endsWith('.js') && !it.name.endsWith('.min.js') && !skip_js_ll.contains(it.name)) cmp(it.name) } // concat js files ant.concat destfile: new File(js_dir, 'all.js').absolutePath, encoding: encoding, append: true, { fileset dir: js_dir.absolutePath, { include name: '*.min.js' } fileset dir: new File(js_dir, 'output').absolutePath, { include name: '*.js' } } println 'Concat js files ok.' } if(args && 'css' == args[0]){ // compress css sample ant.taskdef name: 'yuicompress', classname: 'com.yahoo.platform.yui.compressor.YUICompressTask' // change dir here String dir = '/css/' if(args.size() > 1) dir = args[1] dir = dist_war_dir + dir def css_dir = new File(dir) def ascii_dir = new File(css_dir, 'ascii') def output_dir = new File(css_dir, 'output') ascii_dir.delete() ascii_dir.mkdir() output_dir.delete() output_dir.mkdir() nativeascii(css_dir.absolutePath, ascii_dir.absolutePath) ant.yuicompress linebreak: 300, warn: 'false', munge: 'yes', preserveallsemicolons: 'true', outputfolder: output_dir.absolutePath, { fileset dir: ascii_dir.absolutePath, { include name: '*.css' exclude name: 'all.css' } } // concat css files // new File(css_dir, 'all.css').withPrintWriter{w -> // output_dir.listFiles().each{ // w.println "/* compress file ${it.name} */" // it.eachLine{line -> // w.println line // } // } // } ant.concat destfile: new File(css_dir, 'all.css').absolutePath, encoding: encoding, append: true, { fileset dir: output_dir.absolutePath, { include name: '*.css' } } println 'css file merge done.' }?