forked from alexisvincent/systemjs-hot-reloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhot-reloader.js
92 lines (65 loc) · 3.24 KB
/
hot-reloader.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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
require('systemjs-hmr');
var _socket = require('socket.io-client');
var _socket2 = _interopRequireDefault(_socket);
var _weakee = require('weakee');
var _weakee2 = _interopRequireDefault(_weakee);
var _debug = require('debug');
var _debug2 = _interopRequireDefault(_debug);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* eslint-env browser */
var d = (0, _debug2.default)('hot-reloader');
var HotReloader = function (_Emitter) {
_inherits(HotReloader, _Emitter);
function HotReloader(backendUrl) {
var transform = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (x) {
return x;
};
_classCallCheck(this, HotReloader);
// Set default backend URL
var _this = _possibleConstructorReturn(this, (HotReloader.__proto__ || Object.getPrototypeOf(HotReloader)).call(this));
if (!backendUrl) {
backendUrl = '//' + document.location.host;
}
_this.socket = (0, _socket2.default)(backendUrl);
_this.socket.on('connect', function () {
d('hot reload connected to watcher on ', backendUrl);
_this.socket.emit('identification', navigator.userAgent);
_this.socket.emit('package.json', function (pjson) {
// self.pjson = pjson // maybe needed in the future?
_this.jspmConfigFile = pjson.jspm.configFile || 'config.js';
});
});
_this.socket.on('reload', function () {
d('whole page reload requested');
document.location.reload(true);
});
_this.socket.on('change', function (event) {
var moduleName = transform(event.path);
_this.emit('change', moduleName);
if (moduleName === 'index.html' || moduleName === _this.jspmConfigFile) {
document.location.reload(true);
} else {
System.reload(moduleName).catch(function (err) {
_this.emit('moduleRecordNotFound', err);
// not found any module for this file, not really an error
});
}
});
// emitting errors for jspm-dev-buddy
window.onerror = function (err) {
_this.socket.emit('error', err);
};
_this.socket.on('disconnect', function () {
d('hot reload disconnected from ', backendUrl);
});
return _this;
}
return HotReloader;
}(_weakee2.default);
exports.default = HotReloader;