aboutsummaryrefslogtreecommitdiff
path: root/bin/uglifyjs
blob: ca75f1594b26feb1b39a2d607884232ba40d2b3f (about) (plain)
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
#! /usr/bin/env node
// -*- js -*-

"use strict";

var UglifyJS = require("../tools/node");
var sys = require("util");
var yargs = require("yargs");
var fs = require("fs");
var path = require("path");
var async = require("async");
var acorn;
var ARGS = yargs
    .usage("$0 input1.js [input2.js ...] [options]\n\
Use a single dash to read input from the standard input.\
\n\n\
NOTE: by default there is no mangling/compression.\n\
Without [options] it will simply parse input files and dump the AST\n\
with whitespace and comments discarded.  To achieve compression and\n\
mangling you need to use `-c` and `-m`.\
")
    .describe("source-map", "Specify an output file where to generate source map.")
    .describe("source-map-root", "The path to the original source to be included in the source map.")
    .describe("source-map-url", "The path to the source map to be added in //# sourceMappingURL.  Defaults to the value passed with --source-map.")
    .describe("source-map-include-sources", "Pass this flag if you want to include the content of source files in the source map as sourcesContent property.")
    .describe("in-source-map", "Input source map, useful if you're compressing JS that was generated from some other original code.")
    .describe("screw-ie8", "Pass this flag if you don't care about full compliance with Internet Explorer 6-8 quirks (by default UglifyJS will try to be IE-proof).")
    .describe("expr", "Parse a single expression, rather than a program (for parsing JSON)")
    .describe("p", "Skip prefix for original filenames that appear in source maps. \
For example -p 3 will drop 3 directories from file names and ensure they are relative paths. \
You can also specify -p relative, which will make UglifyJS figure out itself the relative paths between original sources, \
the source map and the output file.")
    .describe("o", "Output file (default STDOUT).")
    .describe("b", "Beautify output/specify output options.")
    .describe("m", "Mangle names/pass mangler options.")
    .describe("r", "Reserved names to exclude from mangling.")
    .describe("c", "Enable compressor/pass compressor options. \
Pass options like -c hoist_vars=false,if_return=false. \
Use -c with no argument to use the default compression options.")
    .describe("d", "Global definitions")
    .describe("e", "Embed everything in a big function, with a configurable parameter/argument list.")

    .describe("comments", "Preserve copyright comments in the output. \
By default this works like Google Closure, keeping JSDoc-style comments that contain \"@license\" or \"@preserve\". \
You can optionally pass one of the following arguments to this flag:\n\
- \"all\" to keep all comments\n\
- a valid JS regexp (needs to start with a slash) to keep only comments that match.\n\
\
Note that currently not *all* comments can be kept when compression is on, \
because of dead code removal or cascading statements into sequences.")

    .describe("preamble", "Preamble to prepend to the output.  You can use this to insert a \
comment, for example for licensing information.  This will not be \
parsed, but the source map will adjust for its presence.")

    .describe("stats", "Display operations run time on STDERR.")
    .describe("acorn", "Use Acorn for parsing.")
    .describe("spidermonkey", "Assume input files are SpiderMonkey AST format (as JSON).")
    .describe("self", "Build itself (UglifyJS2) as a library (implies --wrap=UglifyJS --export-all)")
    .describe("wrap", "Embed everything in a big function, making the “exports” and “global” variables available. \
You need to pass an argument to this option to specify the name that your module will take when included in, say, a browser.")
    .describe("export-all", "Only used when --wrap, this tells UglifyJS to add code to automatically export all globals.")
    .describe("lint", "Display some scope warnings")
    .describe("v", "Verbose")
    .describe("V", "Print version number and exit.")
    .describe("noerr", "Don't throw an error for unknown options in -c, -b or -m.")
    .describe("bare-returns", "Allow return outside of functions.  Useful when minifying CommonJS modules.")
    .describe("keep-fnames", "Do not mangle/drop function names.  Useful for code relying on Function.prototype.name.")
    .describe("quotes", "Quote style (0 - auto, 1 - single, 2 - double, 3 - original)")
    .describe("reserved-file", "File containing reserved names")
    .describe("reserve-domprops", "Make (most?) DOM properties reserved for --mangle-props")
    .describe("mangle-props", "Mangle property names")
    .describe("mangle-regex", "Only mangle property names matching the regex")
    .describe("name-cache", "File to hold mangled names mappings")
    .describe("pure-funcs", "List of functions that can be safely removed if their return value is not used")

    .alias("p", "prefix")
    .alias("o", "output")
    .alias("v", "verbose")
    .alias("b", "beautify")
    .alias("m", "mangle")
    .alias("c", "compress")
    .alias("d", "define")
    .alias("r", "reserved")
    .alias("V", "version")
    .alias("e", "enclose")
    .alias("q", "quotes")

    .string("source-map")
    .string("source-map-root")
    .string("source-map-url")
    .string("b")
    .string("beautify")
    .string("m")
    .string("mangle")
    .string("c")
    .string("compress")
    .string("d")
    .string("define")
    .string("e")
    .string("enclose")
    .string("comments")
    .string("wrap")
    .string("p")
    .string("prefix")
    .string("name-cache")
    .array("reserved-file")
    .array("pure-funcs")

    .boolean("expr")
    .boolean("source-map-include-sources")
    .boolean("screw-ie8")
    .boolean("export-all")
    .boolean("self")
    .boolean("v")
    .boolean("verbose")
    .boolean("stats")
    .boolean("acorn")
    .boolean("spidermonkey")
    .boolean("lint")
    .boolean("V")
    .boolean("version")
    .boolean("noerr")
    .boolean("bare-returns")
    .boolean("keep-fnames")
    .boolean("mangle-props")
    .boolean("reserve-domprops")

    .wrap(80)

    .argv
