-
-
Notifications
You must be signed in to change notification settings - Fork 779
/
index.ts
103 lines (91 loc) · 3.03 KB
/
index.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import sirv from 'sirv'
import type { Plugin, ViteDevServer } from 'vite'
import type { UnocssPluginContext } from '@unocss/core'
import gzipSize from 'gzip-size'
import type { ModuleInfo, ProjectInfo } from '../types'
const _dirname = typeof __dirname !== 'undefined'
? __dirname
: dirname(fileURLToPath(import.meta.url))
export default function UnocssInspector(ctx: UnocssPluginContext): Plugin {
async function configureServer(server: ViteDevServer) {
await ctx.ready
server.middlewares.use('/__unocss', sirv(resolve(_dirname, '../dist/client'), {
single: true,
dev: true,
}))
server.middlewares.use('/__unocss_api', async (req, res, next) => {
if (!req.url)
return next()
if (req.url === '/') {
const info: ProjectInfo = {
version: ctx.uno.version,
// use the resolved config from the dev server
root: server.config.root,
modules: Array.from(ctx.modules.keys()),
config: ctx.uno.config,
configSources: (await ctx.ready).sources,
}
res.setHeader('Content-Type', 'application/json')
res.write(JSON.stringify(info, null, 2))
res.end()
return
}
if (req.url.startsWith('/module')) {
const query = new URLSearchParams(req.url.slice(8))
const id = query.get('id') || ''
const code = ctx.modules.get(id)
if (code == null) {
res.statusCode = 404
res.end()
return
}
const result = await ctx.uno.generate(code, { id, preflights: false })
const mod: ModuleInfo = {
...result,
matched: Array.from(result.matched),
gzipSize: await gzipSize(result.css),
code,
id,
}
res.setHeader('Content-Type', 'application/json')
res.write(JSON.stringify(mod, null, 2))
res.end()
return
}
if (req.url.startsWith('/repl')) {
const query = new URLSearchParams(req.url.slice(5))
const token = query.get('token') || ''
const includeSafelist = JSON.parse(query.get('safelist') ?? 'false')
const result = await ctx.uno.generate(token, { preflights: false, safelist: includeSafelist })
const mod = {
...result,
matched: Array.from(result.matched),
}
res.setHeader('Content-Type', 'application/json')
res.write(JSON.stringify(mod, null, 2))
res.end()
return
}
if (req.url.startsWith('/overview')) {
const result = await ctx.uno.generate(ctx.tokens)
const mod = {
...result,
matched: Array.from(result.matched),
gzipSize: await gzipSize(result.css),
}
res.setHeader('Content-Type', 'application/json')
res.write(JSON.stringify(mod, null, 2))
res.end()
return
}
next()
})
}
return <Plugin>{
name: 'unocss:inspector',
apply: 'serve',
configureServer,
}
}