@samitouri / QOS-React-2 / commits / ceb92cd78c

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.

Ben Alpert committed May 4, 2015 at 17:03 UTC ceb92cd78cdff40d68da15d46bc20f61f2550751
4 files changed +51 -87
bin/jsx-internal
+2 -6
@@ -5,7 +5,7 @@
5
6 var babel = require('babel-core');
7
8 -var propagate = require("../vendor/constants").propagate;
8 +var constants = require('../vendor/constants')(babel);
9
10 require("commoner").version(
11 require("../package.json").version
@@ -29,18 +29,14 @@ require("commoner").version(
29
30 }).process(function(id, source) {
31 var context = this;
32 - var constants = context.config.constants || {};
32
33 // This is where JSX, ES6, etc. desugaring happens.
34 source = babel.transform(source, {
35 blacklist: ['spec.functionName', 'validation.react'],
36 + plugins: [constants],
37 filename: id
38 }).code;
39
40 - // Constant propagation means removing any obviously dead code after
41 - // replacing constant expressions with literal (boolean) values.
42 - source = propagate(constants, source);
43 -
40 if (context.config.mocking) {
41 // Make sure there is exactly one newline at the end of the module.
42 source = source.replace(/\s+$/m, "\n");
grunt/config/jsx.js
+1 -2
@@ -12,8 +12,7 @@ var normal = {
12 rootIDs: rootIDs,
13 getConfig: function() {
14 return {
15 - commonerConfig: grunt.config.data.pkg.commonerConfig,
16 - constants: {}
15 + commonerConfig: grunt.config.data.pkg.commonerConfig
16 };
17 },
18 sourceDir: 'src',
package.json
+1 -2
@@ -57,7 +57,6 @@
57 "phantomjs": "~1.9",
58 "platform": "^1.1.0",
59 "populist": "~0.1.6",
60 - "recast": "^0.9.11",
60 "sauce-tunnel": "~1.1.0",
61 "tmp": "~0.0.18",
62 "typescript": "^1.4.0",
@@ -70,7 +69,7 @@
69 },
70 "preferGlobal": true,
71 "commonerConfig": {
73 - "version": 5
72 + "version": 6
73 },
74 "scripts": {
75 "test": "jest",
vendor/constants.js
+47 -77
@@ -8,83 +8,53 @@
8 */
9 'use strict';
10
11 -var recast = require('recast');
12 -var types = recast.types;
13 -var builders = types.builders;
14 -
15 -function propagate(constants, source) {
16 - return recast.print(transform(recast.parse(source), constants)).code;
17 -}
18 -
19 -var DEV_EXPRESSION = builders.binaryExpression(
20 - '!==',
21 - builders.literal('production'),
22 - builders.memberExpression(
23 - builders.memberExpression(
24 - builders.identifier('process'),
25 - builders.identifier('env'),
11 +module.exports = function(babel) {
12 + var t = babel.types;
13 +
14 + var DEV_EXPRESSION = t.binaryExpression(
15 + '!==',
16 + t.literal('production'),
17 + t.memberExpression(
18 + t.memberExpression(
19 + t.identifier('process'),
20 + t.identifier('env'),
21 + false
22 + ),
23 + t.identifier('NODE_ENV'),
24 false
27 - ),
28 - builders.identifier('NODE_ENV'),
29 - false
30 - )
31 -);
32 -
33 -var visitors = {
34 - visitIdentifier: function(nodePath) {
35 - // If the identifier is the property of a member expression
36 - // (e.g. object.property), then it definitely is not a constant
37 - // expression that we want to replace.
38 - if (nodePath.parentPath.value.type === 'MemberExpression') {
39 - return false;
40 - }
41 -
42 - // replace __DEV__ with process.env.NODE_ENV !== 'production'
43 - if (nodePath.value.name === '__DEV__') {
44 - nodePath.replace(DEV_EXPRESSION);
25 + )
26 + );
27 +
28 + return new babel.Transformer('react.constants', {
29 + Identifier: {
30 + enter: function(node, parent) {
31 + // replace __DEV__ with process.env.NODE_ENV !== 'production'
32 + if (this.isIdentifier({name: '__DEV__'})) {
33 + return DEV_EXPRESSION;
34 + }
35 + }
36 + },
37 + CallExpression: {
38 + exit: function(node, parent) {
39 + if (this.get('callee').isIdentifier({name: 'invariant'})) {
40 + // Truncate the arguments of invariant(condition, ...)
41 + // statements to just the condition based on NODE_ENV
42 + // (dead code removal will remove the extra bytes).
43 + return t.conditionalExpression(
44 + DEV_EXPRESSION,
45 + node,
46 + t.callExpression(node.callee, [node.arguments[0]])
47 + );
48 + } else if (this.get('callee').isIdentifier({name: 'warning'})) {
49 + // Eliminate warning(condition, ...) statements based on NODE_ENV
50 + // (dead code removal will remove the extra bytes).
51 + return t.conditionalExpression(
52 + DEV_EXPRESSION,
53 + node,
54 + t.literal(null)
55 + );
56 + }
57 + }
58 }
46 - // TODO: bring back constant replacement if we decide we need it
47 -
48 - this.traverse(nodePath);
49 - },
50 -
51 - visitCallExpression: function(nodePath) {
52 - var node = nodePath.value;
53 - if (node.callee.name === 'invariant') {
54 - // Truncate the arguments of invariant(condition, ...)
55 - // statements to just the condition based on NODE_ENV
56 - // (dead code removal will remove the extra bytes).
57 - nodePath.replace(
58 - builders.conditionalExpression(
59 - DEV_EXPRESSION,
60 - node,
61 - builders.callExpression(
62 - node.callee,
63 - [node.arguments[0]]
64 - )
65 - )
66 - );
67 - return false;
68 - } else if (node.callee.name === 'warning') {
69 - // Eliminate warning(condition, ...) statements based on NODE_ENV
70 - // (dead code removal will remove the extra bytes).
71 - nodePath.replace(
72 - builders.conditionalExpression(
73 - DEV_EXPRESSION,
74 - node,
75 - builders.literal(null)
76 - )
77 - );
78 - return false;
79 - }
80 - this.traverse(nodePath);
81 - }
59 + });
60 };
83 -
84 -function transform(ast, constants) {
85 - // TODO constants
86 - return recast.visit(ast, visitors);
87 -}
88 -
89 -exports.propagate = propagate;
90 -exports.transform = transform;