|
1 | 1 | let cardsCache = null; |
2 | 2 |
|
3 | | -const cardsImportPath = '../data/cards.json'; |
4 | | - |
5 | 3 | const cardsRequestUrl = (() => { |
6 | 4 | try { |
7 | | - return new URL(cardsImportPath, import.meta.url).href; |
| 5 | + return new URL('../data/cards.json', import.meta.url).href; |
8 | 6 | } catch (error) { |
9 | 7 | console.warn('Unable to resolve cards.json via import.meta.url, falling back to relative path', error); |
10 | 8 | return 'data/cards.json'; |
@@ -112,27 +110,26 @@ function shuffleCards(cards) { |
112 | 110 | return shuffled; |
113 | 111 | } |
114 | 112 |
|
115 | | -async function loadFromImport() { |
| 113 | +function loadFromBundle() { |
| 114 | + if (typeof import.meta.glob !== 'function') { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
116 | 118 | try { |
117 | | - const module = await import(cardsImportPath, { assert: { type: 'json' } }); |
| 119 | + const modules = import.meta.glob('../data/cards.json', { eager: true }); |
| 120 | + const module = modules['../data/cards.json']; |
| 121 | + if (!module) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
118 | 125 | const payload = module.default ?? module; |
119 | 126 | if (!Array.isArray(payload)) { |
120 | 127 | return null; |
121 | 128 | } |
122 | 129 |
|
123 | 130 | return payload; |
124 | 131 | } catch (error) { |
125 | | - try { |
126 | | - const module = await import(cardsImportPath); |
127 | | - const payload = module.default ?? module; |
128 | | - if (!Array.isArray(payload)) { |
129 | | - return null; |
130 | | - } |
131 | | - |
132 | | - return payload; |
133 | | - } catch (nestedError) { |
134 | | - console.warn('Falling back to network fetch for cards', nestedError); |
135 | | - } |
| 132 | + console.warn('Unable to load cards bundle via import.meta.glob', error); |
136 | 133 | return null; |
137 | 134 | } |
138 | 135 | } |
@@ -167,9 +164,9 @@ export async function fetchCards() { |
167 | 164 | return cardsCache; |
168 | 165 | } |
169 | 166 |
|
170 | | - const imported = await loadFromImport(); |
171 | | - if (imported && imported.length > 0) { |
172 | | - cardsCache = prepareCards(imported, { shuffle: true }); |
| 167 | + const bundled = loadFromBundle(); |
| 168 | + if (bundled && bundled.length > 0) { |
| 169 | + cardsCache = prepareCards(bundled, { shuffle: true }); |
173 | 170 | return cardsCache; |
174 | 171 | } |
175 | 172 |
|
|
0 commit comments