Skip to content

Commit

Permalink
fix: [babel][prod] separate default and root 'hot' detection, fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Jul 3, 2019
1 parent fa1e172 commit c6d29c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/babel.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions test/__babel_fixtures__/drop-hot.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
6 changes: 6 additions & 0 deletions test/__snapshots__/babel.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c6d29c9

Please sign in to comment.