forked from mishoo/UglifyJS-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.html
835 lines (688 loc) · 29 KB
/
README.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
lang="en" xml:lang="en">
<head>
<title>UglifyJS -- a JavaScript parser/compressor/beautifier</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<meta name="generator" content="Org-mode"/>
<meta name="generated" content="2011-07-14 12:50:31 EEST"/>
<meta name="author" content="Mihai Bazon"/>
<meta name="description" content="a JavaScript parser/compressor/beautifier in JavaScript"/>
<meta name="keywords" content="javascript, js, parser, compiler, compressor, mangle, minify, minifier"/>
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
html { font-family: Times, serif; font-size: 12pt; }
.title { text-align: center; }
.todo { color: red; }
.done { color: green; }
.tag { background-color: #add8e6; font-weight:normal }
.target { }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
p.verse { margin-left: 3% }
pre {
border: 1pt solid #AEBDCC;
background-color: #F3F5F7;
padding: 5pt;
font-family: courier, monospace;
font-size: 90%;
overflow:auto;
}
table { border-collapse: collapse; }
td, th { vertical-align: top; }
dt { font-weight: bold; }
div.figure { padding: 0.5em; }
div.figure p { text-align: center; }
textarea { overflow-x: auto; }
.linenr { font-size:smaller }
.code-highlighted {background-color:#ffff00;}
.org-info-js_info-navigation { border-style:none; }
#org-info-js_console-label { font-size:10px; font-weight:bold;
white-space:nowrap; }
.org-info-js_search-highlight {background-color:#ffff00; color:#000000;
font-weight:bold; }
/*]]>*/-->
</style>
<link rel="stylesheet" type="text/css" href="docstyle.css" />
<script type="text/javascript">
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">UglifyJS – a JavaScript parser/compressor/beautifier</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1 UglifyJS — a JavaScript parser/compressor/beautifier </a>
<ul>
<li><a href="#sec-1_1">1.1 Unsafe transformations </a>
<ul>
<li><a href="#sec-1_1_1">1.1.1 Calls involving the global Array constructor </a></li>
</ul>
</li>
<li><a href="#sec-1_2">1.2 Install (NPM) </a></li>
<li><a href="#sec-1_3">1.3 Install latest code from GitHub </a></li>
<li><a href="#sec-1_4">1.4 Usage </a>
<ul>
<li><a href="#sec-1_4_1">1.4.1 API </a></li>
<li><a href="#sec-1_4_2">1.4.2 Beautifier shortcoming – no more comments </a></li>
</ul>
</li>
<li><a href="#sec-1_5">1.5 Compression – how good is it? </a></li>
<li><a href="#sec-1_6">1.6 Bugs? </a></li>
<li><a href="#sec-1_7">1.7 Links </a></li>
<li><a href="#sec-1_8">1.8 License </a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-1" class="outline-2">
<h2 id="sec-1"><span class="section-number-2">1</span> UglifyJS — a JavaScript parser/compressor/beautifier </h2>
<div class="outline-text-2" id="text-1">
<p>
This package implements a general-purpose JavaScript
parser/compressor/beautifier toolkit. It is developed on <a href="http://nodejs.org/">NodeJS</a>, but it
should work on any JavaScript platform supporting the CommonJS module system
(and if your platform of choice doesn't support CommonJS, you can easily
implement it, or discard the <code>exports.*</code> lines from UglifyJS sources).
</p>
<p>
The tokenizer/parser generates an abstract syntax tree from JS code. You
can then traverse the AST to learn more about the code, or do various
manipulations on it. This part is implemented in <a href="../lib/parse-js.js">parse-js.js</a> and it's a
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>.
</p>
<p>
( See <a href="http://github.com/mishoo/cl-uglify-js">cl-uglify-js</a> if you're looking for the Common Lisp version of
UglifyJS. )
</p>
<p>
The second part of this package, implemented in <a href="../lib/process.js">process.js</a>, inspects and
manipulates the AST generated by the parser to provide the following:
</p>
<ul>
<li>
ability to re-generate JavaScript code from the AST. Optionally
indented—you can use this if you want to “beautify” a program that has
been compressed, so that you can inspect the source. But you can also run
our code generator to print out an AST without any whitespace, so you
achieve compression as well.
</li>
<li>
shorten variable names (usually to single characters). Our mangler will
analyze the code and generate proper variable names, depending on scope
and usage, and is smart enough to deal with globals defined elsewhere, or
with <code>eval()</code> calls or <code>with{}</code> statements. In short, if <code>eval()</code> or
<code>with{}</code> are used in some scope, then all variables in that scope and any
variables in the parent scopes will remain unmangled, and any references
to such variables remain unmangled as well.
</li>
<li>
various small optimizations that may lead to faster code but certainly
lead to smaller code. Where possible, we do the following:
<ul>
<li>
foo["bar"] ==> foo.bar
</li>
<li>
remove block brackets <code>{}</code>
</li>
<li>
join consecutive var declarations:
var a = 10; var b = 20; ==> var a=10,b=20;
</li>
<li>
resolve simple constant expressions: 1 +2 * 3 ==> 7. We only do the
replacement if the result occupies less bytes; for example 1/3 would
translate to 0.333333333333, so in this case we don't replace it.
</li>
<li>
consecutive statements in blocks are merged into a sequence; in many
cases, this leaves blocks with a single statement, so then we can remove
the block brackets.
</li>
<li>
various optimizations for IF statements:
<ul>
<li>
if (foo) bar(); else baz(); ==> foo?bar():baz();
</li>
<li>
if (!foo) bar(); else baz(); ==> foo?baz():bar();
</li>
<li>
if (foo) bar(); ==> foo&&bar();
</li>
<li>
if (!foo) bar(); ==> foo||bar();
</li>
<li>
if (foo) return bar(); else return baz(); ==> return foo?bar():baz();
</li>
<li>
if (foo) return bar(); else something(); ==> {if(foo)return bar();something()}
</li>
</ul>
</li>
<li>
remove some unreachable code and warn about it (code that follows a
<code>return</code>, <code>throw</code>, <code>break</code> or <code>continue</code> statement, except
function/variable declarations).
</li>
</ul>
</li>
</ul>
</div>
<div id="outline-container-1_1" class="outline-3">
<h3 id="sec-1_1"><span class="section-number-3">1.1</span> <span class="target">Unsafe transformations</span> </h3>
<div class="outline-text-3" id="text-1_1">
<p>
UglifyJS tries its best to achieve great compression while leaving the
semantics of the code intact. In general, if your code logic is broken by
UglifyJS then it's a bug in UglifyJS and you should report it and I should
fix it. :-)
</p>
<p>
However, I opted to include the following potentially unsafe transformations
as default behavior. Discussion is welcome, if you have ideas of how to
handle this better, or any objections to these optimizations, please let me
know.
</p>
</div>
<div id="outline-container-1_1_1" class="outline-4">
<h4 id="sec-1_1_1"><span class="section-number-4">1.1.1</span> Calls involving the global Array constructor </h4>
<div class="outline-text-4" id="text-1_1_1">
<p>
The following transformations occur:
</p>
<pre class="src src-js"><span style="color: #8b0000;">new</span> <span style="color: #4682b4;">Array</span>(1, 2, 3, 4) => [1,2,3,4]
Array(a, b, c) => [a,b,c]
<span style="color: #8b0000;">new</span> <span style="color: #4682b4;">Array</span>(5) => Array(5)
<span style="color: #8b0000;">new</span> <span style="color: #4682b4;">Array</span>(a) => Array(a)
</pre>
<p>
These are all safe if the Array name isn't redefined. JavaScript does allow
one to globally redefine Array (and pretty much everything, in fact) but I
personally don't see why would anyone do that.
</p>
<p>
UglifyJS does handle the case where Array is redefined locally, or even
globally but with a <code>function</code> or <code>var</code> declaration. Therefore, in the
following cases UglifyJS <b>doesn't touch</b> calls or instantiations of Array:
</p>
<pre class="src src-js"><span style="color: #00008b;">// </span><span style="color: #00008b;">case 1. globally declared variable
</span> <span style="color: #8b0000;">var</span> <span style="color: #8b008b;">Array</span>;
<span style="color: #8b0000;">new</span> <span style="color: #4682b4;">Array</span>(1, 2, 3);
Array(a, b);
<span style="color: #00008b;">// </span><span style="color: #00008b;">or (can be declared later)
</span> <span style="color: #8b0000;">new</span> <span style="color: #4682b4;">Array</span>(1, 2, 3);
<span style="color: #8b0000;">var</span> <span style="color: #8b008b;">Array</span>;
<span style="color: #00008b;">// </span><span style="color: #00008b;">or (can be a function)
</span> <span style="color: #8b0000;">new</span> <span style="color: #4682b4;">Array</span>(1, 2, 3);
<span style="color: #8b0000;">function</span> <span style="color: #8b2323;">Array</span>() { ... }
<span style="color: #00008b;">// </span><span style="color: #00008b;">case 2. declared in a function
</span> (<span style="color: #8b0000;">function</span>(){
a = <span style="color: #8b0000;">new</span> <span style="color: #4682b4;">Array</span>(1, 2, 3);
b = Array(5, 6);
<span style="color: #8b0000;">var</span> <span style="color: #8b008b;">Array</span>;
})();
<span style="color: #00008b;">// </span><span style="color: #00008b;">or
</span> (<span style="color: #8b0000;">function</span>(<span style="color: #8b008b;">Array</span>){
<span style="color: #8b0000;">return</span> Array(5, 6, 7);
})();
<span style="color: #00008b;">// </span><span style="color: #00008b;">or
</span> (<span style="color: #8b0000;">function</span>(){
<span style="color: #8b0000;">return</span> <span style="color: #8b0000;">new</span> <span style="color: #4682b4;">Array</span>(1, 2, 3, 4);
<span style="color: #8b0000;">function</span> <span style="color: #8b2323;">Array</span>() { ... }
})();
<span style="color: #00008b;">// </span><span style="color: #00008b;">etc.
</span></pre>
</div>
</div>
</div>
<div id="outline-container-1_2" class="outline-3">
<h3 id="sec-1_2"><span class="section-number-3">1.2</span> Install (NPM) </h3>
<div class="outline-text-3" id="text-1_2">
<p>
UglifyJS is now available through NPM — <code>npm install uglify-js</code> should do
the job.
</p>
</div>
</div>
<div id="outline-container-1_3" class="outline-3">
<h3 id="sec-1_3"><span class="section-number-3">1.3</span> Install latest code from GitHub </h3>
<div class="outline-text-3" id="text-1_3">
<pre class="src src-sh"><span style="color: #00008b;">## </span><span style="color: #00008b;">clone the repository
</span>mkdir -p /where/you/wanna/put/it
<span style="color: #cd0000;">cd</span> /where/you/wanna/put/it
git clone git://github.com/mishoo/UglifyJS.git
<span style="color: #00008b;">## </span><span style="color: #00008b;">make the module available to Node
</span>mkdir -p ~/.node_libraries/
<span style="color: #cd0000;">cd</span> ~/.node_libraries/
ln -s /where/you/wanna/put/it/UglifyJS/uglify-js.js
<span style="color: #00008b;">## </span><span style="color: #00008b;">and if you want the CLI script too:
</span>mkdir -p ~/bin
<span style="color: #cd0000;">cd</span> ~/bin
ln -s /where/you/wanna/put/it/UglifyJS/bin/uglifyjs
<span style="color: #00008b;"># </span><span style="color: #00008b;">(then add ~/bin to your $PATH if it's not there already)
</span></pre>
</div>
</div>
<div id="outline-container-1_4" class="outline-3">
<h3 id="sec-1_4"><span class="section-number-3">1.4</span> Usage </h3>
<div class="outline-text-3" id="text-1_4">
<p>
There is a command-line tool that exposes the functionality of this library
for your shell-scripting needs:
</p>
<pre class="src src-sh">uglifyjs [ options... ] [ filename ]
</pre>
<p>
<code>filename</code> should be the last argument and should name the file from which
to read the JavaScript code. If you don't specify it, it will read code
from STDIN.
</p>
<p>
Supported options:
</p>
<ul>
<li>
<code>-b</code> or <code>--beautify</code> — output indented code; when passed, additional
options control the beautifier:
<ul>
<li>
<code>-i N</code> or <code>--indent N</code> — indentation level (number of spaces)
</li>
<li>
<code>-q</code> or <code>--quote-keys</code> — quote keys in literal objects (by default,
only keys that cannot be identifier names will be quotes).
</li>
</ul>
</li>
<li>
<code>--ascii</code> — pass this argument to encode non-ASCII characters as
<code>\uXXXX</code> sequences. By default UglifyJS won't bother to do it and will
output Unicode characters instead. (the output is always encoded in UTF8,
but if you pass this option you'll only get ASCII).
</li>
<li>
<code>-nm</code> or <code>--no-mangle</code> — don't mangle variable names
</li>
<li>
<code>-ns</code> or <code>--no-squeeze</code> — don't call <code>ast_squeeze()</code> (which does various
optimizations that result in smaller, less readable code).
</li>
<li>
<code>-mt</code> or <code>--mangle-toplevel</code> — mangle names in the toplevel scope too
(by default we don't do this).
</li>
<li>
<code>--no-seqs</code> — when <code>ast_squeeze()</code> is called (thus, unless you pass
<code>--no-squeeze</code>) it will reduce consecutive statements in blocks into a
sequence. For example, "a = 10; b = 20; foo();" will be written as
"a=10,b=20,foo();". In various occasions, this allows us to discard the
block brackets (since the block becomes a single statement). This is ON
by default because it seems safe and saves a few hundred bytes on some
libs that I tested it on, but pass <code>--no-seqs</code> to disable it.
</li>
<li>
<code>--no-dead-code</code> — by default, UglifyJS will remove code that is
obviously unreachable (code that follows a <code>return</code>, <code>throw</code>, <code>break</code> or
<code>continue</code> statement and is not a function/variable declaration). Pass
this option to disable this optimization.
</li>
<li>
<code>-nc</code> or <code>--no-copyright</code> — by default, <code>uglifyjs</code> will keep the initial
comment tokens in the generated code (assumed to be copyright information
etc.). If you pass this it will discard it.
</li>
<li>
<code>-o filename</code> or <code>--output filename</code> — put the result in <code>filename</code>. If
this isn't given, the result goes to standard output (or see next one).
</li>
<li>
<code>--overwrite</code> — if the code is read from a file (not from STDIN) and you
pass <code>--overwrite</code> then the output will be written in the same file.
</li>
<li>
<code>--ast</code> — pass this if you want to get the Abstract Syntax Tree instead
of JavaScript as output. Useful for debugging or learning more about the
internals.
</li>
<li>
<code>-v</code> or <code>--verbose</code> — output some notes on STDERR (for now just how long
each operation takes).
</li>
<li>
<code>--extra</code> — enable additional optimizations that have not yet been
extensively tested. These might, or might not, break your code. If you
find a bug using this option, please report a test case.
</li>
<li>
<code>--unsafe</code> — enable other additional optimizations that are known to be
unsafe in some contrived situations, but could still be generally useful.
For now only this:
<ul>
<li>
foo.toString() ==> foo+""
</li>
</ul>
</li>
<li>
<code>--max-line-len</code> (default 32K characters) — add a newline after around
32K characters. I've seen both FF and Chrome croak when all the code was
on a single line of around 670K. Pass –max-line-len 0 to disable this
safety feature.
</li>
<li>
<code>--reserved-names</code> — some libraries rely on certain names to be used, as
pointed out in issue #92 and #81, so this option allow you to exclude such
names from the mangler. For example, to keep names <code>require</code> and <code>$super</code>
intact you'd specify –reserved-names "require,$super".
</li>
<li>
<code>--inline-script</code> – when you want to include the output literally in an
HTML <code><script></code> tag you can use this option to prevent <code></script</code> from
showing up in the output.
</li>
</ul>
</div>
<div id="outline-container-1_4_1" class="outline-4">
<h4 id="sec-1_4_1"><span class="section-number-4">1.4.1</span> API </h4>
<div class="outline-text-4" id="text-1_4_1">
<p>
To use the library from JavaScript, you'd do the following (example for
NodeJS):
</p>
<pre class="src src-js"><span style="color: #8b0000;">var</span> <span style="color: #8b008b;">jsp</span> = require(<span style="color: #008b00;">"uglify-js"</span>).parser;
<span style="color: #8b0000;">var</span> <span style="color: #8b008b;">pro</span> = require(<span style="color: #008b00;">"uglify-js"</span>).uglify;
<span style="color: #8b0000;">var</span> <span style="color: #8b008b;">orig_code</span> = <span style="color: #008b00;">"... JS code here"</span>;
<span style="color: #8b0000;">var</span> <span style="color: #8b008b;">ast</span> = jsp.parse(orig_code); <span style="color: #00008b;">// </span><span style="color: #00008b;">parse code and get the initial AST
</span>ast = pro.ast_mangle(ast); <span style="color: #00008b;">// </span><span style="color: #00008b;">get a new AST with mangled names
</span>ast = pro.ast_squeeze(ast); <span style="color: #00008b;">// </span><span style="color: #00008b;">get an AST with compression optimizations
</span><span style="color: #8b0000;">var</span> <span style="color: #8b008b;">final_code</span> = pro.gen_code(ast); <span style="color: #00008b;">// </span><span style="color: #00008b;">compressed code here
</span></pre>
<p>
The above performs the full compression that is possible right now. As you
can see, there are a sequence of steps which you can apply. For example if
you want compressed output but for some reason you don't want to mangle
variable names, you would simply skip the line that calls
<code>pro.ast_mangle(ast)</code>.
</p>
<p>
Some of these functions take optional arguments. Here's a description:
</p>
<ul>
<li>
<code>jsp.parse(code, strict_semicolons)</code> – parses JS code and returns an AST.
<code>strict_semicolons</code> is optional and defaults to <code>false</code>. If you pass
<code>true</code> then the parser will throw an error when it expects a semicolon and
it doesn't find it. For most JS code you don't want that, but it's useful
if you want to strictly sanitize your code.
</li>
<li>
<code>pro.ast_mangle(ast, options)</code> – generates a new AST containing mangled
(compressed) variable and function names. It supports the following
options:
<ul>
<li>
<code>toplevel</code> – mangle toplevel names (by default we don't touch them).
</li>
<li>
<code>except</code> – an array of names to exclude from compression.
</li>
</ul>
</li>
<li>
<code>pro.ast_squeeze(ast, options)</code> – employs further optimizations designed
to reduce the size of the code that <code>gen_code</code> would generate from the
AST. Returns a new AST. <code>options</code> can be a hash; the supported options
are:
<ul>
<li>
<code>make_seqs</code> (default true) which will cause consecutive statements in a
block to be merged using the "sequence" (comma) operator
</li>
<li>
<code>dead_code</code> (default true) which will remove unreachable code.
</li>
</ul>
</li>
<li>
<code>pro.gen_code(ast, options)</code> – generates JS code from the AST. By
default it's minified, but using the <code>options</code> argument you can get nicely
formatted output. <code>options</code> is, well, optional :-) and if you pass it it
must be an object and supports the following properties (below you can see
the default values):
<ul>
<li>
<code>beautify: false</code> – pass <code>true</code> if you want indented output
</li>
<li>
<code>indent_start: 0</code> (only applies when <code>beautify</code> is <code>true</code>) – initial
indentation in spaces
</li>
<li>
<code>indent_level: 4</code> (only applies when <code>beautify</code> is <code>true</code>) --
indentation level, in spaces (pass an even number)
</li>
<li>
<code>quote_keys: false</code> – if you pass <code>true</code> it will quote all keys in
literal objects
</li>
<li>
<code>space_colon: false</code> (only applies when <code>beautify</code> is <code>true</code>) – wether
to put a space before the colon in object literals
</li>
<li>
<code>ascii_only: false</code> – pass <code>true</code> if you want to encode non-ASCII
characters as <code>\uXXXX</code>.
</li>
<li>
<code>inline_script: false</code> – pass <code>true</code> to escape occurrences of
<code></script</code> in strings
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-1_4_2" class="outline-4">
<h4 id="sec-1_4_2"><span class="section-number-4">1.4.2</span> Beautifier shortcoming – no more comments </h4>
<div class="outline-text-4" id="text-1_4_2">
<p>
The beautifier can be used as a general purpose indentation tool. It's
useful when you want to make a minified file readable. One limitation,
though, is that it discards all comments, so you don't really want to use it
to reformat your code, unless you don't have, or don't care about, comments.
</p>
<p>
In fact it's not the beautifier who discards comments — they are dumped at
the parsing stage, when we build the initial AST. Comments don't really
make sense in the AST, and while we could add nodes for them, it would be
inconvenient because we'd have to add special rules to ignore them at all
the processing stages.
</p>
</div>
</div>
</div>
<div id="outline-container-1_5" class="outline-3">
<h3 id="sec-1_5"><span class="section-number-3">1.5</span> Compression – how good is it? </h3>
<div class="outline-text-3" id="text-1_5">
<p>
(XXX: this is somewhat outdated. On the jQuery source code we beat Closure
by 168 bytes (560 after gzip) and by many seconds.)
</p>
<p>
There are a few popular JS minifiers nowadays – the two most well known
being the GoogleClosure (GCL) compiler and the YUI compressor. For some
reason they are both written in Java. I didn't really hope to beat any of
them, but finally I did – UglifyJS compresses better than the YUI
compressor, and safer than GoogleClosure.
</p>
<p>
I tested it on two big libraries. <a href="http://www.dynarchlib.com/">DynarchLIB</a> is my own, and it's big enough
to contain probably all the JavaScript tricks known to mankind. <a href="http://jquery.com/">jQuery</a> is
definitely the most popular JavaScript library (to some people, it's a
synonym to JavaScript itself).
</p>
<p>
I cannot swear that there are no bugs in the generated codes, but they
appear to work fine.
</p>
<p>
Compression results:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<caption></caption>
<colgroup><col align="left" /><col align="right" /><col align="right" /><col align="left" /><col align="left" />
</colgroup>
<thead>
<tr><th scope="col">Library</th><th scope="col">Orig. size</th><th scope="col">UglifyJS</th><th scope="col">YUI</th><th scope="col">GCL</th></tr>
</thead>
<tbody>
<tr><td>DynarchLIB</td><td>636896</td><td>241441</td><td>246452 (+5011)</td><td>240439 (-1002) (buggy)</td></tr>
<tr><td>jQuery</td><td>163855</td><td>72006</td><td>79702 (+7696)</td><td>71858 (-148)</td></tr>
</tbody>
</table>
<p>
UglifyJS is the fastest to run. On my laptop UglifyJS takes 1.35s for
DynarchLIB, while YUI takes 2.7s and GCL takes 6.5s.
</p>
<p>
GoogleClosure does a lot of smart ass optimizations. I had to strive really
hard to get close to it. It should be possible to even beat it, but then
again, GCL has a gazillion lines of code and runs terribly slow, so I'm not
sure it worths spending the effort to save a few bytes. Also, GCL doesn't
cope with <code>eval()</code> or <code>with{}</code> – it just dumps a warning and proceeds to
mangle names anyway; my DynarchLIB compiled with it is buggy because of
this.
</p>
<p>
UglifyJS consists of ~1100 lines of code for the tokenizer/parser, and ~1100
lines for the compressor and code generator. That should make it very
maintainable and easily extensible, so I would say it has a good place in
this field and it's bound to become the de-facto standard JS minifier. And
I shall rule the world. :-) Use it, and <b>spread the word</b>!
</p>
</div>
</div>
<div id="outline-container-1_6" class="outline-3">
<h3 id="sec-1_6"><span class="section-number-3">1.6</span> Bugs? </h3>
<div class="outline-text-3" id="text-1_6">
<p>
Unfortunately, for the time being there is no automated test suite. But I
ran the compressor manually on non-trivial code, and then I tested that the
generated code works as expected. A few hundred times.
</p>
<p>
DynarchLIB was started in times when there was no good JS minifier.
Therefore I was quite religious about trying to write short code manually,
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
= 10 : b = 20”, though the more readable version would clearly be to use
“if/else”.
</p>
<p>
Since the parser/compressor runs fine on DL and jQuery, I'm quite confident
that it's solid enough for production use. If you can identify any bugs,
I'd love to hear about them (<a href="http://groups.google.com/group/uglifyjs">use the Google Group</a> or email me directly).
</p>
</div>
</div>
<div id="outline-container-1_7" class="outline-3">
<h3 id="sec-1_7"><span class="section-number-3">1.7</span> Links </h3>
<div class="outline-text-3" id="text-1_7">
<ul>
<li>
Project at GitHub: <a href="http://github.com/mishoo/UglifyJS">http://github.com/mishoo/UglifyJS</a>
</li>
<li>
Google Group: <a href="http://groups.google.com/group/uglifyjs">http://groups.google.com/group/uglifyjs</a>
</li>
<li>
Common Lisp JS parser: <a href="http://marijn.haverbeke.nl/parse-js/">http://marijn.haverbeke.nl/parse-js/</a>
</li>
<li>
JS-to-Lisp compiler: <a href="http://github.com/marijnh/js">http://github.com/marijnh/js</a>
</li>
<li>
Common Lisp JS uglifier: <a href="http://github.com/mishoo/cl-uglify-js">http://github.com/mishoo/cl-uglify-js</a>
</li>
</ul>
</div>
</div>
<div id="outline-container-1_8" class="outline-3">
<h3 id="sec-1_8"><span class="section-number-3">1.8</span> License </h3>
<div class="outline-text-3" id="text-1_8">
<p>
UglifyJS is released under the BSD license:
</p>
<pre class="example">Copyright 2010 (c) Mihai Bazon <[email protected]>
Based on parse-js (http://marijn.haverbeke.nl/parse-js/).
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
</pre>
</div>
</div>
</div>
<div id="footnotes">
<h2 class="footnotes">Footnotes: </h2>
<div id="text-footnotes">
<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
<a href="http://marijn.haverbeke.nl/parse-js/">parse-js</a> library, and Marijn pushed fixes literally in minutes.
</p>
</div>
</div>
<div id="postamble">
<p class="author"> Author: Mihai Bazon
</p>
<p class="date"> Date: 2011-07-14 12:50:31 EEST</p>
<p class="creator">HTML generated by org-mode 7.01trans in emacs 23</p>
</div>
</div>
</body>
</html>