Skip to content

Commit 1c2876d

Browse files
authored
Add a build step to hoist warning conditions (#12537)
1 parent b15b165 commit 1c2876d

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

scripts/rollup/plugins/__tests__/wrap-warning-with-env-check-test.js renamed to scripts/babel/__tests__/wrap-warning-with-env-check-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('wrap-warning-with-env-check', () => {
3636
it('should wrap warning calls', () => {
3737
compare(
3838
"warning(condition, 'a %s b', 'c');",
39-
"__DEV__ ? warning(condition, 'a %s b', 'c') : void 0;"
39+
"__DEV__ ? !condition ? warning(false, 'a %s b', 'c') : void 0 : void 0;"
4040
);
4141
});
4242

scripts/rollup/plugins/wrap-warning-with-env-check.js renamed to scripts/babel/wrap-warning-with-env-check.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,35 @@ module.exports = function(babel, options) {
2525
}
2626

2727
if (path.get('callee').isIdentifier({name: 'warning'})) {
28-
node[SEEN_SYMBOL] = true;
29-
3028
// Turns this code:
3129
//
3230
// warning(condition, argument, argument);
3331
//
3432
// into this:
3533
//
3634
// if (__DEV__) {
37-
// warning(condition, argument, argument);
35+
// if (!condition) {
36+
// warning(false, argument, argument);
37+
// }
3838
// }
3939
//
40-
// The goal is to strip out warning calls entirely in production.
40+
// The goal is to strip out warning calls entirely in production
41+
// and to avoid evaluating the arguments in development.
42+
const condition = node.arguments[0];
43+
const newNode = t.callExpression(
44+
node.callee,
45+
[t.booleanLiteral(false)].concat(node.arguments.slice(1))
46+
);
47+
newNode[SEEN_SYMBOL] = true;
4148
path.replaceWith(
4249
t.ifStatement(
4350
DEV_EXPRESSION,
44-
t.blockStatement([t.expressionStatement(node)])
51+
t.blockStatement([
52+
t.ifStatement(
53+
t.unaryExpression('!', condition),
54+
t.expressionStatement(newNode)
55+
),
56+
])
4557
)
4658
);
4759
}

scripts/jest/preprocessor.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const pathToBabel = path.join(
1717
const pathToBabelPluginDevWithCode = require.resolve(
1818
'../error-codes/replace-invariant-error-codes'
1919
);
20+
const pathToBabelPluginWrapWarning = require.resolve(
21+
'../babel/wrap-warning-with-env-check'
22+
);
2023
const pathToBabelPluginAsyncToGenerator = require.resolve(
2124
'babel-plugin-transform-async-to-generator'
2225
);
@@ -29,6 +32,8 @@ const babelOptions = {
2932
require.resolve('babel-plugin-transform-es2015-modules-commonjs'),
3033

3134
pathToBabelPluginDevWithCode,
35+
pathToBabelPluginWrapWarning,
36+
3237
// Keep stacks detailed in tests.
3338
// Don't put this in .babelrc so that we don't embed filenames
3439
// into ReactART builds that include JSX.
@@ -75,6 +80,7 @@ module.exports = {
7580
pathToBabel,
7681
pathToBabelrc,
7782
pathToBabelPluginDevWithCode,
83+
pathToBabelPluginWrapWarning,
7884
pathToErrorCodes,
7985
]),
8086
};

scripts/rollup/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
8787
return Object.assign({}, options, {
8888
plugins: options.plugins.concat([
8989
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
90-
require('./plugins/wrap-warning-with-env-check'),
90+
require('../babel/wrap-warning-with-env-check'),
9191
]),
9292
});
9393
case UMD_DEV:
@@ -101,7 +101,7 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
101101
// Minify invariant messages
102102
require('../error-codes/replace-invariant-error-codes'),
103103
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
104-
require('./plugins/wrap-warning-with-env-check'),
104+
require('../babel/wrap-warning-with-env-check'),
105105
]),
106106
});
107107
default:

0 commit comments

Comments
 (0)