diff --git a/docs/resources/changelog.md b/docs/resources/changelog.md index b5d76858..448ab5a9 100644 --- a/docs/resources/changelog.md +++ b/docs/resources/changelog.md @@ -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 diff --git a/src/utilities/base-path.ts b/src/utilities/base-path.ts index c4be84c0..ef6ad7e2 100644 --- a/src/utilities/base-path.ts +++ b/src/utilities/base-path.ts @@ -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('/')); }