Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip calls to warning() in __DEV__ #1122

Merged
merged 1 commit into from
Feb 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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