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

59 lines
1.6 KiB
JavaScript

2 years ago
import { defineConfig } from 'vite'
import { dirname, join, resolve } from 'path'
2 years ago
import preactPlugin from '@preact/preset-vite'
import { getBuildRoutesMetadata } from './meta/routes.js'
2 years ago
import crypto from 'crypto'
2 years ago
/** @type {import('vite').UserConfig} */
const sharedConfig = {
root: './frontend',
plugins: [preactPlugin()],
}
2 years ago
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)]
2 years ago
}
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,
2 years ago
},
},
}
} else {
return {
...sharedConfig,
server: {
port: 3000,
proxy: {
'/api': 'http://localhost:4000/',
2 years ago
},
},
}
2 years ago
}
})