使用YUI+Ant 实现JS CSS压缩


Posted in PHP onSeptember 02, 2014

今天研究了一下YUI yahoo开源框架,感觉很猛啊。

于是乎我做了一个YUI的ant实现,网上好多关于bat的实现,我就另辟蹊径,出个关于这个的ant实现,嘿嘿独一无二的文章,如果转载的话,其注明作者和网站

copyright:Mr.chen

好了具体操作如下:

官网:

yuicompressor-2.4.6.jar 下载地址 http://yuilibrary.com/downloads/#yuicompressor

YUIAnt.jar 下载地址 http://www.ubik-ingenierie.com/miscellanous/YUIAnt/

具体的相关代码如下:

#css work dir 
commonCss.dir = css 
 
#js work dir 
commonJs.dir = js 
 
#build temp dir  
output.temp.dir = build 
 
#output files in the directory 
output.dir = ${output.temp.dir}_output 
 
#environment needs lib 
liblib = lib 
 
<?xml version="1.0" encoding="UTF-8"?> 
 
<project name="Compress CSS-JS" default="compress" basedir="."> 
 
  <property file="bulid.properties" /> 
 
  <path id="yuiClasspath"> 
    <fileset dir="${lib}"> 
      <include name="*.*" /> 
    </fileset> 
  </path> 
 
  <!-- #######################Init the environment of the tool ##########################--> 
  <target name="init"> 
    <echo message="begin to init the init" /> 
    <echo message="delete all reference files." /> 
    <delete dir="${output.dir}" /> 
    <echo message="delete end" /> 
    <echo message="make the reference files." /> 
    <mkdir dir="${output.dir}" /> 
    <mkdir dir="${output.temp.dir}" /> 
    <echo message="make end." /> 
  </target> 
 
  <!-- #######################Combine the css files       ##########################--> 
  <target name="combinecss" depends="init" description="Combine common css files"> 
    <echo message="begin to combine the css files to one file." /> 
    <concat destfile="${output.temp.dir}/combined_css.css" encoding="UTF-8" append="false"> 
      <fileset dir="${commonCss.dir}"> 
        <include name="*.css" /> 
      </fileset> 
    </concat> 
    <echo message="combine end." /> 
  </target> 
 
  <!-- #######################Combine the js files       ##########################--> 
  <target name="combinejs"> 
    <echo message="begin to combine the js files to one file." /> 
    <concat destfile="${output.temp.dir}/all_source.js" encoding="utf-8" append="false"> 
      <fileset dir="${commonJs.dir}"> 
        <include name="*.js" /> 
      </fileset> 
    </concat> 
    <echo message="combine end." /> 
  </target> 
 
  <!-- #######################Compress the js and css files ##########################--> 
  <target name="compress" depends="combinecss,combinejs" description="Compress"> 
    <echo message="begin to compress the css file." /> 
    <taskdef name="yuicompress" classname="com.yahoo.platform.yui.compressor.YUICompressTask"> 
      <classpath> 
        <path refid="yuiClasspath" /> 
      </classpath> 
    </taskdef> 
    <!-- first method compress the css files --> 
    <yuicompress linebreak="10000000" warn="false" munge="yes" preserveallsemicolons="true" outputfolder="${output.dir}"> 
      <fileset dir="${output.temp.dir}"> 
        <include name="*.css" /> 
      </fileset> 
    </yuicompress> 
    <echo message ="compress the css end." /> 
    <!-- second method compress the js files--> 
    <echo message ="begin to compress the js file." /> 
    <apply executable="java" parallel="false" failonerror="true"> 
      <fileset dir="${output.temp.dir}" includes="all_source.js" /> 
      <arg line="-jar" /> 
      <arg path="${lib}/yuicompressor-2.4.6.jar" /> 
      <arg line="--charset utf-8" /> 
      <arg line="-o ${output.dir}/combined_js.js" /> 
      <srcfile /> 
    </apply> 
    <echo message ="compress the js end." /> 
    <delete dir="${output.temp.dir}" /> 
  </target> 
 
</project> 
 
@echo off 
echo ################################################ 
echo ##########Tool Compress the js and css########## 
echo ################################################ 
echo Please make sure your css and js in the css'directory and js'directory. 
echo If sure,please enter any button to continue the tool. 
pause 
call ant -buildfile compress.xml compress>build.log 
echo compress end  
pause

 
 相关的文件我提供下载,感觉好的,就留言吧

