diff --git a/bin/6to5-minify b/bin/6to5-minify new file mode 100755 index 0000000000..de65baf7ed --- /dev/null +++ b/bin/6to5-minify @@ -0,0 +1,7 @@ +#!/usr/bin/env node + +var opts = require("./6to5").opts; +opts.optional = (opts.optional || []).concat("minification"); +opts.format = { + compact: true +}; diff --git a/bin/6to5/index.js b/bin/6to5/index.js index 0dbd8f364e..898766e94c 100755 --- a/bin/6to5/index.js +++ b/bin/6to5/index.js @@ -114,12 +114,17 @@ exports.opts = { loose: commander.loose }; -var fn; +setTimeout(function () { + // this is just a hack to give `6to5-minify` and other files including this + // time to modify `exports.opts` -if (commander.outDir) { - fn = require("./dir"); -} else { - fn = require("./file"); -} + var fn; -fn(commander, filenames, exports.opts); + if (commander.outDir) { + fn = require("./dir"); + } else { + fn = require("./file"); + } + + fn(commander, filenames, exports.opts); +}, 0); diff --git a/lib/6to5/generation/buffer.js b/lib/6to5/generation/buffer.js index 224c5468a3..440d13ab8d 100644 --- a/lib/6to5/generation/buffer.js +++ b/lib/6to5/generation/buffer.js @@ -53,10 +53,12 @@ Buffer.prototype.rightBrace = function () { Buffer.prototype.keyword = function (name) { this.push(name); - this.push(" "); + this.space(); }; Buffer.prototype.space = function () { + if (this.format.compact) return; + if (this.buf && !this.isLast([" ", "\n"])) { this.push(" "); } diff --git a/lib/6to5/generation/generators/expressions.js b/lib/6to5/generation/generators/expressions.js index f7376d97b4..cb5a49d253 100644 --- a/lib/6to5/generation/generators/expressions.js +++ b/lib/6to5/generation/generators/expressions.js @@ -17,7 +17,7 @@ exports.UnaryExpression = function (node, print) { } this.push(node.operator); - if (hasSpace) this.space(); + if (hasSpace) this.push(" "); print(node.argument); }; @@ -33,9 +33,13 @@ exports.UpdateExpression = function (node, print) { exports.ConditionalExpression = function (node, print) { print(node.test); - this.push(" ? "); + this.space(); + this.push("?"); + this.space(); print(node.consequent); - this.push(" : "); + this.space(); + this.push(":"); + this.space(); print(node.alternate); }; @@ -44,13 +48,13 @@ exports.NewExpression = function (node, print) { print(node.callee); if (node.arguments.length || this.format.parentheses) { this.push("("); - print.join(node.arguments, { separator: ", " }); + print.list(node.arguments); this.push(")"); } }; exports.SequenceExpression = function (node, print) { - print.join(node.expressions, { separator: ", " }); + print.list(node.expressions); }; exports.ThisExpression = function () { @@ -72,9 +76,7 @@ exports.CallExpression = function (node, print) { separator += " "; } - print.join(node.arguments, { - separator: separator - }); + print.list(node.arguments, { separator: separator }); if (node._prettyCall) { this.newline(); @@ -115,10 +117,11 @@ exports.BinaryExpression = exports.LogicalExpression = exports.AssignmentPattern = exports.AssignmentExpression = function (node, print) { + // todo: add cases where the spaces can be dropped when in compact mode print(node.left); - this.space(); + this.push(" "); this.push(node.operator); - this.space(); + this.push(" "); print(node.right); }; diff --git a/lib/6to5/generation/generators/methods.js b/lib/6to5/generation/generators/methods.js index 0a3caf3a59..d82fc036f8 100644 --- a/lib/6to5/generation/generators/methods.js +++ b/lib/6to5/generation/generators/methods.js @@ -4,11 +4,7 @@ var t = require("../../types"); exports._params = function (node, print) { this.push("("); - - print.join(node.params, { - separator: ", " - }); - + print.list(node.params); this.push(")"); }; @@ -45,11 +41,14 @@ exports.FunctionExpression = function (node, print) { if (node.async) this.push("async "); this.push("function"); if (node.generator) this.push("*"); - this.push(" "); + if (node.id) { - this.space(); + this.push(" "); print(node.id); + } else { + this.space(); } + this._params(node, print); this.space(); print(node.body); diff --git a/lib/6to5/generation/generators/statements.js b/lib/6to5/generation/generators/statements.js index 55fcfd092e..b5f6d02fcd 100644 --- a/lib/6to5/generation/generators/statements.js +++ b/lib/6to5/generation/generators/statements.js @@ -23,6 +23,7 @@ exports.IfStatement = function (node, print) { if (node.alternate) { if (this.isLast("}")) this.push(" "); this.keyword("else"); + this.push(" "); print.indentOnComments(node.alternate); } }; @@ -190,7 +191,7 @@ exports.VariableDeclaration = function (node, print, parent) { sep += " "; } - print.join(node.declarations, { separator: sep }); + print.list(node.declarations, { separator: sep }); if (!t.isFor(parent)) { this.semicolon(); diff --git a/lib/6to5/generation/generators/types.js b/lib/6to5/generation/generators/types.js index 51ec82daad..117af92ea1 100644 --- a/lib/6to5/generation/generators/types.js +++ b/lib/6to5/generation/generators/types.js @@ -27,7 +27,7 @@ exports.ObjectPattern = function (node, print) { this.push("{"); this.space(); - print.join(props, { separator: ", ", indent: true }); + print.list(props, { indent: true }); this.space(); this.push("}"); @@ -49,7 +49,8 @@ exports.Property = function (node, print) { if (node.shorthand) return; } - this.push(": "); + this.push(":"); + this.space(); print(node.value); } }; @@ -71,7 +72,7 @@ exports.ArrayPattern = function (node, print) { // both (all) of the holes. self.push(","); } else { - if (i > 0) self.push(" "); + if (i > 0 && !self.format.compact) self.push(" "); print(elem); if (i < len - 1) self.push(","); } diff --git a/lib/6to5/generation/index.js b/lib/6to5/generation/index.js index 657aada805..61ec7c085d 100644 --- a/lib/6to5/generation/index.js +++ b/lib/6to5/generation/index.js @@ -111,6 +111,16 @@ CodeGenerator.prototype.buildPrint = function (parent) { return self.printJoin(print, nodes, opts); }; + print.list = function (items, opts) { + opts = opts || {}; + + var sep = opts.separator || ", "; + if (self.format.compact) sep = ","; + opts.separator = sep; + + print.join(items, opts); + }; + print.block = function (node) { return self.printBlock(print, node); };