Skip to content

Commit 3bf3b84

Browse files
trygveaagajus
authored andcommitted
fix: make hot reload work on files containing only import statements (#77)
If a file only has import statements, the parser would crash when hot module reloading was enabled because firstNonImportDeclarationNode would be undefined. This makes those files work by adding the hot module block at the end of the file in those cases.
1 parent 711086f commit 3bf3b84

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

src/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ export default ({
9797

9898
const hotAcceptStatement = t.ifStatement(test, consequent);
9999

100-
firstNonImportDeclarationNode.insertBefore(hotAcceptStatement);
100+
if (firstNonImportDeclarationNode) {
101+
firstNonImportDeclarationNode.insertBefore(hotAcceptStatement);
102+
} else {
103+
programPath.pushContainer('body', hotAcceptStatement);
104+
}
101105
};
102106

103107
const getTargetResourcePath = (path: Object, stats:Object) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './bar.css';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.a {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import './bar.css';
2+
3+
if (module.hot) {
4+
module.hot.accept('./bar.css', function () {
5+
require('./bar.css');
6+
});
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../../src",
5+
{
6+
"generateScopedName": "[name]__[local]",
7+
"webpackHotModuleReloading": true
8+
}
9+
]
10+
]
11+
}

0 commit comments

Comments
 (0)