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