forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstants.js
108 lines (99 loc) · 3.36 KB
/
constants.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Copyright 2013-2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var recast = require('recast');
var types = recast.types;
var namedTypes = types.namedTypes;
var builders = types.builders;
var hasOwn = Object.prototype.hasOwnProperty;
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'),
false
),
builders.identifier('NODE_ENV'),
false
)
);
function transform(ast, constants) {
constants = constants || {};
return types.traverse(ast, function(node, traverse) {
if (namedTypes.Identifier.check(node)) {
// 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 (namedTypes.MemberExpression.check(this.parent.node) &&
this.name === 'property' &&
!this.parent.node.computed) {
return false;
}
// There could in principle be a constant called "hasOwnProperty",
// so be careful always to use Object.prototype.hasOwnProperty.
if (node.name === '__DEV__') {
// replace __DEV__ with process.env.NODE_ENV !== 'production'
this.replace(DEV_EXPRESSION);
return false;
} else if (hasOwn.call(constants, node.name)) {
this.replace(builders.literal(constants[node.name]));
return false;
}
} else if (namedTypes.CallExpression.check(node)) {
if (namedTypes.Identifier.check(node.callee) &&
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).
this.replace(
builders.conditionalExpression(
DEV_EXPRESSION,
node,
builders.callExpression(
node.callee,
[node.arguments[0]]
)
)
);
return false;
} else if (namedTypes.Identifier.check(node.callee) &&
node.callee.name === 'warning') {
// Eliminate warning(condition, ...) statements based on NODE_ENV
// (dead code removal will remove the extra bytes).
this.replace(
builders.conditionalExpression(
DEV_EXPRESSION,
node,
builders.literal(null)
)
);
}
}
});
}
if (!module.parent) {
var constants = JSON.parse(process.argv[3]);
recast.run(function(ast, callback) {
callback(transform(ast, constants));
});
}
exports.propagate = propagate;
exports.transform = transform;