[Fresh] Throw in prod and change annotation (#15939)
* Disable React Refresh Babel transform in prod * Throw early if React Refresh runtime is imported in production * @hot reset -> @refresh reset
Dan Abramov committed
Jun 20, 2019 at 14:29 UTC
d4f384d25b9b6da4b88cde08783530df1831d8d4
3 files changed
+25
-6
packages/react-refresh/src/ReactFreshBabelPlugin.js
+15
-2
@@ -8,6 +8,19 @@
8
'use strict';
9
10
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
+
24
const {types: t} = babel;
25
26
const registrationsByProgramPath = new Map();
@@ -206,7 +219,7 @@ export default function(babel) {
219
220
let hasForceResetCommentByFile = new WeakMap();
221
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.
223
function hasForceResetComment(path) {
224
const file = path.hub.file;
225
let hasForceReset = hasForceResetCommentByFile.get(file);
@@ -218,7 +231,7 @@ export default function(babel) {
231
const comments = file.ast.comments;
232
for (let i = 0; i < comments.length; i++) {
233
const cmt = comments[i];
221
- if (cmt.value.indexOf('@hot reset') !== -1) {
234
+ if (cmt.value.indexOf('@refresh reset') !== -1) {
235
hasForceReset = true;
236
break;
237
}
packages/react-refresh/src/ReactFreshRuntime.js
+6
@@ -27,6 +27,12 @@ type Signature = {|
27
getCustomHooks: () => Array<Function>,
28
|};
29
30
+if (!__DEV__) {
31
+ throw new Error(
32
+ 'React Refresh runtime should not be included in the production bundle.',
33
+ );
34
+}
35
+
36
// In old environments, we'll leak previous types after every edit.
37
const PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map;
38
const PossiblyWeakSet = typeof WeakSet === 'function' ? WeakSet : Set;
packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js
+4
-4
@@ -755,7 +755,7 @@ describe('ReactFreshIntegration', () => {
755
}
756
});
757
758
- it('resets state on every edit with @hot reset annotation', () => {
758
+ it('resets state on every edit with @refresh reset annotation', () => {
759
if (__DEV__) {
760
render(`
761
const {useState} = React;
@@ -786,7 +786,7 @@ describe('ReactFreshIntegration', () => {
786
const {useState} = React;
787
const S = 3;
788
789
- /* @hot reset */
789
+ /* @refresh reset */
790
791
export default function App() {
792
const [foo, setFoo] = useState(S);
@@ -804,7 +804,7 @@ describe('ReactFreshIntegration', () => {
804
805
export default function App() {
806
807
- // @hot reset
807
+ // @refresh reset
808
809
const [foo, setFoo] = useState(S);
810
return <h1>D{foo}</h1>;
@@ -848,7 +848,7 @@ describe('ReactFreshIntegration', () => {
848
849
export default function App() {
850
851
- /* @hot reset */
851
+ /* @refresh reset */
852
853
const [foo, setFoo] = useState(S);
854
return <h1>G{foo}</h1>;