;

normalize(ARGS);

if (ARGS.noerr) {
    UglifyJS.DefaultsError.croak = function(msg, defs) {
        print_error("WARN: " + msg);
    };
}

if (ARGS.version || ARGS.V) {
    var json = require("../package.json");
    print(json.name + ' ' + json.version);
    process.exit(0);
}

if (ARGS.ast_help) {
    var desc = UglifyJS.describe_ast();
    print(typeof desc == "string" ? desc : JSON.stringify(desc, null, 2));
    process.exit(0);
}

if (ARGS.h || ARGS.help) {
    print(yargs.help());
    process.exit(0);
}

if (ARGS.acorn) {
    acorn = require("acorn");
}

var COMPRESS = getOptions("c", true);
var MANGLE = getOptions("m", true);
var BEAUTIFY = getOptions("b", true);
var RESERVED = null;

if (ARGS.reserved_file) ARGS.reserved_file.forEach(function(filename){
    RESERVED = UglifyJS.readReservedFile(filename, RESERVED);
});

if (ARGS.reserve_domprops) {
    RESERVED = UglifyJS.readDefaultReservedFile(RESERVED);
}

if (ARGS.d) {
    if (COMPRESS) COMPRESS.global_defs = getOptions("d");
}

if (ARGS.pure_funcs) {
    if (COMPRESS) COMPRESS.pure_funcs = ARGS.pure_funcs;
}

if (ARGS.r) {
    if (MANGLE) MANGLE.except = ARGS.r.replace(/^\s+|\s+$/g).split(/\s*,+\s*/);
}

if (RESERVED && MANGLE) {
    if (!MANGLE.except) MANGLE.except = RESERVED.vars;
    else MANGLE.except = MANGLE.except.concat(RESERVED.vars);
}

function readNameCache(key) {
    return UglifyJS.readNameCache(ARGS.name_cache, key);
}

function writeNameCache(key, cache) {
    return UglifyJS.writeNameCache(ARGS.name_cache, key, cache);
}

function extractRegex(str) {
  if (/^\/.*\/[a-zA-Z]*$/.test(str)) {
    var regex_pos = str.lastIndexOf("/");
    return new RegExp(str.substr(1, regex_pos - 1), str.substr(regex_pos + 1));
  } else {
    throw new Error("Invalid regular expression: " + str);
  }
}

if (ARGS.quotes === true) {
    ARGS.quotes = 3;
}

var OUTPUT_OPTIONS = {
    beautify    : BEAUTIFY ? true : false,
    preamble    : ARGS.preamble || null,
    quote_style : ARGS.quotes != null ? ARGS.quotes : 0
};

if (ARGS.screw_ie8) {
    if (COMPRESS) COMPRESS.screw_ie8 = true;
    if (MANGLE) MANGLE.screw_ie8 = true;
    OUTPUT_OPTIONS.screw_ie8 = true;
}

if (ARGS.keep_fnames) {
    if (COMPRESS) COMPRESS.keep_fnames = true;
    if (MANGLE) MANGLE.keep_fnames = true;
}

if (BEAUTIFY)
    UglifyJS.merge(OUTPUT_OPTIONS, BEAUTIFY);

if (ARGS.comments != null) {
    if (/^\/.*\/[a-zA-Z]*$/.test(ARGS.comments)) {
        try {
            OUTPUT_OPTIONS.comments = extractRegex(ARGS.comments);
        } catch (e) {
            print_error("ERROR: Invalid --comments: " + e.message);
        }
    } else if (ARGS.comments == "all") {
        OUTPUT_OPTIONS.comments = true;
    } else {
        OUTPUT_OPTIONS.comments = function(node, comment) {
            var text = comment.value;
            var type = comment.type;
            if (type == "comment2") {
                // multiline comment
                return /@preserve|@license|@cc_on/i.test(text);
            }
        }
    }
}

