-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: display warnings only in development (#4150)
- Loading branch information
1 parent
7ff843f
commit 44f69a0
Showing
4 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
scripts/babel/__tests__/wrap-warning-with-dev-check-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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');" | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |