@atlaspack/runtime-js 2.14.5-canary.48 → 2.14.5-canary.481

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.
Files changed (36) hide show
  1. package/CHANGELOG.md +580 -0
  2. package/dist/JSRuntime.js +963 -0
  3. package/lib/JSRuntime.js +425 -42
  4. package/lib/helpers/browser/analytics/analytics.d.js +1 -0
  5. package/lib/helpers/browser/css-loader.js +4 -3
  6. package/lib/helpers/browser/html-loader.js +1 -1
  7. package/lib/helpers/browser/import-polyfill.js +1 -1
  8. package/lib/helpers/browser/js-loader.js +4 -3
  9. package/lib/helpers/browser/prefetch-loader.js +1 -1
  10. package/lib/helpers/browser/preload-loader.js +1 -1
  11. package/lib/helpers/browser/sync-js-loader.js +32 -0
  12. package/lib/helpers/browser/wasm-loader.js +1 -1
  13. package/lib/helpers/bundle-manifest.js +1 -1
  14. package/lib/helpers/cacheLoader.js +1 -1
  15. package/lib/helpers/conditional-loader-dev.js +12 -3
  16. package/lib/helpers/conditional-loader.js +22 -2
  17. package/lib/helpers/get-worker-url.js +1 -1
  18. package/lib/helpers/node/css-loader.js +1 -1
  19. package/lib/helpers/node/html-loader.js +1 -1
  20. package/lib/helpers/node/js-loader.js +1 -1
  21. package/lib/helpers/node/wasm-loader.js +1 -1
  22. package/lib/helpers/worker/js-loader.js +1 -1
  23. package/lib/helpers/worker/wasm-loader.js +1 -1
  24. package/lib/types/JSRuntime.d.ts +9 -0
  25. package/package.json +15 -10
  26. package/src/{JSRuntime.js → JSRuntime.ts} +520 -113
  27. package/src/helpers/browser/preload-loader.js +1 -2
  28. package/src/helpers/browser/sync-js-loader.js +37 -0
  29. package/src/helpers/conditional-loader-dev.js +17 -3
  30. package/src/helpers/conditional-loader.js +24 -2
  31. package/test/analytics.test.ts +4 -1
  32. package/test/{bundle-url.test.js → bundle-url.test.ts} +1 -2
  33. package/test/esm-js-loader-retry.test.ts +4 -7
  34. package/tsconfig.json +27 -0
  35. package/tsconfig.tsbuildinfo +1 -0
  36. package/src/helpers/browser/analytics/analytics.js.flow +0 -10
@@ -0,0 +1 @@
1
+ "use strict";
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
 
3
3
  const cacheLoader = require('../cacheLoader');
