1 |
|
2 |
|
3 | let bundleURL: ?string = null;
|
4 | function getBundleURLCached() {
|
5 | if (bundleURL == null) {
|
6 | bundleURL = _getBundleURL();
|
7 | }
|
8 |
|
9 | return bundleURL;
|
10 | }
|
11 |
|
12 | function _getBundleURL(): string {
|
13 |
|
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 |
|
27 | export function getBaseURL(url: ?string): string {
|
28 | if (url == null) {
|
29 | return '/';
|
30 | }
|
31 |
|
32 | return url.replace(/^((?:https?|file|ftp):\/\/.+)\/[^/]+$/, '$1') + '/';
|
33 | }
|
34 |
|
35 | export const getBundleURL = getBundleURLCached;
|