使用minify来管理和压缩JS
十二月 4th, 2008
Minify 是使用 PHP5 开发的用于合并压缩 js/css 文件的应用程序。合并压缩之后的结果可通过 HTTP gzip/deflate 及一些相关头,优化客户端缓存。可参考 Yahoo 的 Rules for High Performance Web Sites。
Minify 的安装使用很简单,下载最新版本,解压,将 min 目录复制到发布目录下,然后访问 http://example.com/min/,在显示的界面中加入你想合并压缩的 js/css 路径,点击 ‘Update’ 之后会为你生成一个 url,如:
http://localhost/min/b=googletesting/js&f=mootools.js,iAction.js,iAjax.js,global.js
接下来你就可以将这个 url 放到你的页面中了。这样可以使请求数减少,传输的字节数也小了很多。而且 Minify 支持服务器端 cache,即将合并压缩的结果 cache 到服务器端文件中,下次访问就不需要再重新做合并压缩的操作了。如果需要组合的文件很多,url 就会变得很长,Minify 支持 group,可以将这些文件分组,这样 url 中只需指定 g=group名字 就可以了。
它具有的性能:
- Combines and minifies multiple CSS or JavaScript files into a single download
- Uses an enhanced port of Douglas Crockford’s JSMin library and custom classes to minify CSS and HTML
- Caches server-side to avoid doing unnecessary work
- Responds with an HTTP 304 (Not Modified) response when the browser has an up-to-date cache copy
- Most modules are lazy-loaded as needed (304 responses use minimal code)
- Automatically rewrites relative URIs in combined CSS files to point to valid locations
- With caching enabled, Minify is capable of handling hundreds of requests per second on a moderately powerful server.
- Content-Encoding: deflate/gzip, based on request headers. Caching allows it so serve deflated files faster than Apache’s mod_deflate option!
- Test cases for most components
- Easy integration of 3rd-party minifiers
- Separate utility classes for HTTP encoding and cache control
目前开发的情况下,单个页面中会有很多个JS文件,使用它的group和urlrewrite结合起来,同时和缓存结合起来,实现了JS发布与管理及维护的一系列工作。
确实是个好东西!

