diff --git a/README.md b/README.md index 99e33af7..c4853c32 100644 --- a/README.md +++ b/README.md @@ -613,6 +613,10 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u - `dead_code` (default: `true`) -- remove unreachable code +- `defaults` (default: `true`) -- Pass `false` to disable most default + enabled `compress` transforms. Useful when you only want to enable a few + `compress` options while disabling the rest. + - `drop_console` (default: `false`) -- Pass `true` to discard calls to `console.*` functions. If you wish to drop a specific function call such as `console.info` and/or retain side effects from function arguments diff --git a/lib/compress.js b/lib/compress.js index 49185aec..b97f03b3 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -47,12 +47,14 @@ function Compressor(options, false_by_default) { if (!(this instanceof Compressor)) return new Compressor(options, false_by_default); TreeTransformer.call(this, this.before, this.after); + if (options.defaults !== undefined && !options.defaults) false_by_default = true; this.options = defaults(options, { booleans : !false_by_default, collapse_vars : !false_by_default, comparisons : !false_by_default, conditionals : !false_by_default, dead_code : !false_by_default, + defaults : true, drop_console : false, drop_debugger : !false_by_default, evaluate : !false_by_default, diff --git a/test/compress/defaults.js b/test/compress/defaults.js new file mode 100644 index 00000000..2971a27e --- /dev/null +++ b/test/compress/defaults.js @@ -0,0 +1,96 @@ +defaults_undefined: { + options = { + defaults: undefined, + } + input: { + if (true) { + console.log(1 + 2); + } + } + expect: { + if (true) + console.log(1 + 2); + } + expect_stdout: "3" +} + +defaults_false: { + options = { + defaults: false, + } + input: { + if (true) { + console.log(1 + 2); + } + } + expect: { + if (true) + console.log(1 + 2); + } + expect_stdout: "3" +} + +defaults_false_evaluate_true: { + options = { + defaults: false, + evaluate: true, + } + input: { + if (true) { + console.log(1 + 2); + } + } + expect: { + if (true) + console.log(3); + } + expect_stdout: "3" +} + +defaults_true: { + options = { + defaults: true, + } + input: { + if (true) { + console.log(1 + 2); + } + } + expect: { + console.log(3); + } + expect_stdout: "3" +} + +defaults_true_conditionals_false: { + options = { + defaults: true, + conditionals: false, + } + input: { + if (true) { + console.log(1 + 2); + } + } + expect: { + if (1) + console.log(3); + } + expect_stdout: "3" +} + +defaults_true_evaluate_false: { + options = { + defaults: true, + evaluate: false, + } + input: { + if (true) { + console.log(1 + 2); + } + } + expect: { + 1 && console.log(1 + 2); + } + expect_stdout: "3" +} diff --git a/test/input/defaults/input.js b/test/input/defaults/input.js new file mode 100644 index 00000000..f2392507 --- /dev/null +++ b/test/input/defaults/input.js @@ -0,0 +1,3 @@ +if (true) { + console.log(1 + 2); +} diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 671d700e..26ea9753 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -682,4 +682,12 @@ describe("bin/uglifyjs", function () { done(); }); }); + it("Should work with -c defaults=false,conditionals", function(done) { + var command = uglifyjscmd + " test/input/defaults/input.js -c defaults=false,conditionals"; + exec(command, function(err, stdout, stderr) { + if (err) throw err; + assert.strictEqual(stdout, 'true&&console.log(1+2);\n'); + done(); + }); + }); }); diff --git a/test/mocha/minify.js b/test/mocha/minify.js index 65392ee4..6a3e8b8e 100644 --- a/test/mocha/minify.js +++ b/test/mocha/minify.js @@ -387,4 +387,25 @@ describe("minify", function() { } }); }); + + it("should work with compress defaults disabled", function() { + var code = 'if (true) { console.log(1 + 2); }'; + var options = { + compress: { + defaults: false, + } + }; + assert.strictEqual(Uglify.minify(code, options).code, 'if(true)console.log(1+2);'); + }); + + it("should work with compress defaults disabled and evaluate enabled", function() { + var code = 'if (true) { console.log(1 + 2); }'; + var options = { + compress: { + defaults: false, + evaluate: true, + } + }; + assert.strictEqual(Uglify.minify(code, options).code, 'if(true)console.log(3);'); + }); }); diff --git a/test/run-tests.js b/test/run-tests.js index 5dcacd87..5b724207 100755 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -131,7 +131,7 @@ function run_compress_tests() { input.figure_out_scope(test.mangle); input.expand_names(test.mangle); } - var cmp = new U.Compressor(options, true); + var cmp = new U.Compressor(options, options.defaults === undefined ? true : !options.defaults); var output = cmp.compress(input); output.figure_out_scope(test.mangle); if (test.mangle) {