Implement -m sort=true

close #83
This commit is contained in:
Mihai Bazon
2013-01-02 12:39:00 +02:00
parent 9916d0e547
commit 83a4ebfedc
2 changed files with 8 additions and 3 deletions

View File

@@ -130,7 +130,7 @@ input files from the command line.
## Mangler options ## Mangler options
To enable the mangler you need to pass `--mangle` (`-m`). Optionally you 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 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 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 (and seems to happen for other libraries I tried it on) therefore it's not

View File

@@ -340,6 +340,7 @@ AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
return defaults(options, { return defaults(options, {
except : [], except : [],
eval : false, eval : false,
sort : false
}); });
}); });
@@ -360,12 +361,16 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
return true; // don't descend again in TreeWalker return true; // don't descend again in TreeWalker
} }
if (node instanceof AST_Scope) { if (node instanceof AST_Scope) {
var p = tw.parent(); var p = tw.parent(), a = [];
node.variables.each(function(symbol){ node.variables.each(function(symbol){
if (options.except.indexOf(symbol.name) < 0) { 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; return;
} }
if (node instanceof AST_Label) { if (node instanceof AST_Label) {