Dojo的压缩
内容:
http://dojotoolkit.org/docs/compressor_system.html
在Dojo的build过程最后一个可选步骤是compression(压缩).像其它JavaScript工具一样,Dojo也用工具减少代码量,可以被浏览器更快的下载.
当然,太多的压缩工具存在,但各方面因素决定Dojo的压缩工具是唯一的.
Example
为了运行以下的例子,推荐你安装jdk1.4.从Dojo官网下载压缩工具.
http://dojotoolkit.org/svn…
如果你看了前文,D:\zkj\dojosource\buildscripts\lib下有这个包
然后编辑infile.js文件,内容如下:
function MyClass(){
this.foo = function(argument1, argument2){
var addedArgs = parseInt(argument1)+parseInt(argument2);
return addedArgs;
}
var anonymousInnerFunction = function(){
// do stuff here!
}
}
function MyFunc(){
// this is a top-level function
}
// 你可以输入一些空格
我们在命令行输入以下命令得到 outfile.js文件
java -jar custom_rhino.jar -c infile.js > outfile.js 2>&1
outfile.js的内容是以下代码.
function MyClass(){
this.foo=function(_1,_2){
var _3=parseInt(_1)+parseInt(_2);
return _3;
};
var _4=function(){
};
}
function MyFunc(){
}
你可以比较一下文件大小.
obelisk:/tmp/compress alex$ ls -lah
…
-rw-r–r– 1 alex users 321B Aug 12 09:21 infile.js
-rw-r–r– 1 alex users 140B Aug 12 09:21 outfile.js
321 bytes to 140 bytes, a 56% reduction. Not bad!
我在window操作系统,但看代码也知道压缩不少.
Riding a Rhino
这些工作是怎么完成的呢?为什么选择这个工具而不是其它呢?Dojo的compressor基础是Rhino. Rhino是来自the Mozilla project的javascript解释引擎(java实现).而其它一些压缩工具一般是自定义的规则表达式来完成压缩的.
相比那些regular-expression为基础的工具,通过Rhino对js代码的解析,Dojo compressor能得到更好的效果,体现在变量的上下文环境,变量名等.Dojo compressor不会改变代码的public API.
API Safety
There are many "obfuscators" available in addition to size reduction tools. Over the years, many people have attempted to "encrypt" or otherwise obfuscate JavaScript sent over the wire to browsers, and it never pans out. Why not? For starters, JavaScript (as implemented in browsers) is completely interpreted. This means that any further compilation beyond source transformations will not work everywhere, and the tool provides a "decryption" tool along with the "encrypted" or obfuscated source, the unencrypted version will be available at runtime for anyone with a debugger to see. For those tools that just transform source code by mangling variable names, it’s even easier to revert their changes. Therefore, obfuscation and encryption aren’t useful goals. Size reduction, on the other hand, is a useful goal.
But not if your size-reduction tool breaks things. There are, of course, many increments available for the "compression" process. Potential choices available to a tool author include:
· removing comments
· collapsing line-beginning whitespace
· removing line-ending whitespace
· collapsing multiple blank lines
· removing all new-line characters
· removing whitespace around operators
· removing whitespace near/around curly braces
· replacing symbols with shorter names (this is how most "obfuscation" is done)
And the list goes on and gets ever-more esoteric as one tries to squeeze every last K out of a JavaScript file. But at some point, you can go too far. The Dojo compressor attempts to strike a balance between debuggability (not replacing all symbols and not removing all newlines) and size reduction.
Getting The Source
Rhino的源代码可以从Mozilla CVS服务器得到:
http://www.mozilla.org/cvs…
具体路径在:
/cvsroot/mozilla/js/rhino
Our patches should apply cleanly against Rhino HEAD and are available in unified diff format from:
http://dojotoolkit.org/svn…
Unlike the rest of Dojo, the Dojo Foundation does not control the copyright of the original work, and we therefore cannot license this code under there AFL. It is made available under the tri-licensing terms of the Mozilla project.
The Future
The Dojo compression tool is by no means the last word in file-size or on-the-wire reduction. Gzipping content on the wire is the next obvious improvement for those deploying applications.
The Dojo package system and JSAN allow developers to include just those packages from a library that they require, and future work on real JS linkers will further strip down capable libraries like Dojo to only absolutely what is needed by application authors.
The Dojo project intends to continue to produce the best Open Source tools for JS and webapp developers, and we will make these transparently available in the Dojo build system, as we do today with the compression and package systems.
About The Author
Alex Russell is the project lead for Dojo and can be reached at . His blog is at: http://alex.dojotoolkit.or…