From 5491e1d7b11e363c79bdd352883e92fa3b711e69 Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Fri, 21 Sep 2012 14:19:05 +0300 Subject: better support for multiple input files: - use a single AST_Toplevel node for all files - keep original source filename in the tokens --- bin/uglifyjs2 | 99 +++++++++++++++++++++++++---------------------------------- 1 file changed, 41 insertions(+), 58 deletions(-) (limited to 'bin/uglifyjs2') diff --git a/bin/uglifyjs2 b/bin/uglifyjs2 index e79f71c8..c10753f4 100755 --- a/bin/uglifyjs2 +++ b/bin/uglifyjs2 @@ -79,6 +79,7 @@ if (files.filter(function(el){ return el == "-" }).length > 1) { var STATS = {}; var OUTPUT_FILE = ARGS.o; +var TOPLEVEL = null; var SOURCE_MAP = ARGS.source_map ? UglifyJS.SourceMap({ file: OUTPUT_FILE, @@ -90,15 +91,48 @@ var output = UglifyJS.OutputStream({ 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())); +files.forEach(function(file) { + if (ARGS.v) { + sys.error("Parsing " + file); + } + var code = read_whole_file(file); + if (ARGS.p != null) { + file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/"); + } + time_it("parse", function(){ + TOPLEVEL = UglifyJS.parse(code, { + filename: file, + toplevel: TOPLEVEL + }); + }); +}); + +time_it("scope", function(){ + TOPLEVEL.figure_out_scope(); +}); + +if (ARGS.c !== true) { + time_it("squeeze", function(){ + var compressor = UglifyJS.Compressor(COMPRESSOR_OPTIONS); + TOPLEVEL = TOPLEVEL.squeeze(compressor); + }); } +time_it("scope", function(){ + TOPLEVEL.figure_out_scope(); + if (!ARGS.m) { + TOPLEVEL.compute_char_frequency(); + UglifyJS.base54.sort(); + } +}); + +if (!ARGS.m) time_it("mangle", function(){ + TOPLEVEL.mangle_names(); +}); +time_it("generate", function(){ + TOPLEVEL.print(output); +}); + output = output.get(); if (SOURCE_MAP) { @@ -127,57 +161,6 @@ if (ARGS.stats) { /* -----[ 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 -- cgit v1.2.3 ls: Add 'getxattr'....Jan (janneke) Nieuwenhuizen 2020-02-11syscalls: Re-enable 'pivot-root' test....Ludovic Courtès 2019-10-05syscalls: Add 'add-to-entropy-count'....Ludovic Courtès 2019-06-27syscalls: Add 'terminal-rows'....Ludovic Courtès 2018-07-03syscalls: Define AT_SYMLINK_NOFOLLOW et al....Ludovic Courtès 2018-04-08tests: Skip 'pivot-root' test on Ubuntu's 4.4 kernels....Ludovic Courtès 2017-11-25syscalls: Adjust utmpx test....Ludovic Courtès