Add a build step to hoist warning conditions (#12537)

This commit is contained in:
Dan Abramov 2018-04-04 17:04:40 +01:00 committed by GitHub
parent b15b165e07
commit 1c2876d5b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 8 deletions

View File

@ -36,7 +36,7 @@ describe('wrap-warning-with-env-check', () => {
it('should wrap warning calls', () => {
compare(
"warning(condition, 'a %s b', 'c');",
"__DEV__ ? warning(condition, 'a %s b', 'c') : void 0;"
"__DEV__ ? !condition ? warning(false, 'a %s b', 'c') : void 0 : void 0;"
);
});

View File

@ -25,8 +25,6 @@ module.exports = function(babel, options) {
}
if (path.get('callee').isIdentifier({name: 'warning'})) {
node[SEEN_SYMBOL] = true;
// Turns this code:
//
// warning(condition, argument, argument);
@ -34,14 +32,28 @@ module.exports = function(babel, options) {
// into this:
//
// if (__DEV__) {
// warning(condition, argument, argument);
// if (!condition) {
// warning(false, argument, argument);
// }
// }
//
// The goal is to strip out warning calls entirely in production.
// The goal is to strip out warning calls entirely in production
// and to avoid evaluating the arguments in development.
const condition = node.arguments[0];
const newNode = t.callExpression(
node.callee,
[t.booleanLiteral(false)].concat(node.arguments.slice(1))
);
newNode[SEEN_SYMBOL] = true;
path.replaceWith(
t.ifStatement(
DEV_EXPRESSION,
t.blockStatement([t.expressionStatement(node)])
t.blockStatement([
t.ifStatement(
t.unaryExpression('!', condition),
t.expressionStatement(newNode)
),
])
)
);
}

View File

@ -17,6 +17,9 @@ const pathToBabel = path.join(
const pathToBabelPluginDevWithCode = require.resolve(
'../error-codes/replace-invariant-error-codes'
);
const pathToBabelPluginWrapWarning = require.resolve(
'../babel/wrap-warning-with-env-check'
);
const pathToBabelPluginAsyncToGenerator = require.resolve(
'babel-plugin-transform-async-to-generator'
);
@ -29,6 +32,8 @@ const babelOptions = {
require.resolve('babel-plugin-transform-es2015-modules-commonjs'),
pathToBabelPluginDevWithCode,
pathToBabelPluginWrapWarning,
// Keep stacks detailed in tests.
// Don't put this in .babelrc so that we don't embed filenames
// into ReactART builds that include JSX.
@ -75,6 +80,7 @@ module.exports = {
pathToBabel,
pathToBabelrc,
pathToBabelPluginDevWithCode,
pathToBabelPluginWrapWarning,
pathToErrorCodes,
]),
};

View File

@ -87,7 +87,7 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
return Object.assign({}, options, {
plugins: options.plugins.concat([
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
require('./plugins/wrap-warning-with-env-check'),
require('../babel/wrap-warning-with-env-check'),
]),
});
case UMD_DEV:
@ -101,7 +101,7 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
// Minify invariant messages
require('../error-codes/replace-invariant-error-codes'),
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
require('./plugins/wrap-warning-with-env-check'),
require('../babel/wrap-warning-with-env-check'),
]),
});
default: