UNPKG

5.31 kBJavaScriptView Raw
1var OVERLAY_ID = '__parcel__error__overlay__';
2
3var OldModule = module.bundle.Module;
4
5function Module(moduleName) {
6 OldModule.call(this, moduleName);
7 this.hot = {
8 data: module.bundle.hotData,
9 _acceptCallbacks: [],
10 _disposeCallbacks: [],
11 accept: function (fn) {
12 this._acceptCallbacks.push(fn || function () {});
13 },
14 dispose: function (fn) {
15 this._disposeCallbacks.push(fn);
16 }
17 };
18
19 module.bundle.hotData = null;
20}
21
22module.bundle.Module = Module;
23var checkedAssets, assetsToAccept;
24
25var parent = module.bundle.parent;
26if ((!parent || !parent.isParcelRequire) && typeof WebSocket !== 'undefined') {
27 var hostname = process.env.HMR_HOSTNAME || location.hostname;
28 var protocol = location.protocol === 'https:' ? 'wss' : 'ws';
29 var ws = new WebSocket(protocol + '://' + hostname + ':' + process.env.HMR_PORT + '/');
30 ws.onmessage = function(event) {
31 checkedAssets = {};
32 assetsToAccept = [];
33
34 var data = JSON.parse(event.data);
35
36 if (data.type === 'update') {
37 var handled = false;
38 data.assets.forEach(function(asset) {
39 if (!asset.isNew) {
40 var didAccept = hmrAcceptCheck(global.parcelRequire, asset.id);
41 if (didAccept) {
42 handled = true;
43 }
44 }
45 });
46
47 // Enable HMR for CSS by default.
48 handled = handled || data.assets.every(function(asset) {
49 return asset.type === 'css' && asset.generated.js;
50 });
51
52 if (handled) {
53 console.clear();
54
55 data.assets.forEach(function (asset) {
56 hmrApply(global.parcelRequire, asset);
57 });
58
59 assetsToAccept.forEach(function (v) {
60 hmrAcceptRun(v[0], v[1]);
61 });
62 } else {
63 window.location.reload();
64 }
65 }
66
67 if (data.type === 'reload') {
68 ws.close();
69 ws.onclose = function () {
70 location.reload();
71 }
72 }
73
74 if (data.type === 'error-resolved') {
75 console.log('[parcel] ✨ Error resolved');
76
77 removeErrorOverlay();
78 }
79
80 if (data.type === 'error') {
81 console.error('[parcel] 🚨 ' + data.error.message + '\n' + data.error.stack);
82
83 removeErrorOverlay();
84
85 var overlay = createErrorOverlay(data);
86 document.body.appendChild(overlay);
87 }
88 };
89}
90
91function removeErrorOverlay() {
92 var overlay = document.getElementById(OVERLAY_ID);
93 if (overlay) {
94 overlay.remove();
95 }
96}
97
98function createErrorOverlay(data) {
99 var overlay = document.createElement('div');
100 overlay.id = OVERLAY_ID;
101
102 // html encode message and stack trace
103 var message = document.createElement('div');
104 var stackTrace = document.createElement('pre');
105 message.innerText = data.error.message;
106 stackTrace.innerText = data.error.stack;
107
108 overlay.innerHTML = (
109 '<div style="background: black; font-size: 16px; color: white; position: fixed; height: 100%; width: 100%; top: 0px; left: 0px; padding: 30px; opacity: 0.85; font-family: Menlo, Consolas, monospace; z-index: 9999;">' +
110 '<span style="background: red; padding: 2px 4px; border-radius: 2px;">ERROR</span>' +
111 '<span style="top: 2px; margin-left: 5px; position: relative;">🚨</span>' +
112 '<div style="font-size: 18px; font-weight: bold; margin-top: 20px;">' + message.innerHTML + '</div>' +
113 '<pre>' + stackTrace.innerHTML + '</pre>' +
114 '</div>'
115 );
116
117 return overlay;
118
119}
120
121function getParents(bundle, id) {
122 var modules = bundle.modules;
123 if (!modules) {
124 return [];
125 }
126
127 var parents = [];
128 var k, d, dep;
129
130 for (k in modules) {
131 for (d in modules[k][1]) {
132 dep = modules[k][1][d];
133 if (dep === id || (Array.isArray(dep) && dep[dep.length - 1] === id)) {
134 parents.push(k);
135 }
136 }
137 }
138
139 if (bundle.parent) {
140 parents = parents.concat(getParents(bundle.parent, id));
141 }
142
143 return parents;
144}
145
146function hmrApply(bundle, asset) {
147 var modules = bundle.modules;
148 if (!modules) {
149 return;
150 }
151
152 if (modules[asset.id] || !bundle.parent) {
153 var fn = new Function('require', 'module', 'exports', asset.generated.js);
154 asset.isNew = !modules[asset.id];
155 modules[asset.id] = [fn, asset.deps];
156 } else if (bundle.parent) {
157 hmrApply(bundle.parent, asset);
158 }
159}
160
161function hmrAcceptCheck(bundle, id) {
162 var modules = bundle.modules;
163 if (!modules) {
164 return;
165 }
166
167 if (!modules[id] && bundle.parent) {
168 return hmrAcceptCheck(bundle.parent, id);
169 }
170
171 if (checkedAssets[id]) {
172 return;
173 }
174 checkedAssets[id] = true;
175
176 var cached = bundle.cache[id];
177
178 assetsToAccept.push([bundle, id]);
179
180 if (cached && cached.hot && cached.hot._acceptCallbacks.length) {
181 return true;
182 }
183
184 return getParents(global.parcelRequire, id).some(function (id) {
185 return hmrAcceptCheck(global.parcelRequire, id)
186 });
187}
188
189function hmrAcceptRun(bundle, id) {
190 var cached = bundle.cache[id];
191 bundle.hotData = {};
192 if (cached) {
193 cached.hot.data = bundle.hotData;
194 }
195
196 if (cached && cached.hot && cached.hot._disposeCallbacks.length) {
197 cached.hot._disposeCallbacks.forEach(function (cb) {
198 cb(bundle.hotData);
199 });
200 }
201
202 delete bundle.cache[id];
203 bundle(id);
204
205 cached = bundle.cache[id];
206 if (cached && cached.hot && cached.hot._acceptCallbacks.length) {
207 cached.hot._acceptCallbacks.forEach(function (cb) {
208 cb();
209 });
210 return true;
211 }
212}