diff --git a/.changeset/short-vans-camp.md b/.changeset/short-vans-camp.md new file mode 100644 index 000000000..5874fc3eb --- /dev/null +++ b/.changeset/short-vans-camp.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-kit-routes': minor +--- + +also generate routes during build diff --git a/packages/vite-plugin-kit-routes/src/lib/plugin.ts b/packages/vite-plugin-kit-routes/src/lib/plugin.ts index 4127dc60d..705b3aab9 100644 --- a/packages/vite-plugin-kit-routes/src/lib/plugin.ts +++ b/packages/vite-plugin-kit-routes/src/lib/plugin.ts @@ -775,7 +775,7 @@ const arrayToRecord = (arr?: string[]) => { return `: Record` } -export const run = (atStart: boolean, o?: Options) => { +export const run = async (atStart: boolean, o?: Options) => { const options = getDefaultOption(o) const files = getFilesUnder(routes_path()) @@ -960,10 +960,14 @@ ${objTypes }) } + const exitPromise = new Promise(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) @@ -1069,8 +1073,8 @@ export function kitRoutes(options?: Options // Run the thing at startup { name: 'kit-routes', - configureServer() { - run(true, options) + async buildStart() { + await run(true, options) }, }, @@ -1080,8 +1084,8 @@ export function kitRoutes(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) }, }, ]), diff --git a/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts b/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts index 9ef245457..9170862e0 100644 --- a/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts +++ b/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts @@ -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, @@ -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, @@ -769,9 +769,9 @@ 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', }) @@ -779,9 +779,9 @@ describe('run()', async () => { 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, })