README.html 40 KB

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