File tree Expand file tree Collapse file tree 4 files changed +85
-1
lines changed
Expand file tree Collapse file tree 4 files changed +85
-1
lines changed Original file line number Diff line number Diff line change 11/* eslint-disable import/no-commonjs */
22
3+ const wrapWarningWithDevCheck = require ( './scripts/babel/wrap-warning-with-dev-check' ) ;
4+
35const isCJS = process . env . BABEL_ENV === 'cjs' ;
46const isES = process . env . BABEL_ENV === 'es' ;
57
@@ -18,6 +20,7 @@ module.exports = api => {
1820
1921 const testPlugins = [
2022 '@babel/plugin-proposal-class-properties' ,
23+ wrapWarningWithDevCheck ,
2124 [
2225 'module-resolver' ,
2326 {
@@ -33,6 +36,7 @@ module.exports = api => {
3336 '@babel/plugin-transform-react-constant-elements' ,
3437 'babel-plugin-transform-react-remove-prop-types' ,
3538 'babel-plugin-transform-react-pure-class-to-function' ,
39+ wrapWarningWithDevCheck ,
3640 ( isCJS || isES ) && [
3741 'inline-replace-variables' ,
3842 {
Original file line number Diff line number Diff line change 144144 "bundlesize" : [
145145 {
146146 "path" : " ./dist/instantsearch.production.min.js" ,
147- "maxSize" : " 69 kB"
147+ "maxSize" : " 66 kB"
148148 },
149149 {
150150 "path" : " ./dist/instantsearch.development.js" ,
Original file line number Diff line number Diff line change 1+ import { transformSync } from '@babel/core' ;
2+ import wrapWarningWithDevCheck from '../wrap-warning-with-dev-check' ;
3+
4+ function babelTransform ( code ) {
5+ return transformSync ( code , {
6+ configFile : false ,
7+ plugins : [ wrapWarningWithDevCheck ] ,
8+ } ) . code ;
9+ }
10+
11+ describe ( 'wrap-warning-with-dev-check' , ( ) => {
12+ test ( 'should wrap warning calls' , ( ) => {
13+ expect ( babelTransform ( "warning(condition, 'message');" ) ) . toEqual (
14+ "__DEV__ ? warning(condition, 'message') : void 0;"
15+ ) ;
16+ } ) ;
17+
18+ test ( 'should not wrap other calls' , ( ) => {
19+ expect ( babelTransform ( "deprecate(fn, 'message');" ) ) . toEqual (
20+ "deprecate(fn, 'message');"
21+ ) ;
22+ } ) ;
23+ } ) ;
Original file line number Diff line number Diff line change 1+ /* eslint-disable import/no-commonjs */
2+
3+ /**
4+ * Babel plugin that wraps `warning` calls with development check to be
5+ * completely stripped from the production bundle.
6+ *
7+ * In the development bundle, warnings get wrapped with their condition
8+ * and their condition becomes false to trigger them without evaluating twice.
9+ *
10+ * Input:
11+ *
12+ * ```
13+ * warning(condition, message);
14+ * ```
15+ *
16+ * Output:
17+ *
18+ * ```
19+ * if (__DEV__) {
20+ * warning(condition, message);
21+ * }
22+ * ```
23+ */
24+
25+ function wrapWarningInDevCheck ( babel ) {
26+ const t = babel . types ;
27+
28+ const DEV_EXPRESSION = t . identifier ( '__DEV__' ) ;
29+ const SEEN_SYMBOL = Symbol ( 'expression.seen' ) ;
30+
31+ return {
32+ visitor : {
33+ CallExpression : {
34+ exit ( path ) {
35+ const node = path . node ;
36+
37+ if ( node [ SEEN_SYMBOL ] ) {
38+ return ;
39+ }
40+
41+ if ( path . get ( 'callee' ) . isIdentifier ( { name : 'warning' } ) ) {
42+ node [ SEEN_SYMBOL ] = true ;
43+
44+ path . replaceWith (
45+ t . ifStatement (
46+ DEV_EXPRESSION ,
47+ t . blockStatement ( [ t . expressionStatement ( node ) ] )
48+ )
49+ ) ;
50+ }
51+ } ,
52+ } ,
53+ } ,
54+ } ;
55+ }
56+
57+ module . exports = wrapWarningInDevCheck ;
You can’t perform that action at this time.
0 commit comments