冒著被人罵的風險發來首頁,要是我火星了。請無視我吧……
jquery 1.5 發布的時候 john resig 大神說所用的代碼最佳化程式從Google Closure切換到UglifyJS,新工具的壓縮效果非常令人滿意。 UglifyJS 是一個服務端node.js的壓縮程式。據說很黃很暴力……
我測試了一下壓縮率確實比較高。 所以值得寫篇文章推薦下。
--------------------------------------------------------------------------------------------
你也可以嘗試一下線上版的Uglifyjs: https://ganquan.info/webkit/
--------------------------------------------------------------------------------------------
如果你對 uglifyjs 有興趣可以按照以下安裝方式進行安裝。
1. 安裝 node.js 環境 (這個不用我教了吧,網上教程一大堆哦。)
2. 進入 https://github.com/mishoo/UglifyJS 右上方 “Download” ZIP下載整個包。
3. 解壓開啟 UglifyJS/bin/uglifyjs
4. 找到
global.sys = require(/^v0\.[012]/.test(process.version) ? "sys" : "util");var fs = require("fs");var uglify = require("uglify-js"), // symlink ~/.node_libraries/uglify-js.js to ../uglify-js.js jsp = uglify.parser,
pro = uglify.uglify;
替換為
global.sys = require(/^v0\.[012]/.test(process.version) ? "sys" : "util");
var fs = require("fs");require.paths.unshift(__dirname + "/../");var uglify = require("index"),jsp = uglify.parser,pro = uglify.uglify;
5. cd 命令到 UglifyJS/bin/ 目錄。執行 ./uglifyjs xx.js 如果安裝成功會把你的壓縮代碼直接顯示在命令列。
你可以通過如下命令形式 ./uglifyjs 1.js 2.js 把 1.js 壓縮後的代碼儲存到 2.js
至此 uglifyjs 安裝完成,我還寫了一個PHP類。可以調用 uglifyjs 進行壓縮
<?php
/*
------------------------------------
# code by https://fengyin.name
# DEMO http://sweet.fengyin.name/
# Dual licensed under the MIT
------------------------------------
*/
$uglifyjs = new uglifyjs(array(
'node_home'=>'/usr/local/bin/node',
'uglifyjs_path'=>'/usr/UglifyJS/bin/uglifyjs', //uglifyjs壓縮公用程式的路徑。
// -------- 附加的參數 --------- //
'prefix'=>'-b', //輸入的參數 -b 是格式化 不輸入為壓縮,還有更多請參考命令用法。
'append'=>''//儲存檔案,如果為空白就是直接在命令列輸出結果。
));
$results = $uglifyjs ->compress($_FILES['file']['tmp_name']);
//////////////////////////
class uglifyjs{
function __construct($options = array()) {
$this->options = $options;
}
function args($option) {
return $option['node_home'].' '.$option['uglifyjs_path'].' '.$option['prefix'].' '.$option['file'].' '.$option['append'];
}
function exec($cmd) {
exec($cmd.' 2>&1',$out, $status);
return json_encode(array(
'shell' => $cmd,
'output' => implode("\n",$out),
'status' => $status
));
}
function compress($file) {
$this->options['file'] = $file;
return $this->exec($this->args($this->options));
}
}
?>
文章最後附上 uglifyjs 的命令列中文說明
(
限於自己的英語水平超級爛,請諸位將就看看就行。
有一些我都不明白的,就沒有翻譯,還有一些翻譯錯的,請多多包涵,一切以英文原版為準。
以下僅供參考~~ 不對翻譯的準確性負責。
)
使用:這是一個指令碼助手(工具)-bin/uglifyjs-使用這個庫可以用來把一個指令碼壓縮到最小。簡介:uglifyjs [ 選項... ] [ 檔案 ]檔案參數應該放在選項後面,uglifyjs 會讀取檔案中的javascript代碼進行處理。如果你不指定輸出的檔案名稱,那麼他會把處理後的內容輸出到命令列中。例: uglifyjs 1.js 2.js 會建立一個 2.js 檔案把 1.js處理完畢的內容輸入到這裡例: uglifyjs 1.js 會讀取1.js內容,並把結果輸出到命令列中。支援的選項:-b or --beautify — 參數 -b 或者 --beautify 用於美化(格式化)代碼。美化之前會壓縮代碼(未驗證。)-i N or --indent N — 縮排層級空格數例:uglifyjs -b 1.js 2.js 會把1.js的內容格式後輸出到2.js-q or --quote-keys — quote keys in literal objects (by default, only keys that cannot be identifier names will be quotes).-nm or --no-mangle — 變數名不縮短,意味著不會把代碼的變數名縮短為 abcdefg -ns or --no-squeeze — 不使用ast_squeeze()(可以使各種最佳化之後的代碼更小,更具有可讀性。)-mt or --mangle-toplevel — mangle names in the toplevel scope too (by default we don’t do this).--no-seqs — 當ast_squeeze()被調用的時候(除非你添加了--no-squeeze 參數才不會被調用)它將減少在一個代碼塊中重複聲明的行。例如:“a = 10; b = 20; foo();”將被重寫為“a=10,b=20,foo();”。在各種場合,這將允許我們丟棄代碼塊中的括弧(在一些語句塊變成一個獨立的聲明行之後)。這是預設的,因為經我們測試,它似乎更安全並且能節省幾百bytes,但加上--no-seqs 後將被禁止。--no-dead-code — 預設情況下,UglifyJS將會刪除代碼中明顯無法訪問的語句(如 return , throw , break 或者continue語句 ,以及一些不是函數/變數的的聲明)。通過這個選項可以禁用掉這個最佳化功能。-nc or --no-copyright — 預設情況下,uglifyjs將在產生的程式碼中保留初始的一些標記(假設著作權資訊等)。如果你設定這個參數將被禁止。-o filename or --output filename — 將結果輸出到一個指定檔案名稱的檔案中。如果沒有這個參數,結果將會被輸出到命令列--overwrite — 如果代碼是從一個檔案中讀取的(不是直接標準輸入的)並且使用了--overwrite參數,那麼結果也將會被寫進相同的檔案中去。--ast — pass this if you want to get the Abstract Syntax Tree instead of JavaScript as output. Useful for debugging or learning more about the internals.--ast — pass 如果你要擷取一個用來取代javascript輸出的抽象文法樹,這些參數會在你進行調試或學習內部組件時很有用。 -v or --verbose — output some notes on STDERR (for now just how long each operation takes).-v or --verbose — output 一些標準錯誤提示(顯示每一次花費多久)
--extra — enable additional optimizations that have not yet been extensively tested. These might, or might not, break your code. If you find a bug using this option, please report a test case.當你發現BUG時你可以增加一個中斷點用來對還沒有徹底的測試的代碼進行最佳化,你可以使用這個選項來產生一個測試案例。 --extra — enable --unsafe — enable other additional optimizations that are known to be unsafe in some contrived situations, but could still be generally useful. For now only this:foo.toString() ==> foo+””
想foo.toString() 這種用法 也是可行的,但是存在一些人為的不安全的情況,這事也需要添加一些最佳化比如: foo+”” --max-line-len (default 32K characters) — add a newline after around 32K characters. I’ve seen both FF and Chrome croak when all the code was on a single line of around 670K. Pass –max-line-len 0 to disable this safety feature.在超過大概32K位元組的時候你要增加一行,我有發現過在Firefox和chorme下檔代碼一行超過670K的時候它會發出警示聲。通過–max-line-len 0 去靜止這個安全屬性--reserved-names — some libraries rely on certain names to be used, as pointed out in issue #92 and #81, so this option allow you to exclude such names from the mangler. For example, to keep names require and $super intact you’d specify –reserved-names “require,$super”.
--reserved-names — 些類庫會依賴於特定的名稱去使用,比如:#92 和#81 所以這個選項允許你去排除一些關鍵字。