Skip to content

Commit

Permalink
separate keep_classnames & keep_fnames (#2510)
Browse files Browse the repository at this point in the history
fixes #2418
  • Loading branch information
alexlamsl committed Nov 25, 2017
1 parent bbf38dc commit f1e3ef5
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 2 deletions.
4 changes: 4 additions & 0 deletions bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ program.option("--config-file <file>", "Read minify() options from JSON file.");
program.option("-d, --define <expr>[=value]", "Global definitions.", parse_js("define"));
program.option("--ecma <version>", "Specify ECMAScript release: 5, 6, 7 or 8.");
program.option("--ie8", "Support non-standard Internet Explorer 8.");
program.option("--keep-classnames", "Do not mangle/drop class names.");
program.option("--keep-fnames", "Do not mangle/drop function names. Useful for code relying on Function.prototype.name.");
program.option("--name-cache <file>", "File to hold mangled name mappings.");
program.option("--self", "Build UglifyJS as a library (implies --wrap UglifyJS)");
Expand Down Expand Up @@ -95,6 +96,9 @@ if (program.define) {
options.compress.global_defs[expr] = program.define[expr];
}
}
if (program.keepClassnames) {
options.keep_classnames = true;
}
if (program.keepFnames) {
options.keep_fnames = true;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function Compressor(options, false_by_default) {
if_return : !false_by_default,
inline : !false_by_default,
join_vars : !false_by_default,
keep_classnames: false,
keep_fargs : true,
keep_fnames : false,
keep_infinity : false,
Expand Down Expand Up @@ -2610,8 +2611,9 @@ merge(Compressor.prototype, {
// pass 3: we should drop declarations not in_use
var tt = new TreeTransformer(
function before(node, descend, in_list) {
if (!compressor.option("keep_fnames")
&& node.name && (node instanceof AST_Function || node instanceof AST_ClassExpression)) {
if (node.name
&& (!compressor.option("keep_classnames") && node instanceof AST_ClassExpression
|| !compressor.option("keep_fnames") && node instanceof AST_Function)) {
var def = node.name.definition();
// any declarations with same name will overshadow
// name of this anonymous function and can therefore
Expand Down
5 changes: 5 additions & 0 deletions lib/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function minify(files, options) {
compress: {},
ecma: undefined,
ie8: false,
keep_classnames: undefined,
keep_fnames: false,
mangle: {},
nameCache: null,
Expand All @@ -65,8 +66,12 @@ function minify(files, options) {
var timings = options.timings && {
start: Date.now()
};
if (options.keep_classnames === undefined) {
options.keep_classnames = options.keep_fnames;
}
set_shorthand("ecma", options, [ "parse", "compress", "output" ]);
set_shorthand("ie8", options, [ "compress", "mangle", "output" ]);
set_shorthand("keep_classnames", options, [ "compress", "mangle" ]);
set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
set_shorthand("toplevel", options, [ "compress", "mangle" ]);
set_shorthand("warnings", options, [ "compress" ]);
Expand Down
98 changes: 98 additions & 0 deletions test/compress/drop-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -1574,3 +1574,101 @@ issue_2288: {
}
}
}

issue_2418_1: {
options = {
unused: true,
}
input: {
class C {}
function F() {}
(class c {});
(function f() {});
}
expect: {
class C {}
function F() {}
(class {});
(function() {});
}
}

issue_2418_2: {
options = {
keep_classnames: false,
keep_fnames: false,
unused: true,
}
input: {
class C {}
function F() {}
(class c {});
(function f() {});
}
expect: {
class C {}
function F() {}
(class {});
(function() {});
}
}

issue_2418_3: {
options = {
keep_classnames: false,
keep_fnames: true,
unused: true,
}
input: {
class C {}
function F() {}
(class c {});
(function f() {});
}
expect: {
class C {}
function F() {}
(class {});
(function f() {});
}
}

issue_2418_4: {
options = {
keep_classnames: true,
keep_fnames: false,
unused: true,
}
input: {
class C {}
function F() {}
(class c {});
(function f() {});
}
expect: {
class C {}
function F() {}
(class c {});
(function() {});
}
}

issue_2418_5: {
options = {
keep_classnames: true,
keep_fnames: true,
unused: true,
}
input: {
class C {}
function F() {}
(class c {});
(function f() {});
}
expect: {
class C {}
function F() {}
(class c {});
(function f() {});
}
}
2 changes: 2 additions & 0 deletions test/compress/hoist_props.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ hoist_class: {
evaluate: true,
hoist_props: true,
inline: true,
keep_classnames: true,
keep_fnames: true,
passes: 2,
reduce_funcs: true,
Expand Down Expand Up @@ -432,6 +433,7 @@ hoist_class_with_new: {
evaluate: true,
hoist_props: true,
inline: true,
keep_classnames: true,
keep_fnames: true,
passes: 2,
reduce_funcs: true,
Expand Down

0 comments on commit f1e3ef5

Please sign in to comment.