aboutsummaryrefslogtreecommitdiff
path: root/node_modules/webpack-hot-client/client/index.js
blob: 2dca618dc4b2ad0149890a01b98e92d2d94c6ff7 (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
'use strict';
/* eslint-disable global-require */

(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

  var options = __hotClientOptions__;

  var log = require('./log'); // eslint-disable-line import/order


  log.level = options.logLevel;

  var update = require('./hot');

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

  if (!options) {
    throw new Error('Something went awry and __hotClientOptions__ is undefined. Possible bad build. HMR cannot be enabled.');
  }

  var currentHash;
  var initial = true;
  var isUnloading;
  window.addEventListener('beforeunload', function () {
    isUnloading = true;
  });

  function reload() {
    if (isUnloading) {
      return;
    }

    if (options.hot) {
      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: function compile(_ref) {
      var compilerName = _ref.compilerName;
      log.info("webpack: Compiling (".concat(compilerName, ")"));
    },
    errors: function errors(_ref2) {
      var errors = _ref2.errors;
      log.error('webpack: Encountered errors while compiling. Reload prevented.');

      for (var i = 0; i < errors.length; i++) {
        log.error(errors[i]);
      }
    },
    hash: function hash(_ref3) {
      var hash = _ref3.hash;
      currentHash = hash;
    },
    invalid: function invalid(_ref4) {
      var fileName = _ref4.fileName;
      log.info("App updated. Recompiling ".concat(fileName));
    },
    ok: function ok() {
      if (initial) {
        initial = false;
        return initial;
      }

      reload();
    },
    'window-reload': function windowReload() {
      window.location.reload();
    },
    warnings: function warnings(_ref5) {
      var warnings = _ref5.warnings;
      log.warn('Warnings while compiling.');

      for (var i = 0; i < warnings.length; i++) {
        log.warn(warnings[i]);
      }

      if (initial) {
        initial = false;
        return initial;
      }

      reload();
    }
  });
})();