/
bundle-url.js
51 lines (45 loc) 路 1.17 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
var bundleURL = {};
function getBundleURLCached(id) {
var value = bundleURL[id];
if (!value) {
value = getBundleURL();
bundleURL[id] = value;
}
return value;
}
function getBundleURL() {
try {
throw new Error();
} catch (err) {
var matches = ('' + err.stack).match(
/(https?|file|ftp|(chrome|moz|safari-web)-extension):\/\/[^)\n]+/g,
);
if (matches) {
// The first two stack frames will be this function and getBundleURLCached.
// Use the 3rd one, which will be a runtime in the original bundle.
return getBaseURL(matches[2]);
}
}
return '/';
}
function getBaseURL(url) {
return (
('' + url).replace(
/^((?:https?|file|ftp|(chrome|moz|safari-web)-extension):\/\/.+)\/[^/]+$/,
'$1',
) + '/'
);
}
// TODO: Replace uses with `new URL(url).origin` when ie11 is no longer supported.
function getOrigin(url) {
let matches = ('' + url).match(
/(https?|file|ftp|(chrome|moz|safari-web)-extension):\/\/[^/]+/,
);
if (!matches) {
throw new Error('Origin not found');
}
return matches[0];
}
exports.getBundleURL = getBundleURLCached;
exports.getBaseURL = getBaseURL;
exports.getOrigin = getOrigin;