Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ddb7a0

Browse files
committed
restore plugin
1 parent fb0a374 commit 5ddb7a0

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
/* eslint-disable quotes */
8+
'use strict';
9+
10+
let babel = require('@babel/core');
11+
let wrapWarningWithEnvCheck = require('../wrap-warning-with-env-check');
12+
13+
function transform(input) {
14+
return babel.transform(input, {
15+
plugins: [wrapWarningWithEnvCheck],
16+
}).code;
17+
}
18+
19+
function compare(input, output) {
20+
const compiled = transform(input);
21+
expect(compiled).toEqual(output);
22+
}
23+
24+
let oldEnv;
25+
26+
describe('wrap-warning-with-env-check', () => {
27+
beforeEach(() => {
28+
oldEnv = process.env.NODE_ENV;
29+
process.env.NODE_ENV = '';
30+
});
31+
32+
afterEach(() => {
33+
process.env.NODE_ENV = oldEnv;
34+
});
35+
36+
it('should wrap warning calls', () => {
37+
compare(
38+
"warning(condition, 'a %s b', 'c');",
39+
"__DEV__ ? !condition ? warning(false, 'a %s b', 'c') : void 0 : void 0;"
40+
);
41+
});
42+
43+
it('should wrap warningWithoutStack calls', () => {
44+
compare(
45+
"warningWithoutStack(condition, 'a %s b', 'c');",
46+
"__DEV__ ? !condition ? warningWithoutStack(false, 'a %s b', 'c') : void 0 : void 0;"
47+
);
48+
});
49+
50+
it('should not wrap invariant calls', () => {
51+
compare(
52+
"invariant(condition, 'a %s b', 'c');",
53+
"invariant(condition, 'a %s b', 'c');"
54+
);
55+
});
56+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
module.exports = function(babel, options) {
10+
const t = babel.types;
11+
12+
const DEV_EXPRESSION = t.identifier('__DEV__');
13+
14+
const SEEN_SYMBOL = Symbol('expression.seen');
15+
16+
return {
17+
visitor: {
18+
CallExpression: {
19+
exit: function(path) {
20+
const node = path.node;
21+
22+
// Ignore if it's already been processed
23+
if (node[SEEN_SYMBOL]) {
24+
return;
25+
}
26+
27+
if (
28+
path.get('callee').isIdentifier({name: 'warning'}) ||
29+
path.get('callee').isIdentifier({name: 'warningWithoutStack'})
30+
) {
31+
// Turns this code:
32+
//
33+
// warning(condition, argument, argument);
34+
//
35+
// into this:
36+
//
37+
// if (__DEV__) {
38+
// if (!condition) {
39+
// warning(false, argument, argument);
40+
// }
41+
// }
42+
//
43+
// The goal is to strip out warning calls entirely in production
44+
// and to avoid evaluating the arguments in development.
45+
const condition = node.arguments[0];
46+
const newNode = t.callExpression(
47+
node.callee,
48+
[t.booleanLiteral(false)].concat(node.arguments.slice(1))
49+
);
50+
newNode[SEEN_SYMBOL] = true;
51+
path.replaceWith(
52+
t.ifStatement(
53+
DEV_EXPRESSION,
54+
t.blockStatement([
55+
t.ifStatement(
56+
t.unaryExpression('!', condition),
57+
t.expressionStatement(newNode)
58+
),
59+
])
60+
)
61+
);
62+
}
63+
},
64+
},
65+
},
66+
};
67+
};

scripts/jest/preprocessor.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const pathToBabel = path.join(
1616
const pathToBabelPluginDevWithCode = require.resolve(
1717
'../error-codes/transform-error-messages'
1818
);
19+
const pathToBabelPluginWrapWarning = require.resolve(
20+
'../babel/wrap-warning-with-env-check'
21+
);
1922
const pathToBabelPluginAsyncToGenerator = require.resolve(
2023
'@babel/plugin-transform-async-to-generator'
2124
);
@@ -31,6 +34,7 @@ const babelOptions = {
3134
require.resolve('@babel/plugin-transform-modules-commonjs'),
3235

3336
pathToBabelPluginDevWithCode,
37+
pathToBabelPluginWrapWarning,
3438

3539
// Keep stacks detailed in tests.
3640
// Don't put this in .babelrc so that we don't embed filenames
@@ -88,6 +92,7 @@ module.exports = {
8892
pathToBabel,
8993
pathToBabelrc,
9094
pathToBabelPluginDevWithCode,
95+
pathToBabelPluginWrapWarning,
9196
pathToTransformInfiniteLoops,
9297
pathToErrorCodes,
9398
]),

scripts/rollup/build.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
124124
plugins: options.plugins.concat([
125125
// Minify invariant messages
126126
require('../error-codes/transform-error-messages'),
127+
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
128+
require('../babel/wrap-warning-with-env-check'),
127129
]),
128130
});
129131
case RN_OSS_DEV:
@@ -139,6 +141,8 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
139141
// Preserve full error messages in React Native build
140142
{noMinify: true},
141143
],
144+
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
145+
require('../babel/wrap-warning-with-env-check'),
142146
]),
143147
});
144148
case UMD_DEV:
@@ -153,6 +157,8 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
153157
path.resolve('./scripts/babel/transform-object-assign-require'),
154158
// Minify invariant messages
155159
require('../error-codes/transform-error-messages'),
160+
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
161+
require('../babel/wrap-warning-with-env-check'),
156162
]),
157163
});
158164
default:

0 commit comments

Comments
 (0)