aboutsummaryrefslogtreecommitdiff
path: root/bin/uglifyjs2
blob: 39a58e1df693b23fede95cad90e10660a6b71f7a (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
#! /usr/bin/env node
// -*- js -*-

var UglifyJS = require("../tools/node");
var sys = require("util");
var optimist = require("optimist");
var fs = require("fs");
var ARGS = optimist
    .usage("uglifyjs2 [options] input1.js input2.js ...\n\
(maximum compression settings are on by default)\
")
    .describe("source-map", "Specify an output file where to generate source map")
    .describe("source-map-root", "The root of the original source to be included in the source map")
    .describe("p", "Skip prefix for original filenames that appear in source maps")
    .describe("o", "Output file (default STDOUT)")
    .describe("stats", "Display operations run time on STDERR")
    .describe("v", "Verbose")
    .describe("b", "Beautify output")

    .alias("p", "prefix")
    .alias("o", "output")
    .alias("v", "verbose")
    .alias("b", "beautify")

    .boolean("b")
    .boolean("v")

    .argv
;

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

if (ARGS.h || ARGS.help) {
    sys.puts(optimist.help());
    process.exit(0);
}

var files = ARGS._.slice();

if (files.length == 0) {
    sys.error("ERROR: No input files.");
    sys.puts(optimist.help());
    process.exit(1);
}

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

if (files.filter(function(el){ return el == "-" }).length > 1) {
    sys.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 SOURCE_MAP = ARGS.source_map ? UglifyJS.SourceMap({
    file: output,
    root: ARGS.source_map_root
}) : null;

var output = UglifyJS.OutputStream({
    beautify: ARGS.b,
    source_map: SOURCE_MAP
});

files.forEach(do_file);

output = output.get();

if (SOURCE_MAP) {
    fs.writeFileSync(ARGS.source_map, SOURCE_MAP, "utf8");
    output += "\n//@ sourceMappingURL=" + ARGS.source_map;
}

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

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

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

function do_file(file) {
    if (ARGS.v) {
        sys.error("Compressing " + file);
    }
    var code = read_whole_file(file);
    var ast;
    time_it("parse", function(){
        ast = UglifyJS.parse(code);
    });
    time_it("scope", function(){
        ast.figure_out_scope();
    });
    time_it("mangle", function(){
        ast.mangle_names();
    });
    time_it("squeeze", function(){
        var compressor = UglifyJS.Compressor({});
        ast = ast.squeeze(compressor);
    });
    time_it("generate", function(){
        if (SOURCE_MAP) {
            if (ARGS.p != null) {
                file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/");
            }
            SOURCE_MAP.set_source(file);
        }
        ast.print(output);
    });
}

function read_whole_file(filename) {
    if (filename == "-") {
        // XXX: this sucks.  How does one read the whole STDIN
        // synchronously?
        filename = "/dev/stdin";
    }
    try {
        return fs.readFileSync(filename, "utf8");
    } catch(ex) {
        sys.error("ERROR: can't read file: " + filename);
        process.exit(1);
    }
}

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;
};