@atlaspack/runtime-js 2.12.1-canary.3607 → 2.12.1-canary.3609

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.
@@ -86,7 +86,7 @@ function _load() {
86
86
  attempt: i
87
87
  }
88
88
  };
89
- window.dispatchEvent(new CustomEvent('atlaspack:import_retry', event));
89
+ globalThis.dispatchEvent(new CustomEvent('atlaspack:import_retry', event));
90
90
  case 17:
91
91
  case "end":
92
92
  return _context.stop();
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
 
3
3
  var mapping = new Map();
4
- function register(baseUrl, manifest) {
4
+ function register( /** @type {string} */baseUrl, /** @type {Array<string>} */manifest // ['id', 'path', 'id2', 'path2']
5
+ ) {
5
6
  for (var i = 0; i < manifest.length - 1; i += 2) {
6
7
  mapping.set(manifest[i], {
7
8
  baseUrl: baseUrl,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/runtime-js",
3
- "version": "2.12.1-canary.3607+fc7589858",
3
+ "version": "2.12.1-canary.3609+4608bfa87",
4
4
  "license": "(MIT OR Apache-2.0)",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -12,16 +12,16 @@
12
12
  "main": "lib/JSRuntime.js",
13
13
  "source": "src/JSRuntime.js",
14
14
  "engines": {
15
- "atlaspack": "2.12.1-canary.3607+fc7589858",
15
+ "atlaspack": "2.12.1-canary.3609+4608bfa87",
16
16
  "node": ">= 16.0.0"
17
17
  },
18
18
  "dependencies": {
19
- "@atlaspack/diagnostic": "2.12.1-canary.3607+fc7589858",
20
- "@atlaspack/domain-sharding": "2.12.1-canary.3607+fc7589858",
21
- "@atlaspack/feature-flags": "2.12.1-canary.3607+fc7589858",
22
- "@atlaspack/plugin": "2.12.1-canary.3607+fc7589858",
23
- "@atlaspack/utils": "2.12.1-canary.3607+fc7589858",
19
+ "@atlaspack/diagnostic": "2.12.1-canary.3609+4608bfa87",
20
+ "@atlaspack/domain-sharding": "2.12.1-canary.3609+4608bfa87",
21
+ "@atlaspack/feature-flags": "2.12.1-canary.3609+4608bfa87",
22
+ "@atlaspack/plugin": "2.12.1-canary.3609+4608bfa87",
23
+ "@atlaspack/utils": "2.12.1-canary.3609+4608bfa87",
24
24
  "nullthrows": "^1.1.1"
25
25
  },
26
- "gitHead": "fc7589858cb8a7330b6ce2c389eb89917a143237"
26
+ "gitHead": "4608bfa87145cdd385b090b70b4d9a10e73334a3"
27
27
  }
@@ -47,7 +47,7 @@ async function load(id) {
47
47
  if (i === maxRetries) throw error;
48
48
  // Dispatch event for reporting
49
49
  const event = {detail: {target: url, attempt: i}};
50
- window.dispatchEvent(
50
+ globalThis.dispatchEvent(
51
51
  new CustomEvent('atlaspack:import_retry', event),
52
52
  );
53
53
  }
@@ -1,6 +1,9 @@
1
1
  var mapping = new Map();
2
2
 
3
- function register(baseUrl, manifest) {
3
+ function register(
4
+ /** @type {string} */ baseUrl,
5
+ /** @type {Array<string>} */ manifest, // ['id', 'path', 'id2', 'path2']
6
+ ) {
4
7
  for (var i = 0; i < manifest.length - 1; i += 2) {
5
8
  mapping.set(manifest[i], {
6
9
  baseUrl: baseUrl,
@@ -0,0 +1,65 @@
1
+ import load from '../src/helpers/browser/esm-js-loader-retry.js';
2
+ import bundleManifest from '../src/helpers/bundle-manifest.js';
3
+ import {mock} from 'node:test';
4
+ import type {Mock} from 'node:test';
5
+ import assert from 'node:assert';
6
+
7
+ declare var globalThis: Window & {[key: string]: any};
8
+
9
+ describe('esm-js-loader-retry', () => {
10
+ let mockSetTimeout: Mock<Window['setTimeout']>;
11
+ let mockParcelImport: Mock<() => Promise<void>>;
12
+
13
+ // eslint-disable-next-line require-await
14
+ const importError = async () => {
15
+ throw new Error('TypeError: Failed to fetch dynamically imported module');
16
+ };
17
+
18
+ before(() => {
19
+ bundleManifest.register('http://localhost', ['1', 'foo.js']);
20
+ });
21
+
22
+ beforeEach(() => {
23
+ mockSetTimeout = mock.fn((callback: any, duration: any, ...args: any[]) =>
24
+ callback(),
25
+ );
26
+ globalThis.setTimeout = mockSetTimeout;
27
+
28
+ mockParcelImport = mock.fn(() => Promise.resolve());
29
+ globalThis.__parcel__import__ = mockParcelImport;
30
+
31
+ globalThis.parcelRequire = mock.fn();
32
+ // @ts-expect-error
33
+ globalThis.navigator = {onLine: true};
34
+ globalThis.CustomEvent = globalThis.CustomEvent || class {};
35
+ globalThis.dispatchEvent = mock.fn();
36
+ });
37
+
38
+ it('should not throw', async () => {
39
+ await assert.doesNotReject(() => load('1'));
40
+ });
41
+
42
+ it('should throw if all requests fail', async () => {
43
+ mockParcelImport.mock.mockImplementationOnce(importError, 0);
44
+ mockParcelImport.mock.mockImplementationOnce(importError, 1);
45
+ mockParcelImport.mock.mockImplementationOnce(importError, 2);
46
+ mockParcelImport.mock.mockImplementationOnce(importError, 3);
47
+ mockParcelImport.mock.mockImplementationOnce(importError, 4);
48
+ mockParcelImport.mock.mockImplementationOnce(importError, 5);
49
+ mockParcelImport.mock.mockImplementationOnce(importError, 6);
50
+ await assert.rejects(() => load('1'));
51
+ });
52
+
53
+ it('should resolve if the first request fails', async () => {
54
+ mockParcelImport.mock.mockImplementationOnce(importError, 0);
55
+ await assert.doesNotReject(() => load('1'));
56
+ });
57
+
58
+ it('should resolve if the first few requests fails', async () => {
59
+ mockParcelImport.mock.mockImplementationOnce(importError, 0);
60
+ mockParcelImport.mock.mockImplementationOnce(importError, 1);
61
+ mockParcelImport.mock.mockImplementationOnce(importError, 2);
62
+ mockParcelImport.mock.mockImplementationOnce(importError, 3);
63
+ await assert.doesNotReject(() => load('1'));
64
+ });
65
+ });