-
Notifications
You must be signed in to change notification settings - Fork 10
/
vite.config.ts
39 lines (36 loc) · 1.04 KB
/
vite.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { defineConfig } from "vite"
import react from "@vitejs/plugin-react"
import path from "path"
import fs from "fs"
export default defineConfig({
resolve: {
alias: {
lib: path.resolve(__dirname, "lib"),
},
},
base: process.env.BUILD_FOR_GH_PAGES ? "/jscad-fiber/" : undefined,
plugins: [
react(),
{
name: "serve-examples",
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url?.startsWith("/example-code/")) {
const filePath = req.url.replace("/example-code/", "")
const fullPath = path.resolve(__dirname, "examples", filePath)
try {
const code = await fs.promises.readFile(fullPath, "utf-8")
res.setHeader("Content-Type", "application/json")
res.end(JSON.stringify({ code }))
} catch (error) {
res.statusCode = 404
res.end("File not found")
}
} else {
next()
}
})
},
},
],
})