From 9b1a40dfc3681ce6410afd6fbae1e72bd34f0901 Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Wed, 13 Mar 2013 09:44:06 +0200 Subject: [PATCH] Support mangling toplevel names Close #127 --- README.md | 19 +++++++++++++------ lib/scope.js | 9 +++++---- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 617197a1..b8ea3ff0 100644 --- a/README.md +++ b/README.md @@ -141,12 +141,19 @@ input files from the command line. ## Mangler options -To enable the mangler you need to pass `--mangle` (`-m`). Optionally you -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 -enabled by default. +To enable the mangler you need to pass `--mangle` (`-m`). The following +(comma-separated) options are supported: + +- `sort` — 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 enabled by default. + +- `toplevel` — mangle names declared in the toplevel scope (disabled by + default). + +- `eval` — mangle names visible in scopes where `eval` or `when` are used + (disabled by default). When mangling is enabled but you want to prevent certain names from being mangled, you can declare those names with `--reserved` (`-r`) — pass a diff --git a/lib/scope.js b/lib/scope.js index 100a4db7..c6a85592 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -57,7 +57,7 @@ function SymbolDef(scope, index, orig) { SymbolDef.prototype = { unmangleable: function(options) { - return this.global + return (this.global && !(options && options.toplevel)) || this.undeclared || (!(options && options.eval) && (this.scope.uses_eval || this.scope.uses_with)); }, @@ -346,9 +346,10 @@ AST_Symbol.DEFMETHOD("global", function(){ AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){ return defaults(options, { - except : [], - eval : false, - sort : false + except : [], + eval : false, + sort : false, + toplevel : false }); }); -- 2.34.1