-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
public-path.js
86 lines (75 loc) · 2.36 KB
/
public-path.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
exports.setPublicPath = function setPublicPath(
systemjsModuleName,
rootDirectoryLevel
) {
if (!rootDirectoryLevel) {
rootDirectoryLevel = 1;
}
if (
typeof systemjsModuleName !== "string" ||
systemjsModuleName.trim().length === 0
) {
throw Error(
"systemjs-webpack-interop: setPublicPath(systemjsModuleName) must be called with a non-empty string 'systemjsModuleName'"
);
}
if (
typeof rootDirectoryLevel !== "number" ||
rootDirectoryLevel <= 0 ||
isNaN(rootDirectoryLevel) ||
!isInteger(rootDirectoryLevel)
) {
throw Error(
"systemjs-webpack-interop: setPublicPath(systemjsModuleName, rootDirectoryLevel) must be called with a positive integer 'rootDirectoryLevel'"
);
}
var moduleUrl;
try {
moduleUrl = window.System.resolve(systemjsModuleName);
if (!moduleUrl) {
throw Error();
}
} catch (err) {
throw Error(
"systemjs-webpack-interop: There is no such module '" +
systemjsModuleName +
"' in the SystemJS registry. Did you misspell the name of your module?"
);
}
__webpack_public_path__ = resolveDirectory(moduleUrl, rootDirectoryLevel);
};
function resolveDirectory(urlString, rootDirectoryLevel) {
// Our friend IE11 doesn't support new URL()
// https://github.com/single-spa/single-spa/issues/612
// https://gist.github.com/jlong/2428561
var a = document.createElement("a");
a.href = urlString;
var pathname = a.pathname[0] === "/" ? a.pathname : "/" + a.pathname;
var numDirsProcessed = 0,
index = pathname.length;
while (numDirsProcessed !== rootDirectoryLevel && index >= 0) {
var char = pathname[--index];
if (char === "/") {
numDirsProcessed++;
}
}
if (numDirsProcessed !== rootDirectoryLevel) {
throw Error(
"systemjs-webpack-interop: rootDirectoryLevel (" +
rootDirectoryLevel +
") is greater than the number of directories (" +
numDirsProcessed +
") in the URL path " +
urlString
);
}
var finalPath = pathname.slice(0, index + 1);
return a.protocol + "//" + a.host + finalPath;
}
exports.resolveDirectory = resolveDirectory;
// borrowed from https://github.com/parshap/js-is-integer/blob/master/index.js
var isInteger =
Number.isInteger ||
function isInteger(val) {
return typeof val === "number" && isFinite(val) && Math.floor(val) === val;
};