From 57953769614ab767ab4b1bf47313f7a689b63492 Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Mon, 17 Feb 2014 17:28:38 -0800 Subject: [PATCH] Strip calls to warning() in __DEV__ Fixes #1120. --- package.json | 2 +- src/vendor/core/invariant.js | 14 +++++++------- src/vendor/core/warning.js | 30 +++++++++++++++++++----------- vendor/constants.js | 11 +++++++++++ 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index cf734d3cc065c..e555a26734cda 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,6 @@ }, "preferGlobal": true, "commonerConfig": { - "version": 3 + "version": 4 } } diff --git a/src/vendor/core/invariant.js b/src/vendor/core/invariant.js index 5cd1010041b6f..28b306c8840d9 100644 --- a/src/vendor/core/invariant.js +++ b/src/vendor/core/invariant.js @@ -16,6 +16,8 @@ * @providesModule invariant */ +"use strict"; + /** * Use invariant() to assert state which your program assumes to be true. * @@ -27,7 +29,7 @@ * will remain to ensure logic does not differ in production. */ -function invariant(condition) { +var invariant = function(condition) { if (!condition) { var error = new Error( 'Minified exception occured; use the non-minified dev environment for ' + @@ -36,12 +38,10 @@ function invariant(condition) { error.framesToPop = 1; throw error; } -} - -module.exports = invariant; +}; if (__DEV__) { - var invariantDev = function(condition, format, a, b, c, d, e, f) { + invariant = function(condition, format, a, b, c, d, e, f) { if (format === undefined) { throw new Error('invariant requires an error message argument'); } @@ -57,6 +57,6 @@ if (__DEV__) { throw error; } }; - - module.exports = invariantDev; } + +module.exports = invariant; diff --git a/src/vendor/core/warning.js b/src/vendor/core/warning.js index bb0be956ce33a..c969d5e6bc238 100644 --- a/src/vendor/core/warning.js +++ b/src/vendor/core/warning.js @@ -16,6 +16,9 @@ * @providesModule warning */ +"use strict"; + +var emptyFunction = require('emptyFunction'); /** * Similar to invariant but only logs a warning if the condition is not met. @@ -23,18 +26,23 @@ * paths. Removing the logging code for production environments will keep the * same logic and follow the same code paths. */ -function warning(condition, format, ...args) { - if (format === undefined) { - throw new Error( - '`warning(condition, format, ...args)` requires a warning ' + - 'message argument' - ); - } - if (!condition) { - var argIndex = 0; - console.warn('Warning: ' + format.replace(/%s/g, () => args[argIndex++])); - } +var warning = emptyFunction; + +if (__DEV__) { + warning = function(condition, format, ...args) { + if (format === undefined) { + throw new Error( + '`warning(condition, format, ...args)` requires a warning ' + + 'message argument' + ); + } + + if (!condition) { + var argIndex = 0; + console.warn('Warning: ' + format.replace(/%s/g, () => args[argIndex++])); + } + }; } module.exports = warning; diff --git a/vendor/constants.js b/vendor/constants.js index 574a3017af751..8c4113c33e1f0 100644 --- a/vendor/constants.js +++ b/vendor/constants.js @@ -81,6 +81,17 @@ function transform(ast, constants) { ) ); 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) + ) + ); } } });