add 6to5-minify

This commit is contained in:
Sebastian McKenzie 2015-02-04 08:23:49 +11:00
parent 6da6bc3eb8
commit c228d76e44
8 changed files with 57 additions and 29 deletions

7
bin/6to5-minify Executable file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env node
var opts = require("./6to5").opts;
opts.optional = (opts.optional || []).concat("minification");
opts.format = {
compact: true
};

View File

@ -114,6 +114,10 @@ exports.opts = {
loose: commander.loose loose: commander.loose
}; };
setTimeout(function () {
// this is just a hack to give `6to5-minify` and other files including this
// time to modify `exports.opts`
var fn; var fn;
if (commander.outDir) { if (commander.outDir) {
@ -123,3 +127,4 @@ if (commander.outDir) {
} }
fn(commander, filenames, exports.opts); fn(commander, filenames, exports.opts);
}, 0);

View File

@ -53,10 +53,12 @@ Buffer.prototype.rightBrace = function () {
Buffer.prototype.keyword = function (name) { Buffer.prototype.keyword = function (name) {
this.push(name); this.push(name);
this.push(" "); this.space();
}; };
Buffer.prototype.space = function () { Buffer.prototype.space = function () {
if (this.format.compact) return;
if (this.buf && !this.isLast([" ", "\n"])) { if (this.buf && !this.isLast([" ", "\n"])) {
this.push(" "); this.push(" ");
} }

View File

@ -17,7 +17,7 @@ exports.UnaryExpression = function (node, print) {
} }
this.push(node.operator); this.push(node.operator);
if (hasSpace) this.space(); if (hasSpace) this.push(" ");
print(node.argument); print(node.argument);
}; };
@ -33,9 +33,13 @@ exports.UpdateExpression = function (node, print) {
exports.ConditionalExpression = function (node, print) { exports.ConditionalExpression = function (node, print) {
print(node.test); print(node.test);
this.space();
this.push("?"); this.push("?");
this.space();
print(node.consequent); print(node.consequent);
this.space();
this.push(":"); this.push(":");
this.space();
print(node.alternate); print(node.alternate);
}; };
@ -44,13 +48,13 @@ exports.NewExpression = function (node, print) {
print(node.callee); print(node.callee);
if (node.arguments.length || this.format.parentheses) { if (node.arguments.length || this.format.parentheses) {
this.push("("); this.push("(");
print.join(node.arguments, { separator: ", " }); print.list(node.arguments);
this.push(")"); this.push(")");
} }
}; };
exports.SequenceExpression = function (node, print) { exports.SequenceExpression = function (node, print) {
print.join(node.expressions, { separator: ", " }); print.list(node.expressions);
}; };
exports.ThisExpression = function () { exports.ThisExpression = function () {
@ -72,9 +76,7 @@ exports.CallExpression = function (node, print) {
separator += " "; separator += " ";
} }
print.join(node.arguments, { print.list(node.arguments, { separator: separator });
separator: separator
});
if (node._prettyCall) { if (node._prettyCall) {
this.newline(); this.newline();
@ -115,10 +117,11 @@ exports.BinaryExpression =
exports.LogicalExpression = exports.LogicalExpression =
exports.AssignmentPattern = exports.AssignmentPattern =
exports.AssignmentExpression = function (node, print) { exports.AssignmentExpression = function (node, print) {
// todo: add cases where the spaces can be dropped when in compact mode
print(node.left); print(node.left);
this.space(); this.push(" ");
this.push(node.operator); this.push(node.operator);
this.space(); this.push(" ");
print(node.right); print(node.right);
}; };

View File

@ -4,11 +4,7 @@ var t = require("../../types");
exports._params = function (node, print) { exports._params = function (node, print) {
this.push("("); this.push("(");
print.list(node.params);
print.join(node.params, {
separator: ", "
});
this.push(")"); this.push(")");
}; };
@ -45,11 +41,14 @@ exports.FunctionExpression = function (node, print) {
if (node.async) this.push("async "); if (node.async) this.push("async ");
this.push("function"); this.push("function");
if (node.generator) this.push("*"); if (node.generator) this.push("*");
this.push(" ");
if (node.id) { if (node.id) {
this.space(); this.push(" ");
print(node.id); print(node.id);
} else {
this.space();
} }
this._params(node, print); this._params(node, print);
this.space(); this.space();
print(node.body); print(node.body);

View File

@ -23,6 +23,7 @@ exports.IfStatement = function (node, print) {
if (node.alternate) { if (node.alternate) {
if (this.isLast("}")) this.push(" "); if (this.isLast("}")) this.push(" ");
this.keyword("else"); this.keyword("else");
this.push(" ");
print.indentOnComments(node.alternate); print.indentOnComments(node.alternate);
} }
}; };
@ -190,7 +191,7 @@ exports.VariableDeclaration = function (node, print, parent) {
sep += " "; sep += " ";
} }
print.join(node.declarations, { separator: sep }); print.list(node.declarations, { separator: sep });
if (!t.isFor(parent)) { if (!t.isFor(parent)) {
this.semicolon(); this.semicolon();

View File

@ -27,7 +27,7 @@ exports.ObjectPattern = function (node, print) {
this.push("{"); this.push("{");
this.space(); this.space();
print.join(props, { separator: ", ", indent: true }); print.list(props, { indent: true });
this.space(); this.space();
this.push("}"); this.push("}");
@ -50,6 +50,7 @@ exports.Property = function (node, print) {
} }
this.push(":"); this.push(":");
this.space();
print(node.value); print(node.value);
} }
}; };
@ -71,7 +72,7 @@ exports.ArrayPattern = function (node, print) {
// both (all) of the holes. // both (all) of the holes.
self.push(","); self.push(",");
} else { } else {
if (i > 0) self.push(" "); if (i > 0 && !self.format.compact) self.push(" ");
print(elem); print(elem);
if (i < len - 1) self.push(","); if (i < len - 1) self.push(",");
} }

View File

@ -111,6 +111,16 @@ CodeGenerator.prototype.buildPrint = function (parent) {
return self.printJoin(print, nodes, opts); 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) { print.block = function (node) {
return self.printBlock(print, node); return self.printBlock(print, node);
}; };