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,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);

View File

@ -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(" ");
}

View File

@ -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);
};

View File

@ -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);

View File

@ -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();

View File

@ -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(",");
}

View File

@ -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);
};