Skip to content

Commit 7633487

Browse files
committed
[Fresh] Throw in prod and change annotation (facebook#15939)
* Disable React Refresh Babel transform in prod * Throw early if React Refresh runtime is imported in production * @hot reset -> @refresh reset
1 parent 527be39 commit 7633487

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

packages/react-refresh/src/ReactFreshBabelPlugin.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
'use strict';
99

1010
export default function(babel) {
11+
if (typeof babel.getEnv === 'function') {
12+
// Only available in Babel 7.
13+
const env = babel.getEnv();
14+
if (env !== 'development') {
15+
throw new Error(
16+
'React Refresh Babel transform should only be enabled in development environment. ' +
17+
'Instead, the environment is: "' +
18+
env +
19+
'".',
20+
);
21+
}
22+
}
23+
1124
const {types: t} = babel;
1225

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

207220
let hasForceResetCommentByFile = new WeakMap();
208221

209-
// We let user do /* @hot reset */ to reset state in the whole file.
222+
// We let user do /* @refresh reset */ to reset state in the whole file.
210223
function hasForceResetComment(path) {
211224
const file = path.hub.file;
212225
let hasForceReset = hasForceResetCommentByFile.get(file);
@@ -218,7 +231,7 @@ export default function(babel) {
218231
const comments = file.ast.comments;
219232
for (let i = 0; i < comments.length; i++) {
220233
const cmt = comments[i];
221-
if (cmt.value.indexOf('@hot reset') !== -1) {
234+
if (cmt.value.indexOf('@refresh reset') !== -1) {
222235
hasForceReset = true;
223236
break;
224237
}

packages/react-refresh/src/ReactFreshRuntime.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ type Signature = {|
2727
getCustomHooks: () => Array<Function>,
2828
|};
2929

30+
if (!__DEV__) {
31+
throw new Error(
32+
'React Refresh runtime should not be included in the production bundle.',
33+
);
34+
}
35+
3036
// In old environments, we'll leak previous types after every edit.
3137
const PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map;
3238
const PossiblyWeakSet = typeof WeakSet === 'function' ? WeakSet : Set;

packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ describe('ReactFreshIntegration', () => {
755755
}
756756
});
757757

758-
it('resets state on every edit with @hot reset annotation', () => {
758+
it('resets state on every edit with @refresh reset annotation', () => {
759759
if (__DEV__) {
760760
render(`
761761
const {useState} = React;
@@ -786,7 +786,7 @@ describe('ReactFreshIntegration', () => {
786786
const {useState} = React;
787787
const S = 3;
788788
789-
/* @hot reset */
789+
/* @refresh reset */
790790
791791
export default function App() {
792792
const [foo, setFoo] = useState(S);
@@ -804,7 +804,7 @@ describe('ReactFreshIntegration', () => {
804804
805805
export default function App() {
806806
807-
// @hot reset
807+
// @refresh reset
808808
809809
const [foo, setFoo] = useState(S);
810810
return <h1>D{foo}</h1>;
@@ -848,7 +848,7 @@ describe('ReactFreshIntegration', () => {
848848
849849
export default function App() {
850850
851-
/* @hot reset */
851+
/* @refresh reset */
852852
853853
const [foo, setFoo] = useState(S);
854854
return <h1>G{foo}</h1>;

0 commit comments

Comments
 (0)