diff --git a/packages/react-refresh/src/ReactFreshBabelPlugin.js b/packages/react-refresh/src/ReactFreshBabelPlugin.js index 034c2666c4d06..49b0dce0f76ae 100644 --- a/packages/react-refresh/src/ReactFreshBabelPlugin.js +++ b/packages/react-refresh/src/ReactFreshBabelPlugin.js @@ -8,9 +8,9 @@ 'use strict'; export default function(babel, opts = {}) { - if (typeof babel.getEnv === 'function') { + if (typeof babel.env === 'function') { // Only available in Babel 7. - const env = babel.getEnv(); + const env = babel.env(); if (env !== 'development' && !opts.skipEnvCheck) { throw new Error( 'React Refresh Babel transform should only be enabled in development environment. ' + diff --git a/packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js b/packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js index c06961bae7a3f..320cfe17041a6 100644 --- a/packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js +++ b/packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js @@ -16,13 +16,15 @@ function transform(input, options = {}) { babel.transform(input, { babelrc: false, configFile: false, + envName: options.envName, plugins: [ '@babel/syntax-jsx', '@babel/syntax-dynamic-import', [ freshPlugin, { - skipEnvCheck: true, + skipEnvCheck: + options.skipEnvCheck === undefined ? true : options.skipEnvCheck, // To simplify debugging tests: emitFullSignatures: true, ...options.freshOptions, @@ -507,4 +509,19 @@ describe('ReactFreshBabelPlugin', () => { ), ).toMatchSnapshot(); }); + + it("respects Babel's envName option", () => { + const envName = 'random'; + expect(() => + transform(`export default function BabelEnv () { return null };`, { + envName, + skipEnvCheck: false, + }), + ).toThrowError( + 'React Refresh Babel transform should only be enabled in development environment. ' + + 'Instead, the environment is: "' + + envName + + '". If you want to override this check, pass {skipEnvCheck: true} as plugin options.', + ); + }); });