Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for unique ids to help track down tricky minification issues #515

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ out: while (args.length > 0) {
var v = args.shift();
switch (v) {
case "-b":
case "--unique_ids":
options.unique_ids = true;
break;
case "--beautify":
options.codegen_options.beautify = true;
break;
Expand Down Expand Up @@ -296,7 +299,8 @@ function squeeze_it(code) {
toplevel : options.mangle_toplevel,
defines : options.defines,
except : options.reserved_names,
no_functions : options.no_mangle_functions
no_functions : options.no_mangle_functions,
unique_ids : options.unique_ids
});
});
if (options.squeeze) ast = time_it("squeeze", function(){
Expand Down
14 changes: 9 additions & 5 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ function ast_walker() {
};

/* -----[ Scope and mangling ]----- */
mangled_id = 0; // for unique mangled names

function Scope(parent) {
this.names = {}; // names defined in this scope
Expand Down Expand Up @@ -329,7 +330,7 @@ Scope.prototype = {
};
},

next_mangled: function() {
next_mangled: function(unique_ids) {
// we must be careful that the new mangled name:
//
// 1. doesn't shadow a mangled name from a parent
Expand Down Expand Up @@ -364,6 +365,8 @@ Scope.prototype = {
// I got "do" once. :-/
if (!is_identifier(m))
continue;
if (unique_ids)
return m + mangled_id++;

return m;
}
Expand All @@ -372,13 +375,13 @@ Scope.prototype = {
this.rev_mangled[m] = name;
return this.mangled[name] = m;
},
get_mangled: function(name, newMangle) {
get_mangled: function(name, newMangle, unique_ids) {
if (this.uses_eval || this.uses_with) return name; // no mangle if eval or with is in use
var s = this.has(name);
if (!s) return name; // not in visible scope, no mangle
if (HOP(s.mangled, name)) return s.mangled[name]; // already mangled in this scope
if (!newMangle) return name; // not found and no mangling requested
return s.set_mangle(name, s.next_mangled());
return s.set_mangle(name, s.next_mangled(unique_ids));
},
references: function(name) {
return name && !this.parent || this.uses_with || this.uses_eval || this.refs[name];
Expand Down Expand Up @@ -516,7 +519,8 @@ function ast_mangle(ast, options) {
toplevel : false,
defines : null,
except : null,
no_functions : false
no_functions : false,
unique_ids : false
});

function get_mangled(name, newMangle) {
Expand All @@ -527,7 +531,7 @@ function ast_mangle(ast, options) {
if (options.no_functions && HOP(scope.names, name) &&
(scope.names[name] == 'defun' || scope.names[name] == 'lambda'))
return name;
return scope.get_mangled(name, newMangle);
return scope.get_mangled(name, newMangle, options.unique_ids);
};

function get_define(name) {
Expand Down