-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
vite.config.js
80 lines (73 loc) · 2.07 KB
/
vite.config.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import fs from 'node:fs/promises'
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import unocss from '@unocss/vite'
import { markdown } from './scripts/vitePluginMarkdown.js'
export default defineConfig({
optimizeDeps: {
// Vite's scanner doesn't scan references via `new URL(...)`.
// In this app, we import the worker with the syntax, so manually add the worker for now.
// TODO: Fix this in Vite
entries: ['**/*.html', './src/utils/worker.js']
},
plugins: [serveAnalysisJson(), unocss(), svelte(), markdown()],
esbuild: {
legalComments: 'none'
},
build: {
rollupOptions: {
input: {
main: new URL('/index.html', import.meta.url).pathname,
rules: new URL('/rules.html', import.meta.url).pathname
}
}
}
})
const analysisJsonUrl = new URL(
'../analysis/cache/_results.json',
import.meta.url
)
/**
* Serve /analysis.json in dev (Handled as Cloudflare worker in prod)
* @returns {import('vite').Plugin}
*/
function serveAnalysisJson() {
/**
* @param {import('vite').Connect.Server} middlewares
*/
function addMiddleware(middlewares) {
middlewares.use(async (req, res, next) => {
if (req.url === '/analysis.json') {
res.setHeader('Content-Type', 'application/json')
// try load local analysis result
try {
res.end(await fs.readFile(analysisJsonUrl))
return
} catch {
// file does not exist
}
// try pre-analysed result
try {
const result = await fetch(
'https://gist.github.com/bluwy/64b0c283d8f0f3f8a8f4eea03c75a3b8/raw/publint_analysis.json'
)
const buffer = await result.arrayBuffer()
res.end(new Uint8Array(buffer))
return
} catch {
// failed to fetch
}
}
next()
})
}
return {
name: 'serve-analysis-json',
configureServer(server) {
addMiddleware(server.middlewares)
},
configurePreviewServer(server) {
addMiddleware(server.middlewares)
}
}
}