@@ -9,9 +9,34 @@ export async function loadCopydeckFor(locale = "en", opts: Options = {}): Promis
99 if ( baseLang !== locale ) tryLocales . push ( baseLang ) ;
1010 if ( ! tryLocales . includes ( "en" ) ) tryLocales . push ( "en" ) ;
1111
12- const baseUrl = opts . base
13- ? new URL ( opts . base , window . location . origin )
14- : new URL ( "./copydecks/" , import . meta. url ) ;
12+ let baseUrl : URL ;
13+ if ( opts . base ) {
14+ baseUrl = new URL ( opts . base , window . location . origin ) ;
15+ } else {
16+ // try to resolve copydecks relative to package location
17+ // in bundled environments (webpack/vite), import.meta.url points to the bundle
18+ try {
19+ const metaUrl = import . meta. url ;
20+ // construct path dynamically to avoid webpack static analysis
21+ const copydecksDir = 'copydecks' ;
22+ const separator = '/' ;
23+ const path = '.' + separator + copydecksDir + separator ;
24+
25+ // check if this looks like a bundled file
26+ if ( metaUrl . includes ( 'index.browser.js' ) || metaUrl . includes ( 'dist/' ) ) {
27+ // in a bundled environment, copydecks are in dist/copydecks
28+ const bundleDir = metaUrl . substring ( 0 , metaUrl . lastIndexOf ( '/' ) ) ;
29+ baseUrl = new URL ( path , bundleDir ) ;
30+ } else {
31+ baseUrl = new URL ( path , metaUrl ) ;
32+ }
33+ } catch {
34+ throw new Error (
35+ 'Unable to resolve copydecks path. Please provide the base URL: ' +
36+ 'loadCopydeckFor(locale, { base: "/path/to/copydecks/" })'
37+ ) ;
38+ }
39+ }
1540
1641 for ( const lang of tryLocales ) {
1742 try {
@@ -22,7 +47,7 @@ export async function loadCopydeckFor(locale = "en", opts: Options = {}): Promis
2247 loadCopydeck ( deck ) ;
2348 return deck ;
2449 }
25- } catch { /* try next */ }
50+ } catch { }
2651 }
2752 throw new Error ( `No copydeck found for ${ tryLocales . join ( ", " ) } ` ) ;
2853}
0 commit comments