forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import fs from 'node:fs'
2import {resolve} from 'node:path'
3
4import preact from '@preact/preset-vite'
5import legacy from '@vitejs/plugin-legacy'
6import type {Plugin, UserConfig} from 'vite'
7import paths from 'vite-tsconfig-paths'
8
9/**
10 * World's hackiest router, for dev only. Serves `/post.html` to requests that start with `/embed/`
11 */
12function devOnlyRouter(): Plugin {
13 return {
14 name: 'embed-to-post-html',
15 configureServer(server) {
16 server.middlewares.use((req, res, next) => {
17 const url = req.url || ''
18 if (!url.startsWith('/embed/')) return next()
19
20 const html = fs.readFileSync(
21 resolve(process.cwd(), 'post.html'),
22 'utf8',
23 )
24
25 server
26 .transformIndexHtml(url, html)
27 .then(transformed => {
28 res.statusCode = 200
29 res.setHeader('Content-Type', 'text/html')
30 res.end(transformed)
31 })
32 .catch(next)
33 })
34 },
35 }
36}
37
38const config: UserConfig = {
39 plugins: [
40 preact(),
41 paths(),
42 legacy({
43 targets: ['defaults', 'not IE 11'],
44 }),
45 devOnlyRouter(),
46 ],
47 build: {
48 assetsDir: 'static',
49 rollupOptions: {
50 input: {
51 index: resolve(__dirname, 'index.html'),
52 post: resolve(__dirname, 'post.html'),
53 },
54 },
55 },
56}
57
58export default config