星期四, 十月 29, 2009

使用 YUI Compressor 壓縮 CSS / JavaScript

使用 Yahoo! 提供的 YUI Compressor 來壓縮(Minify) JavaScript 和 CSS.

註: 此文件操作環境於 Ubuntu / Debian Linux.

官方文件

環境設定

  1. 安裝 Java 執行環境: apt-get install sun-java6-jre
  2. 下載 yuicompressor-2.4.2.zip
  3. unzip yuicompressor-2.4.2.zip

壓縮 JavaScript

  • java -jar yuicompressor-x.y.z.jar myfile.js -o myfile-min.js --charset utf-8 --type js

壓縮 CSS

  • java -jar yuicompressor-x.y.z.jar myfile.css -o myfile-min.css --charset utf-8 --type css

範例

  • # 不用輸入 --type js / css, yuicompressor 依照檔名也會自動判斷.
  • java -jar yuicompressor-2.4.2.jar myfile.js -o myfile-min.js --charset utf-8 --preserve-semi # 保留分號(--preserve-semi)
  • java -jar yuicompressor-2.4.2.jar myfile.css -o myfile-min.css --charset utf-8

參數說明

Usage: java -jar yuicompressor-x.y.z.jar [options] [input file]

Global Options
  -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.

JavaScript Options
  --nomunge                 Minify only, do not obfuscate
  --preserve-semi           Preserve all semicolons
  --disable-optimizations   Disable all micro optimizations

延伸閱讀

相關標籤

this is comment icon Closure from Google does similar thing. [回覆]

可以用Closure 啦!基本上 所有Yahoo 或Microsoft 提供的 Google 都有 也都會提供

Comment by Git Trac (11/06/2009 22:07)

this is comment icon 回 Git Trac [回覆]

嗯嗯, Closure 超強大, 也正打算開始研究看看~ :)

Comment by Tsung (11/07/2009 08:47)
Add this page to del.icio.us

發表迴響

標題

內容 (限制 1000 字)

暱稱

電子郵件

個人網頁


 authimage


PS: 若無法留言, 請先確認是否有打開 JavaScript, 造成您的困擾, 實在萬分對不起 Orz...(如果無法留言, 勞煩可以發信給我好嗎? 謝謝.)
PS2: 若您的留言被誤判, 我都會再自行看過, 不需要一直重覆張貼~