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

[Fresh] Throw in prod and change annotation #15939

Merged
merged 3 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 15 additions & 2 deletions packages/react-refresh/src/ReactFreshBabelPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
'use strict';

export default function(babel) {
if (typeof babel.getEnv === 'function') {
// Only available in Babel 7.
const env = babel.getEnv();
if (env !== 'development') {
throw new Error(
'React Refresh Babel transform should only be enabled in development environment. ' +
'Instead, the environment is: "' +
env +
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess Prettier did this, seems strange though. :/

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yea

'".',
);
}
}

const {types: t} = babel;

const registrationsByProgramPath = new Map();
Expand Down Expand Up @@ -206,7 +219,7 @@ export default function(babel) {

let hasForceResetCommentByFile = new WeakMap();

// We let user do /* @hot reset */ to reset state in the whole file.
// We let user do /* @refresh reset */ to reset state in the whole file.
function hasForceResetComment(path) {
const file = path.hub.file;
let hasForceReset = hasForceResetCommentByFile.get(file);
Expand All @@ -218,7 +231,7 @@ export default function(babel) {
const comments = file.ast.comments;
for (let i = 0; i < comments.length; i++) {
const cmt = comments[i];
if (cmt.value.indexOf('@hot reset') !== -1) {
if (cmt.value.indexOf('@refresh reset') !== -1) {
hasForceReset = true;
break;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/react-refresh/src/ReactFreshRuntime.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ type Signature = {|
getCustomHooks: () => Array<Function>,
|};

if (!__DEV__) {
throw new Error(
'React Refresh runtime should not be included in the production bundle.',
);
}

// In old environments, we'll leak previous types after every edit.
const PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map;
const PossiblyWeakSet = typeof WeakSet === 'function' ? WeakSet : Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ describe('ReactFreshIntegration', () => {
}
});

it('resets state on every edit with @hot reset annotation', () => {
it('resets state on every edit with @refresh reset annotation', () => {
if (__DEV__) {
render(`
const {useState} = React;
Expand Down Expand Up @@ -786,7 +786,7 @@ describe('ReactFreshIntegration', () => {
const {useState} = React;
const S = 3;

/* @hot reset */
/* @refresh reset */

export default function App() {
const [foo, setFoo] = useState(S);
Expand All @@ -804,7 +804,7 @@ describe('ReactFreshIntegration', () => {

export default function App() {

// @hot reset
// @refresh reset

const [foo, setFoo] = useState(S);
return <h1>D{foo}</h1>;
Expand Down Expand Up @@ -848,7 +848,7 @@ describe('ReactFreshIntegration', () => {

export default function App() {

/* @hot reset */
/* @refresh reset */

const [foo, setFoo] = useState(S);
return <h1>G{foo}</h1>;
Expand Down