Skip to content

Commit 07d7b9b

Browse files
authored
Merge pull request #11 from Esirei/patch-1
Update resolvePageComponent helper to work with both glob and glogEager
2 parents aaffee1 + 9703aca commit 07d7b9b

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/inertia-helpers/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
export function resolvePageComponent<T>(path: string, pages: Record<string, () => Promise<T>>) {
2-
if (typeof pages[path] === 'undefined') {
1+
export async function resolvePageComponent<T>(path: string, pages: Record<string, Promise<T> | (() => Promise<T>)>): Promise<T> {
2+
const page = pages[path]
3+
4+
if (typeof page === 'undefined') {
35
throw new Error(`Page not found: ${path}`)
46
}
57

6-
return pages[path]()
8+
return typeof page === 'function' ? page() : page
79
}

tests/__data__/dummy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'Dummy File'

tests/index.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { afterEach, describe, expect, it, vi } from 'vitest'
22
import laravel from '../src'
3+
import { resolvePageComponent } from '../src/inertia-helpers';
34

45
describe('laravel-vite-plugin', () => {
56
afterEach(() => {
@@ -218,3 +219,16 @@ describe('laravel-vite-plugin', () => {
218219
expect(stringNoExternalConfig.ssr.noExternal).toEqual(['foo', 'laravel-vite-plugin'])
219220
})
220221
})
222+
223+
describe('inertia-helpers', () => {
224+
const path = './__data__/dummy.ts'
225+
it('pass glob value to resolvePageComponent', async () => {
226+
const file = await resolvePageComponent<{ default: string }>(path, import.meta.glob('./__data__/*.ts'))
227+
expect(file.default).toBe('Dummy File')
228+
})
229+
230+
it('pass globEager value to resolvePageComponent', async () => {
231+
const file = await resolvePageComponent<{ default: string }>(path, import.meta.globEager('./__data__/*.ts'))
232+
expect(file.default).toBe('Dummy File')
233+
})
234+
})

0 commit comments

Comments
 (0)