From 6dacae769d36c35a3e6d5824ba5a12e16a3331d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Fri, 16 Mar 2018 15:28:23 +0000 Subject: [PATCH] suggestions from @kzc --- lib/compress.js | 7 ++++--- lib/minify.js | 1 + lib/scope.js | 10 +++++----- test/compress/harmony.js | 21 +++++++++++++++++++++ test/mocha/cli.js | 9 +++++++++ 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 37de2b43..2654a077 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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 = {}; diff --git a/lib/minify.js b/lib/minify.js index 73f0e456..a4b74f1c 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -89,6 +89,7 @@ function minify(files, options) { ie8: false, keep_classnames: false, keep_fnames: false, + module: false, properties: false, reserved: [], safari10: false, diff --git a/lib/scope.js b/lib/scope.js index de572f3e..850a1e69 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -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"); diff --git a/test/compress/harmony.js b/test/compress/harmony.js index c0eaab44..9bad924c 100644 --- a/test/compress/harmony.js +++ b/test/compress/harmony.js @@ -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 }; + } +} diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 3804d823..a02de977 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -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) {