var files = ARGS._.slice();

if (ARGS.self) {
    if (files.length > 0) {
        print_error("WARN: Ignoring input files since --self was passed");
    }
    files = UglifyJS.FILES;
    if (!ARGS.wrap) ARGS.wrap = "UglifyJS";
}

var ORIG_MAP = ARGS.in_source_map;

if (ORIG_MAP) {
    ORIG_MAP = JSON.parse(fs.readFileSync(ORIG_MAP));
    if (files.length == 0) {
        print_error("INFO: Using file from the input source map: " + ORIG_MAP.file);
        files = [ ORIG_MAP.file ];
    }
    if (ARGS.source_map_root == null) {
        ARGS.source_map_root = ORIG_MAP.sourceRoot;
    }
}

if (files.length == 0) {
    files = [ "-" ];
}

if (files.indexOf("-") >= 0 && ARGS.source_map) {
    print_error("ERROR: Source map doesn't work with input from STDIN");
    process.exit(1);
}

if (files.filter(function(el){ return el == "-" }).length > 1) {
    print_error("ERROR: Can read a single file from STDIN (two or more dashes specified)");
    process.exit(1);
}

var STATS = {};
var OUTPUT_FILE = ARGS.o;
var TOPLEVEL = null;
var P_RELATIVE = ARGS.p && ARGS.p == "relative";
var SOURCES_CONTENT = {};

var SOURCE_MAP = ARGS.source_map ? UglifyJS.SourceMap({
    file: P_RELATIVE ? path.relative(path.dirname(ARGS.source_map), OUTPUT_FILE) : OUTPUT_FILE,
    root: ARGS.source_map_root,
    orig: ORIG_MAP,
}) : null;

OUTPUT_OPTIONS.source_map = SOURCE_MAP;

try {
    var output = UglifyJS.OutputStream(OUTPUT_OPTIONS);
    var compressor = COMPRESS && UglifyJS.Compressor(COMPRESS);
} catch(ex) {
    if (ex instanceof UglifyJS.DefaultsError) {
        print_error(ex.msg);
        print_error("Supported options:");
        print_error(sys.inspect(ex.defs));
        process.exit(1);
    }
}

async.eachLimit(files, 1, function (file, cb) {
    read_whole_file(file, function (err, code) {
        if (err) {
            print_error("ERROR: can't read file: " + file);
            process.exit(1);
        }
        if (ARGS.p != null) {
            if (P_RELATIVE) {
                file = path.relative(path.dirname(ARGS.source_map), file).replace(/\\/g, '/');
            } else {
                var p = parseInt(ARGS.p, 10);
                if (!isNaN(p)) {
                    file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/");
                }
            }
        }
        SOURCES_CONTENT[file] = code;
        time_it("parse", function(){
            if (ARGS.spidermonkey) {
                var program = JSON.parse(code);
                if (!TOPLEVEL) TOPLEVEL = program;
                else TOPLEVEL.body = TOPLEVEL.body.concat(program.body);
            }
            else if (ARGS.acorn) {
                TOPLEVEL = acorn.parse(code, {
                    locations     : true,
                    sourceFile    : file,
                    program       : TOPLEVEL
                });
            }
            else {
                try {
                    TOPLEVEL = UglifyJS.parse(code, {
                        filename     : file,
                        toplevel     : TOPLEVEL,
                        expression   : ARGS.expr,
                        bare_returns : ARGS.bare_returns,
                    });
                } catch(ex) {
                    if (ex instanceof UglifyJS.JS_Parse_Error) {
                        print_error("Parse error at " + file + ":" + ex.line + "," + ex.col);
                        print_error(ex.message);
                        print_error(ex.stack);
                        process.exit(1);
                    }
                    throw ex;
                }
            };
        });
        cb();
    });
}, function () {
    if (ARGS.acorn || ARGS.spidermonkey) time_it("convert_ast", function(){
        TOPLEVEL = UglifyJS.AST_Node.from_mozilla_ast(TOPLEVEL);
    });

    if (ARGS.wrap != null) {
        TOPLEVEL = TOPLEVEL.wrap_commonjs(ARGS.wrap, ARGS.export_all);
    }

    if (ARGS.enclose != null) {
        var arg_parameter_list = ARGS.enclose;
        if (arg_parameter_list === true) {
            arg_parameter_list = [];
        }
        else if (!(arg_parameter_list instanceof Array)) {
            arg_parameter_list = [arg_parameter_list];
        }
        TOPLEVEL = TOPLEVEL.wrap_enclose(arg_parameter_list);
    }

    if (ARGS.mangle_props || ARGS.name_cache) (function(){
        var reserved = RESERVED ? RESERVED.props : null;
        var cache = readNameCache("props");
        var regex;

        try {
          regex = ARGS.mangle_regex ? extractRegex(ARGS.mangle_regex) : null;
        } catch (e) {
            print_error("ERROR: Invalid --mangle-regex: " + e.message);
            process.exit(1);
        }

        TOPLEVEL = UglifyJS.mangle_properties(TOPLEVEL, {
            reserved   : reserved,
            cache      : cache,
            only_cache : !ARGS.mangle_props,
            regex      : regex
        });
        writeNameCache("props", cache);
    })();

    var TL_CACHE = readNameCache("vars");

    time_it("scope", function(){
        TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8, cache: TL_CACHE });
        if (ARGS.lint) {
            TOPLEVEL.scope_warnings();
        }
    });

    if (COMPRESS) {
        time_it("squeeze", function(){
            TOPLEVEL = TOPLEVEL.transform(compressor);
        });
    }

    time_it("scope", function(){
        TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8, cache: TL_CACHE });
        if (MANGLE && !TL_CACHE) {
            TOPLEVEL.compute_char_frequency(MANGLE);
        }
    });

    if (MANGLE) time_it("mangle", function(){
        MANGLE.cache = TL_CACHE;
        TOPLEVEL.mangle_names(MANGLE);
    });

    writeNameCache("vars", TL_CACHE);

    if (ARGS.source_map_include_sources) {
        for (var file in SOURCES_CONTENT) {
            if (SOURCES_CONTENT.hasOwnProperty(file)) {
                SOURCE_MAP.get().setSourceContent(file, SOURCES_CONTENT[file]);
            }
        }
    }

    time_it("generate", function(){
        TOPLEVEL.print(output);
    });

    output = output.get();

    if (SOURCE_MAP) {
        fs.writeFileSync(ARGS.source_map, SOURCE_MAP, "utf8");
        var source_map_url = ARGS.source_map_url || (
            P_RELATIVE
                ? path.relative(path.dirname(OUTPUT_FILE), ARGS.source_map)
                : ARGS.source_map
        );
        output += "\n//# sourceMappingURL=" + source_map_url;
    }

    if (OUTPUT_FILE) {
        fs.writeFileSync(OUTPUT_FILE, output, "utf8");
    } else {
        print(output);
    }

    if (ARGS.stats) {
        print_error(UglifyJS.string_template("Timing information (compressed {count} files):", {
            count: files.length
        }));
        for (var i in STATS) if (STATS.hasOwnProperty(i)) {
            print_error(UglifyJS.string_template("- {name}: {time}s", {
                name: i,
                time: (STATS[i] / 1000).toFixed(3)
            }));
        }
    }
});

