-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
buildModuleUrl.js
148 lines (129 loc) · 3.72 KB
/
buildModuleUrl.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import defined from "./defined.js";
import DeveloperError from "./DeveloperError.js";
import getAbsoluteUri from "./getAbsoluteUri.js";
import Resource from "./Resource.js";
/*global CESIUM_BASE_URL,define,require*/
const cesiumScriptRegex = /((?:.*\/)|^)Cesium\.js(?:\?|\#|$)/;
function getBaseUrlFromCesiumScript() {
const scripts = document.getElementsByTagName("script");
for (let i = 0, len = scripts.length; i < len; ++i) {
const src = scripts[i].getAttribute("src");
const result = cesiumScriptRegex.exec(src);
if (result !== null) {
return result[1];
}
}
return undefined;
}
let a;
function tryMakeAbsolute(url) {
if (typeof document === "undefined") {
//Node.js and Web Workers. In both cases, the URL will already be absolute.
return url;
}
if (!defined(a)) {
a = document.createElement("a");
}
a.href = url;
// IE only absolutizes href on get, not set
// eslint-disable-next-line no-self-assign
a.href = a.href;
return a.href;
}
let baseResource;
function getCesiumBaseUrl() {
if (defined(baseResource)) {
return baseResource;
}
let baseUrlString;
if (typeof CESIUM_BASE_URL !== "undefined") {
baseUrlString = CESIUM_BASE_URL;
} else if (
typeof define === "object" &&
defined(define.amd) &&
!define.amd.toUrlUndefined &&
defined(require.toUrl)
) {
baseUrlString = getAbsoluteUri(
"..",
buildModuleUrl("Core/buildModuleUrl.js")
);
} else {
baseUrlString = getBaseUrlFromCesiumScript();
}
//>>includeStart('debug', pragmas.debug);
if (!defined(baseUrlString)) {
throw new DeveloperError(
"Unable to determine Cesium base URL automatically, try defining a global variable called CESIUM_BASE_URL."
);
}
//>>includeEnd('debug');
baseResource = new Resource({
url: tryMakeAbsolute(baseUrlString),
});
baseResource.appendForwardSlash();
return baseResource;
}
function buildModuleUrlFromRequireToUrl(moduleID) {
//moduleID will be non-relative, so require it relative to this module, in Core.
return tryMakeAbsolute(require.toUrl(`../${moduleID}`));
}
function buildModuleUrlFromBaseUrl(moduleID) {
const resource = getCesiumBaseUrl().getDerivedResource({
url: moduleID,
});
return resource.url;
}
let implementation;
/**
* Given a relative URL under the Cesium base URL, returns an absolute URL.
* @function
*
* @param {String} relativeUrl The relative path.
* @returns {String} The absolutely URL representation of the provided path.
*
* @example
* const viewer = new Cesium.Viewer("cesiumContainer", {
* imageryProvider: new Cesium.TileMapServiceImageryProvider({
* url: Cesium.buildModuleUrl("Assets/Textures/NaturalEarthII"),
* }),
* baseLayerPicker: false,
* });
*/
function buildModuleUrl(relativeUrl) {
if (!defined(implementation)) {
//select implementation
if (
typeof define === "object" &&
defined(define.amd) &&
!define.amd.toUrlUndefined &&
defined(require.toUrl)
) {
implementation = buildModuleUrlFromRequireToUrl;
} else {
implementation = buildModuleUrlFromBaseUrl;
}
}
const url = implementation(relativeUrl);
return url;
}
// exposed for testing
buildModuleUrl._cesiumScriptRegex = cesiumScriptRegex;
buildModuleUrl._buildModuleUrlFromBaseUrl = buildModuleUrlFromBaseUrl;
buildModuleUrl._clearBaseResource = function () {
baseResource = undefined;
};
/**
* Sets the base URL for resolving modules.
* @param {String} value The new base URL.
*/
buildModuleUrl.setBaseUrl = function (value) {
baseResource = Resource.DEFAULT.getDerivedResource({
url: value,
});
};
/**
* Gets the base URL for resolving modules.
*/
buildModuleUrl.getCesiumBaseUrl = getCesiumBaseUrl;
export default buildModuleUrl;