javascript 代碼優化工具 UglifyJS

安裝:javascript

1. 安裝 node.js 環境 (這個不用我教了吧,網上教程一大堆哦。) java

2. 進入 https://github.com/mishoo/UglifyJS  右上角 「Download」 ZIP下載整個包。node

3. 解壓打開 UglifyJS/bin/uglifyjsgit

4. 找到github

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; 安全

  替換爲jsp

global.sys = require(/^v0\.[012]/.test(process.version) ? "sys" : "util");ide

var fs = require("fs");
require.paths.unshift(__dirname + "/../");
var uglify = require("index"),
jsp = uglify.parser,
pro = uglify.uglify;
 
使用:
 ./uglifyjs 1.js 2.js 把 1.js 壓縮後的代碼保存到 2.js
 
命令行說明:
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字節的時候你要增長一行,我有發現過在火狐和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 因此這個選項容許你去排除一些關鍵字。
相關文章
相關標籤/搜索