@samitouri / QOS-React / commits / 1c2876d5b5

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

Dan Abramov committed Apr 4, 2018 at 17:04 UTC 1c2876d5b558b8591feb335d8d7204bc46f7da8a
4 files changed +26 -8
scripts/babel/__tests__/wrap-warning-with-env-check-test.js renamed
+1 -1
@@ -36,7 +36,7 @@ describe('wrap-warning-with-env-check', () => {
36 it('should wrap warning calls', () => {
37 compare(
38 "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;"
40 );
41 });
42
scripts/babel/wrap-warning-with-env-check.js renamed
+17 -5
@@ -25,8 +25,6 @@ module.exports = function(babel, options) {
25 }
26
27 if (path.get('callee').isIdentifier({name: 'warning'})) {
28 - node[SEEN_SYMBOL] = true;
29 -
28 // Turns this code:
29 //
30 // warning(condition, argument, argument);
@@ -34,14 +32,28 @@ module.exports = function(babel, options) {
32 // into this:
33 //
34 // if (__DEV__) {
37 - // warning(condition, argument, argument);
35 + // if (!condition) {
36 + // warning(false, argument, argument);
37 + // }
38 // }
39 //
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;
48 path.replaceWith(
49 t.ifStatement(
50 DEV_EXPRESSION,
44 - t.blockStatement([t.expressionStatement(node)])
51 + t.blockStatement([
52 + t.ifStatement(
53 + t.unaryExpression('!', condition),
54 + t.expressionStatement(newNode)
55 + ),
56 + ])
57 )
58 );
59 }
scripts/jest/preprocessor.js
+6
@@ -17,6 +17,9 @@ const pathToBabel = path.join(
17 const pathToBabelPluginDevWithCode = require.resolve(
18 '../error-codes/replace-invariant-error-codes'
19 );
20 +const pathToBabelPluginWrapWarning = require.resolve(
21 + '../babel/wrap-warning-with-env-check'
22 +);
23 const pathToBabelPluginAsyncToGenerator = require.resolve(
24 'babel-plugin-transform-async-to-generator'
25 );
@@ -29,6 +32,8 @@ const babelOptions = {
32 require.resolve('babel-plugin-transform-es2015-modules-commonjs'),
33
34 pathToBabelPluginDevWithCode,
35 + pathToBabelPluginWrapWarning,
36 +
37 // Keep stacks detailed in tests.
38 // Don't put this in .babelrc so that we don't embed filenames
39 // into ReactART builds that include JSX.
@@ -75,6 +80,7 @@ module.exports = {
80 pathToBabel,
81 pathToBabelrc,
82 pathToBabelPluginDevWithCode,
83 + pathToBabelPluginWrapWarning,
84 pathToErrorCodes,
85 ]),
86 };
scripts/rollup/build.js
+2 -2
@@ -87,7 +87,7 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
87 return Object.assign({}, options, {
88 plugins: options.plugins.concat([
89 // 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'),
91 ]),
92 });
93 case UMD_DEV:
@@ -101,7 +101,7 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
101 // Minify invariant messages
102 require('../error-codes/replace-invariant-error-codes'),
103 // 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'),
105 ]),
106 });
107 default: