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

fix: display warnings only in development #4150

Merged
merged 6 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable import/no-commonjs */

const wrapWarningWithDevCheck = require('./scripts/babel/wrap-warning-with-dev-check');

const isCJS = process.env.BABEL_ENV === 'cjs';
const isES = process.env.BABEL_ENV === 'es';

Expand Down Expand Up @@ -33,6 +35,7 @@ module.exports = api => {
'@babel/plugin-transform-react-constant-elements',
'babel-plugin-transform-react-remove-prop-types',
'babel-plugin-transform-react-pure-class-to-function',
wrapWarningWithDevCheck,
(isCJS || isES) && [
'inline-replace-variables',
{
Expand Down
23 changes: 23 additions & 0 deletions scripts/babel/__tests__/wrap-warning-with-dev-check-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { transformSync } from '@babel/core';
import wrapWarningWithDevCheck from '../wrap-warning-with-dev-check';

function babelTransform(code) {
return transformSync(code, {
configFile: false,
plugins: [wrapWarningWithDevCheck],
}).code;
}

describe('wrap-warning-with-dev-check', () => {
test('should wrap warning calls', () => {
expect(babelTransform("warning(condition, 'message');")).toEqual(
"__DEV__ ? warning(condition, 'message') : void 0;"
);
});

test('should not wrap other calls', () => {
expect(babelTransform("deprecate(fn, 'message');")).toEqual(
"deprecate(fn, 'message');"
);
});
});
57 changes: 57 additions & 0 deletions scripts/babel/wrap-warning-with-dev-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable import/no-commonjs */

/**
* Babel plugin that wraps `warning` calls with development check to be
* completely stripped from the production bundle.
*
* In the development bundle, warnings get wrapped with their condition
* and their condition becomes false to trigger them without evaluating twice.
*
* Input:
*
* ```
* warning(condition, message);
* ```
*
* Output:
*
* ```
* if (__DEV__) {
* warning(condition, message);
* }
* ```
*/

function wrapWarningInDevCheck(babel) {
const t = babel.types;

const DEV_EXPRESSION = t.identifier('__DEV__');
const SEEN_SYMBOL = Symbol('expression.seen');

return {
visitor: {
CallExpression: {
exit(path) {
const node = path.node;

if (node[SEEN_SYMBOL]) {
return;
}

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

path.replaceWith(
t.ifStatement(
DEV_EXPRESSION,
t.blockStatement([t.expressionStatement(node)])
)
);
}
},
},
},
};
}

module.exports = wrapWarningInDevCheck;