Skip to content

Commit

Permalink
Generate routes during build (#588)
Browse files Browse the repository at this point in the history
* call post update sync

* generate routes during build and dev

* changeset

* await child process close
  • Loading branch information
fehnomenal authored Jan 3, 2024
1 parent d475cfb commit 7d1f998
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-vans-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-kit-routes': minor
---

also generate routes during build
20 changes: 12 additions & 8 deletions packages/vite-plugin-kit-routes/src/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ const arrayToRecord = (arr?: string[]) => {
return `: Record<string, never>`
}

export const run = (atStart: boolean, o?: Options) => {
export const run = async (atStart: boolean, o?: Options) => {
const options = getDefaultOption(o)

const files = getFilesUnder(routes_path())
Expand Down Expand Up @@ -960,10 +960,14 @@ ${objTypes
})
}

const exitPromise = new Promise<void>(resolve => {
child.on('close', () => resolve())
})

await exitPromise

if (shouldLog('update', options)) {
child.on('close', () => {
theEnd(atStart, result, objTypes, options)
})
theEnd(atStart, result, objTypes, options)
}
} else {
theEnd(atStart, result, objTypes, options)
Expand Down Expand Up @@ -1069,8 +1073,8 @@ export function kitRoutes<T extends ExtendTypes = ExtendTypes>(options?: Options
// Run the thing at startup
{
name: 'kit-routes',
configureServer() {
run(true, options)
async buildStart() {
await run(true, options)
},
},

Expand All @@ -1080,8 +1084,8 @@ export function kitRoutes<T extends ExtendTypes = ExtendTypes>(options?: Options
name: 'kit-routes-watch',
logs: [],
watch: ['**/+page.svelte', '**/+page.server.ts', '**/+server.ts'],
run: () => {
run(false, options)
run: async () => {
await run(false, options)
},
},
]),
Expand Down
12 changes: 6 additions & 6 deletions packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ describe('run()', async () => {
for (let i = 0; i < runs.length; i++) {
const toRun = runs[i]
it(`run ${toRun.pathFile}`, async () => {
const ret = run(false, {
const ret = await run(false, {
format: toRun.format,
generated_file_path: getPathROUTES(toRun.pathFile),
...toRun.extra,
Expand All @@ -474,7 +474,7 @@ describe('run()', async () => {
const toRun = getToRunShortened(runs[i])

it(`run ${toRun.pathFile}`, async () => {
const ret = run(false, {
const ret = await run(false, {
format: toRun.format,
generated_file_path: getPathROUTES(toRun.pathFile),
...toRun.extra,
Expand Down Expand Up @@ -769,19 +769,19 @@ describe('run()', async () => {
})
}

it('post_update_run', () => {
it('post_update_run', async () => {
const generated_file_path = 'src/test/ROUTES_post-update.ts'
run(false, {
await run(false, {
generated_file_path,
post_update_run: 'echo done',
})

expect(true).toBe(true)
})

it('with path base', () => {
it('with path base', async () => {
const generated_file_path = 'src/test/ROUTES_base.ts'
run(false, {
await run(false, {
generated_file_path,
path_base: true,
})
Expand Down

0 comments on commit 7d1f998

Please sign in to comment.