4
- module.exports = cacheLoader(function (bundle) {
4
+ module.exports = cacheLoader(function loadCSSBundle(bundle) {
5
5
  return new Promise(function (resolve, reject) {
6
6
  // Don't insert the same link element twice (e.g. if it was already in the HTML)
7
7
  let existingLinks = document.getElementsByTagName('link');
8
- if ([].concat(existingLinks).some(function (link) {
8
+ let isCurrentBundle = function (link) {
9
9
  return link.href === bundle && link.rel.indexOf('stylesheet') > -1;
10
- })) {
10
+ };
11
+ if ([].concat(existingLinks).some(isCurrentBundle)) {
11
12
  resolve();
12
13
  return;
13
14
  }
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  const cacheLoader = require('../cacheLoader');
4
- module.exports = cacheLoader(function (bundle) {
4
+ module.exports = cacheLoader(function loadHTMLBundle(bundle) {
5
5
  return fetch(bundle).then(function (res) {
6
6
  return res.text();
7
7
  });
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  const cacheLoader = require('../cacheLoader');
4
- module.exports = cacheLoader(function (bundle) {
4
+ module.exports = cacheLoader(function importModule(bundle) {
5
5
  return new Promise((resolve, reject) => {
6
6
  // Add a global function to handle when the script loads.
7
7
  let globalName = `i${('' + Math.random()).slice(2)}`;
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
 
3
3
  const cacheLoader = require('../cacheLoader');
4
- module.exports = cacheLoader(function (bundle) {
4
+ module.exports = cacheLoader(function loadJSBundle(bundle) {
5
5
  return new Promise(function (resolve, reject) {
6
6
  // Don't insert the same script twice (e.g. if it was already in the HTML)
7
7
  let existingScripts = document.getElementsByTagName('script');
8
- if ([].concat(existingScripts).some(function (script) {
8
+ let isCurrentBundle = function (script) {
9
9
  return script.src === bundle;
10
- })) {
10
+ };
11
+ if ([].concat(existingScripts).some(isCurrentBundle)) {
11
12
  resolve();
12
13
  return;
13
14
  }
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  const cacheLoader = require('../cacheLoader');
4
- module.exports = cacheLoader(function (bundle, priority) {
4
+ module.exports = cacheLoader(function prefetchJSBundle(bundle, priority) {
5
5
  var link = document.createElement('link');
6
6
  link.rel = 'prefetch';
7
7
  link.href = bundle;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  const cacheLoader = require('../cacheLoader');
4
- module.exports = cacheLoader(function (bundle, priority, isModule) {
4
+ module.exports = cacheLoader(function preloadJSBundle(bundle, priority, isModule) {
5
5
  var link = document.createElement('link');
6
6
  link.charset = 'utf-8';
7
7
  link.rel = isModule ? 'modulepreload' : 'preload';
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ module.exports = function loadJSBundle(bundle) {
4
+ // Don't insert the same script twice (e.g. if it was already in the HTML)
5
+ let existingScripts = document.getElementsByTagName('script');
6
+ let isCurrentBundle = function (script) {
7
+ return script.src === bundle;
8
+ };
9
+ if ([].concat(existingScripts).some(isCurrentBundle)) {
10
+ return;
11
+ }
12
+
13
+ // Request using XHR because it's synchronous and we can't use promises here
14
+ // This has extremely poor performance because we're idle during this fetch, so we only use this so that the app won't crash
15
+ const xhr = new XMLHttpRequest();
16
+ xhr.open('GET', bundle, false);
17
+ try {
18
+ xhr.send();
19
+ if (xhr.status === 200) {
20
+ const script = document.createElement('script');
21
+ script.type = 'text/javascript';
22
+ script.text = xhr.responseText;
23
+
24
+ // Execute the script synchronously
25
+ document.head.appendChild(script);
26
+ } else {
27
+ throw new TypeError(`Failed to fetch dynamically imported module: ${bundle}. Status: ${xhr.status}`);
28
+ }
29
+ } catch (e) {
30
+ throw new TypeError(`Failed to fetch dynamically imported module: ${bundle}. Error: ${e.message}`);
31
+ }
32
+ };
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  const cacheLoader = require('../cacheLoader');
4
- module.exports = cacheLoader(function (bundle) {
4
+ module.exports = cacheLoader(function loadWASMBundle(bundle) {
5
5
  return fetch(bundle).then(function (res) {
6
6
  if (WebAssembly.instantiateStreaming) {
7
7
  return WebAssembly.instantiateStreaming(res);
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  var mapping = new Map();
4
- function register( /** @type {string} */baseUrl, /** @type {Array<string>} */manifest // ['id', 'path', 'id2', 'path2']
4
+ function register(/** @type {string} */baseUrl, /** @type {Array<string>} */manifest // ['id', 'path', 'id2', 'path2']
5
5
  ) {
6
6
  for (var i = 0; i < manifest.length - 1; i += 2) {
7
7
  mapping.set(manifest[i], {
@@ -13,7 +13,7 @@ function getCache(type) {
13
13
  return cachedBundles;
14
14
  }
15
15
  }
16
- module.exports = function (loader, type) {
16
+ module.exports = function cacheLoader(loader, type) {
17
17
  return function (bundle) {
18
18
  let cache = getCache(type);
19
19
  if (cache[bundle]) {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- module.exports = function (cond, ifTrue, ifFalse) {
3
+ module.exports = function loadCond(cond, ifTrue, ifFalse, fallback) {
4
4
  if (typeof globalThis.__MCOND !== 'function') {
5
5
  throw new TypeError('"globalThis.__MCOND" was not set to an object. Ensure the function is set to return the key condition for conditional bundles to load with.');
6
6
  }
@@ -10,7 +10,16 @@ module.exports = function (cond, ifTrue, ifFalse) {
10
10
  try {
11
11
  return globalThis.__MCOND(cond) ? ifTrue() : ifFalse();
12
12
  } catch (err) {
13
- console.error('Conditional dependency was missing. Ensure the server sends the correct scripts to the client ("conditional-manifest.json").');
14
- throw err;
13
+ console.error('Conditional dependency was not registered when executing. Ensure the server sends the correct scripts to the client. Falling back to synchronous bundle loading.');
14
+ if (fallback) {
15
+ globalThis.__ATLASPACK_ERRORS = globalThis.__ATLASPACK_ERRORS || [];
16
+ globalThis.__ATLASPACK_ERRORS.push(new Error(`Sync dependency fallback triggered for condition "${cond}": ${err.message}`));
17
+ for (const url of fallback.urls) {
18
+ fallback.l(url);
19
+ }
20
+ return globalThis.__MCOND(cond) ? ifTrue() : ifFalse();
21
+ } else {
22
+ throw new Error('No fallback urls specified, cannot fallback safely');
23
+ }
15
24
  }
16
25
  };
@@ -1,5 +1,25 @@
1
1
  "use strict";
2
2
 
3
- module.exports = function (cond, ifTrue, ifFalse) {
4
- return globalThis.__MCOND && globalThis.__MCOND(cond) ? ifTrue() : ifFalse();
3
+ const {
4
+ getBundleURL
5
+ } = require('./bundle-url');
6
+ const {
7
+ resolve
8
+ } = require('./bundle-manifest');
9
+ module.exports = function loadCond(cond, ifTrue, ifFalse, fallback) {
10
+ let result = globalThis.__MCOND(cond);
11
+ try {
12
+ return result ? ifTrue() : ifFalse();
13
+ } catch (err) {
14
+ if (fallback) {
15
+ globalThis.__ATLASPACK_ERRORS = globalThis.__ATLASPACK_ERRORS || [];
16
+ globalThis.__ATLASPACK_ERRORS.push(new Error(`Sync dependency fallback triggered for condition "${cond}": ${err.message}`));
17
+ for (const id of fallback.i) {
18
+ fallback.l(new URL(resolve(id), getBundleURL(id)).toString());
19
+ }
20
+ return result ? ifTrue() : ifFalse();
21
+ } else {
22
+ throw err;
23
+ }
24
+ }
5
25
  };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- module.exports = function (workerUrl, origin, isESM) {
3
+ module.exports = function loadWorker(workerUrl, origin, isESM) {
4
4
  if (origin === self.location.origin) {
5
5
  // If the worker bundle's url is on the same origin as the document,
6
6
  // use the worker bundle's own url.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
3
  // loading a CSS style is a no-op in Node.js
4
- module.exports = function () {
4
+ module.exports = function loadCSSBundle() {
5
5
  return Promise.resolve();
6
6
  };
@@ -2,7 +2,7 @@
2
2
 
3
3
  const fs = require('fs');
4
4
  const cacheLoader = require('../cacheLoader');
5
- module.exports = cacheLoader(function (bundle) {
5
+ module.exports = cacheLoader(function loadHTMLBundle(bundle) {
6
6
  return new Promise(function (resolve, reject) {
7
7
  fs.readFile(__dirname + bundle, 'utf8', function (err, data) {
8
8
  if (err) {
@@ -2,7 +2,7 @@
2
2
 
3
3
  const fs = require('fs');
4
4
  const cacheLoader = require('../cacheLoader');
5
- module.exports = cacheLoader(function (bundle) {
5
+ module.exports = cacheLoader(function loadJSBundle(bundle) {
6
6
  return new Promise(function (resolve, reject) {
7
7
  fs.readFile(__dirname + bundle, 'utf8', function (err, data) {
8
8
  if (err) {
@@ -2,7 +2,7 @@
2
2
 
3
3
  const fs = require('fs');
4
4
  const cacheLoader = require('../cacheLoader');
5
- module.exports = cacheLoader(function (bundle) {
5
+ module.exports = cacheLoader(function loadWASMBundle(bundle) {
6
6
  return new Promise(function (resolve, reject) {
7
7
  fs.readFile(__dirname + bundle, function (err, data) {
8
8
  if (err) {
@@ -2,7 +2,7 @@
2
2
 
3
3
  /* global __parcel__importScripts__:readonly*/
4
4
  const cacheLoader = require('../cacheLoader');
5
- module.exports = cacheLoader(function (bundle) {
5
+ module.exports = cacheLoader(function loadJSBundle(bundle) {
6
6
  return new Promise(function (resolve, reject) {
7
7
  try {
8
8
  __parcel__importScripts__(bundle);
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  const cacheLoader = require('../cacheLoader');
4
- module.exports = cacheLoader(function (bundle) {
4
+ module.exports = cacheLoader(function loadWASMBundle(bundle) {
5
5
  return fetch(bundle).then(function (res) {
6
6
  if (WebAssembly.instantiateStreaming) {
7
7
  return WebAssembly.instantiateStreaming(res);
@@ -0,0 +1,9 @@
1
+ import { Runtime } from '@atlaspack/plugin';
2
+ type JSRuntimeConfig = {
3
+ splitManifestThreshold: number;
4
+ domainSharding?: {
5
+ maxShards: number;
6
+ };
7
+ };
8
+ declare const _default: Runtime<JSRuntimeConfig>;
9
+ export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/runtime-js",
3
- "version": "2.14.5-canary.48+3af500201",
3
+ "version": "2.14.5-canary.481+a1d772935",
4
4
  "license": "(MIT OR Apache-2.0)",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -9,19 +9,24 @@
9
9
  "type": "git",
10
10
  "url": "https://github.com/atlassian-labs/atlaspack.git"
11
11
  },
12
- "main": "lib/JSRuntime.js",
13
- "source": "src/JSRuntime.js",
12
+ "main": "./lib/JSRuntime.js",
13
+ "source": "./src/JSRuntime.ts",
14
+ "types": "./lib/types/JSRuntime.d.ts",
14
15
  "engines": {
15
16
  "node": ">= 16.0.0"
16
17
  },
17
18
  "dependencies": {
18
- "@atlaspack/diagnostic": "2.14.1-canary.116+3af500201",
19
- "@atlaspack/domain-sharding": "2.14.1-canary.116+3af500201",
20
- "@atlaspack/feature-flags": "2.14.1-canary.116+3af500201",
21
- "@atlaspack/plugin": "2.14.5-canary.48+3af500201",
22
- "@atlaspack/utils": "2.14.5-canary.48+3af500201",
19
+ "@atlaspack/diagnostic": "2.14.1-canary.549+a1d772935",
20
+ "@atlaspack/domain-sharding": "2.14.1-canary.549+a1d772935",
21
+ "@atlaspack/feature-flags": "2.14.1-canary.549+a1d772935",
22
+ "@atlaspack/plugin": "2.14.5-canary.481+a1d772935",
23
+ "@atlaspack/types-internal": "2.14.1-canary.549+a1d772935",
24
+ "@atlaspack/utils": "2.14.5-canary.481+a1d772935",
23
25
  "nullthrows": "^1.1.1"
24
26
  },
25
27
  "type": "commonjs",
26
- "gitHead": "3af5002017ed237c1bcfed5c5cf3f0da5b3a9f92"
27
- }
28
+ "scripts": {
29
+ "build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
30
+ },
31
+ "gitHead": "a1d772935c9ed74c6d8eafbee6b65eb91ddc38fd"
32
+ }