Skip to content

Commit

Permalink
Support Babel's envName option in React Refresh plugin (#19009)
Browse files Browse the repository at this point in the history
* Fix envName bug

* Replace getEnv with env
  • Loading branch information
kevinweber committed Sep 1, 2020
1 parent 1f38dcf commit b7d18c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/react-refresh/src/ReactFreshBabelPlugin.js
Expand Up @@ -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. ' +
Expand Down
Expand Up @@ -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,
Expand Down Expand Up @@ -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.',
);
});
});

0 comments on commit b7d18c4

Please sign in to comment.