import { defineConfig } from 'vite' import { dirname, join, resolve } from 'path' import preactPlugin from '@preact/preset-vite' import { getBuildRoutesMetadata } from './meta/routes.js' import crypto from 'crypto' /** @type {import('vite').UserConfig} */ const sharedConfig = { root: './frontend', plugins: [preactPlugin()], } function routesToRollupInput([route, file]) { const chunkName = file .replaceAll('.html', '') .replaceAll('index', '') .replace(/^\/|\/$/, '') .replaceAll('/', '-') + '-' + crypto.createHash('md5').update(route).update(file).digest('hex').slice(0, 8) return [chunkName, join(__dirname, 'frontend', file)] } export default defineConfig(async config => { if (config.command === 'build') { const routes = await getBuildRoutesMetadata('out/routes.json') const input = Object.fromEntries( [...Object.entries(routes.static), ...Object.entries(routes.dynamic).map(([route, { htmlFile }]) => [route, htmlFile])].map( routesToRollupInput ) ) console.dir(input) return { ...sharedConfig, build: { outDir: '../out/frontend', rollupOptions: { input, }, }, } } else { return { ...sharedConfig, server: { port: 3000, proxy: { '/api': 'http://localhost:4000/', }, }, } } })