Skip to content

Commit

Permalink
Replace vendor/constants recast transform with babel
Browse files Browse the repository at this point in the history
Built files look the same up to parenthesization and quoting. This only saves 1.5 seconds out of ~20 on a clean build but it's a little simpler.
  • Loading branch information
sophiebits committed May 5, 2015
1 parent aee3614 commit ceb92cd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 87 deletions.
8 changes: 2 additions & 6 deletions bin/jsx-internal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
Expand Down
3 changes: 1 addition & 2 deletions grunt/config/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -70,7 +69,7 @@
},
"preferGlobal": true,
"commonerConfig": {
"version": 5
"version": 6
},
"scripts": {
"test": "jest",
Expand Down
124 changes: 47 additions & 77 deletions vendor/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,83 +8,53 @@
*/
'use strict';

var recast = require('recast');
var types = recast.types;
var builders = types.builders;

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'),
module.exports = function(babel) {
var t = babel.types;

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

// replace __DEV__ with process.env.NODE_ENV !== 'production'
if (nodePath.value.name === '__DEV__') {
nodePath.replace(DEV_EXPRESSION);
)
);

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

0 comments on commit ceb92cd

Please sign in to comment.