在Ubuntu中使用YUICompressor

YUICompressor,是由Yahoo!提供的压缩(Minify)Javascript,CSS的工具。

YUICompressor是用Java写的,所以使用它之前必须先安装Jre:

sudo apt-get install sun-java6-jre

然后,下载YUICompressor-2.4.2.zip,并解压至任意位置。

参数说明

Usage: java -jar yuicompressor-x.y.z.jar [options] [input file]

Global Options
-h, –help Displays this information
–type Specifies the type of the input file
–charset Read the input file using
–line-break
Insert a line break after the specified column number
-v, –verbose Display informational messages and warnings
-o Place the output into . Defaults to stdout.

JavaScript Options
–nomunge Minify only, do not obfuscate
–preserve-semi Preserve all semicolons
–disable-optimizations Disable all micro optimizations

提示:
其实不用输入–type js 或 –type css, YUICompressor也会根据文件后缀名来自动判断。

以下是我写的一段脚本:

#!/usr/bin/python

import os
import sys

compressor_path = '/home/allen/.tools/yuicompressor.jar'
charset_code = 'utf-8'

def compressor(file):
    os.path.splitext(file)
    return '-min'.join([os.path.splitext(file)[0], os.path.splitext(file)[1]])

files = sys.argv[1:]

for file in files:
    os.system('java -jar %s %s -o %s --charset %s --preserve-semi' % (compressor_path, file, compressor(file), charset_code))

把脚本以任意名字保存,如“压缩JS文件”,并将文件放到你的用户目录下的.gnome2/nautilus-scripts/目录中,如:/home/allen/.gnome2/nautilus-scripts/

这样你就可以更加方便的使用YUICompressor了~

版权所有,转载请注明出处。
转载自 <a href="http://www.movoin.com/using-yuicompressor-in-ubuntu.html" title="在Ubuntu中使用YUICompressor" rel="bookmark">在Ubuntu中使用YUICompressor | Movoin Studio</a>
Tags: Linux , Ubuntu

Comments are closed.

无觅相关文章插件,快速提升流量