mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
d7f21d760b
I dropped the part of constants.js that we weren't using (namely the part where we insert constants) but left it open to do that. It should be trivial, we just aren't using it. Fixes #1824
98 lines
2.7 KiB
JavaScript
98 lines
2.7 KiB
JavaScript
/**
|
|
* 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 builders = types.builders;
|
|
|
|
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
|
|
)
|
|
);
|
|
|
|
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;
|
|
}
|
|
|
|
// 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;
|