Skip to content

Commit c6d29c9

Browse files
committed
fix: [babel][prod] separate default and root 'hot' detection, fixes #1283
1 parent fa1e172 commit c6d29c9

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

Diff for: src/babel.prod.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@ const RHLPackage = 'react-hot-loader';
22
const RHLRootPackage = 'react-hot-loader/root';
33
const RHLPackages = [RHLPackage, RHLRootPackage];
44

5-
function isImportedFromRHL(path, name) {
5+
function isImportedFromPackages(path, name, packages) {
66
const binding = path.scope.getBinding(name);
77
const bindingType = binding && binding.path.node.type;
88

99
if (bindingType === 'ImportSpecifier' || bindingType === 'ImportNamespaceSpecifier') {
1010
const bindingParent = binding.path.parent;
11-
return RHLPackages.includes(bindingParent.source.value);
11+
return packages.includes(bindingParent.source.value);
1212
}
1313

1414
return false;
1515
}
1616

17+
function isImportedFromRHL(path, name) {
18+
return isImportedFromPackages(path, name, [RHLPackage]);
19+
}
20+
21+
function isImportedFromRHLRoot(path, name) {
22+
return isImportedFromPackages(path, name, [RHLRootPackage]);
23+
}
24+
1725
function getRHLContext(file) {
1826
const context = [];
1927
const { body } = file.ast.program;
@@ -77,7 +85,7 @@ export default function plugin() {
7785
if (
7886
path.node.callee.name === specifier.local &&
7987
// ensure that this is `hot` from RHL
80-
isImportedFromRHL(path, specifier.local) &&
88+
isImportedFromRHLRoot(path, specifier.local) &&
8189
path.type === 'CallExpression' &&
8290
path.node.arguments[0] &&
8391
path.node.arguments[0].type === 'Identifier'

Diff for: test/__babel_fixtures__/drop-hot.prod.js

+3
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ namedFoo(module)(App);
2323
RHL.foo(module)(App);
2424
NOTRHL.hot(module)(App);
2525

26+
// should not drop incomplete reference
27+
namedFoo(module);
28+
2629
export { a, b, c, d, e, z };

Diff for: test/__snapshots__/babel.test.js.snap

+6
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,9 @@ var e = App;
11181118
RHL.foo(module)(App);
11191119
NOTRHL.hot(module)(App);
11201120
1121+
// should not drop incomplete reference
1122+
(0, _reactHotLoader.foo)(module);
1123+
11211124
exports.a = a;
11221125
exports.b = b;
11231126
exports.c = c;
@@ -2196,6 +2199,9 @@ const e = App;
21962199
RHL.foo(module)(App);
21972200
NOTRHL.hot(module)(App);
21982201
2202+
// should not drop incomplete reference
2203+
(0, _reactHotLoader.foo)(module);
2204+
21992205
exports.a = a;
22002206
exports.b = b;
22012207
exports.c = c;

0 commit comments

Comments
 (0)