- 浏览: 305886 次
- 性别:
- 来自: 长沙
文章分类
最新评论
-
完善自我:
支持一下!
揭秘IT人才特点:中美印日四国程序员比较 -
悲剧了:
好文,看玩thinking in java的提到的异常处理,看 ...
高效的Java异常处理框架(转) -
yin_bp:
开源框架bbossgroups页支持组件异步方法调用哦,详情请 ...
Spring 3中异步方法调用 -
flyjava:
sun的悲哀
Apache怒了,威胁说要离开JCP
一、简介: 目前开发Web应用Javascript发挥的作用越来越大,相关的Javascript框架也比较多。但是有一个问题,我们开发过程中,所有的JS代码都添加了注释,如使用JsDoc,代码的可读性比较强,同时这样的代码也便于调试。但是在产品环境中,我们希望这些JS代码是压缩和混淆过的,这主要是让 JS代码加载的更快,这也是Google AJAX Libraries API出现的原因。YUI Compressor 是一款由 Yahoo 公司开发的、功能非常强大的 JS、CSS 代码混淆和压缩工具,采用Java开发,目前很多Javascript Framework都使用YUI Compressor进行代码分发。 YUI Compressor 官网网址: YUI Compressor 下载页面: 二、使用简介: 在命令行下执行 Java 程序,运行 yuicompressor jar 软件包,来完成任务: //压缩JS //压缩CSS 三、参考官方英文注释: 3.1 How does the YUI Compressor work? The YUI Compressor is written in Java (requires Java >= 1.4) and relies on Rhino to tokenize the source JavaScript file. It starts by analyzing the source JavaScript file to understand how it is structured. It then prints out the token stream, omitting as many white space characters as possible, and replacing all local symbols by a 1 (or 2, or 3) letter symbol wherever such a substitution is appropriate (in the face of evil features such as eval or with, the YUI Compressor takes a defensive approach by not obfuscating any of the scopes containing the evil statement) The CSS compression algorithm uses a set of finely tuned regular expressions to compress the source CSS file. The YUI Compressor is open-source, so don't hesitate to look at the code to understand exactly how it works. 3.2 Using the YUI Compressor from the command line java -jar yuicompressor-x.y.z.jar Usage: java -jar yuicompressor-x.y.z.jar [options] [input file] Global Options JavaScript Options GLOBAL OPTIONS -h, --help --line-break --type js|css --charset character-set -o outfile -v, --verbose JAVASCRIPT ONLY OPTIONS --nomunge --preserve-semi --disable-optimizations The following command line (x.y.z represents the version number): java -jar yuicompressor-x.y.z.jar myfile.js -o myfile-min.jswill minify the file myfile.js and output the file myfile-min.js. For more information on how to use the YUI Compressor, please refer to the documentation included in the archive. The charset parameter isn't always required, but the compressor may throw an error if the file's encoding is incompatible with the system's default encoding. In particular, if your file is encoded in utf-8, you should supply the parameter. java -jar yuicompressor-x.y.z.jar myfile.js -o myfile-min.js --charset utf-8 四、YUI Compressor 压缩 JavaScript 的原理 YUI Compressor 压缩 JavaScript 的内容包括: 1.移除注释 YUI Compressor 包括哪些细微优化呢? • object["property"],如果属性名是合法的 JavaScript 标识符(注:合法的 JavaScript 标识符——由一个字母开头,其后选择性地加上一个或者多个字母、数字或下划线)且不是保留字,将优化为: object.property 比如: (function(){function add(num1, num2) {return num1 + num2;}})(); 进行属标识符替换后: (function(){function A(C, B) {return C+ B;}})(); 再移除额外的空格,最终成了: (function(){function A(C,B){return C+B;}})(); YUI Compressor 标识符替换仅替换函数名和变量名,那哪些不能被替代呢? 1.原始值:字符串、布尔值、数字、null 和 undefined。一般来说字符串占的空间最多,而非数字字面量其次(true、false,null,underfinded)。 但有部分情况下是禁止使用标识符替换的: 1.使用 eval() 函数。解决方法:不使用或者创建一个全局函数封装 eval()。 五、参考资料: 作者:张庆(网眼) 西安 PHP 教育培训中心 2010-8-18
http://developer.yahoo.com/yui/compressor/
http://yuilibrary.com/downloads/#yuicompressor
java -jar yuicompressor-2.4.2.jar --type js --charset utf-8 -v src.js > packed.js
java -jar yuicompressor-2.4.2.jar --type css --charset utf-8 -v src.css > packed.css
-h, --help Displays this information
--type <js|css> Specifies the type of the input file
--charset <charset> Read the input file using <charset>
--line-break <column> Insert a line break after the specified column number
-v, --verbose Display informational messages and warnings
-o <file> Place the output into <file>. Defaults to stdout.
--nomunge Minify only, do not obfuscate
--preserve-semi Preserve all semicolons
--disable-optimizations Disable all micro optimizations
Prints help on how to use the YUI Compressor
Some source control tools don't like files containing lines longer than,
say 8000 characters. The linebreak option is used in that case to split
long lines after a specific column. It can also be used to make the code
more readable, easier to debug (especially with the MS Script Debugger)
Specify 0 to get a line break after each semi-colon in JavaScript, and
after each rule in CSS.
The type of compressor (JavaScript or CSS) is chosen based on the
extension of the input file name (.js or .css) This option is required
if no input file has been specified. Otherwise, this option is only
required if the input file extension is neither 'js' nor 'css'.
If a supported character set is specified, the YUI Compressor will use it
to read the input file. Otherwise, it will assume that the platform's
default character set is being used. The output file is encoded using
the same character set. IMPORTANT: if you do not supply this argument
and the file encoding is not compatible with the system's default
encoding, the compressor will throw an error. In particular, if your
file is encoded in utf-8, you should include this parameter.
Place output in file outfile. If not specified, the YUI Compressor will
default to the standard output, which you can redirect to a file.
Display informational messages and warnings.
Minify only. Do not obfuscate local symbols.
Preserve unnecessary semicolons (such as right before a '}') This option
is useful when compressed code has to be run through JSLint (which is the
case of YUI for example)
Disable all the built-in micro optimizations.Note: If no input file is specified, it defaults to stdin.
2.移除额外的空格
3.细微优化
4.标识符替换(Identifier Replacement)
• {"property":123},如果属性名是合法的 JavaScript 标识符且不是保留字,将优化为 {property:123} (注:在对象字面量中,如果属性名是一个合法的 JavaScript 标识符且不是保留字,并不强制要求用引号引住属性名)。
• 'abcd\'efgh',将优化为 "abcd'efgh"。
• "abcd" + "efgh",如果是字符串相连接,将优化成 "abcdefgh"(注:所有在使用 YUI Compressor 的前提下,对于脚本中的字符串连接,使用连接符 “+” 的效率和可维护性最高)。
对于 JavaScript 最有效的压缩优化,当属标识符替换。
2.全局变量:window、document、XMLHttpRequest等等。使用最多的就是 document、window。
3.属性名,比如:foo.bar。占据的空间仅次于字符串,”.” 操作符无法被代替,且 a.b.c 更加费空间。
4.关键字。经常被过度使用的关键字有:var、return。最好的优化方法:一个函数仅出现一次 var 和 return 关键字。
对于原始值、全局变量、属性名的优化处理方式大致相同:任何字面量值、全局变量或者属性名被使用超过 2 次(包括2次),都应该用局部变量存储代替。
2.使用 with 语句。解决方法:方法同上。
3.JScript 的条件注释。唯一解决的方法:不使用。
由于 YUI Compressor 是建立在 rhino interpreter 基础上的,所以上述所有的优化都是安全的。
《Extreme JavaScript Compression With YUI Compressor》:
http://www.slideshare.net/nzakas/extreme-javascript-compression-with-yui-compressor
来自“网眼视界”:http://blog.why100000.com
发表评论
-
Web编程是函数式编程
2010-11-30 13:44 1033任何一位在两个领域里 ... -
如何开发Web应用程序
2010-11-30 13:41 1068这是一个经常被问到的 ... -
设计Web应用程序时要注意可伸缩性
2010-11-26 09:19 882Max Indelicato是一位软件 ... -
Web 2.0应用客户端性能问题十大根源
2010-11-25 20:19 997Web 2.0应用的推广为用户带来了全新的体验,同时也让开 ... -
HTML压缩(JSP的GZIP实现)
2010-11-24 22:31 4903HTTP 压缩可以大大提高浏览网站的速度,它的 ... -
浏览器加载和渲染html的顺序
2010-11-22 09:45 25131.浏览器加载和渲染html的顺序 1、IE下载的顺序是从上到 ... -
在服务端合并和压缩JavaScript和CSS文件
2010-11-22 09:16 1095Web性能优化最佳实践中最重要的一条是减少HTTP请求 ... -
如何缓存DWR生成的JS文件
2010-11-18 17:37 1936DWR provides a convenient mec ... -
HTTP状态一览
2010-11-17 22:43 743在网站建设的实际应用中,容易出现很多小小的失误,就像m ... -
两款HTTP流量分析工具的比较:HTTP Watch,Fiddler
2010-11-17 17:26 0做Web开发或者Web分析经常需要查看Http通讯的过程, ... -
了解CSS的查找匹配原理,让CSS更简洁、高效
2010-11-17 16:49 0用了这么多年的CSS,现在才明白CSS的真正匹配原理,不知 ... -
高性能WEB开发 - flush让页面分块,逐步呈现
2010-11-17 16:47 0在处理比较耗时的请求的时候,我们总希望先让用户先 ... -
WEB高性能开发 - 疯狂的HTML压缩
2010-11-17 16:46 0前言: ... -
该如何加载google-analytics(或其他第三方)的JS
2010-11-17 16:44 0很多网站为了获取用户访问网站的统计信息,使用了go ... -
高性能WEB开发 - 页面呈现、重绘、回流。
2010-11-17 15:57 0页面呈现流程 在讨论页面重绘、回流之前。需要 ... -
高性能WEB开发 - JS、CSS的合并、压缩、缓存管理
2010-11-17 15:54 0本篇文章主要讨论下目前JS,CSS 合并、压缩、缓存 ... -
高性能WEB开发- 减少请求,响应的数据量
2010-11-17 15:49 0上一篇中我们说 ... -
高性能WEB开发 - 为什么要减少请求数,如何减少请求数!
2010-11-17 15:42 0http请求头的数据量 我们先分析下 ... -
高性能web开发 - 如何加载JS,JS应该放在什么位置?
2010-11-17 15:39 0外部JS的阻塞下载 所有浏览器在下载JS的时候, ... -
Web缓存教程
2010-11-17 15:08 1425原文(英文)地址: http://www.mnot.n ...
相关推荐
java -jar yuicompressor-2.4.2.jar --type js --charset utf-8 -v src.js > packed.js //压缩CSS java -jar yuicompressor-2.4.2.jar --type css --charset utf-8 -v src.css > packed.css 语法: java -jar ...
yuicompressor-2.4.2.jar yuicompressor-2.4.7.jar jsZip.exe yuicompressor yui compressor js压缩工具 javascript压缩工具 css压缩工具 ------------------------------------ //压缩JS java -jar yuicompressor-...
js、css 文件注释清空、代码压缩、中文字符串混淆等功能,绝对好用,已经测试过。yuicompressor(代码压缩工具)
YUI Compressor 支持类库,用于服务器端压缩与混淆 JS、CSS
hm-tools-js-css 功能:根据URL请求参数将多个javascript文件合并成一个javascript文件返回给请求者。根据URL请求参数将多个css文件合并成一个css文件返回...支持使用yuicompressor对js,css文件内容进行实时压缩、混淆
公司原先的JS和CSS文件都是用在线压缩程序压缩的,看似方便,但很容易出错,可能是压缩程序把CSS里面的一些Hack语句给混淆了,于是我决定用大名鼎鼎的YUI compressor。 但YUI compressor也有一个缺点,那就是操作不...
JSCSS压缩工具 YUI Compressor.zip JSON查询语言 Jaql.rar JSON类库 Flexjson.zip JSP标签 Noka Tag.rar JS和CSS压缩混淆 JsCompressor.rar js文件压缩工具 Closure Compiler.rar jviolajones(人脸检测算法)....
13)、JS和CSS压缩使用YUI Compressor。 14)、JAVA代码混淆使用Allatori。 15)、获取系统信息使用Sigar。 16)、单元测试使用JUnit。 17)、配置文件加密使用Jasypt。 18)、缓存使用EhCache或是Memcached。 19)、...
1. yuicompressor升级到2.4.2 2. 重写了compressor.cmd, 添加了比较详细的注释 3. 改变了在注册表的中的注册方式,以避免与css和js的默认编辑器引起冲突 4. 右键菜单项中,添加C作为快速选择键 ...
1.压缩、混淆、美化、格式化都支持,断网条件下可以使用,软件不是使用的在线API. 1.支持批量操作,可以同时处理JS与CSS文件,可以多文件导入,多文件夹搜寻,当然直接复制代码进行操作也可。 2.可以选择对原文件...
使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,时间显示和处理方法前帧。 Java的PList类库 Blister Blister是一个用于操作苹果二进制PList文件格式的Java开源类库(可用于发送数据给iOS应用...
使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,时间显示和处理方法前帧。 Java的PList类库 Blister Blister是一个用于操作苹果二进制PList文件格式的Java开源类库(可用于发送数据给iOS应用...
使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,时间显示和处理方法前帧。 Java的PList类库 Blister Blister是一个用于操作苹果二进制PList文件格式的Java开源类库(可用于发送数据给iOS应用...
使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,时间显示和处理方法前帧。 Java的PList类库 Blister Blister是一个用于操作苹果二进制PList文件格式的Java开源类库(可用于发送数据给iOS应用...
使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,时间显示和处理方法前帧。 Java的PList类库 Blister Blister是一个用于操作苹果二进制PList文件格式的Java开源类库(可用于发送数据给iOS应用...
使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,时间显示和处理方法前帧。 Java的PList类库 Blister Blister是一个用于操作苹果二进制PList文件格式的Java开源类库(可用于发送数据给iOS应用...
使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,时间显示和处理方法前帧。 Java的PList类库 Blister Blister是一个用于操作苹果二进制PList文件格式的Java开源类库(可用于发送数据给iOS应用...
使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,时间显示和处理方法前帧。 Java的PList类库 Blister Blister是一个用于操作苹果二进制PList文件格式的Java开源类库(可用于发送数据给iOS应用...
使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,时间显示和处理方法前帧。 Java的PList类库 Blister Blister是一个用于操作苹果二进制PList文件格式的Java开源类库(可用于发送数据给iOS应用...