program.option("--name-cache <file>", "File to hold mangled name mappings.");
program.option("--self", "Build UglifyJS as a library (implies --wrap UglifyJS)");
program.option("--source-map [options]", "Enable source map/specify source map options.", parse_source_map());
-program.option("--stats", "Display operations run time on STDERR.")
+program.option("--timings", "Display operations run time on STDERR.")
program.option("--toplevel", "Compress and/or mangle variables in toplevel scope.");
program.option("--verbose", "Print diagnostic messages.");
program.option("--warn", "Print warning messages.");
};
}
if (program.parse) {
- if (program.parse.acorn || program.parse.spidermonkey) {
- if (program.sourceMap) fatal("ERROR: inline source map only works with built-in parser");
- } else {
+ if (!program.parse.acorn && !program.parse.spidermonkey) {
options.parse = program.parse;
+ } else if (program.sourceMap && program.sourceMap.content == "inline") {
+ fatal("ERROR: inline source map only works with built-in parser");
}
}
var convert_path = function(name) {
UglifyJS.AST_Node.warn_function = function(msg) {
console.error("WARN:", msg);
};
- if (program.stats) program.stats = Date.now();
+ if (program.timings) options.timings = true;
try {
if (program.parse) {
if (program.parse.acorn) {
return value instanceof UglifyJS.Dictionary ? value.toObject() : value;
}));
}
- if (program.stats) console.error("Elapsed:", Date.now() - program.stats);
+ if (result.timings) for (var phase in result.timings) {
+ console.error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s");
+ }
}
function fatal(message) {
function minify(files, options) {
var warn_function = AST_Node.warn_function;
try {
- if (typeof files == "string") {
- files = [ files ];
- }
options = defaults(options, {
compress: {},
ie8: false,
output: {},
parse: {},
sourceMap: false,
+ timings: false,
toplevel: false,
warnings: false,
wrap: false,
}, true);
+ var timings = options.timings && {
+ start: Date.now()
+ };
set_shorthand("ie8", options, [ "compress", "mangle", "output" ]);
set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
set_shorthand("toplevel", options, [ "compress", "mangle" ]);
warnings.push(warning);
};
}
+ if (timings) timings.parse = Date.now();
var toplevel;
if (files instanceof AST_Toplevel) {
toplevel = files;
} else {
+ if (typeof files == "string") {
+ files = [ files ];
+ }
options.parse = options.parse || {};
options.parse.toplevel = null;
for (var name in files) {
if (options.wrap) {
toplevel = toplevel.wrap_commonjs(options.wrap);
}
- if (options.compress) {
- toplevel.figure_out_scope(options.mangle);
- toplevel = new Compressor(options.compress).compress(toplevel);
- }
+ if (timings) timings.scope1 = Date.now();
+ if (options.compress) toplevel.figure_out_scope(options.mangle);
+ if (timings) timings.compress = Date.now();
+ if (options.compress) toplevel = new Compressor(options.compress).compress(toplevel);
+ if (timings) timings.scope2 = Date.now();
+ if (options.mangle) toplevel.figure_out_scope(options.mangle);
+ if (timings) timings.mangle = Date.now();
if (options.mangle) {
- toplevel.figure_out_scope(options.mangle);
base54.reset();
toplevel.compute_char_frequency(options.mangle);
toplevel.mangle_names(options.mangle);
- if (options.mangle.properties) {
- toplevel = mangle_properties(toplevel, options.mangle.properties);
- }
}
+ if (timings) timings.properties = Date.now();
+ if (options.mangle && options.mangle.properties) {
+ toplevel = mangle_properties(toplevel, options.mangle.properties);
+ }
+ if (timings) timings.output = Date.now();
var result = {};
if (options.output.ast) {
result.ast = toplevel;
root: options.sourceMap.root
});
if (options.sourceMap.includeSources) {
- for (var name in files) {
+ if (files instanceof AST_Toplevel) {
+ throw new Error("original source content unavailable");
+ } else for (var name in files) {
options.output.source_map.get().setSourceContent(name, files[name]);
}
}
}
}
}
+ if (timings) {
+ timings.end = Date.now();
+ result.timings = {
+ parse: 1e-3 * (timings.scope1 - timings.parse),
+ scope: 1e-3 * (timings.compress - timings.scope1 + timings.mangle - timings.scope2),
+ compress: 1e-3 * (timings.scope2 - timings.compress),
+ mangle: 1e-3 * (timings.properties - timings.mangle),
+ properties: 1e-3 * (timings.output - timings.properties),
+ output: 1e-3 * (timings.end - timings.output),
+ total: 1e-3 * (timings.end - timings.start)
+ }
+ }
if (warnings.length) {
result.warnings = warnings;
}
if (!args.length) {
args.push("-mc");
}
-args.push("--stats");
+args.push("--timings");
var urls = [
"https://code.jquery.com/jquery-3.2.1.js",
"https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.4/angular.js",
var info = results[url];
console.log();
console.log(url);
- var elapsed = 0;
- console.log(info.log.replace(/Elapsed: ([0-9]+)\s*/g, function(match, time) {
- elapsed += 1e-3 * parseInt(time);
- return "";
- }));
- console.log("Run-time:", elapsed.toFixed(3), "s");
+ console.log(info.log);
console.log("Original:", info.input, "bytes");
console.log("Uglified:", info.output, "bytes");
console.log("SHA1 sum:", info.sha1);