-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
hmr-runtime.js
118 lines (98 loc) · 2.57 KB
/
hmr-runtime.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
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
var global = (1, eval)('this');
var OldModule = module.bundle.Module;
function Module() {
OldModule.call(this);
this.hot = {
accept: function (fn) {
this._acceptCallback = fn || function () {};
},
dispose: function (fn) {
this._disposeCallback = fn;
}
};
}
module.bundle.Module = Module;
if (!module.bundle.parent && typeof WebSocket !== 'undefined') {
var ws = new WebSocket('ws://' + window.location.hostname + ':{{HMR_PORT}}/');
ws.onmessage = function(event) {
var data = JSON.parse(event.data);
if (data.type === 'update') {
data.assets.forEach(function (asset) {
hmrApply(global.require, asset);
});
data.assets.forEach(function (asset) {
if (!asset.isNew) {
hmrAccept(global.require, asset.id);
}
});
}
if (data.type === 'reload') {
ws.close();
ws.onclose = function () {
window.location.reload();
}
}
if (data.type === 'error-resolved') {
console.log('[parcel] ✨ Error resolved');
}
if (data.type === 'error') {
console.error('[parcel] 🚨 ' + data.error.message + '\n' + 'data.error.stack');
}
};
}
function getParents(bundle, id) {
var modules = bundle.modules;
if (!modules) {
return [];
}
var parents = [];
var k, d, dep;
for (k in modules) {
for (d in modules[k][1]) {
dep = modules[k][1][d];
if (dep === id || (Array.isArray(dep) && dep[dep.length - 1] === id)) {
parents.push(+k);
}
}
}
if (bundle.parent) {
parents = parents.concat(getParents(bundle.parent, id));
}
return parents;
}
function hmrApply(bundle, asset) {
var modules = bundle.modules;
if (!modules) {
return;
}
if (modules[asset.id] || !bundle.parent) {
var fn = new Function('require', 'module', 'exports', asset.generated.js);
asset.isNew = !modules[asset.id];
modules[asset.id] = [fn, asset.deps];
} else if (bundle.parent) {
hmrApply(bundle.parent, asset);
}
}
function hmrAccept(bundle, id) {
var modules = bundle.modules;
if (!modules) {
return;
}
if (!modules[id] && bundle.parent) {
return hmrAccept(bundle.parent, id);
}
var cached = bundle.cache[id];
if (cached && cached.hot._disposeCallback) {
cached.hot._disposeCallback();
}
delete bundle.cache[id];
bundle(id);
cached = bundle.cache[id];
if (cached && cached.hot && cached.hot._acceptCallback) {
cached.hot._acceptCallback();
return true;
}
return getParents(global.require, id).some(function (id) {
return hmrAccept(global.require, id)
});
}