ESC Introduction
ESC is an ECMAScript pre-processor written in JScript, enabling an unlimited number of external scripts to be compressed/crunched into supertight, bandwidth-optimized packages. Featuring several compression-techniques such as comment removal, whitespace stripping, newline stripping and variable substitution ESC can reduce the overall size of your code with up to ~45%. Single, multiple scripts and even directories with scripts can be merged together at the compression level you decide. The processed output can later be appended or written to a file, or piped to another application for further processing via STDOUT.
ESC do NOT support crunching of inline scripts. So any attempt passing HTML, ASP, JSP, PHP or other equivalent documents to ESC is done at your own risk.
ESC supports four levels of compression, where a higher level equals higher compression. Beware though that levels >2 requires your code to be syntaxically perfect or ESC will punish you by producing a broken and useless output.
The compression ratio should hit around 25% using the default compression level on a vanilla looking script, but results as high as ~45% can be achieved depending on the script's design / your style of writing code.
ESC's compression engine is intelligent in the meaning that it has language syntax, statement and keyword awareness and it *knows* about native objects and members provided by the most common scripting hosts. This knowledgebase can easily be extended by pluggin in userdefined maps with additional information to further gain control of the crunching procedure. During processing following things are taken into account :
String and RegExpression read-ahead
Jscript Conditional compilation statements and variables
ECMA-262 Core language definitions (ECMAScript)
Intermediate DOM's (level 0) and DOM level 1
MS Jscript specific objects/methods
Netscape/Mozilla/Opera specific objects/methods/properties
MS WSH (Windows Scripting Host) 1+ native objects
压缩级别分为5种,从0到4
Level 0 :: No compression
Level 1 :: Comment removal
Level 2 :: Whitespace removal
Level 3 :: Newline removal
Level 4 :: Variable substitution
在WINDOWS命令行下执行
cscript ESC.wsf -ow menu2.js menu.js将会把menu.js按照js压缩级别2来压缩(默认js压缩级别为2)为menu2.js
cscript ESC.wsf -l 3 -ow menu3.js menu.js将会把menu.js按照js压缩级别3来压缩为menu3.js
需要注意的是,js压缩级别4会把变量名修改,如果你的js中用到了全局变量或者类的话,就不能使用该压缩级别了,否则其它使用你的js的文件可能会无法正常运行。
试了一下,把yui的menu.js压缩了一下,对应的级别和压缩率分别如下:
js压缩级别1:压缩率44.41%
js压缩级别2:压缩率62.82%
js压缩级别1:压缩率64.93%
原来130多K的js文件压缩后也就40多K,看来还是挺有用处的。
以上是引用原作者的话
##################################################################
如何批量压缩JAVASCRIPT?
!!!!使用前请先到设置参数段设置一下参数!!!!
用法:在WINDOWS下,将想压缩的JAVASCRIPT脚本拖放至此RUN.BAT上
之后将会进行一次性批量压缩,压缩后的脚本为原脚本路径下/C_原文件名
仅仅是为了让大家更方便的利用ESC.wsf压缩JAVASCRIPT,所以请大家保护原作者的权益。
欢迎大家使用此脚本,但请不要更改作者与出处
注:此脚本调用ESC.wsf压缩核心,用于批量压缩JAVASCRIPT脚本
AUTHOR:LIMING [leession@live.cn]