Skip to content

Commit

Permalink
[Fresh] Throw in prod and change annotation (#15939)
Browse files Browse the repository at this point in the history
* Disable React Refresh Babel transform in prod

* Throw early if React Refresh runtime is imported in production

* @hot reset -> @refresh reset
  • Loading branch information
gaearon authored Jun 20, 2019
1 parent ff91bfa commit d4f384d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
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 +
'".',
);
}
}

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

0 comments on commit d4f384d

Please sign in to comment.