This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
/
index.js
105 lines (82 loc) · 2.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* eslint-disable global-require, consistent-return */
(function hotClientEntry() {
// eslint-disable-next-line no-underscore-dangle
if (window.__webpackHotClient__) {
return;
}
// eslint-disable-next-line no-underscore-dangle
window.__webpackHotClient__ = {};
// this is piped in at runtime build via DefinePlugin in /lib/plugins.js
// eslint-disable-next-line no-unused-vars, no-undef
const options = __hotClientOptions__;
const log = require('./log'); // eslint-disable-line import/order
log.level = options.logLevel;
const update = require('./hot');
const socket = require('./socket');
if (!options) {
throw new Error(
'Something went awry and __hotClientOptions__ is undefined. Possible bad build. HMR cannot be enabled.'
);
}
let currentHash;
let initial = true;
let isUnloading;
window.addEventListener('beforeunload', () => {
isUnloading = true;
});
function reload() {
if (isUnloading) {
return;
}
if (options.hmr) {
log.info('App Updated, Reloading Modules');
update(currentHash, options);
} else if (options.reload) {
log.info('Refreshing Page');
window.location.reload();
} else {
log.warn('Please refresh the page manually.');
log.info('The `hot` and `reload` options are set to false.');
}
}
socket(options, {
compile({ compilerName }) {
log.info(`webpack: Compiling (${compilerName})`);
},
errors({ errors }) {
log.error(
'webpack: Encountered errors while compiling. Reload prevented.'
);
for (let i = 0; i < errors.length; i++) {
log.error(errors[i]);
}
},
hash({ hash }) {
currentHash = hash;
},
invalid({ fileName }) {
log.info(`App updated. Recompiling ${fileName}`);
},
ok() {
if (initial) {
initial = false;
return initial;
}
reload();
},
'window-reload': () => {
window.location.reload();
},
warnings({ warnings }) {
log.warn('Warnings while compiling.');
for (let i = 0; i < warnings.length; i++) {
log.warn(warnings[i]);
}
if (initial) {
initial = false;
return initial;
}
reload();
},
});
})();