You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
website/vite.config.js

70 lines
2.1 KiB
JavaScript

2 years ago
import { defineConfig } from 'vite'
import fetch from 'node-fetch'
import { readFile } from 'fs/promises'
import { dirname, resolve } from 'path'
import preactPlugin from '@preact/preset-vite'
const retriveGoRoutes = {
async build() {
console.log('Loading routes from disk...')
const routesRaw = await readFile('out/routes.json', 'utf8')
return JSON.parse(routesRaw)
},
async serve() {
console.log('Loading routes from go server...')
const routesReq = await fetch('http://127.0.0.1:4000/api/dev/routes')
const routes = await routesReq.json()
console.dir(routes, { depth: null })
return routes
},
}
export default defineConfig(async config => {
let routes = await retriveGoRoutes[config.command]()
console.dir(routes)
return {
root: 'frontend',
build: {
outDir: '../out/frontend',
rollupOptions: {
input: {
'main': resolve(__dirname, 'frontend/pages/index.html'),
'lista-utenti': resolve(__dirname, 'frontend/pages/lista-utenti/index.html'),
},
},
},
server: {
port: 3000,
proxy: {
'/api': 'http://localhost:4000/',
},
},
plugins: [
preactPlugin(),
{
name: 'custom-router',
configureServer(server) {
Object.entries(routes).forEach(([route, file]) => {
server.middlewares.use(route, async (req, res, next) => {
let htmlPage = await readFile(resolve(__dirname, file), 'utf8')
htmlPage = htmlPage.replace(/\.\//g, dirname(file) + '/')
const url = file
const html = await server.transformIndexHtml(url, htmlPage)
console.log(url)
res.writeHead(200, { 'Content-Type': 'text/html' }).end(html)
})
})
},
},
],
}
})