PHP 相关文章推荐
给apache2.2加上mod_encoding模块後 php5.2.0 处理url出现bug
Apr 12 PHP
PHPExcel读取Excel文件的实现代码
Dec 06 PHP
使用PHP获取当前url路径的函数以及服务器变量
Jun 29 PHP
php通过字符串调用函数示例
Mar 02 PHP
PHP大转盘中奖概率算法实例
Oct 21 PHP
php中heredoc与nowdoc介绍
Dec 25 PHP
smarty模板引擎中自定义函数的方法
Jan 22 PHP
总结PHP如何获取当前主机、域名、网址、路径、端口和参数等
Sep 09 PHP
thinkphp 手机号和用户名同时登录
Jan 20 PHP
PHP匿名函数(闭包函数)详解
Mar 22 PHP
Laravel 实现Eloquent模型分组查询并返回每个分组的数量 groupBy()
Oct 23 PHP
50个优秀经典PHP算法大集合 附源码
Aug 26 PHP
在Ubuntu 14.04上部署 PHP 环境及 WordPress
Sep 02 #PHP
PHP高级编程实例:编写守护进程
Sep 02 #PHP
php输入流php://input使用浅析
Sep 02 #PHP
php获取URL中带#号等特殊符号参数的解决方法
Sep 02 #PHP
PHP中提问频率最高的11个面试题和答案
Sep 02 #PHP
PHP处理Json字符串解码返回NULL的解决方法
Sep 01 #PHP
PHP实现更新中间关联表数据的两种方法
Sep 01 #PHP
You might like
ASP知识讲座四
2006/10/09 PHP
基于mysql的bbs设计(四)
2006/10/09 PHP
火车头discuz6.1 完美采集的php接口文件
2009/09/13 PHP
PHP QRCODE生成彩色二维码的方法
2016/05/19 PHP
php用户密码加密算法分析【Discuz加密算法】
2016/10/12 PHP
jQuery 性能优化指南(2)
2009/05/21 Javascript
判断目标是否是window,document,和拥有tagName的Element的代码
2010/05/31 Javascript
JS Range HTML文档/文字内容选中、库及应用介绍
2011/05/12 Javascript
Javascript写入txt和读取txt文件示例
2014/02/12 Javascript
从零学JS之你需要了解的几本书
2014/05/19 Javascript
javascript的动态加载、缓存、更新以及复用(一)
2014/06/09 Javascript
浅谈javascript的Touch事件
2015/09/27 Javascript
深入剖析javascript中的exec与match方法
2016/05/18 Javascript
关于JSON与JSONP简单总结
2016/08/16 Javascript
JQuery.dataTables表格插件添加跳转到指定页
2017/06/09 jQuery
Node.js学习之查询字符串解析querystring详解
2017/09/28 Javascript
php结合js实现多条件组合查询
2019/05/28 Javascript
微信小程序实现一个简单swiper代码实例
2019/12/30 Javascript
用JavaScript实现贪吃蛇游戏
2020/10/23 Javascript
[01:23]一分钟告诉你 DOTA2为什么叫信仰2
2014/06/20 DOTA
python 生成目录树及显示文件大小的代码
2009/07/23 Python
python高并发异步服务器核心库forkcore使用方法
2013/11/26 Python
Tesserocr库的正确安装方式
2018/10/19 Python
python在TXT文件中按照某一字符串取出该字符串所在的行方法
2018/12/10 Python
Python 处理图片像素点的实例
2019/01/08 Python
Python实现图像去噪方式(中值去噪和均值去噪)
2019/12/18 Python
利用Python自动化操作AutoCAD的实现
2020/04/01 Python
css3的图形3d翻转效果应用示例
2014/04/08 HTML / CSS
详解CSS3中Media Queries的相关使用
2015/07/17 HTML / CSS
个性化皮包、小袋、生活配件:Mon Purse
2019/03/26 全球购物
信息管理专业学生自荐信格式
2013/09/22 职场文书
不忘国耻振兴中华演讲稿
2014/05/14 职场文书
晋江市人民政府党组群众路线教育实践活动整改方案
2014/10/25 职场文书
2014年科室工作总结
2014/11/20 职场文书
如何在CocosCreator里画个炫酷的雷达图
2021/04/16 Javascript
解析CSS 提取图片主题色功能(小技巧)
2021/05/12 HTML / CSS