From ceb92cd78cdff40d68da15d46bc20f61f2550751 Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Mon, 4 May 2015 17:03:38 -0700 Subject: [PATCH] Replace vendor/constants recast transform with babel 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. --- bin/jsx-internal | 8 +-- grunt/config/jsx.js | 3 +- package.json | 3 +- vendor/constants.js | 120 +++++++++++++++++--------------------------- 4 files changed, 49 insertions(+), 85 deletions(-) diff --git a/bin/jsx-internal b/bin/jsx-internal index 7e4a5cff62..6e62b6993d 100755 --- a/bin/jsx-internal +++ b/bin/jsx-internal @@ -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"); diff --git a/grunt/config/jsx.js b/grunt/config/jsx.js index 002061189e..9c7dcf5079 100644 --- a/grunt/config/jsx.js +++ b/grunt/config/jsx.js @@ -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', diff --git a/package.json b/package.json index 1e43bf3d53..f973e4808e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/vendor/constants.js b/vendor/constants.js index 2620f83ddf..b6e26d27fb 100644 --- a/vendor/constants.js +++ b/vendor/constants.js @@ -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;