-
Notifications
You must be signed in to change notification settings - Fork 2
/
bundle-url.js
51 lines (38 loc) · 1.09 KB
/
bundle-url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use strict";
// This file exists because Parcel doesn't know how to load web worker files on a file:// origin.
// I have a PR out that should help with this.
/* globals document:readonly */
var bundleURL = null;
function getBundleURLCached() {
if (!bundleURL) {
bundleURL = getBundleURL();
}
return bundleURL;
}
function getBundleURL() {
try {
throw new Error();
} catch (err) {
var matches = ('' + err.stack).match(/(https?|file|ftp):\/\/[^)\n]+/g);
if (matches) {
return getBaseURL(matches[0]);
}
}
return '/';
}
function getBaseURL(url) {
return ('' + url).replace(/^((?:https?|file|ftp):\/\/.+)\/[^/]+$/, '$1') + '/';
} // TODO: Replace uses with `new URL(url).origin` when ie11 is no longer supported.
function getOrigin(url) {
if (url.slice(0, 7) === 'file://') {
return 'file://';
}
let matches = ('' + url).match(/(https?|file|ftp):\/\/[^/]+/);
if (!matches) {
throw new Error('Origin not found');
}
return matches[0];
}
exports.getBundleURL = getBundleURLCached;
exports.getBaseURL = getBaseURL;
exports.getOrigin = getOrigin;