Skip to content

Commit 3fb6685

Browse files
fix: legacy initial for webpack v4
1 parent 9f7a054 commit 3fb6685

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

client-src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import sendMessage from "./utils/sendMessage.js";
1010
import reloadApp from "./utils/reloadApp.js";
1111
import createSocketURL from "./utils/createSocketURL.js";
1212

13-
const status = { isUnloading: false, currentHash: __webpack_hash__ };
13+
const status = {
14+
isUnloading: false,
15+
// TODO Workaround for webpack v4, `__webpack_hash__` is not replaced without HotModuleReplacement
16+
// eslint-disable-next-line camelcase
17+
currentHash: typeof __webpack_hash__ !== "undefined" ? __webpack_hash__ : "",
18+
};
19+
// console.log(__webpack_hash__);
1420
const options = {
1521
hot: false,
1622
liveReload: false,

client-src/utils/reloadApp.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@
33
import hotEmitter from "webpack/hot/emitter.js";
44
import { log } from "./log.js";
55

6-
function reloadApp({ hot, liveReload }, { isUnloading, currentHash }) {
7-
if (isUnloading) {
6+
function reloadApp({ hot, liveReload }, status) {
7+
if (status.isUnloading) {
88
return;
99
}
1010

11-
const isInitial = currentHash.indexOf(__webpack_hash__) === 0;
11+
// TODO Workaround for webpack v4, `__webpack_hash__` is not replaced without HotModuleReplacement plugin
12+
const webpackHash =
13+
// eslint-disable-next-line camelcase
14+
typeof __webpack_hash__ !== "undefined"
15+
? // eslint-disable-next-line camelcase
16+
__webpack_hash__
17+
: status.previousHash || "";
18+
const isInitial = status.currentHash.indexOf(webpackHash) === 0;
1219

1320
if (isInitial) {
21+
const isLegacyInitial =
22+
webpackHash === "" && hot === false && liveReload === true;
23+
24+
if (isLegacyInitial) {
25+
status.previousHash = status.currentHash;
26+
}
27+
1428
return;
1529
}
1630

@@ -30,11 +44,11 @@ function reloadApp({ hot, liveReload }, { isUnloading, currentHash }) {
3044
if (hot && allowToHot) {
3145
log.info("App hot update...");
3246

33-
hotEmitter.emit("webpackHotUpdate", currentHash);
47+
hotEmitter.emit("webpackHotUpdate", status.currentHash);
3448

3549
if (typeof self !== "undefined" && self.window) {
3650
// broadcast update to window
37-
self.postMessage(`webpackHotUpdate${currentHash}`, "*");
51+
self.postMessage(`webpackHotUpdate${status.currentHash}`, "*");
3852
}
3953
}
4054
// allow refreshing the page only if liveReload isn't disabled

0 commit comments

Comments
 (0)