@samitouri / QOS-React-2 / commits / 5466d0a063

first work: __DEV__

fix invariant Get browserify working remove dead code elimination step since it is not needed due to minifier use industry standard NODE_ENV

petehunt committed Dec 2, 2013 at 01:21 UTC 5466d0a06382892106da7e237f3330b43a60cc95
3 files changed +45 -31
grunt/config/browserify.js
+9 -3
@@ -3,6 +3,7 @@
3
4 'use strict';
5
6 +var envify = require('envify/custom');
7 var grunt = require('grunt');
8 var UglifyJS = require('uglify-js');
9
@@ -59,13 +60,15 @@ var basic = {
60 outfile: './build/react.js',
61 debug: false,
62 standalone: 'React',
62 - after: [simpleBannerify]
63 + transforms: [envify({NODE_ENV: 'development'})],
64 + after: [simpleBannerify, muffinize]
65 };
66
67 var min = grunt.util._.merge({}, basic, {
68 outfile: './build/react.min.js',
69 debug: false,
68 - after: [minify, bannerify]
70 + transforms: [envify({NODE_ENV: 'production'})],
71 + after: [minify, bannerify, muffinize]
72 });
73
74 var transformer = {
@@ -85,6 +88,7 @@ var addons = {
88 outfile: './build/react-with-addons.js',
89 debug: false,
90 standalone: 'React',
91 + transforms: [envify({NODE_ENV: 'development'})],
92 packageName: 'React (with addons)',
93 after: [simpleBannerify]
94 };
@@ -92,7 +96,8 @@ var addons = {
96 var addonsMin = grunt.util._.merge({}, addons, {
97 outfile: './build/react-with-addons.min.js',
98 debug: false,
95 - after: [minify, bannerify]
99 + transforms: [envify({NODE_ENV: 'production'})],
100 + after: [minify, bannerify, muffinize]
101 });
102
103 var withCodeCoverageLogging = {
@@ -103,6 +108,7 @@ var withCodeCoverageLogging = {
108 debug: true,
109 standalone: 'React',
110 transforms: [
111 + envify({NODE_ENV: 'development'}),
112 require('coverify')
113 ]
114 };
package.json
+2
@@ -39,6 +39,8 @@
39 },
40 "devDependencies": {
41 "browserify": "~2.36.1",
42 + "browserify": "~2.34.1",
43 + "envify": "~0.2.0",
44 "populist": "~0.1.5",
45 "grunt-cli": "~0.1.9",
46 "grunt": "~0.4.1",
vendor/constants.js
+34 -28
@@ -26,6 +26,20 @@ function propagate(constants, source) {
26 return recast.print(transform(recast.parse(source), constants));
27 }
28
29 +var DEV_EXPRESSION = builders.binaryExpression(
30 + '!==',
31 + builders.literal('production'),
32 + builders.memberExpression(
33 + builders.memberExpression(
34 + builders.identifier('process'),
35 + builders.identifier('env'),
36 + false
37 + ),
38 + builders.identifier('NODE_ENV'),
39 + false
40 + )
41 +);
42 +
43 function transform(ast, constants) {
44 constants = constants || {};
45
@@ -42,39 +56,31 @@ function transform(ast, constants) {
56
57 // There could in principle be a constant called "hasOwnProperty",
58 // so be careful always to use Object.prototype.hasOwnProperty.
45 - if (hasOwn.call(constants, node.name)) {
59 + if (node.name === '__DEV__') {
60 + // replace __DEV__ with process.env.NODE_ENV === 'dev'
61 + this.replace(DEV_EXPRESSION);
62 + return false;
63 + } else if (hasOwn.call(constants, node.name)) {
64 this.replace(builders.literal(constants[node.name]));
65 return false;
66 }
67
68 } else if (namedTypes.CallExpression.check(node)) {
51 - if (!constants.__DEV__) {
52 - if (namedTypes.Identifier.check(node.callee) &&
53 - node.callee.name === 'invariant') {
54 - // Truncate the arguments of invariant(condition, ...)
55 - // statements to just the condition.
56 - node.arguments.length = 1;
57 - }
58 - }
59 -
60 - } else if (namedTypes.IfStatement.check(node) &&
61 - namedTypes.Literal.check(node.test)) {
62 - if (node.test.value) {
63 - // If the alternate (then) branch is dead code, remove it.
64 - this.get("alternate").replace();
65 -
66 - // This is what happens when you replace a node with nothing and
67 - // it can't be removed from a list of statements.
68 - assert.strictEqual(node.alternate, null);
69 -
70 - } else if (node.alternate) {
71 - // Replace the whole if-statement with just the alternate clause.
72 - this.replace(node.alternate);
73 - return false;
74 -
75 - } else {
76 - // Remove the entire if-statement.
77 - this.replace();
69 + if (namedTypes.Identifier.check(node.callee) &&
70 + node.callee.name === 'invariant') {
71 + // Truncate the arguments of invariant(condition, ...)
72 + // statements to just the condition based on NODE_ENV
73 + // (dead code removal will remove the extra bytes).
74 + this.replace(
75 + builders.conditionalExpression(
76 + DEV_EXPRESSION,
77 + node,
78 + builders.callExpression(
79 + node.callee,
80 + [node.arguments[0]]
81 + )
82 + )
83 + );
84 return false;
85 }
86 }