1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | if(!module.hot) {
|
12 | throw new Error("[HMR] Hot Module Replacement is disabled.");
|
13 | }
|
14 | if (!process.send) {
|
15 | throw new Error("[HMR] You need to spawn the process.");
|
16 | }
|
17 | var checkForUpdate = function checkForUpdate(fromUpdate) {
|
18 | module.hot.check().then(function(updatedModules) {
|
19 | if(!updatedModules) {
|
20 | if(fromUpdate)
|
21 | console.log("[HMR] Update applied.");
|
22 | else
|
23 | console.warn("[HMR] Nothing to update.");
|
24 | return;
|
25 | }
|
26 |
|
27 | return module.hot.apply({
|
28 | ignoreUnaccepted: true,
|
29 | onUnaccepted: function(data) {
|
30 | console.warn("Ignored an update to unaccepted module " + data.chain.join(" -> "));
|
31 | },
|
32 | }).then(function(renewedModules) {
|
33 | const unacceptedModules = updatedModules.filter(moduleId => (
|
34 | renewedModules && !renewedModules.includes(moduleId)
|
35 | ));
|
36 | require("webpack/hot/log-apply-result")(updatedModules, renewedModules);
|
37 | if (unacceptedModules.length) {
|
38 | process.send('restart');
|
39 | return;
|
40 | }
|
41 |
|
42 | checkForUpdate(true);
|
43 | });
|
44 | }).catch(function(err) {
|
45 | var status = module.hot.status();
|
46 | if(["abort", "fail"].indexOf(status) >= 0) {
|
47 | console.warn("[HMR] Cannot apply update.");
|
48 | console.warn("[HMR] " + err.stack || err.message);
|
49 | } else {
|
50 | console.warn("[HMR] Update failed: " + err.stack || err.message);
|
51 | }
|
52 | process.send('restart');
|
53 | });
|
54 | };
|
55 |
|
56 | process.on(__resourceQuery.substr(1) || "SIGUSR2", function() {
|
57 | if(module.hot.status() !== "idle") {
|
58 | console.warn("[HMR] Got signal but currently in " + module.hot.status() + " state.");
|
59 | console.warn("[HMR] Need to be in idle state to start hot update.");
|
60 | return;
|
61 | }
|
62 |
|
63 | checkForUpdate();
|
64 | });
|