Walk by faith code, hack, curious

JSMin

http://crockford.com/javascript/jsmin

JSMin
the javascript minifier
douglas crockford
www.crockform.com
2003-12-04

JSMin是一个过滤器,他能够移除js文件所有的注释和不需要的空行.他能够把js文件压缩到原来的一半左右,好吃就是能够很快的被下载到本地,它同时提倡一种更好看的编程风格,去除了那些不需要的东西.

JSMin做了些什么:
JSMin就是一个过滤器,他能够忽略并修改一些字符,当不改变程序的逻辑的情况下最小化js文件,可能这样做的结果是很难去debug,并且不容易阅读.

JSMin首先会将回车符合(carriage returns)替换为换行(linefeeds),同时把其他的所有的控制字符替换为一个空格,然后把单行注释(//)替换为一个换行.把多行注释替换为空格,多个空格会被替换为一个空格,多个换行会被替换为一个换行,

它会忽略空格,除非空格是一个非ascii编码的字节,或者一个ascii的字母或者数字,
或者下面的几个字符:
¥ $ _
通常他对于处理换行的情况比较谨慎,因为会起到分号的作用.如果一个换行符它是非ascii字节,或者是一个ascii字母或者数字,或者下面的几个字符:
\ $ _ { [ ( + –
这个时候是不会被忽略的.
and if it follows a non-ASCII character or an ASCII letter or digit or one of these characters:

\ $ _ } ] ) + – ” ‘

No other characters are omitted or modified.

JSMin knows to not modify quoted strings and regular expression literals.

JSMin does not obfuscate, but it does uglify.
Before:

// is.js

// (c) 2001 Douglas Crockford
// 2001 June 3

// is

// The -is- object is used to identify the browser.  Every browser edition
// identifies itself, but there is no standard way of doing it, and some of
// the identification is deceptive. This is because the authors of web
// browsers are liars. For example, Microsoft’s IE browsers claim to be
// Mozilla 4. Netscape 6 claims to be version 5.

var is = {
ie:      navigator.appName == ‘Microsoft Internet Explorer’,
java:    navigator.javaEnabled(),
ns:      navigator.appName == ‘Netscape’,
ua:      navigator.userAgent.toLowerCase(),
version: parseFloat(navigator.appVersion.substr(21)) ||
parseFloat(navigator.appVersion),
win:     navigator.platform == ‘Win32’
}
is.mac = is.ua.indexOf(‘mac’) >= 0;
if (is.ua.indexOf(‘opera’) >= 0) {
is.ie = is.ns = false;
is.opera = true;
}
if (is.ua.indexOf(‘gecko’) >= 0) {
is.ie = is.ns = false;
is.gecko = true;
}

After:

var is={ie:navigator.appName==’Microsoft Internet Explorer’,java:navigator.javaEnabled(),ns:navigator.appName==’Netscape’,ua:navigator.userAgent.toLowerCase(),version:parseFloat(navigator.appVersion.substr(21))||parseFloat(navigator.appVersion),win:navigator.platform==’Win32′}
is.mac=is.ua.indexOf(‘mac’)>=0;if(is.ua.indexOf(‘opera’)>=0){is.ie=is.ns=false;is.opera=true;}
if(is.ua.indexOf(‘gecko’)>=0){is.ie=is.ns=false;is.gecko=true;}

字符编码:
jsmin需要但是并不会去验证输入到程序里的字符编码格式,ascii和utf-8都是可以的,但是不保证其他的编码格式也是正确的.
注意:
确保备份你的原文件,因为jsmin不会给你提供一个撤销的功能.
Do not put raw control characters inside a quoted string. That is an extremely bad practice. Use \xhh notation instead. JSMin will replace control characters with spaces or linefeeds.

使用括号来区别混乱的序列,举例,最小化想把

a + ++b

改为

a+++b

当时会被改成这样子:

a++ + b

这个时候就需要括号来清楚地区分顺序:

a + (++b)

JSLint(http://www.jslint.com/) checks for all of these problems. It is suggested that JSLint be used before using JSMin.
建议在使用jsmin之前使用jslint来检测代码质量

命令行选项:
Optional parameters will be listed at the beginning of the output as comments.
This is a convenient way of replacing copyright messages and other documentation.

Example:

jsmin <fulljslint.js >jslint.js “(c)2002 Douglas Crockford”

Errors

JSMin can produce three error messages to stderr:

Unterminated comment.

Unterminated string constant.

Unterminated regular expression.

It ignores all other errors that may be present in your source program.

一些有用的地址:

http://www.yuiblog.com/blog/
http://coderjournal.com/
http://developer.yahoo.com/
http://yuiblog.com/blog/2006/03/06/minification-v-obfuscation/
http://www.julienlecomte.net/blog/2007/08/11/
http://dojotoolkit.org/
http://dean.edwards.name/packer/
http://compressorrater.thruhere.net/
http://en.wikipedia.org/wiki/Http_compression