Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Babel's envName option in React Refresh plugin #19009

Merged
merged 2 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-refresh/src/ReactFreshBabelPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
export default function(babel, opts = {}) {
if (typeof babel.getEnv === 'function') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change this check too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaearon Good callout, thanks! It's fixed now.

// 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
Original file line number Diff line number Diff line change
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 @@ -498,4 +500,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.',
);
});
});