aboutsummaryrefslogtreecommitdiff
path: root/bin/uglifyjs2
blob: e79f71c8b1e4e3d5b29856132e4128d4bf690e93 (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
#! /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.\n\
Use a single dash to read input from the standard input.\
")
    .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("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.")
    .describe("o", "Output file (default STDOUT)")
    .describe("b", "Beautify output")
    .describe("m", "Don't mangle names")
    .describe("c", "Disable compressor, or pass compressor options. \
Pass options like -c hoist_vars=false,if_return=false. \
Use -c with no argument if you want to disable the squeezer entirely")

    .describe("stats", "Display operations run time on STDERR")
    .describe("v", "Verbose")

    .alias("p", "prefix")
    .alias("o", "output")
    .alias("v", "verbose")
    .alias("b", "beautify")
    .alias("c", "options")
    .alias("m", "no-mangle")

    .boolean("b")
    .boolean("v")
    .boolean("stats")
    .boolean("m")
    .string("c")

    .wrap(80)

    .argv
;

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

normalize(ARGS);

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

var COMPRESSOR_OPTIONS = {};
if (ARGS.c && ARGS.c !== true) {
    ARGS.c.replace(/^\s+|\s+$/g).split(/\s*,+\s*/).forEach(function(opt){
        var a = opt.split(/\s*=\s*/);
        COMPRESSOR_OPTIONS[a[0]] = new Function("return(" + a[1] + ")")();
    });
}

var files = ARGS._.slice();

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

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_FILE,
    root: ARGS.source_map_root
}) : null;

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

files = files.map(do_file_1);
files = files.map(do_file_2);
UglifyJS.base54.sort();
files.forEach(do_file_3);
if (ARGS.v) {
    sys.error("BASE54 digits: " + UglifyJS.base54.get());
    //sys.error("Frequency: " + sys.inspect(UglifyJS.base54.freq()));
}

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_1(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();
    });
    if (ARGS.c !== true) {
        time_it("squeeze", function(){
            var compressor = UglifyJS.Compressor(COMPRESSOR_OPTIONS);
            ast = ast.squeeze(compressor);
        });
    }
    ast.filename = file;
    return ast;
}

function do_file_2(ast) {
    time_it("scope", function(){
        ast.figure_out_scope();
        if (!ARGS.m) {
            ast.compute_char_frequency();
        }
    });
    return ast;
}

function do_file_3(ast) {
    var file = ast.filename;
    // if (ARGS.v) {
    //     sys.error("Mangling/generating " + file);
    // }
    if (!ARGS.m) time_it("mangle", function(){
        ast.mangle_names();
    });
    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;
};
z.scm (guile-fibers)[arguments]: Add #:configure-flags. Switch to GUILE-3.0. (guile2.2-fibers): New variable. (guile3.0-fibers): Deprecate. (guile-syntax-highlight): Switch to GUILE-3.0. (guile2.2-syntax-highlight): New variable. (guile3.0-syntax-highlight): Deprecate. (guile-colorized): Switch to GUILE-3.0. (guile2.2-colorized): New variable. (guile3.0-colorized): Deprecate. (guile-pfds): Switch to GUILE-3.0. (guile2.2-pfds): New variable. (guile3.0-pfds): Deprecate. (guile-simple-zmq): Switch to GUILE-3.0. (guile2.2-simple-zmq): New variable. (guile3.0-simple-zmq): Deprecate. (guile-newt): Switch to GUILE-3.0. (guile2.2-newt): New variable. (guile3.0-newt): Deprecate. (guile-parted): Switch to GUILE-3.0. (guile2.2-parted): New variable. (guile3.0-parted): Deprecate. (guile-config): Switch to GUILE-3.0. (guile2.2-config): New variable. (guile3.0-config): Deprecate. (guile-hall): Switch to GUILE-3.0. (guile2.2-hall): New variable. (guile3.0-hall): Deprecate. (guile-ics): Switch to GUILE-3.0. (guile2.2-ics): New variable. (guile3.0-ics): Deprecate. (guile-wisp)[arguments]: Add 'support-guile-3.0' phase. Switch to GUILE-3.0. (guile2.2-wisp): New variable. (guile3.0-wisp): Deprecate. (guile-lib): Switch to GUILE-3.0. (guile2.2-lib): New variable. (guile3.0-lib): Deprecate. (guile-minikanren): Switch to GUILE-3.0. (guile2.2-minikanren): New variable. (guile3.0-minikanren): Deprecate. (guile-irregex): Switch to GUILE-3.0. (guile2.2-irregex): New variable. (guile3.0-irregex): Deprecate. (haunt): Switch to GUILE-3.0, and remove GUILE-READER. (guile2.2-haunt): New variable. (guile3.0-haunt): Deprecate. (guile-commonmark): Switch to GUILE-3.0. (guile2.2-commonmark): New variable. (guile3.0-commonmark): Deprecate. (mcron): Switch to GUILE-3.0. (guile2.0-mcron): New variable. (guile3.0-mcron): Deprecate. (guile-picture-language): Switch to GUILE-3.0. (guile2.2-picture-language): New variable. (guile3.0-picture-language): Deprecate. (guile-gi): Switch to GUILE-3.0. (guile2.2-gi): New variable. (guile3.0-gi): Deprecate. (guile-hashing): Switch to GUILE-3.0. (guile2.2-hashing): New variable. (guile3.0-hashing): Deprecate. * gnu/packages/package-management.scm (guix): Switch to GUILE-3.0. (guile2.2-guix): New variable. (guile3.0-guix): Deprecate. (gwl): Replace "guile3.0-" with "guile-". (guix-jupyter)[source]: Adjust for Guile 3.0. Switch to GUILE-3.0. * gnu/packages/ssh.scm (guile-ssh): Switch to GUILE-3.0. (guile2.2-ssh): New variable. (guile3.0-ssh): Deprecate. * gnu/packages/admin.scm (shepherd): Switch to GUILE-3.0. (guile2.2-shepherd): New variable. (guile3.0-shepherd): Deprecate. * gnu/packages/mail.scm (mailutils): Switch to GUILE-3.0. (guile2.2-mailutils): New variable. (guile3.0-mailutils): Deprecate. * gnu/packages/plotutils.scm (guile-charting): Switch to GUILE-3.0. (guile2.2-charting): New variable. (guile3.0-charting): Deprecate. * gnu/packages/version-control.scm (libgit2): Switch to GUILE-3.0. * gnu/packages/vpn.scm (vpnc-scripts): Switch to GUILE-3.0. * gnu/packages/web.scm (guix-data-service): Switch to GUILE-3.0. (hpcguix-web): Switch to GUILE-3.0. * guix/self.scm (specification->package): Refer to the "guile-" variants instead of "guile3.0-". * guix/gexp.scm (default-guile): Change to GUILE-3.0. * build-aux/build-self.scm (build): #:guile-version defaults to "3.0". * gnu/packages/commencement.scm (guile-final): Base on GUILE-3.0/FIXED. 2020-02-20build-self: Show wider backtraces.Ludovic Courtès * build-aux/build-self.scm (build): Add 'setenv' call for "COLUMNS".