suggestions from @kzc

This commit is contained in:
Fábio Santos 2018-03-16 15:28:23 +00:00
parent 924f7bc3a9
commit 6dacae769d
5 changed files with 40 additions and 8 deletions

View File

@ -131,6 +131,10 @@ function Compressor(options, false_by_default) {
return top_retain.indexOf(def.name) >= 0;
};
}
if (this.options["module"]) {
this.directives["use strict"] = true;
this.options["toplevel"] = true;
}
var toplevel = this.options["toplevel"];
this.toplevel = typeof toplevel == "string" ? {
funcs: /funcs/.test(toplevel),
@ -139,9 +143,6 @@ function Compressor(options, false_by_default) {
funcs: toplevel,
vars: toplevel
};
if (this.options['module']) {
this.directives['use strict'] = true;
}
var sequences = this.options["sequences"];
this.sequences_limit = sequences == 1 ? 800 : sequences | 0;
this.warnings_produced = {};

View File

@ -89,6 +89,7 @@ function minify(files, options) {
ie8: false,
keep_classnames: false,
keep_fnames: false,
module: false,
properties: false,
reserved: [],
safari10: false,

View File

@ -110,11 +110,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
// pass 1: setup scope chaining and handle definitions
var self = this;
var scope = null;
if (options.module) {
scope = new AST_Scope(self);
}
self.parent_scope = scope;
var scope = self.parent_scope = null;
var labels = new Dictionary();
var defun = null;
var in_destructuring = null;
@ -518,9 +514,13 @@ AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options) {
ie8 : false,
keep_classnames: false,
keep_fnames : false,
module : false,
reserved : [],
toplevel : false,
});
if (options["module"]) {
options.toplevel = true;
}
if (!Array.isArray(options.reserved)) options.reserved = [];
// Never mangle arguments
push_uniq(options.reserved, "arguments");

View File

@ -1629,3 +1629,24 @@ module_mangle_scope: {
let e = 10;
}
}
module_enabled: {
options = {
module: true,
reduce_vars: true,
unused: true,
}
mangle = {
module: true,
}
input: {
let apple = 10, b = 20;
console.log(apple++, b, apple++);
export { apple };
}
expect: {
let o = 10;
console.log(o++, 20, o++);
export { o as apple };
}
}

View File

@ -734,6 +734,15 @@ describe("bin/uglifyjs", function () {
done();
});
});
it("Should mangle toplevel names with the --module option", function(done) {
var command = uglifyjscmd + " test/input/module/input.js --module -mc";
exec(command, function (err, stdout, stderr) {
if (err) throw err;
assert.strictEqual(stdout, "let e=1;export{e as foo};\n")
done();
});
});
it("Should fail with --define a-b", function(done) {
var command = uglifyjscmd + " test/input/issue-505/input.js --define a-b";
exec(command, function (err, stdout, stderr) {