利用YUI与Ant怎么对JS和CSS进行压缩?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
具体的相关代码如下:
#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
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。