7
7
< title > UglifyJS -- a JavaScript parser/compressor/beautifier</ title >
8
8
< meta http-equiv ="Content-Type " content ="text/html;charset=utf-8 "/>
9
9
< meta name ="generator " content ="Org-mode "/>
10
- < meta name ="generated " content ="2011-02-28 22:35:00 EET "/>
10
+ < meta name ="generated " content ="2011-04-04 00:07:13 EEST "/>
11
11
< meta name ="author " content ="Mihai Bazon "/>
12
12
< meta name ="description " content ="a JavaScript parser/compressor/beautifier in JavaScript "/>
13
13
< meta name ="keywords " content ="javascript, js, parser, compiler, compressor, mangle, minify, minifier "/>
@@ -87,16 +87,18 @@ <h2>Table of Contents</h2>
87
87
< li > < a href ="#sec-1_1_1 "> 1.1.1 Calls involving the global Array constructor </ a > </ li >
88
88
</ ul >
89
89
</ li >
90
- < li > < a href ="#sec-1_2 "> 1.2 Usage </ a >
90
+ < li > < a href ="#sec-1_2 "> 1.2 Install (NPM) </ a > </ li >
91
+ < li > < a href ="#sec-1_3 "> 1.3 Install latest code from GitHub </ a > </ li >
92
+ < li > < a href ="#sec-1_4 "> 1.4 Usage </ a >
91
93
< ul >
92
- < li > < a href ="#sec-1_2_1 "> 1.2 .1 API </ a > </ li >
93
- < li > < a href ="#sec-1_2_2 "> 1.2 .2 Beautifier shortcoming – no more comments </ a > </ li >
94
+ < li > < a href ="#sec-1_4_1 "> 1.4 .1 API </ a > </ li >
95
+ < li > < a href ="#sec-1_4_2 "> 1.4 .2 Beautifier shortcoming – no more comments </ a > </ li >
94
96
</ ul >
95
97
</ li >
96
- < li > < a href ="#sec-1_3 "> 1.3 Compression – how good is it? </ a > </ li >
97
- < li > < a href ="#sec-1_4 "> 1.4 Bugs? </ a > </ li >
98
- < li > < a href ="#sec-1_5 "> 1.5 Links </ a > </ li >
99
- < li > < a href ="#sec-1_6 "> 1.6 License </ a > </ li >
98
+ < li > < a href ="#sec-1_5 "> 1.5 Compression – how good is it? </ a > </ li >
99
+ < li > < a href ="#sec-1_6 "> 1.6 Bugs? </ a > </ li >
100
+ < li > < a href ="#sec-1_7 "> 1.7 Links </ a > </ li >
101
+ < li > < a href ="#sec-1_8 "> 1.8 License </ a > </ li >
100
102
</ ul >
101
103
</ li >
102
104
</ ul >
@@ -108,9 +110,6 @@ <h2 id="sec-1"><span class="section-number-2">1</span> UglifyJS — a JavaSc
108
110
< div class ="outline-text-2 " id ="text-1 ">
109
111
110
112
111
- < p >
112
- < b > Update</ b > : please read the section on < a href ="#sec-1_1 "> unsafe transformations</ a > .
113
- </ p >
114
113
< p >
115
114
This package implements a general-purpose JavaScript
116
115
parser/compressor/beautifier toolkit. It is developed on < a href ="http://nodejs.org/ "> NodeJS</ a > , but it
@@ -250,10 +249,10 @@ <h4 id="sec-1_1_1"><span class="section-number-4">1.1.1</span> Calls involving t
250
249
251
250
252
251
253
- < pre class ="src src-espresso "> < span style ="color: #afeeee; font-weight: bold; "> new</ span > < span style ="color: #87ceeb; font-weight: bold ; "> Array</ span > (1, 2, 3, 4) => [1,2,3,4]
252
+ < pre class ="src src-js "> < span style ="color: #a020f0; "> new</ span > < span style ="color: #228b22 ; "> Array</ span > (1, 2, 3, 4) => [1,2,3,4]
254
253
Array(a, b, c) => [a,b,c]
255
- < span style ="color: #afeeee; font-weight: bold; "> new</ span > < span style ="color: #87ceeb; font-weight: bold ; "> Array</ span > (5) => Array(5)
256
- < span style ="color: #afeeee; font-weight: bold; "> new</ span > < span style ="color: #87ceeb; font-weight: bold ; "> Array</ span > (a) => Array(a)
254
+ < span style ="color: #a020f0; "> new</ span > < span style ="color: #228b22 ; "> Array</ span > (5) => Array(5)
255
+ < span style ="color: #a020f0; "> new</ span > < span style ="color: #228b22 ; "> Array</ span > (a) => Array(a)
257
256
</ pre >
258
257
259
258
@@ -271,38 +270,38 @@ <h4 id="sec-1_1_1"><span class="section-number-4">1.1.1</span> Calls involving t
271
270
272
271
273
272
274
- < pre class ="src src-espresso "> < span style ="color: #add8e6 ; "> // </ span > < span style ="color: #add8e6 ; "> case 1. globally declared variable
275
- </ span > < span style ="color: #afeeee; font-weight: bold; "> var</ span > < span style ="color: #40e0d0; font-weight: bold ; "> Array</ span > ;
276
- < span style ="color: #afeeee; font-weight: bold; "> new</ span > < span style ="color: #87ceeb; font-weight: bold ; "> Array</ span > (1, 2, 3);
273
+ < pre class ="src src-js "> < span style ="color: #b22222 ; "> // </ span > < span style ="color: #b22222 ; "> case 1. globally declared variable
274
+ </ span > < span style ="color: #a020f0; "> var</ span > < span style ="color: #b8860b ; "> Array</ span > ;
275
+ < span style ="color: #a020f0; "> new</ span > < span style ="color: #228b22 ; "> Array</ span > (1, 2, 3);
277
276
Array(a, b);
278
277
279
- < span style ="color: #add8e6 ; "> // </ span > < span style ="color: #add8e6 ; "> or (can be declared later)
280
- </ span > < span style ="color: #afeeee; font-weight: bold; "> new</ span > < span style ="color: #87ceeb; font-weight: bold ; "> Array</ span > (1, 2, 3);
281
- < span style ="color: #afeeee; font-weight: bold; "> var</ span > < span style ="color: #40e0d0; font-weight: bold ; "> Array</ span > ;
278
+ < span style ="color: #b22222 ; "> // </ span > < span style ="color: #b22222 ; "> or (can be declared later)
279
+ </ span > < span style ="color: #a020f0; "> new</ span > < span style ="color: #228b22 ; "> Array</ span > (1, 2, 3);
280
+ < span style ="color: #a020f0; "> var</ span > < span style ="color: #b8860b ; "> Array</ span > ;
282
281
283
- < span style ="color: #add8e6 ; "> // </ span > < span style ="color: #add8e6 ; "> or (can be a function)
284
- </ span > < span style ="color: #afeeee; font-weight: bold; "> new</ span > < span style ="color: #87ceeb; font-weight: bold ; "> Array</ span > (1, 2, 3);
285
- < span style ="color: #afeeee; font-weight: bold; "> function</ span > < span style ="color: #7fffd4; font-weight: bold ; "> Array</ span > () { ... }
282
+ < span style ="color: #b22222 ; "> // </ span > < span style ="color: #b22222 ; "> or (can be a function)
283
+ </ span > < span style ="color: #a020f0; "> new</ span > < span style ="color: #228b22 ; "> Array</ span > (1, 2, 3);
284
+ < span style ="color: #a020f0; "> function</ span > < span style ="color: #0000ff ; "> Array</ span > () { ... }
286
285
287
- < span style ="color: #add8e6 ; "> // </ span > < span style ="color: #add8e6 ; "> case 2. declared in a function
288
- </ span > (< span style ="color: #afeeee; font-weight: bold ; "> function</ span > (){
289
- a = < span style ="color: #afeeee; font-weight: bold; "> new</ span > < span style ="color: #87ceeb; font-weight: bold ; "> Array</ span > (1, 2, 3);
286
+ < span style ="color: #b22222 ; "> // </ span > < span style ="color: #b22222 ; "> case 2. declared in a function
287
+ </ span > (< span style ="color: #a020f0 ; "> function</ span > (){
288
+ a = < span style ="color: #a020f0; "> new</ span > < span style ="color: #228b22 ; "> Array</ span > (1, 2, 3);
290
289
b = Array(5, 6);
291
- < span style ="color: #afeeee; font-weight: bold; "> var</ span > < span style ="color: #40e0d0; font-weight: bold ; "> Array</ span > ;
290
+ < span style ="color: #a020f0; "> var</ span > < span style ="color: #b8860b ; "> Array</ span > ;
292
291
})();
293
292
294
- < span style ="color: #add8e6 ; "> // </ span > < span style ="color: #add8e6 ; "> or
295
- </ span > (< span style ="color: #afeeee; font-weight: bold; "> function</ span > (< span style ="color: #40e0d0; font-weight: bold ; "> Array</ span > ){
296
- < span style ="color: #afeeee; font-weight: bold ; "> return</ span > Array(5, 6, 7);
293
+ < span style ="color: #b22222 ; "> // </ span > < span style ="color: #b22222 ; "> or
294
+ </ span > (< span style ="color: #a020f0; "> function</ span > (< span style ="color: #b8860b ; "> Array</ span > ){
295
+ < span style ="color: #a020f0 ; "> return</ span > Array(5, 6, 7);
297
296
})();
298
297
299
- < span style ="color: #add8e6 ; "> // </ span > < span style ="color: #add8e6 ; "> or
300
- </ span > (< span style ="color: #afeeee; font-weight: bold ; "> function</ span > (){
301
- < span style ="color: #afeeee; font-weight: bold; "> return</ span > < span style ="color: #afeeee; font-weight: bold; "> new</ span > < span style ="color: #87ceeb; font-weight: bold ; "> Array</ span > (1, 2, 3, 4);
302
- < span style ="color: #afeeee; font-weight: bold; "> function</ span > < span style ="color: #7fffd4; font-weight: bold ; "> Array</ span > () { ... }
298
+ < span style ="color: #b22222 ; "> // </ span > < span style ="color: #b22222 ; "> or
299
+ </ span > (< span style ="color: #a020f0 ; "> function</ span > (){
300
+ < span style ="color: #a020f0; "> return</ span > < span style ="color: #a020f0; "> new</ span > < span style ="color: #228b22 ; "> Array</ span > (1, 2, 3, 4);
301
+ < span style ="color: #a020f0; "> function</ span > < span style ="color: #0000ff ; "> Array</ span > () { ... }
303
302
})();
304
303
305
- < span style ="color: #add8e6 ; "> // </ span > < span style ="color: #add8e6 ; "> etc.
304
+ < span style ="color: #b22222 ; "> // </ span > < span style ="color: #b22222 ; "> etc.
306
305
</ span > </ pre >
307
306
308
307
@@ -313,13 +312,57 @@ <h4 id="sec-1_1_1"><span class="section-number-4">1.1.1</span> Calls involving t
313
312
</ div >
314
313
315
314
< div id ="outline-container-1_2 " class ="outline-3 ">
316
- < h3 id ="sec-1_2 "> < span class ="section-number-3 "> 1.2</ span > Usage </ h3 >
315
+ < h3 id ="sec-1_2 "> < span class ="section-number-3 "> 1.2</ span > Install (NPM) </ h3 >
317
316
< div class ="outline-text-3 " id ="text-1_2 ">
318
317
319
318
320
319
< p >
321
- There is a helper script now — < code > bin/uglifyjs</ code > — that uses the library to
322
- compress a script using the maximum compression settings. Synopsis:
320
+ UglifyJS is now available through NPM — < code > npm install uglify-js</ code > should do
321
+ the job.
322
+ </ p >
323
+ </ div >
324
+
325
+ </ div >
326
+
327
+ < div id ="outline-container-1_3 " class ="outline-3 ">
328
+ < h3 id ="sec-1_3 "> < span class ="section-number-3 "> 1.3</ span > Install latest code from GitHub </ h3 >
329
+ < div class ="outline-text-3 " id ="text-1_3 ">
330
+
331
+
332
+
333
+
334
+
335
+ < pre class ="src src-sh "> < span style ="color: #b22222; "> ## </ span > < span style ="color: #b22222; "> clone the repository
336
+ </ span > mkdir -p /where/you/wanna/put/it
337
+ < span style ="color: #da70d6; "> cd</ span > /where/you/wanna/put/it
338
+ git clone git://github.com/mishoo/UglifyJS.git
339
+
340
+ < span style ="color: #b22222; "> ## </ span > < span style ="color: #b22222; "> make the module available to Node
341
+ </ span > mkdir -p ~/.node_libraries/
342
+ < span style ="color: #da70d6; "> cd</ span > ~/.node_libraries/
343
+ ln -s /where/you/wanna/put/it/UglifyJS/uglify-js.js
344
+
345
+ < span style ="color: #b22222; "> ## </ span > < span style ="color: #b22222; "> and if you want the CLI script too:
346
+ </ span > mkdir -p ~/bin
347
+ < span style ="color: #da70d6; "> cd</ span > ~/bin
348
+ ln -s /where/you/wanna/put/it/UglifyJS/bin/uglifyjs
349
+ < span style ="color: #b22222; "> # </ span > < span style ="color: #b22222; "> (then add ~/bin to your $PATH if it's not there already)
350
+ </ span > </ pre >
351
+
352
+
353
+
354
+ </ div >
355
+
356
+ </ div >
357
+
358
+ < div id ="outline-container-1_4 " class ="outline-3 ">
359
+ < h3 id ="sec-1_4 "> < span class ="section-number-3 "> 1.4</ span > Usage </ h3 >
360
+ < div class ="outline-text-3 " id ="text-1_4 ">
361
+
362
+
363
+ < p >
364
+ There is a command-line tool that exposes the functionality of this library
365
+ for your shell-scripting needs:
323
366
</ p >
324
367
325
368
@@ -456,26 +499,26 @@ <h3 id="sec-1_2"><span class="section-number-3">1.2</span> Usage </h3>
456
499
457
500
</ div >
458
501
459
- < div id ="outline-container-1_2_1 " class ="outline-4 ">
460
- < h4 id ="sec-1_2_1 "> < span class ="section-number-4 "> 1.2 .1</ span > API </ h4 >
461
- < div class ="outline-text-4 " id ="text-1_2_1 ">
502
+ < div id ="outline-container-1_4_1 " class ="outline-4 ">
503
+ < h4 id ="sec-1_4_1 "> < span class ="section-number-4 "> 1.4 .1</ span > API </ h4 >
504
+ < div class ="outline-text-4 " id ="text-1_4_1 ">
462
505
463
506
464
507
< p >
465
- Symlink the < b > lib </ b > directory as < b > ~/.node_libraries/uglifyjs </ b > , so that the
466
- < b > require </ b > calls in the following sample will work :
508
+ To use the library from JavaScript, you'd do the following (example for
509
+ NodeJS) :
467
510
</ p >
468
511
469
512
470
513
471
- < pre class ="src src-espresso "> < span style ="color: #afeeee; font-weight: bold; "> var</ span > < span style ="color: #40e0d0; font-weight: bold; "> jsp</ span > = require(< span style ="color: #87cefa ; "> "uglifyjs/parse -js"</ span > );
472
- < span style ="color: #afeeee; font-weight: bold; "> var</ span > < span style ="color: #40e0d0; font-weight: bold; "> pro</ span > = require(< span style ="color: #87cefa ; "> "uglifyjs/process "</ span > );
514
+ < pre class ="src src-js "> < span style ="color: #a020f0; "> var</ span > < span style ="color: #b8860b; "> jsp</ span > = require(< span style ="color: #bc8f8f ; "> "uglify -js"</ span > ).parser ;
515
+ < span style ="color: #a020f0; "> var</ span > < span style ="color: #b8860b; "> pro</ span > = require(< span style ="color: #bc8f8f ; "> "uglify-js "</ span > ).uglify ;
473
516
474
- < span style ="color: #afeeee; font-weight: bold; "> var</ span > < span style ="color: #40e0d0; font-weight: bold; "> orig_code</ span > = < span style ="color: #87cefa ; "> "... JS code here"</ span > ;
475
- < span style ="color: #afeeee; font-weight: bold; "> var</ span > < span style ="color: #40e0d0; font-weight: bold; "> ast</ span > = jsp.parse(orig_code); < span style ="color: #add8e6 ; "> // </ span > < span style ="color: #add8e6 ; "> parse code and get the initial AST
476
- </ span > ast = pro.ast_mangle(ast); < span style ="color: #add8e6 ; "> // </ span > < span style ="color: #add8e6 ; "> get a new AST with mangled names
477
- </ span > ast = pro.ast_squeeze(ast); < span style ="color: #add8e6 ; "> // </ span > < span style ="color: #add8e6 ; "> get an AST with compression optimizations
478
- </ span > < span style ="color: #afeeee; font-weight: bold; "> var</ span > < span style ="color: #40e0d0; font-weight: bold; "> final_code</ span > = pro.gen_code(ast); < span style ="color: #add8e6 ; "> // </ span > < span style ="color: #add8e6 ; "> compressed code here
517
+ < span style ="color: #a020f0; "> var</ span > < span style ="color: #b8860b; "> orig_code</ span > = < span style ="color: #bc8f8f ; "> "... JS code here"</ span > ;
518
+ < span style ="color: #a020f0; "> var</ span > < span style ="color: #b8860b; "> ast</ span > = jsp.parse(orig_code); < span style ="color: #b22222 ; "> // </ span > < span style ="color: #b22222 ; "> parse code and get the initial AST
519
+ </ span > ast = pro.ast_mangle(ast); < span style ="color: #b22222 ; "> // </ span > < span style ="color: #b22222 ; "> get a new AST with mangled names
520
+ </ span > ast = pro.ast_squeeze(ast); < span style ="color: #b22222 ; "> // </ span > < span style ="color: #b22222 ; "> get an AST with compression optimizations
521
+ </ span > < span style ="color: #a020f0; "> var</ span > < span style ="color: #b8860b; "> final_code</ span > = pro.gen_code(ast); < span style ="color: #b22222 ; "> // </ span > < span style ="color: #b22222 ; "> compressed code here
479
522
</ span > </ pre >
480
523
481
524
@@ -572,9 +615,9 @@ <h4 id="sec-1_2_1"><span class="section-number-4">1.2.1</span> API </h4>
572
615
573
616
</ div >
574
617
575
- < div id ="outline-container-1_2_2 " class ="outline-4 ">
576
- < h4 id ="sec-1_2_2 "> < span class ="section-number-4 "> 1.2 .2</ span > Beautifier shortcoming – no more comments </ h4 >
577
- < div class ="outline-text-4 " id ="text-1_2_2 ">
618
+ < div id ="outline-container-1_4_2 " class ="outline-4 ">
619
+ < h4 id ="sec-1_4_2 "> < span class ="section-number-4 "> 1.4 .2</ span > Beautifier shortcoming – no more comments </ h4 >
620
+ < div class ="outline-text-4 " id ="text-1_4_2 ">
578
621
579
622
580
623
< p >
@@ -595,9 +638,9 @@ <h4 id="sec-1_2_2"><span class="section-number-4">1.2.2</span> Beautifier shortc
595
638
596
639
</ div >
597
640
598
- < div id ="outline-container-1_3 " class ="outline-3 ">
599
- < h3 id ="sec-1_3 "> < span class ="section-number-3 "> 1.3 </ span > Compression – how good is it? </ h3 >
600
- < div class ="outline-text-3 " id ="text-1_3 ">
641
+ < div id ="outline-container-1_5 " class ="outline-3 ">
642
+ < h3 id ="sec-1_5 "> < span class ="section-number-3 "> 1.5 </ span > Compression – how good is it? </ h3 >
643
+ < div class ="outline-text-3 " id ="text-1_5 ">
601
644
602
645
603
646
< p >
@@ -662,9 +705,9 @@ <h3 id="sec-1_3"><span class="section-number-3">1.3</span> Compression – h
662
705
663
706
</ div >
664
707
665
- < div id ="outline-container-1_4 " class ="outline-3 ">
666
- < h3 id ="sec-1_4 "> < span class ="section-number-3 "> 1.4 </ span > Bugs? </ h3 >
667
- < div class ="outline-text-3 " id ="text-1_4 ">
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 ">
668
711
669
712
670
713
< p >
@@ -688,9 +731,9 @@ <h3 id="sec-1_4"><span class="section-number-3">1.4</span> Bugs? </h3>
688
731
689
732
</ div >
690
733
691
- < div id ="outline-container-1_5 " class ="outline-3 ">
692
- < h3 id ="sec-1_5 "> < span class ="section-number-3 "> 1.5 </ span > Links </ h3 >
693
- < div class ="outline-text-3 " id ="text-1_5 ">
734
+ < div id ="outline-container-1_7 " class ="outline-3 ">
735
+ < h3 id ="sec-1_7 "> < span class ="section-number-3 "> 1.7 </ span > Links </ h3 >
736
+ < div class ="outline-text-3 " id ="text-1_7 ">
694
737
695
738
696
739
< ul >
@@ -716,9 +759,9 @@ <h3 id="sec-1_5"><span class="section-number-3">1.5</span> Links </h3>
716
759
717
760
</ div >
718
761
719
- < div id ="outline-container-1_6 " class ="outline-3 ">
720
- < h3 id ="sec-1_6 "> < span class ="section-number-3 "> 1.6 </ span > License </ h3 >
721
- < div class ="outline-text-3 " id ="text-1_6 ">
762
+ < div id ="outline-container-1_8 " class ="outline-3 ">
763
+ < h3 id ="sec-1_8 "> < span class ="section-number-3 "> 1.8 </ span > License </ h3 >
764
+ < div class ="outline-text-3 " id ="text-1_8 ">
722
765
723
766
724
767
< p >
@@ -774,7 +817,7 @@ <h2 class="footnotes">Footnotes: </h2>
774
817
< div id ="postamble ">
775
818
< p class ="author "> Author: Mihai Bazon
776
819
</ p >
777
- < p class ="date "> Date: 2011-02-28 22:35:00 EET </ p >
820
+ < p class ="date "> Date: 2011-04-04 00:07:13 EEST </ p >
778
821
< p class ="creator "> HTML generated by org-mode 7.01trans in emacs 23</ p >
779
822
</ div >
780
823
</ div >
0 commit comments