yuicompressor-2.4.2.jar 和 YUIAnt-zh_CN.jar ANT 压缩JS文件,提高js运行速度

27 篇文章 0 订阅

1、说明在使用extjs 的时候,运行起来比较卡,特别是运行数据和js过多的时候,就会出现卡死的情况,或者等待时间比较长。

在解决这个有很多优化方法,今天来说其中一种,那就是在Ant脚本打包的时候,把js中空格、注释去掉、以及合并,合并今天不说了,还未实现这个,在研究中

2、首先,需要准备二个 .jar 文件,分别是 YUIAnt.jar 和 yuicompressor-2.4.x.jar 。

3、俩个文件的附件

下载地址在我的资源中

http://download.csdn.net/detail/dracotianlong/5033927

4、build.xml文件如下

<!-- JS压缩过程中的临时目录  -->
		<property name="jsDir" value="${basedir}/release/web/js" />
		<!-- CSS压缩过程中的临时目录  -->
		<property name="cssDir" value="${basedir}/release/web/css" />
		<!-- ext3 css压缩过程中的临时目录  -->
		<property name="cssExtDir" value="${basedir}/release/web/ext3" />
		<!-- ext3 js压缩过程中的临时目录  -->
		<property name="extUxJsDir" value="${basedir}/release/web/ext3/ux" />

		<!-- JS源文件  -->
		<property name="jsSourceDir" value="${webapp}/js" />
		<!-- CSS源文件  -->
		<property name="cssSourceDir" value="${webapp}/css" />
		<!-- Ext CSS源文件  -->
		<property name="cssExtSourceDir" value="${webapp}/ext3" />
		<!-- Ext JS源文件  -->
		<property name="extUxJsSourceDir" value="${webapp}/ext3/ux" />
		<!-- yui 依赖包 -->
		<path id="yuiClasspath">
			<fileset dir="${basedir}">
				<include name="metadata/tools/yui/yuicompressor-2.4.2.jar" />
				<include name="metadata/tools/yui/YUIAnt-zh_CN.jar" />
			</fileset>
		</path>

<!-- 开始压缩JS -->
	<target name="copy-web-depress-js" depends="init">
		<delete dir="${basedir}/release/web" />
		<copy todir="${basedir}/release/web">
			<fileset dir="${webapp}"/>
		</copy>
		<taskdef name="uxJsCompress"  classname="com.yahoo.platform.yui.compressor.YUICompressTask">
			<classpath>
				<path refid="yuiClasspath" />
			</classpath>
		</taskdef>
		<uxJsCompress linebreak="9000000" warn="false"  charset="utf-8" encoding="utf-8" munge="yes" preserveallsemicolons="true" outputfolder="${extUxJsDir}">
			<fileset dir="${extUxJsSourceDir}">
				<include name="**/*.js"  />
			</fileset>
		</uxJsCompress>

		<taskdef name="yuicompress" classname="com.yahoo.platform.yui.compressor.YUICompressTask">
			<classpath>
				<path refid="yuiClasspath" />
			</classpath>
		</taskdef>
		<yuicompress linebreak="9000000" warn="false" charset="utf-8"  encoding="utf-8" munge="yes" preserveallsemicolons="true" outputfolder="${jsDir}">
			<fileset dir="${jsSourceDir}">
				<include name="**/*.js" />
			</fileset>
		</yuicompress>

		<taskdef name="cssYuicompress" classname="com.yahoo.platform.yui.compressor.YUICompressTask">
			<classpath>
				<path refid="yuiClasspath" />
			</classpath>
		</taskdef>

		<cssYuicompress linebreak="9000000" warn="false" charset="utf-8" encoding="utf-8" munge="yes" preserveallsemicolons="true" outputfolder="${cssDir}">
			<fileset dir="${cssSourceDir}">
				<include name="**/*.css" />
			</fileset>
		</cssYuicompress>

		<taskdef name="cssExtYuicompress" classname="com.yahoo.platform.yui.compressor.YUICompressTask">
			<classpath>
				<path refid="yuiClasspath" />
			</classpath>
		</taskdef>

		<cssExtYuicompress linebreak="9000000" warn="false" charset="utf-8" encoding="utf-8" munge="yes" preserveallsemicolons="true" outputfolder="${cssExtDir}">
			<fileset dir="${cssExtSourceDir}">
				<include name="**/*.css" />
			</fileset>
		</cssExtYuicompress>
	</target>
	<!-- 结束压缩JS -->

5、在这里说一点,在使用js压缩的时候YUIAnt.jar 是不支持utf-8字符集的。即
encoding="utf-8

需要到我的资源中下载,无需资源分

http://download.csdn.net/detail/dracotianlong/5033927



7、个人感受,在弄了很小半天,吧这个问题搞定了,自以为可以提高点js加载速度了,结果还是有点小慢,没有jquery那么的快,估计要用gzjs压缩才会变快点,现在我分析是的在IE下运行速度比较慢点,在firefox下比较快,看来还是每个模块加载的js有点多。


优化速度对比:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值