UNPKG

778 BJavaScriptView Raw
1// @flow strict-local
2
3let bundleURL: ?string = null;
4function getBundleURLCached() {
5 if (bundleURL == null) {
6 bundleURL = _getBundleURL();
7 }
8
9 return bundleURL;
10}
11
12function _getBundleURL(): string {
13 // Attempt to find the URL of the current script and use that as the base URL
14 try {
15 throw new Error();
16 } catch (err) {
17 let stack: string = typeof err.stack === 'string' ? err.stack : '';
18 let matches = stack.match(/(https?|file|ftp):\/\/[^)\n]+/g);
19 if (matches) {
20 return getBaseURL(matches[0]);
21 }
22 }
23
24 return '/';
25}
26
27export function getBaseURL(url: ?string): string {
28 if (url == null) {
29 return '/';
30 }
31
32 return url.replace(/^((?:https?|file|ftp):\/\/.+)\/[^/]+$/, '$1') + '/';
33}
34
35export const getBundleURL = getBundleURLCached;