@atlaspack/runtime-browser-hmr 2.14.30 → 2.14.32-dev-ts-project-refs-d30e9754f.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,588 @@
1
+ "use strict";
2
+ /* global HMR_HOST, HMR_PORT, HMR_ENV_HASH, HMR_SECURE, HMR_USE_SSE, chrome, browser, __parcel__import__, __parcel__importScripts__, ServiceWorkerGlobalScope */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ // /flow-to-ts helpers
5
+ var OVERLAY_ID = '__parcel__error__overlay__';
6
+ // @ts-expect-error TS2339
7
+ var OldModule = module.bundle.Module;
8
+ function Module(moduleName) {
9
+ // @ts-expect-error TS2683
10
+ OldModule.call(this, moduleName);
11
+ // @ts-expect-error TS2683
12
+ this.hot = {
13
+ // @ts-expect-error TS2339
14
+ data: module.bundle.hotData[moduleName],
15
+ _acceptCallbacks: [],
16
+ _disposeCallbacks: [],
17
+ // @ts-expect-error TS7006
18
+ accept: function (fn) {
19
+ this._acceptCallbacks.push(fn || function () { });
20
+ },
21
+ // @ts-expect-error TS7006
22
+ dispose: function (fn) {
23
+ this._disposeCallbacks.push(fn);
24
+ },
25
+ };
26
+ // @ts-expect-error TS2339
27
+ module.bundle.hotData[moduleName] = undefined;
28
+ }
29
+ // @ts-expect-error TS2339
30
+ module.bundle.Module = Module;
31
+ // @ts-expect-error TS2339
32
+ module.bundle.hotData = {};
33
+ // @ts-expect-error TS7034
34
+ var checkedAssets /*: {|[string]: boolean|} */,
35
+ // @ts-expect-error TS7034
36
+ disposedAssets /*: {|[string]: boolean|} */,
37
+ // @ts-expect-error TS7034
38
+ assetsToDispose /*: Array<[ParcelRequire, string]> */,
39
+ // @ts-expect-error TS7034
40
+ assetsToAccept /*: Array<[ParcelRequire, string]> */;
41
+ function getHostname() {
42
+ return (
43
+ // @ts-expect-error TS2304
44
+ HMR_HOST ||
45
+ (location.protocol.indexOf('http') === 0 ? location.hostname : 'localhost'));
46
+ }
47
+ function getPort() {
48
+ // @ts-expect-error TS2304
49
+ return HMR_PORT || location.port;
50
+ }
51
+ // eslint-disable-next-line no-redeclare
52
+ // @ts-expect-error TS2339
53
+ var parent = module.bundle.parent;
54
+ if ((!parent || !parent.isParcelRequire) && typeof WebSocket !== 'undefined') {
55
+ var hostname = getHostname();
56
+ var port = getPort();
57
+ var protocol =
58
+ // @ts-expect-error TS2304
59
+ HMR_SECURE ||
60
+ (location.protocol == 'https:' &&
61
+ !['localhost', '127.0.0.1', '0.0.0.0'].includes(hostname))
62
+ ? 'wss'
63
+ : 'ws';
64
+ var ws;
65
+ // @ts-expect-error TS2304
66
+ if (HMR_USE_SSE) {
67
+ ws = new EventSource('/__parcel_hmr');
68
+ }
69
+ else {
70
+ try {
71
+ ws = new WebSocket(protocol + '://' + hostname + (port ? ':' + port : '') + '/');
72
+ }
73
+ catch (err) {
74
+ if (err.message) {
75
+ console.error(err.message);
76
+ }
77
+ ws = {};
78
+ }
79
+ }
80
+ // Web extension context
81
+ var extCtx =
82
+ // @ts-expect-error TS2304
83
+ typeof browser === 'undefined'
84
+ ? // @ts-expect-error TS2304
85
+ typeof chrome === 'undefined'
86
+ ? null
87
+ : // @ts-expect-error TS2304
88
+ chrome
89
+ : // @ts-expect-error TS2304
90
+ browser;
91
+ // Safari doesn't support sourceURL in error stacks.
92
+ // eval may also be disabled via CSP, so do a quick check.
93
+ var supportsSourceURL = false;
94
+ try {
95
+ (0, eval)('throw new Error("test"); //# sourceURL=test.js');
96
+ }
97
+ catch (err) {
98
+ supportsSourceURL = err.stack.includes('test.js');
99
+ }
100
+ // @ts-expect-error TS2339
101
+ ws.onmessage = async function (event /*: {data: string, ...} */) {
102
+ checkedAssets = {} /*: {|[string]: boolean|} */;
103
+ disposedAssets = {} /*: {|[string]: boolean|} */;
104
+ assetsToAccept = [];
105
+ assetsToDispose = [];
106
+ var data /*: HMRMessage */ = JSON.parse(event.data);
107
+ if (data.type === 'reload') {
108
+ fullReload();
109
+ }
110
+ else if (data.type === 'update') {
111
+ // Remove error overlay if there is one
112
+ if (typeof document !== 'undefined') {
113
+ removeErrorOverlay();
114
+ }
115
+ let assets = data.assets.filter(
116
+ // @ts-expect-error TS7006
117
+ (asset) => asset.envHash === HMR_ENV_HASH);
118
+ // Handle HMR Update
119
+ // @ts-expect-error TS7006
120
+ let handled = assets.every((asset) => {
121
+ return (asset.type === 'css' ||
122
+ (asset.type === 'js' &&
123
+ // @ts-expect-error TS2339
124
+ hmrAcceptCheck(module.bundle.root, asset.id, asset.depsByBundle)));
125
+ });
126
+ if (handled) {
127
+ console.clear();
128
+ // Dispatch custom event so other runtimes (e.g React Refresh) are aware.
129
+ if (typeof window !== 'undefined' &&
130
+ typeof CustomEvent !== 'undefined') {
131
+ window.dispatchEvent(new CustomEvent('parcelhmraccept'));
132
+ }
133
+ await hmrApplyUpdates(assets);
134
+ hmrDisposeQueue();
135
+ // Run accept callbacks. This will also re-execute other disposed assets in topological order.
136
+ let processedAssets = {};
137
+ for (let i = 0; i < assetsToAccept.length; i++) {
138
+ // @ts-expect-error TS7005
139
+ let id = assetsToAccept[i][1];
140
+ if (!processedAssets[id]) {
141
+ // @ts-expect-error TS7005
142
+ hmrAccept(assetsToAccept[i][0], id);
143
+ processedAssets[id] = true;
144
+ }
145
+ }
146
+ }
147
+ else
148
+ fullReload();
149
+ }
150
+ if (data.type === 'error') {
151
+ // Log parcel errors to console
152
+ for (let ansiDiagnostic of data.diagnostics.ansi) {
153
+ let stack = ansiDiagnostic.codeframe
154
+ ? ansiDiagnostic.codeframe
155
+ : ansiDiagnostic.stack;
156
+ console.error('🚨 [parcel]: ' +
157
+ ansiDiagnostic.message +
158
+ '\n' +
159
+ stack +
160
+ '\n\n' +
161
+ ansiDiagnostic.hints.join('\n'));
162
+ }
163
+ if (typeof document !== 'undefined') {
164
+ // Render the fancy html overlay
165
+ removeErrorOverlay();
166
+ var overlay = createErrorOverlay(data.diagnostics.html);
167
+ document.body.appendChild(overlay);
168
+ }
169
+ }
170
+ };
171
+ if (ws instanceof WebSocket) {
172
+ ws.onerror = function (e) {
173
+ if (e.message) {
174
+ console.error(e.message);
175
+ }
176
+ };
177
+ ws.onclose = function (e) {
178
+ if (process.env.ATLASPACK_BUILD_ENV !== 'test') {
179
+ console.warn('[parcel] 🚨 Connection to the HMR server was lost');
180
+ }
181
+ };
182
+ }
183
+ }
184
+ function removeErrorOverlay() {
185
+ var overlay = document.getElementById(OVERLAY_ID);
186
+ if (overlay) {
187
+ overlay.remove();
188
+ console.log('[parcel] ✨ Error resolved');
189
+ }
190
+ }
191
+ function createErrorOverlay(diagnostics) {
192
+ var overlay = document.createElement('div');
193
+ overlay.id = OVERLAY_ID;
194
+ let errorHTML = '<div style="background: black; opacity: 0.85; font-size: 16px; color: white; position: fixed; height: 100%; width: 100%; top: 0px; left: 0px; padding: 30px; font-family: Menlo, Consolas, monospace; z-index: 9999;">';
195
+ for (let diagnostic of diagnostics) {
196
+ let stack = diagnostic.frames.length
197
+ ? // @ts-expect-error TS7006
198
+ diagnostic.frames.reduce((p, frame) => {
199
+ return `${p}
200
+ <a href="/__parcel_launch_editor?file=${encodeURIComponent(frame.location)}" style="text-decoration: underline; color: #888" onclick="fetch(this.href); return false">${frame.location}</a>
201
+ ${frame.code}`;
202
+ }, '')
203
+ : diagnostic.stack;
204
+ errorHTML += `
205
+ <div>
206
+ <div style="font-size: 18px; font-weight: bold; margin-top: 20px;">
207
+ 🚨 ${diagnostic.message}
208
+ </div>
209
+ <pre>${stack}</pre>
210
+ <div>
211
+ ${diagnostic.hints
212
+ // @ts-expect-error TS7006
213
+ .map((hint) => '<div>💡 ' + hint + '</div>')
214
+ .join('')}
215
+ </div>
216
+ ${diagnostic.documentation
217
+ ? `<div>📝 <a style="color: violet" href="${diagnostic.documentation}" target="_blank">Learn more</a></div>`
218
+ : ''}
219
+ </div>
220
+ `;
221
+ }
222
+ errorHTML += '</div>';
223
+ overlay.innerHTML = errorHTML;
224
+ return overlay;
225
+ }
226
+ function fullReload() {
227
+ if ('reload' in location) {
228
+ location.reload();
229
+ }
230
+ else if (extCtx && extCtx.runtime && extCtx.runtime.reload) {
231
+ extCtx.runtime.reload();
232
+ }
233
+ }
234
+ function getParents(
235
+ // @ts-expect-error TS2304
236
+ bundle, id) {
237
+ var modules = bundle.modules;
238
+ if (!modules) {
239
+ return [];
240
+ }
241
+ // @ts-expect-error TS2304
242
+ var parents = [];
243
+ var k, d, dep;
244
+ for (k in modules) {
245
+ for (d in modules[k][1]) {
246
+ dep = modules[k][1][d];
247
+ if (dep === id || (Array.isArray(dep) && dep[dep.length - 1] === id)) {
248
+ parents.push([bundle, k]);
249
+ }
250
+ }
251
+ }
252
+ if (bundle.parent) {
253
+ parents = parents.concat(getParents(bundle.parent, id));
254
+ }
255
+ return parents;
256
+ }
257
+ function updateLink(link) {
258
+ var href = link.getAttribute('href');
259
+ if (!href) {
260
+ return;
261
+ }
262
+ var newLink = link.cloneNode();
263
+ // @ts-expect-error TS2345
264
+ newLink.onload = function () {
265
+ if (link.parentNode !== null) {
266
+ link.parentNode.removeChild(link);
267
+ }
268
+ };
269
+ // @ts-expect-error TS2339
270
+ newLink.setAttribute('href', href.split('?')[0] + '?' + Date.now());
271
+ // @ts-expect-error TS18047
272
+ link.parentNode.insertBefore(newLink, link.nextSibling);
273
+ }
274
+ // @ts-expect-error TS7034
275
+ var cssTimeout = null;
276
+ function reloadCSS() {
277
+ // @ts-expect-error TS7005
278
+ if (cssTimeout) {
279
+ return;
280
+ }
281
+ cssTimeout = setTimeout(function () {
282
+ var document = window.document;
283
+ var links = document.querySelectorAll('link[rel="stylesheet"]');
284
+ for (var i = 0; i < links.length; i++) {
285
+ var href /*: string */ = links[i].getAttribute('href');
286
+ var hostname = getHostname();
287
+ var servedFromHMRServer = hostname === 'localhost'
288
+ ? new RegExp('^(https?:\\/\\/(0.0.0.0|127.0.0.1)|localhost):' + getPort()).test(href)
289
+ : // @ts-expect-error TS2345
290
+ href.indexOf(hostname + ':' + getPort());
291
+ var absolute =
292
+ // @ts-expect-error TS2345
293
+ /^https?:\/\//i.test(href) &&
294
+ // @ts-expect-error TS18047
295
+ href.indexOf(location.origin) !== 0 &&
296
+ !servedFromHMRServer;
297
+ if (!absolute) {
298
+ // @ts-expect-error TS2345
299
+ updateLink(links[i]);
300
+ }
301
+ }
302
+ cssTimeout = null;
303
+ }, 50);
304
+ }
305
+ // @ts-expect-error TS2304
306
+ function hmrDownload(asset) {
307
+ if (asset.type === 'js') {
308
+ if (typeof document !== 'undefined') {
309
+ let script = document.createElement('script');
310
+ script.src = asset.url + '?t=' + Date.now();
311
+ if (asset.outputFormat === 'esmodule') {
312
+ script.type = 'module';
313
+ }
314
+ return new Promise((resolve, reject) => {
315
+ script.onload = () => resolve(script);
316
+ script.onerror = reject;
317
+ document.head?.appendChild(script);
318
+ });
319
+ // @ts-expect-error TS2304
320
+ }
321
+ else if (typeof importScripts === 'function') {
322
+ // Worker scripts
323
+ if (asset.outputFormat === 'esmodule') {
324
+ // @ts-expect-error TS2304
325
+ return __parcel__import__(asset.url + '?t=' + Date.now());
326
+ }
327
+ else {
328
+ return new Promise((resolve, reject) => {
329
+ try {
330
+ // @ts-expect-error TS2304
331
+ __parcel__importScripts__(asset.url + '?t=' + Date.now());
332
+ // @ts-expect-error TS2794
333
+ resolve();
334
+ }
335
+ catch (err) {
336
+ reject(err);
337
+ }
338
+ });
339
+ }
340
+ }
341
+ }
342
+ }
343
+ // @ts-expect-error TS2304
344
+ async function hmrApplyUpdates(assets) {
345
+ // @ts-expect-error TS7017
346
+ global.parcelHotUpdate = Object.create(null);
347
+ let scriptsToRemove;
348
+ try {
349
+ // If sourceURL comments aren't supported in eval, we need to load
350
+ // the update from the dev server over HTTP so that stack traces
351
+ // are correct in errors/logs. This is much slower than eval, so
352
+ // we only do it if needed (currently just Safari).
353
+ // https://bugs.webkit.org/show_bug.cgi?id=137297
354
+ // This path is also taken if a CSP disallows eval.
355
+ if (!supportsSourceURL) {
356
+ let promises = assets.map((asset) => hmrDownload(asset)?.catch((err) => {
357
+ // Web extension fix
358
+ if (extCtx &&
359
+ extCtx.runtime &&
360
+ extCtx.runtime.getManifest().manifest_version == 3 &&
361
+ // @ts-expect-error TS2304
362
+ typeof ServiceWorkerGlobalScope != 'undefined' &&
363
+ // @ts-expect-error TS2304
364
+ global instanceof ServiceWorkerGlobalScope) {
365
+ extCtx.runtime.reload();
366
+ return;
367
+ }
368
+ throw err;
369
+ }));
370
+ scriptsToRemove = await Promise.all(promises);
371
+ }
372
+ assets.forEach(function (asset) {
373
+ // @ts-expect-error TS2339
374
+ hmrApply(module.bundle.root, asset);
375
+ });
376
+ }
377
+ finally {
378
+ // @ts-expect-error TS7017
379
+ delete global.parcelHotUpdate;
380
+ if (scriptsToRemove) {
381
+ // @ts-expect-error TS7006
382
+ scriptsToRemove.forEach((script) => {
383
+ if (script) {
384
+ document.head?.removeChild(script);
385
+ }
386
+ });
387
+ }
388
+ }
389
+ }
390
+ function hmrApply(
391
+ // @ts-expect-error TS2304
392
+ bundle /*: ParcelRequire */,
393
+ // @ts-expect-error TS2304
394
+ asset /*: HMRAsset */) {
395
+ var modules = bundle.modules;
396
+ if (!modules) {
397
+ return;
398
+ }
399
+ if (asset.type === 'css') {
400
+ reloadCSS();
401
+ }
402
+ else if (asset.type === 'js') {
403
+ let deps = asset.depsByBundle[bundle.HMR_BUNDLE_ID];
404
+ if (deps) {
405
+ if (modules[asset.id]) {
406
+ // Remove dependencies that are removed and will become orphaned.
407
+ // This is necessary so that if the asset is added back again, the cache is gone, and we prevent a full page reload.
408
+ let oldDeps = modules[asset.id][1];
409
+ for (let dep in oldDeps) {
410
+ if (!deps[dep] || deps[dep] !== oldDeps[dep]) {
411
+ let id = oldDeps[dep];
412
+ // @ts-expect-error TS2339
413
+ let parents = getParents(module.bundle.root, id);
414
+ if (parents.length === 1) {
415
+ // @ts-expect-error TS2339
416
+ hmrDelete(module.bundle.root, id);
417
+ }
418
+ }
419
+ }
420
+ }
421
+ if (supportsSourceURL) {
422
+ // Global eval. We would use `new Function` here but browser
423
+ // support for source maps is better with eval.
424
+ (0, eval)(asset.output);
425
+ }
426
+ // @ts-expect-error TS7017
427
+ let fn = global.parcelHotUpdate[asset.id];
428
+ modules[asset.id] = [fn, deps];
429
+ }
430
+ // Always traverse to the parent bundle, even if we already replaced the asset in this bundle.
431
+ // This is required in case modules are duplicated. We need to ensure all instances have the updated code.
432
+ if (bundle.parent) {
433
+ hmrApply(bundle.parent, asset);
434
+ }
435
+ }
436
+ }
437
+ // @ts-expect-error TS2304
438
+ function hmrDelete(bundle, id) {
439
+ let modules = bundle.modules;
440
+ if (!modules) {
441
+ return;
442
+ }
443
+ if (modules[id]) {
444
+ // Collect dependencies that will become orphaned when this module is deleted.
445
+ let deps = modules[id][1];
446
+ let orphans = [];
447
+ for (let dep in deps) {
448
+ // @ts-expect-error TS2339
449
+ let parents = getParents(module.bundle.root, deps[dep]);
450
+ if (parents.length === 1) {
451
+ orphans.push(deps[dep]);
452
+ }
453
+ }
454
+ // Delete the module. This must be done before deleting dependencies in case of circular dependencies.
455
+ delete modules[id];
456
+ delete bundle.cache[id];
457
+ // Now delete the orphans.
458
+ orphans.forEach((id) => {
459
+ // @ts-expect-error TS2339
460
+ hmrDelete(module.bundle.root, id);
461
+ });
462
+ }
463
+ else if (bundle.parent) {
464
+ hmrDelete(bundle.parent, id);
465
+ }
466
+ }
467
+ function hmrAcceptCheck(
468
+ // @ts-expect-error TS2304
469
+ bundle /*: ParcelRequire */, id /*: string */, depsByBundle /*: ?{ [string]: { [string]: string } }*/) {
470
+ if (hmrAcceptCheckOne(bundle, id, depsByBundle)) {
471
+ return true;
472
+ }
473
+ // Traverse parents breadth first. All possible ancestries must accept the HMR update, or we'll reload.
474
+ // @ts-expect-error TS2339
475
+ let parents = getParents(module.bundle.root, id);
476
+ let accepted = false;
477
+ while (parents.length > 0) {
478
+ let v = parents.shift();
479
+ // @ts-expect-error TS18048
480
+ let a = hmrAcceptCheckOne(v[0], v[1], null);
481
+ if (a) {
482
+ // If this parent accepts, stop traversing upward, but still consider siblings.
483
+ accepted = true;
484
+ }
485
+ else {
486
+ // Otherwise, queue the parents in the next level upward.
487
+ // @ts-expect-error TS2339
488
+ let p = getParents(module.bundle.root, v[1]);
489
+ if (p.length === 0) {
490
+ // If there are no parents, then we've reached an entry without accepting. Reload.
491
+ accepted = false;
492
+ break;
493
+ }
494
+ parents.push(...p);
495
+ }
496
+ }
497
+ return accepted;
498
+ }
499
+ function hmrAcceptCheckOne(
500
+ // @ts-expect-error TS2304
501
+ bundle /*: ParcelRequire */, id /*: string */, depsByBundle /*: ?{ [string]: { [string]: string } }*/) {
502
+ var modules = bundle.modules;
503
+ if (!modules) {
504
+ return;
505
+ }
506
+ if (depsByBundle && !depsByBundle[bundle.HMR_BUNDLE_ID]) {
507
+ // If we reached the root bundle without finding where the asset should go,
508
+ // there's nothing to do. Mark as "accepted" so we don't reload the page.
509
+ if (!bundle.parent) {
510
+ return true;
511
+ }
512
+ return hmrAcceptCheck(bundle.parent, id, depsByBundle);
513
+ }
514
+ // @ts-expect-error TS7005
515
+ if (checkedAssets[id]) {
516
+ return true;
517
+ }
518
+ // @ts-expect-error TS7005
519
+ checkedAssets[id] = true;
520
+ var cached = bundle.cache[id];
521
+ assetsToDispose.push([bundle, id]);
522
+ if (!cached || (cached.hot && cached.hot._acceptCallbacks.length)) {
523
+ assetsToAccept.push([bundle, id]);
524
+ return true;
525
+ }
526
+ }
527
+ function hmrDisposeQueue() {
528
+ // Dispose all old assets.
529
+ for (let i = 0; i < assetsToDispose.length; i++) {
530
+ // @ts-expect-error TS7005
531
+ let id = assetsToDispose[i][1];
532
+ // @ts-expect-error TS7005
533
+ if (!disposedAssets[id]) {
534
+ // @ts-expect-error TS7005
535
+ hmrDispose(assetsToDispose[i][0], id);
536
+ disposedAssets[id] = true;
537
+ }
538
+ }
539
+ assetsToDispose = [];
540
+ }
541
+ function hmrDispose(
542
+ // @ts-expect-error TS2304
543
+ bundle /*: ParcelRequire */, id /*: string */) {
544
+ var cached = bundle.cache[id];
545
+ bundle.hotData[id] = {};
546
+ if (cached && cached.hot) {
547
+ cached.hot.data = bundle.hotData[id];
548
+ }
549
+ if (cached && cached.hot && cached.hot._disposeCallbacks.length) {
550
+ // @ts-expect-error TS7006
551
+ cached.hot._disposeCallbacks.forEach(function (cb) {
552
+ cb(bundle.hotData[id]);
553
+ });
554
+ }
555
+ delete bundle.cache[id];
556
+ }
557
+ function hmrAccept(
558
+ // @ts-expect-error TS2304
559
+ bundle /*: ParcelRequire */, id /*: string */) {
560
+ // Execute the module.
561
+ bundle(id);
562
+ // Run the accept callbacks in the new version of the module.
563
+ var cached = bundle.cache[id];
564
+ if (cached && cached.hot && cached.hot._acceptCallbacks.length) {
565
+ let assetsToAlsoAccept = [];
566
+ // @ts-expect-error TS7006
567
+ cached.hot._acceptCallbacks.forEach(function (cb) {
568
+ let additionalAssets = cb(function () {
569
+ // @ts-expect-error TS2339
570
+ return getParents(module.bundle.root, id);
571
+ });
572
+ if (Array.isArray(additionalAssets) && additionalAssets.length) {
573
+ // @ts-expect-error TS2345
574
+ assetsToAlsoAccept.push(...additionalAssets);
575
+ }
576
+ });
577
+ if (assetsToAlsoAccept.length > 0) {
578
+ let handled = assetsToAlsoAccept.every(function (a) {
579
+ // @ts-expect-error TS2554
580
+ return hmrAcceptCheck(a[0], a[1]);
581
+ });
582
+ if (!handled) {
583
+ return fullReload();
584
+ }
585
+ hmrDisposeQueue();
586
+ }
587
+ }
588
+ }