Files
tuono/packages/tuono-react-vite-plugin/src/plugin.ts
T

48 lines
997 B
TypeScript
Raw Normal View History

2025-02-16 15:39:25 +01:00
import { normalize } from 'node:path'
2024-07-08 21:39:00 +02:00
import type { Plugin } from 'vite'
import { routeGenerator } from './fs-routing/generator'
2024-12-01 10:07:35 +01:00
2024-07-08 21:39:00 +02:00
const ROUTES_DIRECTORY_PATH = './src/routes'
let lock = false
export function TuonoReactPlugin(): Plugin {
2024-07-08 21:39:00 +02:00
const generate = async (): Promise<void> => {
if (lock) return
lock = true
try {
await routeGenerator()
} catch (err) {
console.error(err)
} finally {
lock = false
}
}
const handleFile = async (file: string): Promise<void> => {
const filePath = normalize(file)
if (filePath.startsWith(ROUTES_DIRECTORY_PATH)) {
await generate()
}
}
return {
name: 'vite-plugin-tuono-react',
2024-07-08 21:39:00 +02:00
configResolved: async (): Promise<void> => {
await generate()
},
watchChange: async (
file: string,
context: { event: string },
): Promise<void> => {
if (['create', 'update', 'delete'].includes(context.event)) {
await handleFile(file)
}
},
}
}