UNPKG

749 BJavaScriptView Raw
1// src/utilities/base-path.ts
2var basePath = "";
3function setBasePath(path) {
4 basePath = path;
5}
6function getBasePath() {
7 if (!basePath) {
8 const scripts = [...document.getElementsByTagName("script")];
9 const configScript = scripts.find((script) => script.hasAttribute("data-shoelace"));
10 if (configScript) {
11 setBasePath(configScript.getAttribute("data-shoelace"));
12 } else {
13 const fallbackScript = scripts.find((s) => /shoelace(\.min)?\.js($|\?)/.test(s.src));
14 let path = "";
15 if (fallbackScript) {
16 path = fallbackScript.getAttribute("src");
17 }
18 setBasePath(path.split("/").slice(0, -1).join("/"));
19 }
20 }
21 return basePath.replace(/\/$/, "");
22}
23
24export {
25 setBasePath,
26 getBasePath
27};