add some minification transformers

This commit is contained in:
Sebastian McKenzie
2015-02-04 08:23:58 +11:00
parent c228d76e44
commit 49847e70af
5 changed files with 115 additions and 1 deletions

View File

@@ -80,5 +80,10 @@ module.exports = {
"spec.undefinedToVoid": require("./spec/undefined-to-void"),
"minification.propertyLiterals": require("./minification/property-literals"),
"minification.memberExpressionLiterals": require("./minification/member-expression-literals")
"minification.memberExpressionLiterals": require("./minification/member-expression-literals"),
"minification.removeDebugger": require("./minification/remove-debugger"),
"minification.removeConsoleCalls": require("./minification/remove-console-calls"),
"minification.deadCodeElimination": require("./minification/dead-code-elimination"),
"minification.renameLocalVariables": require("./minification/rename-local-variables")
};

View File

@@ -0,0 +1,77 @@
var t = require("../../../types");
exports.optional = true;
exports.ExpressionStatement = function (node, parent, scope, context, file) {
// remove consequenceless expressions such as local variables and literals
//
// var foo = true; foo; -> var foo = true;
// "foo"; ->
//
var expr = node.expression;
if (t.isLiteral(expr) || (t.isIdentifier(node) && t.hasBinding(node.name))) {
context.remove();
}
};
exports.IfStatement = {
exit: function (node, parent, scope, context, file) {
// todo: in scenarios where we can just return the consequent or
// alternate we should drop the block statement if it contains no
// block scoped variables
var consequent = node.consequent;
var alternate = node.alternate;
var test = node.test;
// we can check if a test will be truthy 100% and if so we can inline
// the consequent and completely ignore the alternate
//
// if (true) { foo; } -> { foo; }
// if ("foo") { foo; } -> { foo; }
//
if (t.isLiteral(test) && test.value) {
return alternate;
}
// we can check if a test will be falsy 100% and if so we can inline
// the alternate if there is one and completely remove the consequent
//
// if ("") { bar; } else { foo; } -> { foo; }
// if ("") { bar; } ->
//
if (t.isFalsyExpression(test)) {
if (alternate) {
return alternate;
} else {
return context.remove();
}
}
// remove alternate blocks that are empty
//
// if (foo) { foo; } else {} -> if (foo) { foo; }
//
if (t.isBlockStatement(alternate) && !alternate.body.length) {
alternate = node.alternate = null;
}
// turn alternate blocks into a consequent and flip the test if the
// consequent block is empty
//
// if (foo) {} else { bar; }
// if (!foo) { bar; }
//
if (t.blockStatement(consequent) && !consequent.body.length &&
t.isBlockStatement(alternate) && alternate.body.length) {
node.consequent = node.alternate;
node.alternate = null;
node.test = t.unaryExpression("!", test, true);
}
}
};

View File

@@ -0,0 +1,13 @@
"use strict";
var t = require("../../../types");
var isConsole = t.buildMatchMemberExpression("console", true);
exports.optional = true;
exports.CallExpression = function (node, parent, scope, context, file) {
if (isConsole(node.callee)) {
context.remove();
}
};

View File

@@ -0,0 +1,9 @@
var t = require("../../../types");
exports.optional = true;
exports.ExpressionStatement = function (node, parent, scope, context) {
if (t.isIdentifier(node.expression, { name: "debugger" })) {
context.remove();
}
};

View File

@@ -0,0 +1,10 @@
var t = require("../../../types");
exports.optional = true;
exports.Scope = function () {
// todo: get all binding identifiers, generate compact names
// that wont collide and then call the remap identifier helper
// this transformer **has** to be ran last as it will absolutley
// destroy the scope tree
};