README.html 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5. lang="en" xml:lang="en">
  6. <head>
  7. <title>UglifyJS &ndash; a JavaScript parser/compressor/beautifier</title>
  8. <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  9. <meta name="generator" content="Org-mode"/>
  10. <meta name="generated" content="2011-12-09 14:59:08 EET"/>
  11. <meta name="author" content="Mihai Bazon"/>
  12. <meta name="description" content="a JavaScript parser/compressor/beautifier in JavaScript"/>
  13. <meta name="keywords" content="javascript, js, parser, compiler, compressor, mangle, minify, minifier"/>
  14. <style type="text/css">
  15. <!--/*--><![CDATA[/*><!--*/
  16. html { font-family: Times, serif; font-size: 12pt; }
  17. .title { text-align: center; }
  18. .todo { color: red; }
  19. .done { color: green; }
  20. .tag { background-color: #add8e6; font-weight:normal }
  21. .target { }
  22. .timestamp { color: #bebebe; }
  23. .timestamp-kwd { color: #5f9ea0; }
  24. .right {margin-left:auto; margin-right:0px; text-align:right;}
  25. .left {margin-left:0px; margin-right:auto; text-align:left;}
  26. .center {margin-left:auto; margin-right:auto; text-align:center;}
  27. p.verse { margin-left: 3% }
  28. pre {
  29. border: 1pt solid #AEBDCC;
  30. background-color: #F3F5F7;
  31. padding: 5pt;
  32. font-family: courier, monospace;
  33. font-size: 90%;
  34. overflow:auto;
  35. }
  36. table { border-collapse: collapse; }
  37. td, th { vertical-align: top; }
  38. th.right { text-align:center; }
  39. th.left { text-align:center; }
  40. th.center { text-align:center; }
  41. td.right { text-align:right; }
  42. td.left { text-align:left; }
  43. td.center { text-align:center; }
  44. dt { font-weight: bold; }
  45. div.figure { padding: 0.5em; }
  46. div.figure p { text-align: center; }
  47. div.inlinetask {
  48. padding:10px;
  49. border:2px solid gray;
  50. margin:10px;
  51. background: #ffffcc;
  52. }
  53. textarea { overflow-x: auto; }
  54. .linenr { font-size:smaller }
  55. .code-highlighted {background-color:#ffff00;}
  56. .org-info-js_info-navigation { border-style:none; }
  57. #org-info-js_console-label { font-size:10px; font-weight:bold;
  58. white-space:nowrap; }
  59. .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
  60. font-weight:bold; }
  61. /*]]>*/-->
  62. </style>
  63. <link rel="stylesheet" type="text/css" href="docstyle.css" />
  64. <script type="text/javascript">
  65. <!--/*--><![CDATA[/*><!--*/
  66. function CodeHighlightOn(elem, id)
  67. {
  68. var target = document.getElementById(id);
  69. if(null != target) {
  70. elem.cacheClassElem = elem.className;
  71. elem.cacheClassTarget = target.className;
  72. target.className = "code-highlighted";
  73. elem.className = "code-highlighted";
  74. }
  75. }
  76. function CodeHighlightOff(elem, id)
  77. {
  78. var target = document.getElementById(id);
  79. if(elem.cacheClassElem)
  80. elem.className = elem.cacheClassElem;
  81. if(elem.cacheClassTarget)
  82. target.className = elem.cacheClassTarget;
  83. }
  84. /*]]>*///-->
  85. </script>
  86. </head>
  87. <body>
  88. <div id="preamble">
  89. </div>
  90. <div id="content">
  91. <h1 class="title">UglifyJS &ndash; a JavaScript parser/compressor/beautifier</h1>
  92. <div id="table-of-contents">
  93. <h2>Table of Contents</h2>
  94. <div id="text-table-of-contents">
  95. <ul>
  96. <li><a href="#sec-1">1 UglifyJS &mdash; a JavaScript parser/compressor/beautifier </a>
  97. <ul>
  98. <li><a href="#sec-1-1">1.1 Unsafe transformations </a>
  99. <ul>
  100. <li><a href="#sec-1-1-1">1.1.1 Calls involving the global Array constructor </a></li>
  101. <li><a href="#sec-1-1-2">1.1.2 <code>obj.toString()</code> ==&gt; <code>obj+“”</code> </a></li>
  102. </ul>
  103. </li>
  104. <li><a href="#sec-1-2">1.2 Install (NPM) </a></li>
  105. <li><a href="#sec-1-3">1.3 Install latest code from GitHub </a></li>
  106. <li><a href="#sec-1-4">1.4 Usage </a>
  107. <ul>
  108. <li><a href="#sec-1-4-1">1.4.1 API </a></li>
  109. <li><a href="#sec-1-4-2">1.4.2 Beautifier shortcoming &ndash; no more comments </a></li>
  110. <li><a href="#sec-1-4-3">1.4.3 Use as a code pre-processor </a></li>
  111. </ul>
  112. </li>
  113. <li><a href="#sec-1-5">1.5 Compression &ndash; how good is it? </a></li>
  114. <li><a href="#sec-1-6">1.6 Bugs? </a></li>
  115. <li><a href="#sec-1-7">1.7 Links </a></li>
  116. <li><a href="#sec-1-8">1.8 License </a></li>
  117. </ul>
  118. </li>
  119. </ul>
  120. </div>
  121. </div>
  122. <div id="outline-container-1" class="outline-2">
  123. <h2 id="sec-1"><span class="section-number-2">1</span> UglifyJS &mdash; a JavaScript parser/compressor/beautifier </h2>
  124. <div class="outline-text-2" id="text-1">
  125. <p>
  126. This package implements a general-purpose JavaScript
  127. parser/compressor/beautifier toolkit. It is developed on <a href="http://nodejs.org/">NodeJS</a>, but it
  128. should work on any JavaScript platform supporting the CommonJS module system
  129. (and if your platform of choice doesn't support CommonJS, you can easily
  130. implement it, or discard the <code>exports.*</code> lines from UglifyJS sources).
  131. </p>
  132. <p>
  133. The tokenizer/parser generates an abstract syntax tree from JS code. You
  134. can then traverse the AST to learn more about the code, or do various
  135. manipulations on it. This part is implemented in <a href="../lib/parse-js.js">parse-js.js</a> and it's a
  136. port to JavaScript of the excellent <a href="http://marijn.haverbeke.nl/parse-js/">parse-js</a> Common Lisp library from <a href="http://marijn.haverbeke.nl/">Marijn Haverbeke</a>.
  137. </p>
  138. <p>
  139. ( See <a href="http://github.com/mishoo/cl-uglify-js">cl-uglify-js</a> if you're looking for the Common Lisp version of
  140. UglifyJS. )
  141. </p>
  142. <p>
  143. The second part of this package, implemented in <a href="../lib/process.js">process.js</a>, inspects and
  144. manipulates the AST generated by the parser to provide the following:
  145. </p>
  146. <ul>
  147. <li>ability to re-generate JavaScript code from the AST. Optionally
  148. indented&mdash;you can use this if you want to “beautify” a program that has
  149. been compressed, so that you can inspect the source. But you can also run
  150. our code generator to print out an AST without any whitespace, so you
  151. achieve compression as well.
  152. </li>
  153. <li>shorten variable names (usually to single characters). Our mangler will
  154. analyze the code and generate proper variable names, depending on scope
  155. and usage, and is smart enough to deal with globals defined elsewhere, or
  156. with <code>eval()</code> calls or <code>with{}</code> statements. In short, if <code>eval()</code> or
  157. <code>with{}</code> are used in some scope, then all variables in that scope and any
  158. variables in the parent scopes will remain unmangled, and any references
  159. to such variables remain unmangled as well.
  160. </li>
  161. <li>various small optimizations that may lead to faster code but certainly
  162. lead to smaller code. Where possible, we do the following:
  163. <ul>
  164. <li>foo["bar"] ==&gt; foo.bar
  165. </li>
  166. <li>remove block brackets <code>{}</code>
  167. </li>
  168. <li>join consecutive var declarations:
  169. var a = 10; var b = 20; ==&gt; var a=10,b=20;
  170. </li>
  171. <li>resolve simple constant expressions: 1 +2 * 3 ==&gt; 7. We only do the
  172. replacement if the result occupies less bytes; for example 1/3 would
  173. translate to 0.333333333333, so in this case we don't replace it.
  174. </li>
  175. <li>consecutive statements in blocks are merged into a sequence; in many
  176. cases, this leaves blocks with a single statement, so then we can remove
  177. the block brackets.
  178. </li>
  179. <li>various optimizations for IF statements:
  180. <ul>
  181. <li>if (foo) bar(); else baz(); ==&gt; foo?bar():baz();
  182. </li>
  183. <li>if (!foo) bar(); else baz(); ==&gt; foo?baz():bar();
  184. </li>
  185. <li>if (foo) bar(); ==&gt; foo&amp;&amp;bar();
  186. </li>
  187. <li>if (!foo) bar(); ==&gt; foo||bar();
  188. </li>
  189. <li>if (foo) return bar(); else return baz(); ==&gt; return foo?bar():baz();
  190. </li>
  191. <li>if (foo) return bar(); else something(); ==&gt; {if(foo)return bar();something()}
  192. </li>
  193. </ul>
  194. </li>
  195. <li>remove some unreachable code and warn about it (code that follows a
  196. <code>return</code>, <code>throw</code>, <code>break</code> or <code>continue</code> statement, except
  197. function/variable declarations).
  198. </li>
  199. <li>act a limited version of a pre-processor (c.f. the pre-processor of
  200. C/C++) to allow you to safely replace selected global symbols with
  201. specified values. When combined with the optimisations above this can
  202. make UglifyJS operate slightly more like a compilation process, in
  203. that when certain symbols are replaced by constant values, entire code
  204. blocks may be optimised away as unreachable.
  205. </li>
  206. </ul>
  207. </li>
  208. </ul>
  209. </div>
  210. <div id="outline-container-1-1" class="outline-3">
  211. <h3 id="sec-1-1"><span class="section-number-3">1.1</span> <span class="target">Unsafe transformations</span> </h3>
  212. <div class="outline-text-3" id="text-1-1">
  213. <p>
  214. The following transformations can in theory break code, although they're
  215. probably safe in most practical cases. To enable them you need to pass the
  216. <code>--unsafe</code> flag.
  217. </p>
  218. </div>
  219. <div id="outline-container-1-1-1" class="outline-4">
  220. <h4 id="sec-1-1-1"><span class="section-number-4">1.1.1</span> Calls involving the global Array constructor </h4>
  221. <div class="outline-text-4" id="text-1-1-1">
  222. <p>
  223. The following transformations occur:
  224. </p>
  225. <pre class="src src-js"><span class="org-keyword">new</span> <span class="org-type">Array</span>(1, 2, 3, 4) =&gt; [1,2,3,4]
  226. Array(a, b, c) =&gt; [a,b,c]
  227. <span class="org-keyword">new</span> <span class="org-type">Array</span>(5) =&gt; Array(5)
  228. <span class="org-keyword">new</span> <span class="org-type">Array</span>(a) =&gt; Array(a)
  229. </pre>
  230. <p>
  231. These are all safe if the Array name isn't redefined. JavaScript does allow
  232. one to globally redefine Array (and pretty much everything, in fact) but I
  233. personally don't see why would anyone do that.
  234. </p>
  235. <p>
  236. UglifyJS does handle the case where Array is redefined locally, or even
  237. globally but with a <code>function</code> or <code>var</code> declaration. Therefore, in the
  238. following cases UglifyJS <b>doesn't touch</b> calls or instantiations of Array:
  239. </p>
  240. <pre class="src src-js"><span class="org-comment-delimiter">// </span><span class="org-comment">case 1. globally declared variable</span>
  241. <span class="org-keyword">var</span> <span class="org-variable-name">Array</span>;
  242. <span class="org-keyword">new</span> <span class="org-type">Array</span>(1, 2, 3);
  243. Array(a, b);
  244. <span class="org-comment-delimiter">// </span><span class="org-comment">or (can be declared later)</span>
  245. <span class="org-keyword">new</span> <span class="org-type">Array</span>(1, 2, 3);
  246. <span class="org-keyword">var</span> <span class="org-variable-name">Array</span>;
  247. <span class="org-comment-delimiter">// </span><span class="org-comment">or (can be a function)</span>
  248. <span class="org-keyword">new</span> <span class="org-type">Array</span>(1, 2, 3);
  249. <span class="org-keyword">function</span> <span class="org-function-name">Array</span>() { ... }
  250. <span class="org-comment-delimiter">// </span><span class="org-comment">case 2. declared in a function</span>
  251. (<span class="org-keyword">function</span>(){
  252. a = <span class="org-keyword">new</span> <span class="org-type">Array</span>(1, 2, 3);
  253. b = Array(5, 6);
  254. <span class="org-keyword">var</span> <span class="org-variable-name">Array</span>;
  255. })();
  256. <span class="org-comment-delimiter">// </span><span class="org-comment">or</span>
  257. (<span class="org-keyword">function</span>(<span class="org-variable-name">Array</span>){
  258. <span class="org-keyword">return</span> Array(5, 6, 7);
  259. })();
  260. <span class="org-comment-delimiter">// </span><span class="org-comment">or</span>
  261. (<span class="org-keyword">function</span>(){
  262. <span class="org-keyword">return</span> <span class="org-keyword">new</span> <span class="org-type">Array</span>(1, 2, 3, 4);
  263. <span class="org-keyword">function</span> <span class="org-function-name">Array</span>() { ... }
  264. })();
  265. <span class="org-comment-delimiter">// </span><span class="org-comment">etc.</span>
  266. </pre>
  267. </div>
  268. </div>
  269. <div id="outline-container-1-1-2" class="outline-4">
  270. <h4 id="sec-1-1-2"><span class="section-number-4">1.1.2</span> <code>obj.toString()</code> ==&gt; <code>obj+“”</code> </h4>
  271. <div class="outline-text-4" id="text-1-1-2">
  272. </div>
  273. </div>
  274. </div>
  275. <div id="outline-container-1-2" class="outline-3">
  276. <h3 id="sec-1-2"><span class="section-number-3">1.2</span> Install (NPM) </h3>
  277. <div class="outline-text-3" id="text-1-2">
  278. <p>
  279. UglifyJS is now available through NPM &mdash; <code>npm install uglify-js</code> should do
  280. the job.
  281. </p>
  282. </div>
  283. </div>
  284. <div id="outline-container-1-3" class="outline-3">
  285. <h3 id="sec-1-3"><span class="section-number-3">1.3</span> Install latest code from GitHub </h3>
  286. <div class="outline-text-3" id="text-1-3">
  287. <pre class="src src-sh"><span class="org-comment-delimiter">## </span><span class="org-comment">clone the repository</span>
  288. mkdir -p /where/you/wanna/put/it
  289. <span class="org-builtin">cd</span> /where/you/wanna/put/it
  290. git clone git://github.com/mishoo/UglifyJS.git
  291. <span class="org-comment-delimiter">## </span><span class="org-comment">make the module available to Node</span>
  292. mkdir -p ~/.node_libraries/
  293. <span class="org-builtin">cd</span> ~/.node_libraries/
  294. ln -s /where/you/wanna/put/it/UglifyJS/uglify-js.js
  295. <span class="org-comment-delimiter">## </span><span class="org-comment">and if you want the CLI script too:</span>
  296. mkdir -p ~/bin
  297. <span class="org-builtin">cd</span> ~/bin
  298. ln -s /where/you/wanna/put/it/UglifyJS/bin/uglifyjs
  299. <span class="org-comment-delimiter"># </span><span class="org-comment">(then add ~/bin to your $PATH if it's not there already)</span>
  300. </pre>
  301. </div>
  302. </div>
  303. <div id="outline-container-1-4" class="outline-3">
  304. <h3 id="sec-1-4"><span class="section-number-3">1.4</span> Usage </h3>
  305. <div class="outline-text-3" id="text-1-4">
  306. <p>
  307. There is a command-line tool that exposes the functionality of this library
  308. for your shell-scripting needs:
  309. </p>
  310. <pre class="src src-sh">uglifyjs [ options... ] [ filename ]
  311. </pre>
  312. <p>
  313. <code>filename</code> should be the last argument and should name the file from which
  314. to read the JavaScript code. If you don't specify it, it will read code
  315. from STDIN.
  316. </p>
  317. <p>
  318. Supported options:
  319. </p>
  320. <ul>
  321. <li><code>-b</code> or <code>--beautify</code> &mdash; output indented code; when passed, additional
  322. options control the beautifier:
  323. <ul>
  324. <li><code>-i N</code> or <code>--indent N</code> &mdash; indentation level (number of spaces)
  325. </li>
  326. <li><code>-q</code> or <code>--quote-keys</code> &mdash; quote keys in literal objects (by default,
  327. only keys that cannot be identifier names will be quotes).
  328. </li>
  329. </ul>
  330. </li>
  331. <li><code>--ascii</code> &mdash; pass this argument to encode non-ASCII characters as
  332. <code>\uXXXX</code> sequences. By default UglifyJS won't bother to do it and will
  333. output Unicode characters instead. (the output is always encoded in UTF8,
  334. but if you pass this option you'll only get ASCII).
  335. </li>
  336. <li><code>-nm</code> or <code>--no-mangle</code> &mdash; don't mangle names.
  337. </li>
  338. <li><code>-nmf</code> or <code>--no-mangle-functions</code> &ndash; in case you want to mangle variable
  339. names, but not touch function names.
  340. </li>
  341. <li><code>-ns</code> or <code>--no-squeeze</code> &mdash; don't call <code>ast_squeeze()</code> (which does various
  342. optimizations that result in smaller, less readable code).
  343. </li>
  344. <li><code>-mt</code> or <code>--mangle-toplevel</code> &mdash; mangle names in the toplevel scope too
  345. (by default we don't do this).
  346. </li>
  347. <li><code>--no-seqs</code> &mdash; when <code>ast_squeeze()</code> is called (thus, unless you pass
  348. <code>--no-squeeze</code>) it will reduce consecutive statements in blocks into a
  349. sequence. For example, "a = 10; b = 20; foo();" will be written as
  350. "a=10,b=20,foo();". In various occasions, this allows us to discard the
  351. block brackets (since the block becomes a single statement). This is ON
  352. by default because it seems safe and saves a few hundred bytes on some
  353. libs that I tested it on, but pass <code>--no-seqs</code> to disable it.
  354. </li>
  355. <li><code>--no-dead-code</code> &mdash; by default, UglifyJS will remove code that is
  356. obviously unreachable (code that follows a <code>return</code>, <code>throw</code>, <code>break</code> or
  357. <code>continue</code> statement and is not a function/variable declaration). Pass
  358. this option to disable this optimization.
  359. </li>
  360. <li><code>-nc</code> or <code>--no-copyright</code> &mdash; by default, <code>uglifyjs</code> will keep the initial
  361. comment tokens in the generated code (assumed to be copyright information
  362. etc.). If you pass this it will discard it.
  363. </li>
  364. <li><code>-o filename</code> or <code>--output filename</code> &mdash; put the result in <code>filename</code>. If
  365. this isn't given, the result goes to standard output (or see next one).
  366. </li>
  367. <li><code>--overwrite</code> &mdash; if the code is read from a file (not from STDIN) and you
  368. pass <code>--overwrite</code> then the output will be written in the same file.
  369. </li>
  370. <li><code>--ast</code> &mdash; pass this if you want to get the Abstract Syntax Tree instead
  371. of JavaScript as output. Useful for debugging or learning more about the
  372. internals.
  373. </li>
  374. <li><code>-v</code> or <code>--verbose</code> &mdash; output some notes on STDERR (for now just how long
  375. each operation takes).
  376. </li>
  377. <li><code>-d SYMBOL[=VALUE]</code> or <code>--define SYMBOL[=VALUE]</code> &mdash; will replace
  378. all instances of the specified symbol where used as an identifier
  379. (except where symbol has properly declared by a var declaration or
  380. use as function parameter or similar) with the specified value. This
  381. argument may be specified multiple times to define multiple
  382. symbols - if no value is specified the symbol will be replaced with
  383. the value <code>true</code>, or you can specify a numeric value (such as
  384. <code>1024</code>), a quoted string value (such as ="object"= or
  385. ='https://github.com'<code>), or the name of another symbol or keyword (such as =null</code> or <code>document</code>).
  386. This allows you, for example, to assign meaningful names to key
  387. constant values but discard the symbolic names in the uglified
  388. version for brevity/efficiency, or when used wth care, allows
  389. UglifyJS to operate as a form of <b>conditional compilation</b>
  390. whereby defining appropriate values may, by dint of the constant
  391. folding and dead code removal features above, remove entire
  392. superfluous code blocks (e.g. completely remove instrumentation or
  393. trace code for production use).
  394. Where string values are being defined, the handling of quotes are
  395. likely to be subject to the specifics of your command shell
  396. environment, so you may need to experiment with quoting styles
  397. depending on your platform, or you may find the option
  398. <code>--define-from-module</code> more suitable for use.
  399. </li>
  400. <li><code>-define-from-module SOMEMODULE</code> &mdash; will load the named module (as
  401. per the NodeJS <code>require()</code> function) and iterate all the exported
  402. properties of the module defining them as symbol names to be defined
  403. (as if by the <code>--define</code> option) per the name of each property
  404. (i.e. without the module name prefix) and given the value of the
  405. property. This is a much easier way to handle and document groups of
  406. symbols to be defined rather than a large number of <code>--define</code>
  407. options.
  408. </li>
  409. <li><code>--unsafe</code> &mdash; enable other additional optimizations that are known to be
  410. unsafe in some contrived situations, but could still be generally useful.
  411. For now only these:
  412. <ul>
  413. <li>foo.toString() ==&gt; foo+""
  414. </li>
  415. <li>new Array(x,&hellip;) ==&gt; [x,&hellip;]
  416. </li>
  417. <li>new Array(x) ==&gt; Array(x)
  418. </li>
  419. </ul>
  420. </li>
  421. <li><code>--max-line-len</code> (default 32K characters) &mdash; add a newline after around
  422. 32K characters. I've seen both FF and Chrome croak when all the code was
  423. on a single line of around 670K. Pass &ndash;max-line-len 0 to disable this
  424. safety feature.
  425. </li>
  426. <li><code>--reserved-names</code> &mdash; some libraries rely on certain names to be used, as
  427. pointed out in issue #92 and #81, so this option allow you to exclude such
  428. names from the mangler. For example, to keep names <code>require</code> and <code>$super</code>
  429. intact you'd specify &ndash;reserved-names "require,$super".
  430. </li>
  431. <li><code>--inline-script</code> &ndash; when you want to include the output literally in an
  432. HTML <code>&lt;script&gt;</code> tag you can use this option to prevent <code>&lt;/script</code> from
  433. showing up in the output.
  434. </li>
  435. <li><code>--lift-vars</code> &ndash; when you pass this, UglifyJS will apply the following
  436. transformations (see the notes in API, <code>ast_lift_variables</code>):
  437. <ul>
  438. <li>put all <code>var</code> declarations at the start of the scope
  439. </li>
  440. <li>make sure a variable is declared only once
  441. </li>
  442. <li>discard unused function arguments
  443. </li>
  444. <li>discard unused inner (named) functions
  445. </li>
  446. <li>finally, try to merge assignments into that one <code>var</code> declaration, if
  447. possible.
  448. </li>
  449. </ul>
  450. </li>
  451. </ul>
  452. </div>
  453. <div id="outline-container-1-4-1" class="outline-4">
  454. <h4 id="sec-1-4-1"><span class="section-number-4">1.4.1</span> API </h4>
  455. <div class="outline-text-4" id="text-1-4-1">
  456. <p>
  457. To use the library from JavaScript, you'd do the following (example for
  458. NodeJS):
  459. </p>
  460. <pre class="src src-js"><span class="org-keyword">var</span> <span class="org-variable-name">jsp</span> = require(<span class="org-string">"uglify-js"</span>).parser;
  461. <span class="org-keyword">var</span> <span class="org-variable-name">pro</span> = require(<span class="org-string">"uglify-js"</span>).uglify;
  462. <span class="org-keyword">var</span> <span class="org-variable-name">orig_code</span> = <span class="org-string">"... JS code here"</span>;
  463. <span class="org-keyword">var</span> <span class="org-variable-name">ast</span> = jsp.parse(orig_code); <span class="org-comment-delimiter">// </span><span class="org-comment">parse code and get the initial AST</span>
  464. ast = pro.ast_mangle(ast); <span class="org-comment-delimiter">// </span><span class="org-comment">get a new AST with mangled names</span>
  465. ast = pro.ast_squeeze(ast); <span class="org-comment-delimiter">// </span><span class="org-comment">get an AST with compression optimizations</span>
  466. <span class="org-keyword">var</span> <span class="org-variable-name">final_code</span> = pro.gen_code(ast); <span class="org-comment-delimiter">// </span><span class="org-comment">compressed code here</span>
  467. </pre>
  468. <p>
  469. The above performs the full compression that is possible right now. As you
  470. can see, there are a sequence of steps which you can apply. For example if
  471. you want compressed output but for some reason you don't want to mangle
  472. variable names, you would simply skip the line that calls
  473. <code>pro.ast_mangle(ast)</code>.
  474. </p>
  475. <p>
  476. Some of these functions take optional arguments. Here's a description:
  477. </p>
  478. <ul>
  479. <li><code>jsp.parse(code, strict_semicolons)</code> &ndash; parses JS code and returns an AST.
  480. <code>strict_semicolons</code> is optional and defaults to <code>false</code>. If you pass
  481. <code>true</code> then the parser will throw an error when it expects a semicolon and
  482. it doesn't find it. For most JS code you don't want that, but it's useful
  483. if you want to strictly sanitize your code.
  484. </li>
  485. <li><code>pro.ast_lift_variables(ast)</code> &ndash; merge and move <code>var</code> declarations to the
  486. scop of the scope; discard unused function arguments or variables; discard
  487. unused (named) inner functions. It also tries to merge assignments
  488. following the <code>var</code> declaration into it.
  489. <p>
  490. If your code is very hand-optimized concerning <code>var</code> declarations, this
  491. lifting variable declarations might actually increase size. For me it
  492. helps out. On jQuery it adds 865 bytes (243 after gzip). YMMV. Also
  493. note that (since it's not enabled by default) this operation isn't yet
  494. heavily tested (please report if you find issues!).
  495. </p>
  496. <p>
  497. Note that although it might increase the image size (on jQuery it gains
  498. 865 bytes, 243 after gzip) it's technically more correct: in certain
  499. situations, dead code removal might drop variable declarations, which
  500. would not happen if the variables are lifted in advance.
  501. </p>
  502. <p>
  503. Here's an example of what it does:
  504. </p></li>
  505. </ul>
  506. <pre class="src src-js"><span class="org-keyword">function</span> <span class="org-function-name">f</span>(<span class="org-variable-name">a</span>, <span class="org-variable-name">b</span>, <span class="org-variable-name">c</span>, <span class="org-variable-name">d</span>, <span class="org-variable-name">e</span>) {
  507. <span class="org-keyword">var</span> <span class="org-variable-name">q</span>;
  508. <span class="org-keyword">var</span> <span class="org-variable-name">w</span>;
  509. w = 10;
  510. q = 20;
  511. <span class="org-keyword">for</span> (<span class="org-keyword">var</span> <span class="org-variable-name">i</span> = 1; i &lt; 10; ++i) {
  512. <span class="org-keyword">var</span> <span class="org-variable-name">boo</span> = foo(a);
  513. }
  514. <span class="org-keyword">for</span> (<span class="org-keyword">var</span> <span class="org-variable-name">i</span> = 0; i &lt; 1; ++i) {
  515. <span class="org-keyword">var</span> <span class="org-variable-name">boo</span> = bar(c);
  516. }
  517. <span class="org-keyword">function</span> <span class="org-function-name">foo</span>(){ ... }
  518. <span class="org-keyword">function</span> <span class="org-function-name">bar</span>(){ ... }
  519. <span class="org-keyword">function</span> <span class="org-function-name">baz</span>(){ ... }
  520. }
  521. <span class="org-comment-delimiter">// </span><span class="org-comment">transforms into ==&gt;</span>
  522. <span class="org-keyword">function</span> <span class="org-function-name">f</span>(<span class="org-variable-name">a</span>, <span class="org-variable-name">b</span>, <span class="org-variable-name">c</span>) {
  523. <span class="org-keyword">var</span> <span class="org-variable-name">i</span>, <span class="org-variable-name">boo</span>, <span class="org-variable-name">w</span> = 10, <span class="org-variable-name">q</span> = 20;
  524. <span class="org-keyword">for</span> (i = 1; i &lt; 10; ++i) {
  525. boo = foo(a);
  526. }
  527. <span class="org-keyword">for</span> (i = 0; i &lt; 1; ++i) {
  528. boo = bar(c);
  529. }
  530. <span class="org-keyword">function</span> <span class="org-function-name">foo</span>() { ... }
  531. <span class="org-keyword">function</span> <span class="org-function-name">bar</span>() { ... }
  532. }
  533. </pre>
  534. <ul>
  535. <li><code>pro.ast_mangle(ast, options)</code> &ndash; generates a new AST containing mangled
  536. (compressed) variable and function names. It supports the following
  537. options:
  538. <ul>
  539. <li><code>toplevel</code> &ndash; mangle toplevel names (by default we don't touch them).
  540. </li>
  541. <li><code>except</code> &ndash; an array of names to exclude from compression.
  542. </li>
  543. <li><code>defines</code> &ndash; an object with properties named after symbols to
  544. replace (see the <code>--define</code> option for the script) and the values
  545. representing the AST replacement value.
  546. </li>
  547. </ul>
  548. </li>
  549. <li><code>pro.ast_squeeze(ast, options)</code> &ndash; employs further optimizations designed
  550. to reduce the size of the code that <code>gen_code</code> would generate from the
  551. AST. Returns a new AST. <code>options</code> can be a hash; the supported options
  552. are:
  553. <ul>
  554. <li><code>make_seqs</code> (default true) which will cause consecutive statements in a
  555. block to be merged using the "sequence" (comma) operator
  556. </li>
  557. <li><code>dead_code</code> (default true) which will remove unreachable code.
  558. </li>
  559. </ul>
  560. </li>
  561. <li><code>pro.gen_code(ast, options)</code> &ndash; generates JS code from the AST. By
  562. default it's minified, but using the <code>options</code> argument you can get nicely
  563. formatted output. <code>options</code> is, well, optional :-) and if you pass it it
  564. must be an object and supports the following properties (below you can see
  565. the default values):
  566. <ul>
  567. <li><code>beautify: false</code> &ndash; pass <code>true</code> if you want indented output
  568. </li>
  569. <li><code>indent_start: 0</code> (only applies when <code>beautify</code> is <code>true</code>) &ndash; initial
  570. indentation in spaces
  571. </li>
  572. <li><code>indent_level: 4</code> (only applies when <code>beautify</code> is <code>true</code>) --
  573. indentation level, in spaces (pass an even number)
  574. </li>
  575. <li><code>quote_keys: false</code> &ndash; if you pass <code>true</code> it will quote all keys in
  576. literal objects
  577. </li>
  578. <li><code>space_colon: false</code> (only applies when <code>beautify</code> is <code>true</code>) &ndash; wether
  579. to put a space before the colon in object literals
  580. </li>
  581. <li><code>ascii_only: false</code> &ndash; pass <code>true</code> if you want to encode non-ASCII
  582. characters as <code>\uXXXX</code>.
  583. </li>
  584. <li><code>inline_script: false</code> &ndash; pass <code>true</code> to escape occurrences of
  585. <code>&lt;/script</code> in strings
  586. </li>
  587. </ul>
  588. </li>
  589. </ul>
  590. </div>
  591. </div>
  592. <div id="outline-container-1-4-2" class="outline-4">
  593. <h4 id="sec-1-4-2"><span class="section-number-4">1.4.2</span> Beautifier shortcoming &ndash; no more comments </h4>
  594. <div class="outline-text-4" id="text-1-4-2">
  595. <p>
  596. The beautifier can be used as a general purpose indentation tool. It's
  597. useful when you want to make a minified file readable. One limitation,
  598. though, is that it discards all comments, so you don't really want to use it
  599. to reformat your code, unless you don't have, or don't care about, comments.
  600. </p>
  601. <p>
  602. In fact it's not the beautifier who discards comments &mdash; they are dumped at
  603. the parsing stage, when we build the initial AST. Comments don't really
  604. make sense in the AST, and while we could add nodes for them, it would be
  605. inconvenient because we'd have to add special rules to ignore them at all
  606. the processing stages.
  607. </p>
  608. </div>
  609. </div>
  610. <div id="outline-container-1-4-3" class="outline-4">
  611. <h4 id="sec-1-4-3"><span class="section-number-4">1.4.3</span> Use as a code pre-processor </h4>
  612. <div class="outline-text-4" id="text-1-4-3">
  613. <p>
  614. The <code>--define</code> option can be used, particularly when combined with the
  615. constant folding logic, as a form of pre-processor to enable or remove
  616. particular constructions, such as might be used for instrumenting
  617. development code, or to produce variations aimed at a specific
  618. platform.
  619. </p>
  620. <p>
  621. The code below illustrates the way this can be done, and how the
  622. symbol replacement is performed.
  623. </p>
  624. <pre class="src src-js">CLAUSE1: <span class="org-keyword">if</span> (<span class="org-keyword">typeof</span> DEVMODE === <span class="org-string">'undefined'</span>) {
  625. DEVMODE = <span class="org-constant">true</span>;
  626. }
  627. <span class="org-function-name">CLAUSE2</span>: <span class="org-keyword">function</span> init() {
  628. <span class="org-keyword">if</span> (DEVMODE) {
  629. console.log(<span class="org-string">"init() called"</span>);
  630. }
  631. ....
  632. DEVMODE &amp;amp;&amp;amp; console.log(<span class="org-string">"init() complete"</span>);
  633. }
  634. <span class="org-function-name">CLAUSE3</span>: <span class="org-keyword">function</span> reportDeviceStatus(<span class="org-variable-name">device</span>) {
  635. <span class="org-keyword">var</span> <span class="org-variable-name">DEVMODE</span> = device.mode, <span class="org-variable-name">DEVNAME</span> = device.name;
  636. <span class="org-keyword">if</span> (DEVMODE === <span class="org-string">'open'</span>) {
  637. ....
  638. }
  639. }
  640. </pre>
  641. <p>
  642. When the above code is normally executed, the undeclared global
  643. variable <code>DEVMODE</code> will be assigned the value <b>true</b> (see <code>CLAUSE1</code>)
  644. and so the <code>init()</code> function (<code>CLAUSE2</code>) will write messages to the
  645. console log when executed, but in <code>CLAUSE3</code> a locally declared
  646. variable will mask access to the <code>DEVMODE</code> global symbol.
  647. </p>
  648. <p>
  649. If the above code is processed by UglifyJS with an argument of
  650. <code>--define DEVMODE=false</code> then UglifyJS will replace <code>DEVMODE</code> with the
  651. boolean constant value <b>false</b> within <code>CLAUSE1</code> and <code>CLAUSE2</code>, but it
  652. will leave <code>CLAUSE3</code> as it stands because there <code>DEVMODE</code> resolves to
  653. a validly declared variable.
  654. </p>
  655. <p>
  656. And more so, the constant-folding features of UglifyJS will recognise
  657. that the <code>if</code> condition of <code>CLAUSE1</code> is thus always false, and so will
  658. remove the test and body of <code>CLAUSE1</code> altogether (including the
  659. otherwise slightly problematical statement <code>false = true;</code> which it
  660. will have formed by replacing <code>DEVMODE</code> in the body). Similarly,
  661. within <code>CLAUSE2</code> both calls to <code>console.log()</code> will be removed
  662. altogether.
  663. </p>
  664. <p>
  665. In this way you can mimic, to a limited degree, the functionality of
  666. the C/C++ pre-processor to enable or completely remove blocks
  667. depending on how certain symbols are defined - perhaps using UglifyJS
  668. to generate different versions of source aimed at different
  669. environments
  670. </p>
  671. <p>
  672. It is recommmended (but not made mandatory) that symbols designed for
  673. this purpose are given names consisting of <code>UPPER_CASE_LETTERS</code> to
  674. distinguish them from other (normal) symbols and avoid the sort of
  675. clash that <code>CLAUSE3</code> above illustrates.
  676. </p>
  677. </div>
  678. </div>
  679. </div>
  680. <div id="outline-container-1-5" class="outline-3">
  681. <h3 id="sec-1-5"><span class="section-number-3">1.5</span> Compression &ndash; how good is it? </h3>
  682. <div class="outline-text-3" id="text-1-5">
  683. <p>
  684. Here are updated statistics. (I also updated my Google Closure and YUI
  685. installations).
  686. </p>
  687. <p>
  688. We're still a lot better than YUI in terms of compression, though slightly
  689. slower. We're still a lot faster than Closure, and compression after gzip
  690. is comparable.
  691. </p>
  692. <table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
  693. <caption></caption>
  694. <colgroup><col class="left" /><col class="left" /><col class="right" /><col class="left" /><col class="right" /><col class="left" /><col class="right" />
  695. </colgroup>
  696. <thead>
  697. <tr><th scope="col" class="left">File</th><th scope="col" class="left">UglifyJS</th><th scope="col" class="right">UglifyJS+gzip</th><th scope="col" class="left">Closure</th><th scope="col" class="right">Closure+gzip</th><th scope="col" class="left">YUI</th><th scope="col" class="right">YUI+gzip</th></tr>
  698. </thead>
  699. <tbody>
  700. <tr><td class="left">jquery-1.6.2.js</td><td class="left">91001 (0:01.59)</td><td class="right">31896</td><td class="left">90678 (0:07.40)</td><td class="right">31979</td><td class="left">101527 (0:01.82)</td><td class="right">34646</td></tr>
  701. <tr><td class="left">paper.js</td><td class="left">142023 (0:01.65)</td><td class="right">43334</td><td class="left">134301 (0:07.42)</td><td class="right">42495</td><td class="left">173383 (0:01.58)</td><td class="right">48785</td></tr>
  702. <tr><td class="left">prototype.js</td><td class="left">88544 (0:01.09)</td><td class="right">26680</td><td class="left">86955 (0:06.97)</td><td class="right">26326</td><td class="left">92130 (0:00.79)</td><td class="right">28624</td></tr>
  703. <tr><td class="left">thelib-full.js (DynarchLIB)</td><td class="left">251939 (0:02.55)</td><td class="right">72535</td><td class="left">249911 (0:09.05)</td><td class="right">72696</td><td class="left">258869 (0:01.94)</td><td class="right">76584</td></tr>
  704. </tbody>
  705. </table>
  706. </div>
  707. </div>
  708. <div id="outline-container-1-6" class="outline-3">
  709. <h3 id="sec-1-6"><span class="section-number-3">1.6</span> Bugs? </h3>
  710. <div class="outline-text-3" id="text-1-6">
  711. <p>
  712. Unfortunately, for the time being there is no automated test suite. But I
  713. ran the compressor manually on non-trivial code, and then I tested that the
  714. generated code works as expected. A few hundred times.
  715. </p>
  716. <p>
  717. DynarchLIB was started in times when there was no good JS minifier.
  718. Therefore I was quite religious about trying to write short code manually,
  719. and as such DL contains a lot of syntactic hacks<sup><a class="footref" name="fnr.1" href="#fn.1">1</a></sup> such as “foo == bar ? a
  720. = 10 : b = 20”, though the more readable version would clearly be to use
  721. “if/else”.
  722. </p>
  723. <p>
  724. Since the parser/compressor runs fine on DL and jQuery, I'm quite confident
  725. that it's solid enough for production use. If you can identify any bugs,
  726. I'd love to hear about them (<a href="http://groups.google.com/group/uglifyjs">use the Google Group</a> or email me directly).
  727. </p>
  728. </div>
  729. </div>
  730. <div id="outline-container-1-7" class="outline-3">
  731. <h3 id="sec-1-7"><span class="section-number-3">1.7</span> Links </h3>
  732. <div class="outline-text-3" id="text-1-7">
  733. <ul>
  734. <li>Twitter: <a href="http://twitter.com/UglifyJS">@UglifyJS</a>
  735. </li>
  736. <li>Project at GitHub: <a href="http://github.com/mishoo/UglifyJS">http://github.com/mishoo/UglifyJS</a>
  737. </li>
  738. <li>Google Group: <a href="http://groups.google.com/group/uglifyjs">http://groups.google.com/group/uglifyjs</a>
  739. </li>
  740. <li>Common Lisp JS parser: <a href="http://marijn.haverbeke.nl/parse-js/">http://marijn.haverbeke.nl/parse-js/</a>
  741. </li>
  742. <li>JS-to-Lisp compiler: <a href="http://github.com/marijnh/js">http://github.com/marijnh/js</a>
  743. </li>
  744. <li>Common Lisp JS uglifier: <a href="http://github.com/mishoo/cl-uglify-js">http://github.com/mishoo/cl-uglify-js</a>
  745. </li>
  746. </ul>
  747. </div>
  748. </div>
  749. <div id="outline-container-1-8" class="outline-3">
  750. <h3 id="sec-1-8"><span class="section-number-3">1.8</span> License </h3>
  751. <div class="outline-text-3" id="text-1-8">
  752. <p>
  753. UglifyJS is released under the BSD license:
  754. </p>
  755. <pre class="example">Copyright 2010 (c) Mihai Bazon &lt;[email protected]&gt;
  756. Based on parse-js (http://marijn.haverbeke.nl/parse-js/).
  757. Redistribution and use in source and binary forms, with or without
  758. modification, are permitted provided that the following conditions
  759. are met:
  760. * Redistributions of source code must retain the above
  761. copyright notice, this list of conditions and the following
  762. disclaimer.
  763. * Redistributions in binary form must reproduce the above
  764. copyright notice, this list of conditions and the following
  765. disclaimer in the documentation and/or other materials
  766. provided with the distribution.
  767. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  768. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  769. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  770. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  771. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  772. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  773. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  774. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  775. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  776. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  777. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  778. SUCH DAMAGE.
  779. </pre>
  780. <div id="footnotes">
  781. <h2 class="footnotes">Footnotes: </h2>
  782. <div id="text-footnotes">
  783. <p class="footnote"><sup><a class="footnum" name="fn.1" href="#fnr.1">1</a></sup> I even reported a few bugs and suggested some fixes in the original
  784. <a href="http://marijn.haverbeke.nl/parse-js/">parse-js</a> library, and Marijn pushed fixes literally in minutes.
  785. </p></div>
  786. </div>
  787. </div>
  788. </div>
  789. </div>
  790. </div>
  791. <div id="postamble">
  792. <p class="date">Date: 2011-12-09 14:59:08 EET</p>
  793. <p class="author">Author: Mihai Bazon</p>
  794. <p class="creator">Org version 7.7 with Emacs version 23</p>
  795. <a href="http://validator.w3.org/check?uri=referer">Validate XHTML 1.0</a>
  796. </div>
  797. </body>
  798. </html>