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

Add a build step to hoist warning conditions #12537

Merged
merged 1 commit into from
Apr 4, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('wrap-warning-with-env-check', () => {
it('should wrap warning calls', () => {
compare(
"warning(condition, 'a %s b', 'c');",
"__DEV__ ? warning(condition, 'a %s b', 'c') : void 0;"
"__DEV__ ? !condition ? warning(false, 'a %s b', 'c') : void 0 : void 0;"
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,35 @@ module.exports = function(babel, options) {
}

if (path.get('callee').isIdentifier({name: 'warning'})) {
node[SEEN_SYMBOL] = true;

// Turns this code:
//
// warning(condition, argument, argument);
//
// into this:
//
// if (__DEV__) {
// warning(condition, argument, argument);
// if (!condition) {
// warning(false, argument, argument);
// }
// }
//
// The goal is to strip out warning calls entirely in production.
// The goal is to strip out warning calls entirely in production
// and to avoid evaluating the arguments in development.
const condition = node.arguments[0];
const newNode = t.callExpression(
node.callee,
[t.booleanLiteral(false)].concat(node.arguments.slice(1))
);
newNode[SEEN_SYMBOL] = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems kind of silly that we're using fbjs for warning and invariant at this point 😁

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still need for React Native (and to go through our custom fbsource RN infra there).
Although we might as well have implemented that through the new forking infra.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that's what I meant. We may have well just built our own thing.

path.replaceWith(
t.ifStatement(
DEV_EXPRESSION,
t.blockStatement([t.expressionStatement(node)])
t.blockStatement([
t.ifStatement(
t.unaryExpression('!', condition),
t.expressionStatement(newNode)
),
])
)
);
}
Expand Down
6 changes: 6 additions & 0 deletions scripts/jest/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const pathToBabel = path.join(
const pathToBabelPluginDevWithCode = require.resolve(
'../error-codes/replace-invariant-error-codes'
);
const pathToBabelPluginWrapWarning = require.resolve(
'../babel/wrap-warning-with-env-check'
);
const pathToBabelPluginAsyncToGenerator = require.resolve(
'babel-plugin-transform-async-to-generator'
);
Expand All @@ -29,6 +32,8 @@ const babelOptions = {
require.resolve('babel-plugin-transform-es2015-modules-commonjs'),

pathToBabelPluginDevWithCode,
pathToBabelPluginWrapWarning,

// Keep stacks detailed in tests.
// Don't put this in .babelrc so that we don't embed filenames
// into ReactART builds that include JSX.
Expand Down Expand Up @@ -75,6 +80,7 @@ module.exports = {
pathToBabel,
pathToBabelrc,
pathToBabelPluginDevWithCode,
pathToBabelPluginWrapWarning,
pathToErrorCodes,
]),
};
4 changes: 2 additions & 2 deletions scripts/rollup/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
return Object.assign({}, options, {
plugins: options.plugins.concat([
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
require('./plugins/wrap-warning-with-env-check'),
require('../babel/wrap-warning-with-env-check'),
]),
});
case UMD_DEV:
Expand All @@ -101,7 +101,7 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
// Minify invariant messages
require('../error-codes/replace-invariant-error-codes'),
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
require('./plugins/wrap-warning-with-env-check'),
require('../babel/wrap-warning-with-env-check'),
]),
});
default:
Expand Down