Skip to content

Commit

Permalink
Use jstranform instead of recast
Browse files Browse the repository at this point in the history
This gains us a massive speedup, envify w/ recast takes almost 7 secs on my mba
when browserifying React codebase. While envify w/ jstransform takes this down
to 1.5 sec (pre-recast speed) and deliver the same advantages as recast
transform (preserves code formatting).
  • Loading branch information
andreypopp committed Feb 17, 2014
1 parent c66ef44 commit 6216358
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
31 changes: 4 additions & 27 deletions custom.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
var through = require('through')
, recast = require('recast')
, xtend = require('xtend')
, jstransform = require('jstransform')
, createVisitors = require('./visitors')

var types = recast.types.namedTypes
, build = recast.types.builders
, traverse = recast.types.traverse
, processEnvPattern = /\bprocess\.env\b/
var processEnvPattern = /\bprocess\.env\b/

module.exports = function(rootEnv) {
rootEnv = rootEnv || process.env || {}
Expand All @@ -28,28 +26,7 @@ module.exports = function(rootEnv) {
var source = buffer.join('')

if (processEnvPattern.test(source)) {
var ast = recast.parse(source)

traverse(ast, function(node) {
if (
types.MemberExpression.check(node)
&& !node.computed
&& types.Identifier.check(node.property)
&& types.MemberExpression.check(node.object)
&& types.Identifier.check(node.object.object)
&& node.object.object.name === 'process'
&& types.Identifier.check(node.object.property)
&& node.object.property.name === 'env'
) {
var key = node.property.name
if (key in env) {
this.replace(build.literal(env[key]))
return false
}
}
});

source = recast.print(ast).code
source = jstransform.transform(createVisitors(env), source).code;
}

this.queue(source)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"dependencies": {
"xtend": "~2.1.2",
"through": "~2.3.4",
"recast": "~0.5.6"
"esprima-fb": "~3001.1.0-dev-harmony-fb",
"jstransform": "~3.0.0"
},
"keywords": [
"environment",
Expand Down
32 changes: 32 additions & 0 deletions visitors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var Syntax = require('esprima-fb').Syntax;
var utils = require('jstransform/src/utils');

function create(env) {

function visitProcessEnv(traverse, node, path, state) {
var key = node.property.name;
if (env[key] !== undefined) {
utils.catchup(node.range[0], state);
utils.append(JSON.stringify(env[key]), state);
utils.move(node.range[1], state);
}
return false;
}

visitProcessEnv.test = function(node, path, state) {
return (
node.type === Syntax.MemberExpression
&& !node.computed
&& node.property.type === Syntax.Identifier
&& node.object.type === Syntax.MemberExpression
&& node.object.object.type === Syntax.Identifier
&& node.object.object.name === 'process'
&& node.object.property.type === Syntax.Identifier
&& node.object.property.name === 'env'
)
};

return [visitProcessEnv];
}

module.exports = create;

0 comments on commit 6216358

Please sign in to comment.