GeezerVison is showing github.com — Support Babel's envName option in React Refresh plugin (#19009) · react/react@b7d18c4
File treeExpand file treeCollapse file tree Open diff view settings packages/react-refresh/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-refresh/src/ReactFreshBabelPlugin.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | 'use strict'; | 9 | 9 |
| 10 | 10 | export default function(babel, opts = {}) { | 11 | | - if (typeof babel.getEnv === 'function') { | | 11 | + if (typeof babel.env === 'function') { | 12 | 12 | // Only available in Babel 7. | 13 | | - const env = babel.getEnv(); | | 13 | + const env = babel.env(); | 14 | 14 | if (env !== 'development' && !opts.skipEnvCheck) { | 15 | 15 | throw new Error( | 16 | 16 | 'React Refresh Babel transform should only be enabled in development environment. ' + | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js +18-1Lines changed: 18 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,13 +16,15 @@ function transform(input, options = {}) { | 16 | 16 | babel.transform(input, { | 17 | 17 | babelrc: false, | 18 | 18 | configFile: false, | | 19 | + envName: options.envName, | 19 | 20 | plugins: [ | 20 | 21 | '@babel/syntax-jsx', | 21 | 22 | '@babel/syntax-dynamic-import', | 22 | 23 | [ | 23 | 24 | freshPlugin, | 24 | 25 | { | 25 | | - skipEnvCheck: true, | | 26 | + skipEnvCheck: | | 27 | + options.skipEnvCheck === undefined ? true : options.skipEnvCheck, | 26 | 28 | // To simplify debugging tests: | 27 | 29 | emitFullSignatures: true, | 28 | 30 | ...options.freshOptions, | @@ -507,4 +509,19 @@ describe('ReactFreshBabelPlugin', () => { | 507 | 509 | ), | 508 | 510 | ).toMatchSnapshot(); | 509 | 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 | + }); | 510 | 527 | }); |
|