1、关于YUI Compressor
YUI Compressor – The Yahoo! JavaScript and CSS Compressor
The YUI Compressor is a JavaScript compressor which, in addition to removing comments and white-spaces, obfuscates local variables using the smallest possible variable name. This obfuscation is safe, even when using constructs such as ‘eval’ or ‘with’ (although the compression is not optimal is those cases) Compared to jsmin, the average savings is around 20%.
The YUI Compressor is also able to safely compress CSS files. The decision on which compressor is being used is made on the file extension (js or css)
对于大量使用JavaScript和CSS的AJAX应用来说,如果JavaScript和CSS大小很大,则传输到客户端的时间会很久,网站性能不佳。而压缩JavaScript和CSS是自然的事情(其中YUI Compressor的压缩率大约为20%);
大致就是这么个意思,其实我们只要关注YUI Compressor能实现什么就行。
2、JAVA环境搭建(JDK)
JAVA请到这里下载:
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
JDK环境变量配置的步骤如下:
1.我的电脑–>属性–>高级–>环境变量.
2.配置用户变量:
a.新建 JAVA_HOME
C:\Program Files\Java\jdk1.7.0_11(JDK的安装路径)
b.在PATH变量后添加
%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin
3.测试环境变量配置是否成功:
开始–〉运行–〉CMD
键盘敲入: JAVA
出现相应的命令,而不是出错信息,即表示配置成功!
至此,条件都准备好了。
3、右键集成YUI Compressor
右键集成的方法,我百度出一大把,其中有介绍利用Filemenu Tools工具的,操作相对简单,只要安装好这个软件,并进行相关设置即可;不过在测试时,发现压缩后的文件名不够理想。
最后发现“琼台博客”分享的“给YUI Compressor添加右键命令”最实用也最方便;
若有需要,可以移步至琼台博客查看详细教程;教程主要部分转载如下:
三、下载 compressor.cmd 放到D:\yuicompressor文件夹里
四、新建一个 compressor.reg 文件输入以下内容
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\AllFilesystemObjects\shell]
[HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\YUI Compressor]
[HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\YUI Compressor\command]
@="D:\\yuicompressor\\compressor.cmd \"%1\""
五、使用记事本打开compressor.cmd
其中有一处”%JAVA_HOME%\bin\java.exe” -jar “%~dp0\build\yuicompressor-2.4.7.jar” –charset UTF-8 “%~nx1″ -o “%RESULT_FILE%”
这里的build\yuicompressor-2.4.7.jar 意思是相对于自己当前路径的build路径下的yuicompressor-2.4.7.jar 文件,一般你下载压缩后这个文件通常放在build目录里,无需修改,需要注意的是后面的版本号对应真实文件就好。
如你下载的是2.4.2版本,那么你解压后build目录里的文件应该是yuicompressor-2.4.2.jar。
最后一处,就是 –charset UTF-8 了,这里我默认使用了UTF-8,如果使用GBK的请改成–charset GB18030就好
六、双击 compressor.reg 导入信息到注册表,安装完毕。找一个JS或CSS文件右键就会看到菜单了,只需要执行以下,即可生成压缩版。
右键js文件,弹出的菜单里,有一个 YUI Compressor 选项,单击它对选中的文件压缩