Support Babel's envName option in React Refresh plugin (#19009)
* Fix envName bug * Replace getEnv with env
Kevin Weber committed
Sep 1, 2020 at 05:50 UTC
b7d18c4daf244b991858cc9b0706b64589f4fd60
2 files changed
+20
-3
packages/react-refresh/src/ReactFreshBabelPlugin.js
+2
-2
@@ -8,9 +8,9 @@
8
'use strict';
9
10
export default function(babel, opts = {}) {
11
- if (typeof babel.getEnv === 'function') {
11
+ if (typeof babel.env === 'function') {
12
// Only available in Babel 7.
13
- const env = babel.getEnv();
13
+ const env = babel.env();
14
if (env !== 'development' && !opts.skipEnvCheck) {
15
throw new Error(
16
'React Refresh Babel transform should only be enabled in development environment. ' +
packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js
+18
-1
@@ -16,13 +16,15 @@ function transform(input, options = {}) {
16
babel.transform(input, {
17
babelrc: false,
18
configFile: false,
19
+ envName: options.envName,
20
plugins: [
21
'@babel/syntax-jsx',
22
'@babel/syntax-dynamic-import',
23
[
24
freshPlugin,
25
{
25
- skipEnvCheck: true,
26
+ skipEnvCheck:
27
+ options.skipEnvCheck === undefined ? true : options.skipEnvCheck,
28
// To simplify debugging tests:
29
emitFullSignatures: true,
30
...options.freshOptions,
@@ -507,4 +509,19 @@ describe('ReactFreshBabelPlugin', () => {
509
),
510
).toMatchSnapshot();
511
});
512
+
513
+ it("respects Babel's envName option", () => {
514
+ const envName = 'random';
515
+ expect(() =>
516
+ transform(`export default function BabelEnv () { return null };`, {
517
+ envName,
518
+ skipEnvCheck: false,
519
+ }),
520
+ ).toThrowError(
521
+ 'React Refresh Babel transform should only be enabled in development environment. ' +
522
+ 'Instead, the environment is: "' +
523
+ envName +
524
+ '". If you want to override this check, pass {skipEnvCheck: true} as plugin options.',
525
+ );
526
+ });
527
});