/* -----[ functions ]----- */

function normalize(o) {
    for (var i in o) if (o.hasOwnProperty(i) && /-/.test(i)) {
        o[i.replace(/-/g, "_")] = o[i];
        delete o[i];
    }
}

function getOptions(x, constants) {
    x = ARGS[x];
    if (x == null) return null;
    var ret = {};
    if (x !== "") {
        var ast;
        try {
            ast = UglifyJS.parse(x, { expression: true });
        } catch(ex) {
            if (ex instanceof UglifyJS.JS_Parse_Error) {
                print_error("Error parsing arguments in: " + x);
                process.exit(1);
            }
        }
        ast.walk(new UglifyJS.TreeWalker(function(node){
            if (node instanceof UglifyJS.AST_Seq) return; // descend
            if (node instanceof UglifyJS.AST_Assign) {
                var name = node.left.print_to_string({ beautify: false }).replace(/-/g, "_");
                var value = node.right;
                if (constants)
                    value = new Function("return (" + value.print_to_string() + ")")();
                ret[name] = value;
                return true;    // no descend
            }
            if (node instanceof UglifyJS.AST_Symbol || node instanceof UglifyJS.AST_Binary) {
                var name = node.print_to_string({ beautify: false }).replace(/-/g, "_");
                ret[name] = true;
                return true;    // no descend
            }
            print_error(node.TYPE)
            print_error("Error parsing arguments in: " + x);
            process.exit(1);
        }));
    }
    return ret;
}

function read_whole_file(filename, cb) {
    if (filename == "-") {
        var chunks = [];
        process.stdin.setEncoding('utf-8');
        process.stdin.on('data', function (chunk) {
            chunks.push(chunk);
        }).on('end', function () {
            cb(null, chunks.join(""));
        });
        process.openStdin();
    } else {
        fs.readFile(filename, "utf-8", cb);
    }
}

function time_it(name, cont) {
    var t1 = new Date().getTime();
    var ret = cont();
    if (ARGS.stats) {
        var spent = new Date().getTime() - t1;
        if (STATS[name]) STATS[name] += spent;
        else STATS[name] = spent;
    }
    return ret;
}

function print_error(msg) {
    console.error("%s", msg);
}

function print(txt) {
    console.log("%s", txt);
}