From 47c12d641adb03b722b0c7318665a734eb6cb732 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 20 Jun 2019 13:08:50 +0100 Subject: [PATCH 1/3] Disable React Refresh Babel transform in prod --- packages/react-refresh/src/ReactFreshBabelPlugin.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/react-refresh/src/ReactFreshBabelPlugin.js b/packages/react-refresh/src/ReactFreshBabelPlugin.js index eb46585561674..31b20969cc87f 100644 --- a/packages/react-refresh/src/ReactFreshBabelPlugin.js +++ b/packages/react-refresh/src/ReactFreshBabelPlugin.js @@ -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(); From 1cbe83ca054d9ff440fb5e3c0d61ad5cafb0d2bd Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 20 Jun 2019 13:11:29 +0100 Subject: [PATCH 2/3] Throw early if React Refresh runtime is imported in production --- packages/react-refresh/src/ReactFreshRuntime.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/react-refresh/src/ReactFreshRuntime.js b/packages/react-refresh/src/ReactFreshRuntime.js index 17060d59bc15a..f0fe7b3f0b662 100644 --- a/packages/react-refresh/src/ReactFreshRuntime.js +++ b/packages/react-refresh/src/ReactFreshRuntime.js @@ -27,6 +27,12 @@ type Signature = {| getCustomHooks: () => Array, |}; +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; From fddbb52379eea32f3174710e7fa705394f45b36d Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 20 Jun 2019 14:22:34 +0100 Subject: [PATCH 3/3] @hot reset -> @refresh reset --- packages/react-refresh/src/ReactFreshBabelPlugin.js | 4 ++-- .../src/__tests__/ReactFreshIntegration-test.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/react-refresh/src/ReactFreshBabelPlugin.js b/packages/react-refresh/src/ReactFreshBabelPlugin.js index 31b20969cc87f..a8568b257e74d 100644 --- a/packages/react-refresh/src/ReactFreshBabelPlugin.js +++ b/packages/react-refresh/src/ReactFreshBabelPlugin.js @@ -219,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); @@ -231,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; } diff --git a/packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js b/packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js index cfe8575265bfb..1def5d0b24f4e 100644 --- a/packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js +++ b/packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js @@ -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; @@ -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); @@ -804,7 +804,7 @@ describe('ReactFreshIntegration', () => { export default function App() { - // @hot reset + // @refresh reset const [foo, setFoo] = useState(S); return

D{foo}

; @@ -848,7 +848,7 @@ describe('ReactFreshIntegration', () => { export default function App() { - /* @hot reset */ + /* @refresh reset */ const [foo, setFoo] = useState(S); return

G{foo}

;