-
Notifications
You must be signed in to change notification settings - Fork 296
/
process-update.js
157 lines (138 loc) · 4.36 KB
/
process-update.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Based heavily on https://github.com/webpack/webpack/blob/
* c0afdf9c6abc1dd70707c594e473802a566f7b6e/hot/only-dev-server.js
* Original copyright Tobias Koppers @sokra (MIT license)
*/
/* global window __webpack_hash__ */
if (!module.hot) {
throw new Error('[HMR] Hot Module Replacement is disabled.');
}
var hmrDocsUrl = 'https://webpack.js.org/concepts/hot-module-replacement/'; // eslint-disable-line max-len
var lastHash;
var failureStatuses = { abort: 1, fail: 1 };
var applyOptions = {
ignoreUnaccepted: true,
ignoreDeclined: true,
ignoreErrored: true,
onUnaccepted: function (data) {
console.warn(
'Ignored an update to unaccepted module ' + data.chain.join(' -> ')
);
},
onDeclined: function (data) {
console.warn(
'Ignored an update to declined module ' + data.chain.join(' -> ')
);
},
onErrored: function (data) {
console.error(data.error);
console.warn(
'Ignored an error while updating module ' +
data.moduleId +
' (' +
data.type +
')'
);
},
};
function upToDate(hash) {
if (hash) lastHash = hash;
return lastHash == __webpack_hash__;
}
module.exports = function (hash, moduleMap, options) {
var reload = options.reload;
if (!upToDate(hash) && module.hot.status() == 'idle') {
if (options.log) console.log('[HMR] Checking for updates on the server...');
check();
}
function check() {
var cb = function (err, updatedModules) {
if (err) return handleError(err);
if (!updatedModules) {
if (options.warn) {
console.warn('[HMR] Cannot find update (Full reload needed)');
console.warn('[HMR] (Probably because of restarting the server)');
}
performReload();
return null;
}
var applyCallback = function (applyErr, renewedModules) {
if (applyErr) return handleError(applyErr);
if (!upToDate()) check();
logUpdates(updatedModules, renewedModules);
};
var applyResult = module.hot.apply(applyOptions, applyCallback);
// webpack 2 promise
if (applyResult && applyResult.then) {
// HotModuleReplacement.runtime.js refers to the result as `outdatedModules`
applyResult.then(function (outdatedModules) {
applyCallback(null, outdatedModules);
});
applyResult.catch(applyCallback);
}
};
var result = module.hot.check(false, cb);
// webpack 2 promise
if (result && result.then) {
result.then(function (updatedModules) {
cb(null, updatedModules);
});
result.catch(cb);
}
}
function logUpdates(updatedModules, renewedModules) {
var unacceptedModules = updatedModules.filter(function (moduleId) {
return renewedModules && renewedModules.indexOf(moduleId) < 0;
});
if (unacceptedModules.length > 0) {
if (options.warn) {
console.warn(
"[HMR] The following modules couldn't be hot updated: " +
'(Full reload needed)\n' +
'This is usually because the modules which have changed ' +
'(and their parents) do not know how to hot reload themselves. ' +
'See ' +
hmrDocsUrl +
' for more details.'
);
unacceptedModules.forEach(function (moduleId) {
console.warn('[HMR] - ' + (moduleMap[moduleId] || moduleId));
});
}
performReload();
return;
}
if (options.log) {
if (!renewedModules || renewedModules.length === 0) {
console.log('[HMR] Nothing hot updated.');
} else {
console.log('[HMR] Updated modules:');
renewedModules.forEach(function (moduleId) {
console.log('[HMR] - ' + (moduleMap[moduleId] || moduleId));
});
}
if (upToDate()) {
console.log('[HMR] App is up to date.');
}
}
}
function handleError(err) {
if (module.hot.status() in failureStatuses) {
if (options.warn) {
console.warn('[HMR] Cannot check for update (Full reload needed)');
console.warn('[HMR] ' + (err.stack || err.message));
}
performReload();
return;
}
if (options.warn) {
console.warn('[HMR] Update check failed: ' + (err.stack || err.message));
}
}
function performReload() {
if (reload) {
if (options.warn) console.warn('[HMR] Reloading page');
window.location.reload();
}
}
};