From c6d29c95d5a825b82de7499b867f20067a479057 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Thu, 4 Jul 2019 08:49:41 +1000 Subject: [PATCH] fix: [babel][prod] separate default and root 'hot' detection, fixes #1283 --- src/babel.prod.js | 14 +++++++++++--- test/__babel_fixtures__/drop-hot.prod.js | 3 +++ test/__snapshots__/babel.test.js.snap | 6 ++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/babel.prod.js b/src/babel.prod.js index 5b6166fb2..cdfa8587a 100644 --- a/src/babel.prod.js +++ b/src/babel.prod.js @@ -2,18 +2,26 @@ const RHLPackage = 'react-hot-loader'; const RHLRootPackage = 'react-hot-loader/root'; const RHLPackages = [RHLPackage, RHLRootPackage]; -function isImportedFromRHL(path, name) { +function isImportedFromPackages(path, name, packages) { const binding = path.scope.getBinding(name); const bindingType = binding && binding.path.node.type; if (bindingType === 'ImportSpecifier' || bindingType === 'ImportNamespaceSpecifier') { const bindingParent = binding.path.parent; - return RHLPackages.includes(bindingParent.source.value); + return packages.includes(bindingParent.source.value); } return false; } +function isImportedFromRHL(path, name) { + return isImportedFromPackages(path, name, [RHLPackage]); +} + +function isImportedFromRHLRoot(path, name) { + return isImportedFromPackages(path, name, [RHLRootPackage]); +} + function getRHLContext(file) { const context = []; const { body } = file.ast.program; @@ -77,7 +85,7 @@ export default function plugin() { if ( path.node.callee.name === specifier.local && // ensure that this is `hot` from RHL - isImportedFromRHL(path, specifier.local) && + isImportedFromRHLRoot(path, specifier.local) && path.type === 'CallExpression' && path.node.arguments[0] && path.node.arguments[0].type === 'Identifier' diff --git a/test/__babel_fixtures__/drop-hot.prod.js b/test/__babel_fixtures__/drop-hot.prod.js index 486487916..47039ffa5 100644 --- a/test/__babel_fixtures__/drop-hot.prod.js +++ b/test/__babel_fixtures__/drop-hot.prod.js @@ -23,4 +23,7 @@ namedFoo(module)(App); RHL.foo(module)(App); NOTRHL.hot(module)(App); +// should not drop incomplete reference +namedFoo(module); + export { a, b, c, d, e, z }; diff --git a/test/__snapshots__/babel.test.js.snap b/test/__snapshots__/babel.test.js.snap index 50400b03d..c49e18394 100644 --- a/test/__snapshots__/babel.test.js.snap +++ b/test/__snapshots__/babel.test.js.snap @@ -1118,6 +1118,9 @@ var e = App; RHL.foo(module)(App); NOTRHL.hot(module)(App); +// should not drop incomplete reference +(0, _reactHotLoader.foo)(module); + exports.a = a; exports.b = b; exports.c = c; @@ -2196,6 +2199,9 @@ const e = App; RHL.foo(module)(App); NOTRHL.hot(module)(App); +// should not drop incomplete reference +(0, _reactHotLoader.foo)(module); + exports.a = a; exports.b = b; exports.c = c;