使用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 相关文章推荐
PHP写MySQL数据 实现代码
Jun 15 PHP
PHP函数篇之掌握ord()与chr()函数应用
Dec 05 PHP
Zend的AutoLoad机制介绍
Sep 27 PHP
PHP、Nginx、Apache中禁止网页被iframe引用的方法
Oct 01 PHP
PHP安全的URL字符串base64编码和解码
Jun 19 PHP
smarty中post用法实例
Nov 28 PHP
基于php的CMS中展示文章类实例分析
Jun 18 PHP
joomla数据库操作示例代码
Jan 06 PHP
PHP实现自动识别原编码并对字符串进行编码转换的方法
Jul 13 PHP
Yii2框架实现数据库常用操作总结
Feb 08 PHP
ThinkPHP实现的rsa非对称加密类示例
May 29 PHP
PHP通过GD库实现验证码功能示例
Feb 23 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
天津市收音机工业发展史
2021/03/04 无线电
php URL验证正则表达式
2011/07/19 PHP
php把大写命名转换成下划线分割命名
2015/04/27 PHP
CI框架实现框架前后端分离的方法详解
2016/12/30 PHP
php 查找数组元素提高效率的方法详解
2017/05/05 PHP
PHP 记录访客的浏览信息方法
2018/01/29 PHP
laravel 实现上传图片到本地和前台访问示例
2019/10/21 PHP
javascript图像处理—仿射变换深度理解
2013/01/16 Javascript
jQuery实现列表自动循环滚动鼠标悬停时停止滚动
2013/09/06 Javascript
实现网页页面跳转的几种方法(meta标签、js实现、php实现)
2014/05/20 Javascript
分享9个最好用的JavaScript开发工具和代码编辑器
2015/03/24 Javascript
浅析Node.js中使用依赖注入的相关问题及解决方法
2015/06/24 Javascript
JavaScript必知必会(五) eval 的使用
2016/06/08 Javascript
利用策略模式与装饰模式扩展JavaScript表单验证功能
2017/02/14 Javascript
node.js平台下的mysql数据库配置及连接
2017/03/31 Javascript
利用node.js本地搭建HTTP服务器
2017/04/19 Javascript
Three.js入门之hello world以及如何绘制线
2017/09/25 Javascript
JS字符串去除连续或全部重复字符的实例
2018/03/08 Javascript
vue - props 声明数组和对象操作
2020/07/30 Javascript
python绘制简单彩虹图
2018/11/19 Python
Python实战购物车项目的实现参考
2019/02/20 Python
Django获取应用下的所有models的例子
2019/08/30 Python
Python压缩模块zipfile实现原理及用法解析
2020/08/14 Python
Python接口自动化测试的实现
2020/08/28 Python
基于Django快速集成Echarts代码示例
2020/12/01 Python
html5教程调用绘图api画简单的圆形代码分享
2013/12/04 HTML / CSS
Bench加拿大官方网站:英国城市服装品牌
2017/11/03 全球购物
大四学生毕业自荐信
2013/11/07 职场文书
安全资料员岗位职责
2013/12/14 职场文书
公司节能减排方案
2014/05/16 职场文书
放飞梦想演讲稿200字
2014/08/26 职场文书
2019 入党申请书范文
2019/07/10 职场文书
MySQL 全文索引使用指南
2021/05/25 MySQL
CSS实现章节添加自增序号的方法
2021/06/23 HTML / CSS
Java移除无效括号的方法实现
2021/08/07 Java/Android
redis击穿 雪崩 穿透超详细解决方案梳理
2022/03/17 Redis