@atlaspack/runtime-js 2.12.1-canary.3354
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.
- package/LICENSE +201 -0
- package/lib/JSRuntime.js +562 -0
- package/lib/helpers/browser/css-loader.js +28 -0
- package/lib/helpers/browser/esm-js-loader-retry.js +51 -0
- package/lib/helpers/browser/esm-js-loader.js +7 -0
- package/lib/helpers/browser/html-loader.js +8 -0
- package/lib/helpers/browser/import-polyfill.js +32 -0
- package/lib/helpers/browser/js-loader.js +35 -0
- package/lib/helpers/browser/prefetch-loader.js +13 -0
- package/lib/helpers/browser/preload-loader.js +14 -0
- package/lib/helpers/browser/wasm-loader.js +16 -0
- package/lib/helpers/bundle-manifest.js +20 -0
- package/lib/helpers/bundle-url.js +39 -0
- package/lib/helpers/cacheLoader.js +27 -0
- package/lib/helpers/get-worker-url.js +15 -0
- package/lib/helpers/node/css-loader.js +6 -0
- package/lib/helpers/node/html-loader.js +19 -0
- package/lib/helpers/node/js-loader.js +21 -0
- package/lib/helpers/node/wasm-loader.js +19 -0
- package/lib/helpers/worker/js-loader.js +14 -0
- package/lib/helpers/worker/wasm-loader.js +16 -0
- package/package.json +25 -0
- package/src/JSRuntime.js +742 -0
- package/src/helpers/.babelrc +9 -0
- package/src/helpers/.eslintrc.json +3 -0
- package/src/helpers/browser/css-loader.js +32 -0
- package/src/helpers/browser/esm-js-loader-retry.js +26 -0
- package/src/helpers/browser/esm-js-loader.js +6 -0
- package/src/helpers/browser/html-loader.js +7 -0
- package/src/helpers/browser/import-polyfill.js +32 -0
- package/src/helpers/browser/js-loader.js +42 -0
- package/src/helpers/browser/prefetch-loader.js +13 -0
- package/src/helpers/browser/preload-loader.js +19 -0
- package/src/helpers/browser/wasm-loader.js +17 -0
- package/src/helpers/bundle-manifest.js +21 -0
- package/src/helpers/bundle-url.js +51 -0
- package/src/helpers/cacheLoader.js +29 -0
- package/src/helpers/get-worker-url.js +15 -0
- package/src/helpers/node/css-loader.js +4 -0
- package/src/helpers/node/html-loader.js +18 -0
- package/src/helpers/node/js-loader.js +20 -0
- package/src/helpers/node/wasm-loader.js +20 -0
- package/src/helpers/worker/js-loader.js +13 -0
- package/src/helpers/worker/wasm-loader.js +17 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const cacheLoader = require('../cacheLoader');
|
|
2
|
+
|
|
3
|
+
module.exports = cacheLoader(function loadCSSBundle(bundle) {
|
|
4
|
+
return new Promise(function (resolve, reject) {
|
|
5
|
+
// Don't insert the same link element twice (e.g. if it was already in the HTML)
|
|
6
|
+
let existingLinks = document.getElementsByTagName('link');
|
|
7
|
+
let isCurrentBundle = function (link) {
|
|
8
|
+
return link.href === bundle && link.rel.indexOf('stylesheet') > -1;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
if ([].concat(existingLinks).some(isCurrentBundle)) {
|
|
12
|
+
resolve();
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
var link = document.createElement('link');
|
|
17
|
+
link.rel = 'stylesheet';
|
|
18
|
+
link.href = bundle;
|
|
19
|
+
link.onerror = function (e) {
|
|
20
|
+
link.onerror = link.onload = null;
|
|
21
|
+
link.remove();
|
|
22
|
+
reject(e);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
link.onload = function () {
|
|
26
|
+
link.onerror = link.onload = null;
|
|
27
|
+
resolve();
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
document.getElementsByTagName('head')[0].appendChild(link);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
async function load(id) {
|
|
2
|
+
if (!atlaspackRequire.retryState) {
|
|
3
|
+
atlaspackRequire.retryState = {};
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (!globalThis.navigator.onLine) {
|
|
7
|
+
await new Promise(res =>
|
|
8
|
+
globalThis.addEventListener('online', res, {once: true}),
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let url = require('../bundle-manifest').resolve(id);
|
|
13
|
+
|
|
14
|
+
if (atlaspackRequire.retryState[id] != undefined) {
|
|
15
|
+
url = `${url}?retry=${atlaspackRequire.retryState[id]}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
// eslint-disable-next-line no-undef
|
|
20
|
+
return await __atlaspack__import__(url);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
atlaspackRequire.retryState[id] = Date.now();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = load;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const cacheLoader = require('../cacheLoader');
|
|
2
|
+
|
|
3
|
+
module.exports = cacheLoader(function importModule(bundle) {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
// Add a global function to handle when the script loads.
|
|
6
|
+
let globalName = `i${('' + Math.random()).slice(2)}`;
|
|
7
|
+
global[globalName] = m => {
|
|
8
|
+
resolve(m);
|
|
9
|
+
cleanup();
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// Remove script on load or error
|
|
13
|
+
let cleanup = () => {
|
|
14
|
+
delete global[globalName];
|
|
15
|
+
script.onerror = null;
|
|
16
|
+
script.remove();
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Append an inline script tag into the document head
|
|
20
|
+
let script = document.createElement('script');
|
|
21
|
+
script.async = true;
|
|
22
|
+
script.type = 'module';
|
|
23
|
+
script.charset = 'utf-8';
|
|
24
|
+
script.textContent = `import * as m from '${bundle}'; ${globalName}(m);`;
|
|
25
|
+
script.onerror = function (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
cleanup();
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
document.head.appendChild(script);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const cacheLoader = require('../cacheLoader');
|
|
2
|
+
|
|
3
|
+
module.exports = cacheLoader(function loadJSBundle(bundle) {
|
|
4
|
+
return new Promise(function (resolve, reject) {
|
|
5
|
+
// Don't insert the same script twice (e.g. if it was already in the HTML)
|
|
6
|
+
let existingScripts = document.getElementsByTagName('script');
|
|
7
|
+
let isCurrentBundle = function (script) {
|
|
8
|
+
return script.src === bundle;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
if ([].concat(existingScripts).some(isCurrentBundle)) {
|
|
12
|
+
resolve();
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
var preloadLink = document.createElement('link');
|
|
17
|
+
preloadLink.href = bundle;
|
|
18
|
+
preloadLink.rel = 'preload';
|
|
19
|
+
preloadLink.as = 'script';
|
|
20
|
+
document.head.appendChild(preloadLink);
|
|
21
|
+
|
|
22
|
+
var script = document.createElement('script');
|
|
23
|
+
script.async = true;
|
|
24
|
+
script.type = 'text/javascript';
|
|
25
|
+
script.src = bundle;
|
|
26
|
+
script.onerror = function (e) {
|
|
27
|
+
var error = new TypeError(
|
|
28
|
+
`Failed to fetch dynamically imported module: ${bundle}. Error: ${e.message}`,
|
|
29
|
+
);
|
|
30
|
+
script.onerror = script.onload = null;
|
|
31
|
+
script.remove();
|
|
32
|
+
reject(error);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
script.onload = function () {
|
|
36
|
+
script.onerror = script.onload = null;
|
|
37
|
+
resolve();
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
document.getElementsByTagName('head')[0].appendChild(script);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const cacheLoader = require('../cacheLoader');
|
|
2
|
+
|
|
3
|
+
module.exports = cacheLoader(function prefetchJSBundle(bundle, priority) {
|
|
4
|
+
var link = document.createElement('link');
|
|
5
|
+
link.rel = 'prefetch';
|
|
6
|
+
link.href = bundle;
|
|
7
|
+
if (priority) {
|
|
8
|
+
link.as = priority;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
document.getElementsByTagName('head')[0].appendChild(link);
|
|
12
|
+
return Promise.resolve();
|
|
13
|
+
}, 'prefetch');
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const cacheLoader = require('../cacheLoader');
|
|
2
|
+
|
|
3
|
+
module.exports = cacheLoader(function preloadJSBundle(
|
|
4
|
+
bundle,
|
|
5
|
+
priority,
|
|
6
|
+
isModule,
|
|
7
|
+
) {
|
|
8
|
+
var link = document.createElement('link');
|
|
9
|
+
link.charset = 'utf-8';
|
|
10
|
+
link.rel = isModule ? 'modulepreload' : 'preload';
|
|
11
|
+
link.href = bundle;
|
|
12
|
+
if (priority) {
|
|
13
|
+
link.as = priority;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
document.getElementsByTagName('head')[0].appendChild(link);
|
|
17
|
+
return Promise.resolve();
|
|
18
|
+
},
|
|
19
|
+
'preload');
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const cacheLoader = require('../cacheLoader');
|
|
2
|
+
|
|
3
|
+
module.exports = cacheLoader(function loadWASMBundle(bundle) {
|
|
4
|
+
return fetch(bundle)
|
|
5
|
+
.then(function (res) {
|
|
6
|
+
if (WebAssembly.instantiateStreaming) {
|
|
7
|
+
return WebAssembly.instantiateStreaming(res);
|
|
8
|
+
} else {
|
|
9
|
+
return res.arrayBuffer().then(function (data) {
|
|
10
|
+
return WebAssembly.instantiate(data);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
.then(function (wasmModule) {
|
|
15
|
+
return wasmModule.instance.exports;
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
var mapping = new Map();
|
|
2
|
+
|
|
3
|
+
function register(baseUrl, manifest) {
|
|
4
|
+
for (var i = 0; i < manifest.length - 1; i += 2) {
|
|
5
|
+
mapping.set(manifest[i], {
|
|
6
|
+
baseUrl: baseUrl,
|
|
7
|
+
path: manifest[i + 1],
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function resolve(id) {
|
|
13
|
+
var resolved = mapping.get(id);
|
|
14
|
+
if (resolved == null) {
|
|
15
|
+
throw new Error('Could not resolve bundle with id ' + id);
|
|
16
|
+
}
|
|
17
|
+
return new URL(resolved.path, resolved.baseUrl).toString();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports.register = register;
|
|
21
|
+
module.exports.resolve = resolve;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var bundleURL = {};
|
|
2
|
+
function getBundleURLCached(id) {
|
|
3
|
+
var value = bundleURL[id];
|
|
4
|
+
if (!value) {
|
|
5
|
+
value = getBundleURL();
|
|
6
|
+
bundleURL[id] = value;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function getBundleURL() {
|
|
13
|
+
try {
|
|
14
|
+
throw new Error();
|
|
15
|
+
} catch (err) {
|
|
16
|
+
var matches = ('' + err.stack).match(
|
|
17
|
+
/(https?|file|ftp|(chrome|moz|safari-web)-extension):\/\/[^)\n]+/g,
|
|
18
|
+
);
|
|
19
|
+
if (matches) {
|
|
20
|
+
// The first two stack frames will be this function and getBundleURLCached.
|
|
21
|
+
// Use the 3rd one, which will be a runtime in the original bundle.
|
|
22
|
+
return getBaseURL(matches[2]);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return '/';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getBaseURL(url) {
|
|
30
|
+
return (
|
|
31
|
+
('' + url).replace(
|
|
32
|
+
/^((?:https?|file|ftp|(chrome|moz|safari-web)-extension):\/\/.+)\/[^/]+$/,
|
|
33
|
+
'$1',
|
|
34
|
+
) + '/'
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// TODO: Replace uses with `new URL(url).origin` when ie11 is no longer supported.
|
|
39
|
+
function getOrigin(url) {
|
|
40
|
+
let matches = ('' + url).match(
|
|
41
|
+
/(https?|file|ftp|(chrome|moz|safari-web)-extension):\/\/[^/]+/,
|
|
42
|
+
);
|
|
43
|
+
if (!matches) {
|
|
44
|
+
throw new Error('Origin not found');
|
|
45
|
+
}
|
|
46
|
+
return matches[0];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
exports.getBundleURL = getBundleURLCached;
|
|
50
|
+
exports.getBaseURL = getBaseURL;
|
|
51
|
+
exports.getOrigin = getOrigin;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
let cachedBundles = {};
|
|
2
|
+
let cachedPreloads = {};
|
|
3
|
+
let cachedPrefetches = {};
|
|
4
|
+
|
|
5
|
+
function getCache(type) {
|
|
6
|
+
switch (type) {
|
|
7
|
+
case 'preload':
|
|
8
|
+
return cachedPreloads;
|
|
9
|
+
case 'prefetch':
|
|
10
|
+
return cachedPrefetches;
|
|
11
|
+
default:
|
|
12
|
+
return cachedBundles;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = function cacheLoader(loader, type) {
|
|
17
|
+
return function (bundle) {
|
|
18
|
+
let cache = getCache(type);
|
|
19
|
+
|
|
20
|
+
if (cache[bundle]) {
|
|
21
|
+
return cache[bundle];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return (cache[bundle] = loader.apply(null, arguments).catch(function (e) {
|
|
25
|
+
delete cache[bundle];
|
|
26
|
+
throw e;
|
|
27
|
+
}));
|
|
28
|
+
};
|
|
29
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module.exports = function loadWorker(workerUrl, origin, isESM) {
|
|
2
|
+
if (origin === self.location.origin) {
|
|
3
|
+
// If the worker bundle's url is on the same origin as the document,
|
|
4
|
+
// use the worker bundle's own url.
|
|
5
|
+
return workerUrl;
|
|
6
|
+
} else {
|
|
7
|
+
// Otherwise, create a blob URL which loads the worker bundle with `importScripts`.
|
|
8
|
+
let source = isESM
|
|
9
|
+
? 'import ' + JSON.stringify(workerUrl) + ';'
|
|
10
|
+
: 'importScripts(' + JSON.stringify(workerUrl) + ');';
|
|
11
|
+
return URL.createObjectURL(
|
|
12
|
+
new Blob([source], {type: 'application/javascript'}),
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const cacheLoader = require('../cacheLoader');
|
|
3
|
+
|
|
4
|
+
module.exports = cacheLoader(function loadHTMLBundle(bundle) {
|
|
5
|
+
return new Promise(function (resolve, reject) {
|
|
6
|
+
fs.readFile(__dirname + bundle, 'utf8', function (err, data) {
|
|
7
|
+
if (err) {
|
|
8
|
+
reject(err);
|
|
9
|
+
} else {
|
|
10
|
+
// wait for the next event loop iteration, so we are sure
|
|
11
|
+
// the current module is fully loaded
|
|
12
|
+
setImmediate(function () {
|
|
13
|
+
resolve(data);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const cacheLoader = require('../cacheLoader');
|
|
3
|
+
|
|
4
|
+
module.exports = cacheLoader(function loadJSBundle(bundle) {
|
|
5
|
+
return new Promise(function (resolve, reject) {
|
|
6
|
+
fs.readFile(__dirname + bundle, 'utf8', function (err, data) {
|
|
7
|
+
if (err) {
|
|
8
|
+
reject(err);
|
|
9
|
+
} else {
|
|
10
|
+
// wait for the next event loop iteration, so we are sure
|
|
11
|
+
// the current module is fully loaded
|
|
12
|
+
setImmediate(function () {
|
|
13
|
+
resolve(data);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}).then(function (code) {
|
|
18
|
+
new Function('', code)();
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const cacheLoader = require('../cacheLoader');
|
|
3
|
+
|
|
4
|
+
module.exports = cacheLoader(function loadWASMBundle(bundle) {
|
|
5
|
+
return new Promise(function (resolve, reject) {
|
|
6
|
+
fs.readFile(__dirname + bundle, function (err, data) {
|
|
7
|
+
if (err) {
|
|
8
|
+
reject(err);
|
|
9
|
+
} else {
|
|
10
|
+
resolve(data.buffer);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
})
|
|
14
|
+
.then(function (data) {
|
|
15
|
+
return WebAssembly.instantiate(data);
|
|
16
|
+
})
|
|
17
|
+
.then(function (wasmModule) {
|
|
18
|
+
return wasmModule.instance.exports;
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/* global __atlaspack__importScripts__:readonly*/
|
|
2
|
+
const cacheLoader = require('../cacheLoader');
|
|
3
|
+
|
|
4
|
+
module.exports = cacheLoader(function loadJSBundle(bundle) {
|
|
5
|
+
return new Promise(function (resolve, reject) {
|
|
6
|
+
try {
|
|
7
|
+
__atlaspack__importScripts__(bundle);
|
|
8
|
+
resolve();
|
|
9
|
+
} catch (e) {
|
|
10
|
+
reject(e);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const cacheLoader = require('../cacheLoader');
|
|
2
|
+
|
|
3
|
+
module.exports = cacheLoader(function loadWASMBundle(bundle) {
|
|
4
|
+
return fetch(bundle)
|
|
5
|
+
.then(function (res) {
|
|
6
|
+
if (WebAssembly.instantiateStreaming) {
|
|
7
|
+
return WebAssembly.instantiateStreaming(res);
|
|
8
|
+
} else {
|
|
9
|
+
return res.arrayBuffer().then(function (data) {
|
|
10
|
+
return WebAssembly.instantiate(data);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
.then(function (wasmModule) {
|
|
15
|
+
return wasmModule.instance.exports;
|
|
16
|
+
});
|
|
17
|
+
});
|