Merge pull request #3813 from spicyj/babel

Replace vendor/constants recast transform with babel
This commit is contained in:
Ben Alpert 2015-05-04 17:43:14 -07:00
commit 5509cd74a4
4 changed files with 49 additions and 85 deletions

View File

@ -5,7 +5,7 @@
var babel = require('babel-core');
var propagate = require("../vendor/constants").propagate;
var constants = require('../vendor/constants')(babel);
require("commoner").version(
require("../package.json").version
@ -29,18 +29,14 @@ require("commoner").version(
}).process(function(id, source) {
var context = this;
var constants = context.config.constants || {};
// This is where JSX, ES6, etc. desugaring happens.
source = babel.transform(source, {
blacklist: ['spec.functionName', 'validation.react'],
plugins: [constants],
filename: id
}).code;
// Constant propagation means removing any obviously dead code after
// replacing constant expressions with literal (boolean) values.
source = propagate(constants, source);
if (context.config.mocking) {
// Make sure there is exactly one newline at the end of the module.
source = source.replace(/\s+$/m, "\n");

View File

@ -12,8 +12,7 @@ var normal = {
rootIDs: rootIDs,
getConfig: function() {
return {
commonerConfig: grunt.config.data.pkg.commonerConfig,
constants: {}
commonerConfig: grunt.config.data.pkg.commonerConfig
};
},
sourceDir: 'src',

View File

@ -57,7 +57,6 @@
"phantomjs": "~1.9",
"platform": "^1.1.0",
"populist": "~0.1.6",
"recast": "^0.9.11",
"sauce-tunnel": "~1.1.0",
"tmp": "~0.0.18",
"typescript": "^1.4.0",
@ -70,7 +69,7 @@
},
"preferGlobal": true,
"commonerConfig": {
"version": 5
"version": 6
},
"scripts": {
"test": "jest",

120
vendor/constants.js vendored
View File

@ -8,83 +8,53 @@
*/
'use strict';
var recast = require('recast');
var types = recast.types;
var builders = types.builders;
module.exports = function(babel) {
var t = babel.types;
function propagate(constants, source) {
return recast.print(transform(recast.parse(source), constants)).code;
}
var DEV_EXPRESSION = builders.binaryExpression(
'!==',
builders.literal('production'),
builders.memberExpression(
builders.memberExpression(
builders.identifier('process'),
builders.identifier('env'),
var DEV_EXPRESSION = t.binaryExpression(
'!==',
t.literal('production'),
t.memberExpression(
t.memberExpression(
t.identifier('process'),
t.identifier('env'),
false
),
t.identifier('NODE_ENV'),
false
),
builders.identifier('NODE_ENV'),
false
)
);
)
);
var visitors = {
visitIdentifier: function(nodePath) {
// If the identifier is the property of a member expression
// (e.g. object.property), then it definitely is not a constant
// expression that we want to replace.
if (nodePath.parentPath.value.type === 'MemberExpression') {
return false;
return new babel.Transformer('react.constants', {
Identifier: {
enter: function(node, parent) {
// replace __DEV__ with process.env.NODE_ENV !== 'production'
if (this.isIdentifier({name: '__DEV__'})) {
return DEV_EXPRESSION;
}
}
},
CallExpression: {
exit: function(node, parent) {
if (this.get('callee').isIdentifier({name: 'invariant'})) {
// Truncate the arguments of invariant(condition, ...)
// statements to just the condition based on NODE_ENV
// (dead code removal will remove the extra bytes).
return t.conditionalExpression(
DEV_EXPRESSION,
node,
t.callExpression(node.callee, [node.arguments[0]])
);
} else if (this.get('callee').isIdentifier({name: 'warning'})) {
// Eliminate warning(condition, ...) statements based on NODE_ENV
// (dead code removal will remove the extra bytes).
return t.conditionalExpression(
DEV_EXPRESSION,
node,
t.literal(null)
);
}
}
}
// replace __DEV__ with process.env.NODE_ENV !== 'production'
if (nodePath.value.name === '__DEV__') {
nodePath.replace(DEV_EXPRESSION);
}
// TODO: bring back constant replacement if we decide we need it
this.traverse(nodePath);
},
visitCallExpression: function(nodePath) {
var node = nodePath.value;
if (node.callee.name === 'invariant') {
// Truncate the arguments of invariant(condition, ...)
// statements to just the condition based on NODE_ENV
// (dead code removal will remove the extra bytes).
nodePath.replace(
builders.conditionalExpression(
DEV_EXPRESSION,
node,
builders.callExpression(
node.callee,
[node.arguments[0]]
)
)
);
return false;
} else if (node.callee.name === 'warning') {
// Eliminate warning(condition, ...) statements based on NODE_ENV
// (dead code removal will remove the extra bytes).
nodePath.replace(
builders.conditionalExpression(
DEV_EXPRESSION,
node,
builders.literal(null)
)
);
return false;
}
this.traverse(nodePath);
}
});
};
function transform(ast, constants) {
// TODO constants
return recast.visit(ast, visitors);
}
exports.propagate = propagate;
exports.transform = transform;