[Pkg-javascript-commits] [uglifyjs] 37/491: support `minify()` output as AST (#1878)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:20 UTC 2018


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/3.3.10-1
in repository uglifyjs.

commit e54748365cba0509c82c089cdc2ef6a8bb1a724b
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Mon May 8 02:11:45 2017 +0800

    support `minify()` output as AST (#1878)
    
    - `options.output.ast` (default `false`)
    - `options.output.code` (default `true`)
---
 bin/uglifyjs  | 23 +++++++++++++++++++----
 lib/minify.js | 54 ++++++++++++++++++++++++++++++------------------------
 2 files changed, 49 insertions(+), 28 deletions(-)

diff --git a/bin/uglifyjs b/bin/uglifyjs
index 57e33d9..c4f9dc5 100755
--- a/bin/uglifyjs
+++ b/bin/uglifyjs
@@ -212,7 +212,14 @@ function run() {
         fatal("ERROR: " + ex.message);
     }
     if (program.output == "spidermonkey") {
-        console.log(JSON.stringify(UglifyJS.parse(result.code).to_mozilla_ast(), null, 2));
+        console.log(JSON.stringify(UglifyJS.minify(result.code, {
+            compress: false,
+            mangle: false,
+            output: {
+                ast: true,
+                code: false
+            }
+        }).ast.to_mozilla_ast(), null, 2));
     } else if (program.output) {
         fs.writeFileSync(program.output, result.code);
         if (result.map) {
@@ -278,9 +285,17 @@ function parse_js(flag, constants) {
     return function(value, options) {
         options = options || {};
         try {
-            UglifyJS.parse(value, {
-                expression: true
-            }).walk(new UglifyJS.TreeWalker(function(node) {
+            UglifyJS.minify(value, {
+                parse: {
+                    expression: true
+                },
+                compress: false,
+                mangle: false,
+                output: {
+                    ast: true,
+                    code: false
+                }
+            }).ast.walk(new UglifyJS.TreeWalker(function(node) {
                 if (node instanceof UglifyJS.AST_Assign) {
                     var name = node.left.print_to_string();
                     var value = node.right;
diff --git a/lib/minify.js b/lib/minify.js
index fe762db..27cc911 100644
--- a/lib/minify.js
+++ b/lib/minify.js
@@ -108,32 +108,38 @@ function minify(files, options) {
                 toplevel = mangle_properties(toplevel, options.mangle.properties);
             }
         }
-        if (options.sourceMap) {
-            if (typeof options.sourceMap.content == "string") {
-                options.sourceMap.content = JSON.parse(options.sourceMap.content);
-            }
-            options.output.source_map = SourceMap({
-                file: options.sourceMap.filename,
-                orig: options.sourceMap.content,
-                root: options.sourceMap.root
-            });
-            if (options.sourceMap.includeSources) {
-                for (var name in files) {
-                    options.output.source_map.get().setSourceContent(name, files[name]);
+        var result = {};
+        if (options.output.ast) {
+            result.ast = toplevel;
+        }
+        if (!HOP(options.output, "code") || options.output.code) {
+            if (options.sourceMap) {
+                if (typeof options.sourceMap.content == "string") {
+                    options.sourceMap.content = JSON.parse(options.sourceMap.content);
+                }
+                options.output.source_map = SourceMap({
+                    file: options.sourceMap.filename,
+                    orig: options.sourceMap.content,
+                    root: options.sourceMap.root
+                });
+                if (options.sourceMap.includeSources) {
+                    for (var name in files) {
+                        options.output.source_map.get().setSourceContent(name, files[name]);
+                    }
                 }
             }
-        }
-        var stream = OutputStream(options.output);
-        toplevel.print(stream);
-        var result = {
-            code: stream.get()
-        };
-        if (options.sourceMap) {
-            result.map = options.output.source_map.toString();
-            if (options.sourceMap.url == "inline") {
-                result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(result.map);
-            } else if (options.sourceMap.url) {
-                result.code += "\n//# sourceMappingURL=" + options.sourceMap.url;
+            delete options.output.ast;
+            delete options.output.code;
+            var stream = OutputStream(options.output);
+            toplevel.print(stream);
+            result.code = stream.get();
+            if (options.sourceMap) {
+                result.map = options.output.source_map.toString();
+                if (options.sourceMap.url == "inline") {
+                    result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(result.map);
+                } else if (options.sourceMap.url) {
+                    result.code += "\n//# sourceMappingURL=" + options.sourceMap.url;
+                }
             }
         }
         if (warnings.length) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list