Skip to content

Commit

Permalink
Merge pull request #1122 from spicyj/gh-1120
Browse files Browse the repository at this point in the history
Strip calls to warning() in __DEV__
  • Loading branch information
zpao committed Feb 18, 2014
2 parents 8ac5975 + 5795376 commit c79974d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@
},
"preferGlobal": true,
"commonerConfig": {
"version": 3
"version": 4
}
}
14 changes: 7 additions & 7 deletions src/vendor/core/invariant.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* @providesModule invariant
*/

"use strict";

/**
* Use invariant() to assert state which your program assumes to be true.
*
Expand All @@ -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 ' +
Expand All @@ -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');
}
Expand All @@ -57,6 +57,6 @@ if (__DEV__) {
throw error;
}
};

module.exports = invariantDev;
}

module.exports = invariant;
30 changes: 19 additions & 11 deletions src/vendor/core/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,33 @@
* @providesModule warning
*/

"use strict";

var emptyFunction = require('emptyFunction');

/**
* Similar to invariant but only logs a warning if the condition is not met.
* This can be used to log issues in development environments in critical
* 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;
11 changes: 11 additions & 0 deletions vendor/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);
}
}
});
Expand Down

0 comments on commit c79974d

Please sign in to comment.