aboutsummaryrefslogtreecommitdiff
path: root/node_modules/webpack-hot-client/client/hot.js
blob: 5408d19676ae39f51019949f0359cf80b02bdab7 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict';

var log = require('./log');

var refresh = 'Please refresh the page.';
var hotOptions = {
  ignoreUnaccepted: true,
  ignoreDeclined: true,
  ignoreErrored: true,
  onUnaccepted: function onUnaccepted(data) {
    var chain = [].concat(data.chain);
    var last = chain[chain.length - 1];

    if (last === 0) {
      chain.pop();
    }

    log.warn("Ignored an update to unaccepted module ".concat(chain.join(' ➭ ')));
  },
  onDeclined: function onDeclined(data) {
    log.warn("Ignored an update to declined module ".concat(data.chain.join(' ➭ ')));
  },
  onErrored: function onErrored(data) {
    log.warn("Ignored an error while updating module ".concat(data.moduleId, " <").concat(data.type, ">"));
    log.warn(data.error);
  }
};
var lastHash;

function upToDate() {
  return lastHash.indexOf(__webpack_hash__) >= 0;
}

function result(modules, appliedModules) {
  var unaccepted = modules.filter(function (moduleId) {
    return appliedModules && appliedModules.indexOf(moduleId) < 0;
  });

  if (unaccepted.length > 0) {
    var message = 'The following modules could not be updated:';
    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
      for (var _iterator = unaccepted[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
        var _moduleId = _step.value;
        message += "\n          \u29BB ".concat(_moduleId);
      }
    } catch (err) {
      _didIteratorError = true;
      _iteratorError = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion && _iterator.return != null) {
          _iterator.return();
        }
      } finally {
        if (_didIteratorError) {
          throw _iteratorError;
        }
      }
    }

    log.warn(message);
  }

  if (!(appliedModules || []).length) {
    log.info('No Modules Updated.');
  } else {
    var _message = ['The following modules were updated:'];
    var _iteratorNormalCompletion2 = true;
    var _didIteratorError2 = false;
    var _iteratorError2 = undefined;

    try {
      for (var _iterator2 = appliedModules[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
        var _moduleId3 = _step2.value;

        _message.push("         \u21BB ".concat(_moduleId3));
      }
    } catch (err) {
      _didIteratorError2 = true;
      _iteratorError2 = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
          _iterator2.return();
        }
      } finally {
        if (_didIteratorError2) {
          throw _iteratorError2;
        }
      }
    }

    log.info(_message.join('\n'));
    var numberIds = appliedModules.every(function (moduleId) {
      return typeof moduleId === 'number';
    });

    if (numberIds) {
      log.info('Please consider using the NamedModulesPlugin for module names.');
    }
  }
}

function check(options) {
  module.hot.check().then(function (modules) {
    if (!modules) {
      log.warn("Cannot find update. The server may have been restarted. ".concat(refresh));

      if (options.reload) {
        window.location.reload();
      }

      return;
    }

    var hotOpts = options.reload ? {} : hotOptions;
    return module.hot.apply(hotOpts).then(function (appliedModules) {
      if (!upToDate()) {
        check(options);
      }

      result(modules, appliedModules);

      if (upToDate()) {
        log.info('App is up to date.');
      }
    }).catch(function (err) {
      var status = module.hot.status();

      if (['abort', 'fail'].indexOf(status) >= 0) {
        log.warn("Cannot apply update. ".concat(refresh));
        log.warn(err.stack || err.message);

        if (options.reload) {
          window.location.reload();
        }
      } else {
        log.warn("Update failed: ".concat(err.stack) || err.message);
      }
    });
  }).catch(function (err) {
    var status = module.hot.status();

    if (['abort', 'fail'].indexOf(status) >= 0) {
      log.warn("Cannot check for update. ".concat(refresh));
      log.warn(err.stack || err.message);

      if (options.reload) {
        window.location.reload();
      }
    } else {
      log.warn("Update check failed: ".concat(err.stack) || err.message);
    }
  });
}

if (module.hot) {
  log.info('Hot Module Replacement Enabled. Waiting for signal.');
} else {
  log.error('Hot Module Replacement is disabled.');
}

module.exports = function update(currentHash, options) {
  lastHash = currentHash;

  if (!upToDate()) {
    var status = module.hot.status();

    if (status === 'idle') {
      log.info('Checking for updates to the bundle.');
      check(options);
    } else if (['abort', 'fail'].indexOf(status) >= 0) {
      log.warn("Cannot apply update. A previous update ".concat(status, "ed. ").concat(refresh));

      if (options.reload) {
        window.location.reload();
      }
    }
  }
};