Skip to content

Commit 9aa16c2

Browse files
kevinweberkoto
authored andcommitted
Support Babel's envName option in React Refresh plugin (facebook#19009)
* Fix envName bug * Replace getEnv with env
1 parent 28e1e33 commit 9aa16c2

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

packages/react-refresh/src/ReactFreshBabelPlugin.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
'use strict';
99

1010
export default function(babel, opts = {}) {
11-
if (typeof babel.getEnv === 'function') {
11+
if (typeof babel.env === 'function') {
1212
// Only available in Babel 7.
13-
const env = babel.getEnv();
13+
const env = babel.env();
1414
if (env !== 'development' && !opts.skipEnvCheck) {
1515
throw new Error(
1616
'React Refresh Babel transform should only be enabled in development environment. ' +

packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ function transform(input, options = {}) {
1616
babel.transform(input, {
1717
babelrc: false,
1818
configFile: false,
19+
envName: options.envName,
1920
plugins: [
2021
'@babel/syntax-jsx',
2122
'@babel/syntax-dynamic-import',
2223
[
2324
freshPlugin,
2425
{
25-
skipEnvCheck: true,
26+
skipEnvCheck:
27+
options.skipEnvCheck === undefined ? true : options.skipEnvCheck,
2628
// To simplify debugging tests:
2729
emitFullSignatures: true,
2830
...options.freshOptions,
@@ -507,4 +509,19 @@ describe('ReactFreshBabelPlugin', () => {
507509
),
508510
).toMatchSnapshot();
509511
});
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+
});
510527
});

0 commit comments

Comments
 (0)