Skip to content

Commit

Permalink
refactor base path logic
Browse files Browse the repository at this point in the history
  • Loading branch information
claviska committed Jul 12, 2021
1 parent 4aeb804 commit a48eef2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
1 change: 1 addition & 0 deletions docs/resources/changelog.md
Expand Up @@ -12,6 +12,7 @@ This release improves how component dependencies are imported. If you've been ch

- Added "Reflects" column to the properties table
- Dependencies are now automatically imported for all components
- Improved base path utility logic

## 2.0.0-beta.46

Expand Down
27 changes: 12 additions & 15 deletions src/utilities/base-path.ts
Expand Up @@ -25,22 +25,19 @@ export function getBasePath() {
//
// Alternatively, you can set the base path manually using the exported setBasePath() function.
//
if (!basePath) {
const allScripts = [...document.getElementsByTagName('script')] as HTMLScriptElement[];
const el = allScripts.find(script => script.hasAttribute('data-shoelace'));
const scripts = [...document.getElementsByTagName('script')] as HTMLScriptElement[];
const configScript = scripts.find(script => script.hasAttribute('data-shoelace'));

if (el) {
// Use the data-shoelace attribute
setBasePath(el.getAttribute('data-shoelace')!);
} else {
// Fallback to auto-detection
const script = document.querySelector('script[src$="shoelace.js"], script[src$="shoelace.min.js"]');
let path = '';
if (configScript) {
// Use the data-shoelace attribute
setBasePath(configScript.getAttribute('data-shoelace')!);
} else {
const fallbackScript = scripts.find(s => /shoelace(\.min)?\.js$/.test(s.src));
let path = '';

if (script) {
path = script.getAttribute('src')!;
}

setBasePath(path.split('/').slice(0, -1).join('/'));
if (fallbackScript) {
path = fallbackScript.getAttribute('src')!;
}

setBasePath(path.split('/').slice(0, -1).join('/'));
}

0 comments on commit a48eef2

Please sign in to comment.