25 lines
822 B
TypeScript
25 lines
822 B
TypeScript
import { type FastifyPluginCallback } from 'fastify'
|
|
import _ from 'lodash'
|
|
import fp from 'fastify-plugin'
|
|
import type { Config, Entry, EntryWithoutName } from './vite/types.ts'
|
|
|
|
const vitePlugin: FastifyPluginCallback<Config> = async function (fastify, config) {
|
|
const parsedConfig = {
|
|
mode: config.mode || 'production',
|
|
entries: _.map(config.entries, (entry, name) => getEntryConfig(name, entry, config)) as Entry[],
|
|
}
|
|
|
|
const setup = (await import(`./vite/${config.mode}.ts`)).default
|
|
|
|
await setup(fastify, parsedConfig)
|
|
}
|
|
|
|
function getEntryConfig(name: string, entry: EntryWithoutName, config: Config): Entry {
|
|
return Object.assign(_.pick(config, ['createErrorHandler', 'createPreHandler', 'ssr', 'template']), entry, { name })
|
|
}
|
|
|
|
export default fp(vitePlugin, {
|
|
name: 'vite',
|
|
fastify: '5.x',
|
|
})
|