From 83a4ebfedcd920871e583ef61151524b2d57b789 Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Wed, 2 Jan 2013 12:39:00 +0200 Subject: [PATCH] Implement `-m sort=true` close #83 --- README.md | 2 +- lib/scope.js | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index de343c70..4840c741 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ input files from the command line. ## Mangler options To enable the mangler you need to pass `--mangle` (`-m`). Optionally you -can pass `-m sort` (we'll possibly have other flags in the future) in order +can pass `-m sort=true` (we'll possibly have other flags in the future) in order to assign shorter names to most frequently used variables. This saves a few hundred bytes on jQuery before gzip, but the output is _bigger_ after gzip (and seems to happen for other libraries I tried it on) therefore it's not diff --git a/lib/scope.js b/lib/scope.js index 758b61f7..34ca5fb3 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -340,6 +340,7 @@ AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){ return defaults(options, { except : [], eval : false, + sort : false }); }); @@ -360,12 +361,16 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){ return true; // don't descend again in TreeWalker } if (node instanceof AST_Scope) { - var p = tw.parent(); + var p = tw.parent(), a = []; node.variables.each(function(symbol){ if (options.except.indexOf(symbol.name) < 0) { - to_mangle.push(symbol); + a.push(symbol); } }); + if (options.sort) a.sort(function(a, b){ + return b.references.length - a.references.length; + }); + to_mangle.push.apply(to_mangle, a); return; } if (node instanceof AST_Label) { -- 2.34.1